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