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, false);
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                                    testSupportsStringCaseSensitiveQuery();
134    
135                                    return buildNumber;
136                            }
137                    }
138                    catch (Exception e) {
139                            if (_log.isWarnEnabled()) {
140                                    _log.warn(e.getMessage());
141                            }
142                    }
143                    finally {
144                            DataAccess.cleanUp(con, ps, rs);
145                    }
146    
147                    // Create tables and populate with default data
148    
149                    if (GetterUtil.getBoolean(
150                                    PropsUtil.get(PropsKeys.SCHEMA_RUN_ENABLED))) {
151    
152                            releaseLocalService.createTablesAndPopulate();
153    
154                            testSupportsStringCaseSensitiveQuery();
155    
156                            Release release = getRelease(
157                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
158                                    ReleaseInfo.getBuildNumber());
159    
160                            return release.getBuildNumber();
161                    }
162                    else {
163                            throw new NoSuchReleaseException(
164                                    "The database needs to be populated");
165                    }
166            }
167    
168            public Release getRelease(String servletContextName, int buildNumber)
169                    throws PortalException, SystemException {
170    
171                    if (Validator.isNull(servletContextName)) {
172                            throw new IllegalArgumentException(
173                                    "Servlet context name cannot be null");
174                    }
175    
176                    Release release = null;
177    
178                    if (servletContextName.equals(
179                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
180    
181                            release = releasePersistence.findByPrimaryKey(
182                                    ReleaseConstants.DEFAULT_ID);
183                    }
184                    else {
185                            release = releasePersistence.findByServletContextName(
186                                    servletContextName);
187                    }
188    
189                    return release;
190            }
191    
192            public Release updateRelease(
193                            long releaseId, int buildNumber, Date buildDate, boolean verified)
194                    throws PortalException, SystemException {
195    
196                    Release release = releasePersistence.findByPrimaryKey(releaseId);
197    
198                    release.setModifiedDate(new Date());
199                    release.setBuildNumber(buildNumber);
200                    release.setBuildDate(buildDate);
201                    release.setVerified(verified);
202    
203                    releasePersistence.update(release, false);
204    
205                    return release;
206            }
207    
208            protected void testSupportsStringCaseSensitiveQuery()
209                    throws SystemException {
210    
211                    DB db = DBFactoryUtil.getDB();
212    
213                    int count = testSupportsStringCaseSensitiveQuery(
214                            ReleaseConstants.TEST_STRING);
215    
216                    if (count == 0) {
217                            try {
218                                    db.runSQL(
219                                            "alter table Release_ add testString VARCHAR(1024) null");
220                            }
221                            catch (Exception e) {
222                                    if (_log.isDebugEnabled()) {
223                                            _log.debug(e.getMessage());
224                                    }
225                            }
226    
227                            try {
228                                    db.runSQL(
229                                            "update Release_ set testString = '" +
230                                                    ReleaseConstants.TEST_STRING + "'");
231                            }
232                            catch (Exception e) {
233                                    if (_log.isDebugEnabled()) {
234                                            _log.debug(e.getMessage());
235                                    }
236                            }
237    
238                            count = testSupportsStringCaseSensitiveQuery(
239                                    ReleaseConstants.TEST_STRING);
240                    }
241    
242                    if (count == 0) {
243                            throw new SystemException(
244                                    "Release_ table was not initialized properly");
245                    }
246    
247                    count = testSupportsStringCaseSensitiveQuery(
248                            ReleaseConstants.TEST_STRING.toUpperCase());
249    
250                    if (count == 0) {
251                            db.setSupportsStringCaseSensitiveQuery(true);
252                    }
253                    else {
254                            db.setSupportsStringCaseSensitiveQuery(false);
255                    }
256            }
257    
258            protected int testSupportsStringCaseSensitiveQuery(String testString) {
259                    int count = 0;
260    
261                    Connection con = null;
262                    PreparedStatement ps = null;
263                    ResultSet rs = null;
264    
265                    try {
266                            con = DataAccess.getConnection();
267    
268                            ps = con.prepareStatement(_TEST_DATABASE_STRING_CASE_SENSITIVITY);
269    
270                            ps.setLong(1, ReleaseConstants.DEFAULT_ID);
271                            ps.setString(2, testString);
272    
273                            rs = ps.executeQuery();
274    
275                            if (rs.next()) {
276                                    count = rs.getInt(1);
277                            }
278                    }
279                    catch (Exception e) {
280                            if (_log.isWarnEnabled()) {
281                                    _log.warn(e.getMessage());
282                            }
283                    }
284                    finally {
285                            DataAccess.cleanUp(con, ps, rs);
286                    }
287    
288                    return count;
289            }
290    
291            private static final String _GET_BUILD_NUMBER =
292                    "select buildNumber from Release_ where releaseId = ?";
293    
294            private static final String _TEST_DATABASE_STRING_CASE_SENSITIVITY =
295                    "select count(*) from Release_ where releaseId = ? and testString = ?";
296    
297            private static Log _log = LogFactoryUtil.getLog(
298                    ReleaseLocalServiceImpl.class);
299    
300    }