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