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.db.DB;
29  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
30  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
31  import com.liferay.portal.kernel.log.Log;
32  import com.liferay.portal.kernel.log.LogFactoryUtil;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.PropsKeys;
35  import com.liferay.portal.kernel.util.ReleaseInfo;
36  import com.liferay.portal.model.Release;
37  import com.liferay.portal.model.ReleaseConstants;
38  import com.liferay.portal.service.base.ReleaseLocalServiceBaseImpl;
39  import com.liferay.portal.util.PropsUtil;
40  
41  import java.sql.Connection;
42  import java.sql.PreparedStatement;
43  import java.sql.ResultSet;
44  
45  import java.util.Date;
46  
47  /**
48   * <a href="ReleaseLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   */
52  public class ReleaseLocalServiceImpl extends ReleaseLocalServiceBaseImpl {
53  
54      public Release addRelease() throws SystemException {
55          Release release = releasePersistence.create(
56              ReleaseConstants.DEFAULT_ID);
57  
58          Date now = new Date();
59  
60          release.setCreateDate(now);
61          release.setModifiedDate(now);
62          release.setTestString(ReleaseConstants.TEST_STRING);
63  
64          releasePersistence.update(release, false);
65  
66          return release;
67      }
68  
69      public void createTablesAndPopulate() throws SystemException {
70          try {
71              DB db = DBFactoryUtil.getDB();
72  
73              db.runSQLTemplate("portal-tables.sql", false);
74              db.runSQLTemplate("portal-data-common.sql", false);
75              db.runSQLTemplate("portal-data-counter.sql", false);
76  
77              if (!GetterUtil.getBoolean(
78                      PropsUtil.get(PropsKeys.SCHEMA_RUN_MINIMAL))) {
79  
80                  db.runSQLTemplate("portal-data-sample.vm", false);
81              }
82  
83              db.runSQLTemplate("portal-data-release.sql", false);
84              db.runSQLTemplate("indexes.sql", false);
85              db.runSQLTemplate("sequences.sql", false);
86          }
87          catch (Exception e) {
88              _log.error(e, e);
89  
90              throw new SystemException(e);
91          }
92      }
93  
94      public int getBuildNumberOrCreate()
95          throws PortalException, SystemException {
96  
97          // Get release build number
98  
99          Connection con = null;
100         PreparedStatement ps = null;
101         ResultSet rs = null;
102 
103         try {
104             con = DataAccess.getConnection();
105 
106             ps = con.prepareStatement(_GET_BUILD_NUMBER);
107 
108             rs = ps.executeQuery();
109 
110             if (rs.next()) {
111                 int buildNumber = rs.getInt("buildNumber");
112 
113                 if (_log.isDebugEnabled()) {
114                     _log.debug("Build number " + buildNumber);
115                 }
116 
117                 testSupportsStringCaseSensitiveQuery();
118 
119                 return buildNumber;
120             }
121         }
122         catch (Exception e) {
123             if (_log.isWarnEnabled()) {
124                 _log.warn(e.getMessage());
125             }
126         }
127         finally {
128             DataAccess.cleanUp(con, ps, rs);
129         }
130 
131         // Create tables and populate with default data
132 
133         if (GetterUtil.getBoolean(
134                 PropsUtil.get(PropsKeys.SCHEMA_RUN_ENABLED))) {
135 
136             if (_log.isInfoEnabled()) {
137                 _log.info("Create tables and populate with default data");
138             }
139 
140             releaseLocalService.createTablesAndPopulate();
141 
142             testSupportsStringCaseSensitiveQuery();
143 
144             return getRelease().getBuildNumber();
145         }
146         else {
147             throw new NoSuchReleaseException(
148                 "The database needs to be populated");
149         }
150     }
151 
152     public Release getRelease() throws SystemException {
153         Release release = releasePersistence.fetchByPrimaryKey(
154             ReleaseConstants.DEFAULT_ID);
155 
156         if (release == null) {
157             release = releaseLocalService.addRelease();
158         }
159 
160         return release;
161     }
162 
163     public Release updateRelease(boolean verified) throws SystemException {
164         Release release = getRelease();
165 
166         release.setModifiedDate(new Date());
167         release.setBuildNumber(ReleaseInfo.getBuildNumber());
168         release.setBuildDate(ReleaseInfo.getBuildDate());
169         release.setVerified(verified);
170 
171         releasePersistence.update(release, false);
172 
173         return release;
174     }
175 
176     protected void testSupportsStringCaseSensitiveQuery()
177         throws SystemException {
178 
179         DB db = DBFactoryUtil.getDB();
180 
181         int count = testSupportsStringCaseSensitiveQuery(
182             ReleaseConstants.TEST_STRING);
183 
184         if (count == 0) {
185             try {
186                 db.runSQL(
187                     "alter table Release_ add testString VARCHAR(1024) null");
188             }
189             catch (Exception e) {
190                 if (_log.isDebugEnabled()) {
191                     _log.debug(e.getMessage());
192                 }
193             }
194 
195             try {
196                 db.runSQL(
197                     "update Release_ set testString = '" +
198                         ReleaseConstants.TEST_STRING + "'");
199             }
200             catch (Exception e) {
201                 if (_log.isDebugEnabled()) {
202                     _log.debug(e.getMessage());
203                 }
204             }
205 
206             count = testSupportsStringCaseSensitiveQuery(
207                 ReleaseConstants.TEST_STRING);
208         }
209 
210         if (count == 0) {
211             throw new SystemException(
212                 "Release_ table was not initialized properly");
213         }
214 
215         count = testSupportsStringCaseSensitiveQuery(
216             ReleaseConstants.TEST_STRING.toUpperCase());
217 
218         if (count == 0) {
219             db.setSupportsStringCaseSensitiveQuery(true);
220         }
221         else {
222             db.setSupportsStringCaseSensitiveQuery(false);
223         }
224     }
225 
226     protected int testSupportsStringCaseSensitiveQuery(String testString) {
227         int count = 0;
228 
229         Connection con = null;
230         PreparedStatement ps = null;
231         ResultSet rs = null;
232 
233         try {
234             con = DataAccess.getConnection();
235 
236             ps = con.prepareStatement(_TEST_DATABASE_STRING_CASE_SENSITIVITY);
237 
238             ps.setString(1, testString);
239 
240             rs = ps.executeQuery();
241 
242             if (rs.next()) {
243                 count = rs.getInt(1);
244             }
245         }
246         catch (Exception e) {
247             if (_log.isWarnEnabled()) {
248                 _log.warn(e.getMessage());
249             }
250         }
251         finally {
252             DataAccess.cleanUp(con, ps, rs);
253         }
254 
255         return count;
256     }
257 
258     private static final String _GET_BUILD_NUMBER =
259         "select buildNumber from Release_";
260 
261     private static final String _TEST_DATABASE_STRING_CASE_SENSITIVITY =
262         "select count(*) from Release_ where testString = ?";
263 
264     private static Log _log =
265         LogFactoryUtil.getLog(ReleaseLocalServiceImpl.class);
266 
267 }