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