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