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