1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.NoSuchReleaseException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.model.Release;
30  import com.liferay.portal.model.impl.ReleaseImpl;
31  import com.liferay.portal.service.base.ReleaseLocalServiceBaseImpl;
32  import com.liferay.portal.service.persistence.ReleaseUtil;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  import com.liferay.portal.tools.sql.DBUtil;
35  import com.liferay.portal.util.PropsUtil;
36  import com.liferay.portal.util.ReleaseInfo;
37  import com.liferay.util.dao.DataAccess;
38  
39  import java.sql.Connection;
40  import java.sql.PreparedStatement;
41  import java.sql.ResultSet;
42  
43  import java.util.Date;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  /**
49   * <a href="ReleaseLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class ReleaseLocalServiceImpl extends ReleaseLocalServiceBaseImpl {
55  
56      public int getBuildNumberOrCreate()
57          throws PortalException, SystemException {
58  
59          // Get release build number
60  
61          Connection con = null;
62          PreparedStatement ps = null;
63          ResultSet rs = null;
64  
65          try {
66              con = HibernateUtil.getConnection();
67  
68              ps = con.prepareStatement(_GET_BUILD_NUMBER);
69  
70              rs = ps.executeQuery();
71  
72              if (rs.next()) {
73                  int buildNumber = rs.getInt("buildNumber");
74  
75                  if (_log.isDebugEnabled()) {
76                      _log.debug("Build number " + buildNumber);
77                  }
78  
79                  return buildNumber;
80              }
81          }
82          catch (Exception e) {
83              if (_log.isWarnEnabled()) {
84                  _log.warn(e.getMessage());
85              }
86          }
87          finally {
88              DataAccess.cleanUp(con, ps, rs);
89          }
90  
91          // Create tables and populate with default data
92  
93          if (GetterUtil.getBoolean(
94                  PropsUtil.get(PropsUtil.SCHEMA_RUN_ENABLED))) {
95  
96              if (_log.isInfoEnabled()) {
97                  _log.info("Create tables and populate with default data");
98              }
99  
100             createTablesAndPopulate();
101 
102             return getRelease().getBuildNumber();
103         }
104         else {
105             throw new NoSuchReleaseException(
106                 "The database needs to be populated");
107         }
108     }
109 
110     public Release getRelease() throws PortalException, SystemException {
111         Release release = null;
112 
113         try {
114             release = ReleaseUtil.findByPrimaryKey(ReleaseImpl.DEFAULT_ID);
115         }
116         catch (NoSuchReleaseException nsre) {
117             release = ReleaseUtil.create(ReleaseImpl.DEFAULT_ID);
118 
119             Date now = new Date();
120 
121             release.setCreateDate(now);
122             release.setModifiedDate(now);
123 
124             ReleaseUtil.update(release);
125         }
126 
127         return release;
128     }
129 
130     public Release updateRelease(boolean verified)
131         throws PortalException, SystemException {
132 
133         Release release = getRelease();
134 
135         release.setModifiedDate(new Date());
136         release.setBuildNumber(ReleaseInfo.getBuildNumber());
137         release.setBuildDate(ReleaseInfo.getBuildDate());
138         release.setVerified(verified);
139 
140         ReleaseUtil.update(release);
141 
142         return release;
143     }
144 
145     protected void createTablesAndPopulate() throws SystemException {
146         try {
147             DBUtil dbUtil = DBUtil.getInstance();
148 
149             dbUtil.runSQLTemplate("portal-tables.sql", false);
150             dbUtil.runSQLTemplate("portal-data-common.sql", false);
151             dbUtil.runSQLTemplate("portal-data-counter.sql", false);
152 
153             if (!GetterUtil.getBoolean(
154                     PropsUtil.get(PropsUtil.SCHEMA_RUN_MINIMAL))) {
155 
156                 dbUtil.runSQLTemplate("portal-data-sample.vm", false);
157             }
158 
159             dbUtil.runSQLTemplate("portal-data-release.sql", false);
160             dbUtil.runSQLTemplate("indexes.sql", false);
161             dbUtil.runSQLTemplate("sequences.sql", false);
162         }
163         catch (Exception e) {
164             _log.error(e, e);
165 
166             throw new SystemException(e);
167         }
168     }
169 
170     private static final String _GET_BUILD_NUMBER =
171         "select buildNumber from Release_";
172 
173     private static Log _log = LogFactory.getLog(ReleaseLocalServiceImpl.class);
174 
175 }