1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.NoSuchReleaseException;
18  import com.liferay.portal.PortalException;
19  import com.liferay.portal.SystemException;
20  import com.liferay.portal.kernel.dao.db.DB;
21  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
22  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
23  import com.liferay.portal.kernel.dao.shard.ShardUtil;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.PropsKeys;
28  import com.liferay.portal.kernel.util.ReleaseInfo;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Release;
31  import com.liferay.portal.model.ReleaseConstants;
32  import com.liferay.portal.service.base.ReleaseLocalServiceBaseImpl;
33  import com.liferay.portal.util.PropsUtil;
34  
35  import java.sql.Connection;
36  import java.sql.PreparedStatement;
37  import java.sql.ResultSet;
38  
39  import java.util.Date;
40  
41  /**
42   * <a href="ReleaseLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class ReleaseLocalServiceImpl extends ReleaseLocalServiceBaseImpl {
47  
48      public Release addRelease(String servletContextName, int buildNumber)
49          throws SystemException {
50  
51          Release release = null;
52  
53          if (servletContextName.equals(
54                  ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
55  
56              release = releasePersistence.create(
57                  ReleaseConstants.DEFAULT_ID);
58          }
59          else {
60              long releaseId = counterLocalService.increment();
61  
62              release = releasePersistence.create(releaseId);
63          }
64  
65          Date now = new Date();
66  
67          release.setCreateDate(now);
68          release.setModifiedDate(now);
69          release.setServletContextName(servletContextName);
70          release.setBuildNumber(buildNumber);
71  
72          if (servletContextName.equals(
73                  ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
74  
75              release.setTestString(ReleaseConstants.TEST_STRING);
76          }
77  
78          releasePersistence.update(release, false);
79  
80          return release;
81      }
82  
83      public void createTablesAndPopulate() throws SystemException {
84          try {
85              if (_log.isInfoEnabled()) {
86                  _log.info("Create tables and populate with default data");
87              }
88  
89              DB db = DBFactoryUtil.getDB();
90  
91              db.runSQLTemplate("portal-tables.sql", false);
92              db.runSQLTemplate("portal-data-common.sql", false);
93              db.runSQLTemplate("portal-data-counter.sql", false);
94  
95              if (!GetterUtil.getBoolean(
96                      PropsUtil.get(PropsKeys.SCHEMA_RUN_MINIMAL)) &&
97                  !ShardUtil.isEnabled()) {
98  
99                  db.runSQLTemplate("portal-data-sample.vm", false);
100             }
101 
102             db.runSQLTemplate("portal-data-release.sql", false);
103             db.runSQLTemplate("indexes.sql", false);
104             db.runSQLTemplate("sequences.sql", false);
105         }
106         catch (Exception e) {
107             _log.error(e, e);
108 
109             throw new SystemException(e);
110         }
111     }
112 
113     public int getBuildNumberOrCreate()
114         throws PortalException, SystemException {
115 
116         // Get release build number
117 
118         Connection con = null;
119         PreparedStatement ps = null;
120         ResultSet rs = null;
121 
122         try {
123             con = DataAccess.getConnection();
124 
125             ps = con.prepareStatement(_GET_BUILD_NUMBER);
126 
127             ps.setLong(1, ReleaseConstants.DEFAULT_ID);
128 
129             rs = ps.executeQuery();
130 
131             if (rs.next()) {
132                 int buildNumber = rs.getInt("buildNumber");
133 
134                 if (_log.isDebugEnabled()) {
135                     _log.debug("Build number " + buildNumber);
136                 }
137 
138                 testSupportsStringCaseSensitiveQuery();
139 
140                 return buildNumber;
141             }
142         }
143         catch (Exception e) {
144             if (_log.isWarnEnabled()) {
145                 _log.warn(e.getMessage());
146             }
147         }
148         finally {
149             DataAccess.cleanUp(con, ps, rs);
150         }
151 
152         // Create tables and populate with default data
153 
154         if (GetterUtil.getBoolean(
155                 PropsUtil.get(PropsKeys.SCHEMA_RUN_ENABLED))) {
156 
157             releaseLocalService.createTablesAndPopulate();
158 
159             testSupportsStringCaseSensitiveQuery();
160 
161             Release release = getRelease(
162                 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
163                 ReleaseInfo.getBuildNumber());
164 
165             return release.getBuildNumber();
166         }
167         else {
168             throw new NoSuchReleaseException(
169                 "The database needs to be populated");
170         }
171     }
172 
173     public Release getRelease(String servletContextName, int buildNumber)
174         throws PortalException, SystemException {
175 
176         if (Validator.isNull(servletContextName)) {
177             throw new IllegalArgumentException(
178                 "Servlet context name cannot be null");
179         }
180 
181         servletContextName = servletContextName.toLowerCase();
182 
183         Release release = null;
184 
185         if (servletContextName.equals(
186                 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
187 
188             release = releasePersistence.findByPrimaryKey(
189                 ReleaseConstants.DEFAULT_ID);
190         }
191         else {
192             release = releasePersistence.findByServletContextName(
193                 servletContextName);
194         }
195 
196         return release;
197     }
198 
199     public Release updateRelease(
200             long releaseId, int buildNumber, Date buildDate, boolean verified)
201         throws PortalException, SystemException {
202 
203         Release release = releasePersistence.findByPrimaryKey(releaseId);
204 
205         release.setModifiedDate(new Date());
206         release.setBuildNumber(buildNumber);
207         release.setBuildDate(buildDate);
208         release.setVerified(verified);
209 
210         releasePersistence.update(release, false);
211 
212         return release;
213     }
214 
215     protected void testSupportsStringCaseSensitiveQuery()
216         throws SystemException {
217 
218         DB db = DBFactoryUtil.getDB();
219 
220         int count = testSupportsStringCaseSensitiveQuery(
221             ReleaseConstants.TEST_STRING);
222 
223         if (count == 0) {
224             try {
225                 db.runSQL(
226                     "alter table Release_ add testString VARCHAR(1024) null");
227             }
228             catch (Exception e) {
229                 if (_log.isDebugEnabled()) {
230                     _log.debug(e.getMessage());
231                 }
232             }
233 
234             try {
235                 db.runSQL(
236                     "update Release_ set testString = '" +
237                         ReleaseConstants.TEST_STRING + "'");
238             }
239             catch (Exception e) {
240                 if (_log.isDebugEnabled()) {
241                     _log.debug(e.getMessage());
242                 }
243             }
244 
245             count = testSupportsStringCaseSensitiveQuery(
246                 ReleaseConstants.TEST_STRING);
247         }
248 
249         if (count == 0) {
250             throw new SystemException(
251                 "Release_ table was not initialized properly");
252         }
253 
254         count = testSupportsStringCaseSensitiveQuery(
255             ReleaseConstants.TEST_STRING.toUpperCase());
256 
257         if (count == 0) {
258             db.setSupportsStringCaseSensitiveQuery(true);
259         }
260         else {
261             db.setSupportsStringCaseSensitiveQuery(false);
262         }
263     }
264 
265     protected int testSupportsStringCaseSensitiveQuery(String testString) {
266         int count = 0;
267 
268         Connection con = null;
269         PreparedStatement ps = null;
270         ResultSet rs = null;
271 
272         try {
273             con = DataAccess.getConnection();
274 
275             ps = con.prepareStatement(_TEST_DATABASE_STRING_CASE_SENSITIVITY);
276 
277             ps.setLong(1, ReleaseConstants.DEFAULT_ID);
278             ps.setString(2, testString);
279 
280             rs = ps.executeQuery();
281 
282             if (rs.next()) {
283                 count = rs.getInt(1);
284             }
285         }
286         catch (Exception e) {
287             if (_log.isWarnEnabled()) {
288                 _log.warn(e.getMessage());
289             }
290         }
291         finally {
292             DataAccess.cleanUp(con, ps, rs);
293         }
294 
295         return count;
296     }
297 
298     private static final String _GET_BUILD_NUMBER =
299         "select buildNumber from Release_ where releaseId = ?";
300 
301     private static final String _TEST_DATABASE_STRING_CASE_SENSITIVITY =
302         "select count(*) from Release_ where releaseId = ? and testString = ?";
303 
304     private static Log _log = LogFactoryUtil.getLog(
305         ReleaseLocalServiceImpl.class);
306 
307 }