1   /**
2    * Copyright (c) 2000-2009 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.dao.jdbc.DataAccess;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.ReleaseInfo;
33  import com.liferay.portal.model.Release;
34  import com.liferay.portal.model.impl.ReleaseImpl;
35  import com.liferay.portal.service.base.ReleaseLocalServiceBaseImpl;
36  import com.liferay.portal.tools.sql.DBUtil;
37  import com.liferay.portal.util.PropsKeys;
38  import com.liferay.portal.util.PropsUtil;
39  
40  import java.sql.Connection;
41  import java.sql.PreparedStatement;
42  import java.sql.ResultSet;
43  
44  import java.util.Date;
45  
46  /**
47   * <a href="ReleaseLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class ReleaseLocalServiceImpl extends ReleaseLocalServiceBaseImpl {
53  
54      public int getBuildNumberOrCreate()
55          throws PortalException, SystemException {
56  
57          // Get release build number
58  
59          Connection con = null;
60          PreparedStatement ps = null;
61          ResultSet rs = null;
62  
63          try {
64              con = DataAccess.getConnection();
65  
66              ps = con.prepareStatement(_GET_BUILD_NUMBER);
67  
68              rs = ps.executeQuery();
69  
70              if (rs.next()) {
71                  int buildNumber = rs.getInt("buildNumber");
72  
73                  if (_log.isDebugEnabled()) {
74                      _log.debug("Build number " + buildNumber);
75                  }
76  
77                  return buildNumber;
78              }
79          }
80          catch (Exception e) {
81              if (_log.isWarnEnabled()) {
82                  _log.warn(e.getMessage());
83              }
84          }
85          finally {
86              DataAccess.cleanUp(con, ps, rs);
87          }
88  
89          // Create tables and populate with default data
90  
91          if (GetterUtil.getBoolean(
92                  PropsUtil.get(PropsKeys.SCHEMA_RUN_ENABLED))) {
93  
94              if (_log.isInfoEnabled()) {
95                  _log.info("Create tables and populate with default data");
96              }
97  
98              createTablesAndPopulate();
99  
100             return getRelease().getBuildNumber();
101         }
102         else {
103             throw new NoSuchReleaseException(
104                 "The database needs to be populated");
105         }
106     }
107 
108     public Release getRelease() throws SystemException {
109         Release release = releasePersistence.fetchByPrimaryKey(
110             ReleaseImpl.DEFAULT_ID);
111 
112         if (release == null) {
113             release = releasePersistence.create(ReleaseImpl.DEFAULT_ID);
114 
115             Date now = new Date();
116 
117             release.setCreateDate(now);
118             release.setModifiedDate(now);
119 
120             releasePersistence.update(release, false);
121         }
122 
123         return release;
124     }
125 
126     public Release updateRelease(boolean verified) throws SystemException {
127         Release release = getRelease();
128 
129         release.setModifiedDate(new Date());
130         release.setBuildNumber(ReleaseInfo.getBuildNumber());
131         release.setBuildDate(ReleaseInfo.getBuildDate());
132         release.setVerified(verified);
133 
134         releasePersistence.update(release, false);
135 
136         return release;
137     }
138 
139     protected void createTablesAndPopulate() throws SystemException {
140         try {
141             DBUtil dbUtil = DBUtil.getInstance();
142 
143             dbUtil.runSQLTemplate("portal-tables.sql", false);
144             dbUtil.runSQLTemplate("portal-data-common.sql", false);
145             dbUtil.runSQLTemplate("portal-data-counter.sql", false);
146 
147             if (!GetterUtil.getBoolean(
148                     PropsUtil.get(PropsKeys.SCHEMA_RUN_MINIMAL))) {
149 
150                 dbUtil.runSQLTemplate("portal-data-sample.vm", false);
151             }
152 
153             dbUtil.runSQLTemplate("portal-data-release.sql", false);
154             dbUtil.runSQLTemplate("indexes.sql", false);
155             dbUtil.runSQLTemplate("sequences.sql", false);
156             dbUtil.runSQLTemplate("quartz-tables.sql", false);
157         }
158         catch (Exception e) {
159             _log.error(e, e);
160 
161             throw new SystemException(e);
162         }
163     }
164 
165     private static final String _GET_BUILD_NUMBER =
166         "select buildNumber from Release_";
167 
168     private static Log _log =
169         LogFactoryUtil.getLog(ReleaseLocalServiceImpl.class);
170 
171 }