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                    servletContextName = servletContextName.toLowerCase();
177    
178                    Release release = null;
179    
180                    if (servletContextName.equals(
181                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
182    
183                            release = releasePersistence.findByPrimaryKey(
184                                    ReleaseConstants.DEFAULT_ID);
185                    }
186                    else {
187                            release = releasePersistence.findByServletContextName(
188                                    servletContextName);
189                    }
190    
191                    return release;
192            }
193    
194            public Release updateRelease(
195                            long releaseId, int buildNumber, Date buildDate, boolean verified)
196                    throws PortalException, SystemException {
197    
198                    Release release = releasePersistence.findByPrimaryKey(releaseId);
199    
200                    release.setModifiedDate(new Date());
201                    release.setBuildNumber(buildNumber);
202                    release.setBuildDate(buildDate);
203                    release.setVerified(verified);
204    
205                    releasePersistence.update(release, false);
206    
207                    return release;
208            }
209    
210            protected void testSupportsStringCaseSensitiveQuery()
211                    throws SystemException {
212    
213                    DB db = DBFactoryUtil.getDB();
214    
215                    int count = testSupportsStringCaseSensitiveQuery(
216                            ReleaseConstants.TEST_STRING);
217    
218                    if (count == 0) {
219                            try {
220                                    db.runSQL(
221                                            "alter table Release_ add testString VARCHAR(1024) null");
222                            }
223                            catch (Exception e) {
224                                    if (_log.isDebugEnabled()) {
225                                            _log.debug(e.getMessage());
226                                    }
227                            }
228    
229                            try {
230                                    db.runSQL(
231                                            "update Release_ set testString = '" +
232                                                    ReleaseConstants.TEST_STRING + "'");
233                            }
234                            catch (Exception e) {
235                                    if (_log.isDebugEnabled()) {
236                                            _log.debug(e.getMessage());
237                                    }
238                            }
239    
240                            count = testSupportsStringCaseSensitiveQuery(
241                                    ReleaseConstants.TEST_STRING);
242                    }
243    
244                    if (count == 0) {
245                            throw new SystemException(
246                                    "Release_ table was not initialized properly");
247                    }
248    
249                    count = testSupportsStringCaseSensitiveQuery(
250                            ReleaseConstants.TEST_STRING.toUpperCase());
251    
252                    if (count == 0) {
253                            db.setSupportsStringCaseSensitiveQuery(true);
254                    }
255                    else {
256                            db.setSupportsStringCaseSensitiveQuery(false);
257                    }
258            }
259    
260            protected int testSupportsStringCaseSensitiveQuery(String testString) {
261                    int count = 0;
262    
263                    Connection con = null;
264                    PreparedStatement ps = null;
265                    ResultSet rs = null;
266    
267                    try {
268                            con = DataAccess.getConnection();
269    
270                            ps = con.prepareStatement(_TEST_DATABASE_STRING_CASE_SENSITIVITY);
271    
272                            ps.setLong(1, ReleaseConstants.DEFAULT_ID);
273                            ps.setString(2, testString);
274    
275                            rs = ps.executeQuery();
276    
277                            if (rs.next()) {
278                                    count = rs.getInt(1);
279                            }
280                    }
281                    catch (Exception e) {
282                            if (_log.isWarnEnabled()) {
283                                    _log.warn(e.getMessage());
284                            }
285                    }
286                    finally {
287                            DataAccess.cleanUp(con, ps, rs);
288                    }
289    
290                    return count;
291            }
292    
293            private static final String _GET_BUILD_NUMBER =
294                    "select buildNumber from Release_ where releaseId = ?";
295    
296            private static final String _TEST_DATABASE_STRING_CASE_SENSITIVITY =
297                    "select count(*) from Release_ where releaseId = ? and testString = ?";
298    
299            private static Log _log = LogFactoryUtil.getLog(
300                    ReleaseLocalServiceImpl.class);
301    
302    }