001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.NoSuchReleaseException;
018    import com.liferay.portal.kernel.dao.db.DB;
019    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
020    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
021    import com.liferay.portal.kernel.dao.shard.ShardUtil;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.PropsKeys;
028    import com.liferay.portal.kernel.util.ReleaseInfo;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.Release;
031    import com.liferay.portal.model.ReleaseConstants;
032    import com.liferay.portal.service.base.ReleaseLocalServiceBaseImpl;
033    import com.liferay.portal.util.PropsUtil;
034    import com.liferay.portal.util.PropsValues;
035    
036    import java.sql.Connection;
037    import java.sql.PreparedStatement;
038    import java.sql.ResultSet;
039    
040    import java.util.Date;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class ReleaseLocalServiceImpl extends ReleaseLocalServiceBaseImpl {
046    
047            public Release addRelease(String servletContextName, int buildNumber)
048                    throws SystemException {
049    
050                    Release release = null;
051    
052                    if (servletContextName.equals(
053                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
054    
055                            release = releasePersistence.create(ReleaseConstants.DEFAULT_ID);
056                    }
057                    else {
058                            long releaseId = counterLocalService.increment();
059    
060                            release = releasePersistence.create(releaseId);
061                    }
062    
063                    Date now = new Date();
064    
065                    release.setCreateDate(now);
066                    release.setModifiedDate(now);
067                    release.setServletContextName(servletContextName);
068                    release.setBuildNumber(buildNumber);
069    
070                    if (servletContextName.equals(
071                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
072    
073                            release.setTestString(ReleaseConstants.TEST_STRING);
074                    }
075    
076                    releasePersistence.update(release);
077    
078                    return release;
079            }
080    
081            public void createTablesAndPopulate() throws SystemException {
082                    try {
083                            if (_log.isInfoEnabled()) {
084                                    _log.info("Create tables and populate with default data");
085                            }
086    
087                            DB db = DBFactoryUtil.getDB();
088    
089                            db.runSQLTemplate("portal-tables.sql", false);
090                            db.runSQLTemplate("portal-data-common.sql", false);
091                            db.runSQLTemplate("portal-data-counter.sql", false);
092    
093                            if (!PropsValues.SCHEMA_RUN_MINIMAL && !ShardUtil.isEnabled()) {
094                                    db.runSQLTemplate("portal-data-sample.vm", false);
095                            }
096    
097                            db.runSQLTemplate("portal-data-release.sql", false);
098                            db.runSQLTemplate("indexes.sql", false);
099                            db.runSQLTemplate("sequences.sql", false);
100                    }
101                    catch (Exception e) {
102                            _log.error(e, e);
103    
104                            throw new SystemException(e);
105                    }
106            }
107    
108            public int getBuildNumberOrCreate()
109                    throws PortalException, SystemException {
110    
111                    // Get release build number
112    
113                    Connection con = null;
114                    PreparedStatement ps = null;
115                    ResultSet rs = null;
116    
117                    try {
118                            con = DataAccess.getConnection();
119    
120                            ps = con.prepareStatement(_GET_BUILD_NUMBER);
121    
122                            ps.setLong(1, ReleaseConstants.DEFAULT_ID);
123    
124                            rs = ps.executeQuery();
125    
126                            if (rs.next()) {
127                                    int buildNumber = rs.getInt("buildNumber");
128    
129                                    if (_log.isDebugEnabled()) {
130                                            _log.debug("Build number " + buildNumber);
131                                    }
132    
133                                    DB db = DBFactoryUtil.getDB();
134    
135                                    try {
136                                            db.runSQL("alter table Release_ add state_ INTEGER");
137                                    }
138                                    catch (Exception e) {
139                                            if (_log.isDebugEnabled()) {
140                                                    _log.debug(e.getMessage());
141                                            }
142                                    }
143    
144                                    testSupportsStringCaseSensitiveQuery();
145    
146                                    return buildNumber;
147                            }
148                    }
149                    catch (Exception e) {
150                            if (_log.isWarnEnabled()) {
151                                    _log.warn(e.getMessage());
152                            }
153                    }
154                    finally {
155                            DataAccess.cleanUp(con, ps, rs);
156                    }
157    
158                    // Create tables and populate with default data
159    
160                    if (GetterUtil.getBoolean(
161                                    PropsUtil.get(PropsKeys.SCHEMA_RUN_ENABLED))) {
162    
163                            releaseLocalService.createTablesAndPopulate();
164    
165                            testSupportsStringCaseSensitiveQuery();
166    
167                            Release release = getRelease(
168                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
169                                    ReleaseInfo.getParentBuildNumber());
170    
171                            return release.getBuildNumber();
172                    }
173                    else {
174                            throw new NoSuchReleaseException(
175                                    "The database needs to be populated");
176                    }
177            }
178    
179            public Release getRelease(String servletContextName, int buildNumber)
180                    throws PortalException, SystemException {
181    
182                    if (Validator.isNull(servletContextName)) {
183                            throw new IllegalArgumentException(
184                                    "Servlet context name cannot be null");
185                    }
186    
187                    Release release = null;
188    
189                    if (servletContextName.equals(
190                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
191    
192                            release = releasePersistence.findByPrimaryKey(
193                                    ReleaseConstants.DEFAULT_ID);
194                    }
195                    else {
196                            release = releasePersistence.findByServletContextName(
197                                    servletContextName);
198                    }
199    
200                    return release;
201            }
202    
203            public Release updateRelease(
204                            long releaseId, int buildNumber, Date buildDate, boolean verified)
205                    throws PortalException, SystemException {
206    
207                    Release release = releasePersistence.findByPrimaryKey(releaseId);
208    
209                    release.setModifiedDate(new Date());
210                    release.setBuildNumber(buildNumber);
211                    release.setBuildDate(buildDate);
212                    release.setVerified(verified);
213    
214                    releasePersistence.update(release);
215    
216                    return release;
217            }
218    
219            protected void testSupportsStringCaseSensitiveQuery()
220                    throws SystemException {
221    
222                    DB db = DBFactoryUtil.getDB();
223    
224                    int count = testSupportsStringCaseSensitiveQuery(
225                            ReleaseConstants.TEST_STRING);
226    
227                    if (count == 0) {
228                            try {
229                                    db.runSQL(
230                                            "alter table Release_ add testString VARCHAR(1024) null");
231                            }
232                            catch (Exception e) {
233                                    if (_log.isDebugEnabled()) {
234                                            _log.debug(e.getMessage());
235                                    }
236                            }
237    
238                            try {
239                                    db.runSQL(
240                                            "update Release_ set testString = '" +
241                                                    ReleaseConstants.TEST_STRING + "'");
242                            }
243                            catch (Exception e) {
244                                    if (_log.isDebugEnabled()) {
245                                            _log.debug(e.getMessage());
246                                    }
247                            }
248    
249                            count = testSupportsStringCaseSensitiveQuery(
250                                    ReleaseConstants.TEST_STRING);
251                    }
252    
253                    if (count == 0) {
254                            throw new SystemException(
255                                    "Release_ table was not initialized properly");
256                    }
257    
258                    count = testSupportsStringCaseSensitiveQuery(
259                            ReleaseConstants.TEST_STRING.toUpperCase());
260    
261                    if (count == 0) {
262                            db.setSupportsStringCaseSensitiveQuery(true);
263                    }
264                    else {
265                            db.setSupportsStringCaseSensitiveQuery(false);
266                    }
267            }
268    
269            protected int testSupportsStringCaseSensitiveQuery(String testString) {
270                    int count = 0;
271    
272                    Connection con = null;
273                    PreparedStatement ps = null;
274                    ResultSet rs = null;
275    
276                    try {
277                            con = DataAccess.getConnection();
278    
279                            ps = con.prepareStatement(_TEST_DATABASE_STRING_CASE_SENSITIVITY);
280    
281                            ps.setLong(1, ReleaseConstants.DEFAULT_ID);
282                            ps.setString(2, testString);
283    
284                            rs = ps.executeQuery();
285    
286                            if (rs.next()) {
287                                    count = rs.getInt(1);
288                            }
289                    }
290                    catch (Exception e) {
291                            if (_log.isWarnEnabled()) {
292                                    _log.warn(e.getMessage());
293                            }
294                    }
295                    finally {
296                            DataAccess.cleanUp(con, ps, rs);
297                    }
298    
299                    return count;
300            }
301    
302            private static final String _GET_BUILD_NUMBER =
303                    "select buildNumber from Release_ where releaseId = ?";
304    
305            private static final String _TEST_DATABASE_STRING_CASE_SENSITIVITY =
306                    "select count(*) from Release_ where releaseId = ? and testString = ?";
307    
308            private static Log _log = LogFactoryUtil.getLog(
309                    ReleaseLocalServiceImpl.class);
310    
311    }