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