001    /**
002     * Copyright (c) 2000-2010 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    
035    import java.sql.Connection;
036    import java.sql.PreparedStatement;
037    import java.sql.ResultSet;
038    
039    import java.util.Date;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class ReleaseLocalServiceImpl extends ReleaseLocalServiceBaseImpl {
045    
046            public Release addRelease(String servletContextName, int buildNumber)
047                    throws SystemException {
048    
049                    Release release = null;
050    
051                    if (servletContextName.equals(
052                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
053    
054                            release = releasePersistence.create(
055                                    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 (!GetterUtil.getBoolean(
094                                            PropsUtil.get(PropsKeys.SCHEMA_RUN_MINIMAL)) &&
095                                    !ShardUtil.isEnabled()) {
096    
097                                    db.runSQLTemplate("portal-data-sample.vm", false);
098                            }
099    
100                            db.runSQLTemplate("portal-data-release.sql", false);
101                            db.runSQLTemplate("indexes.sql", false);
102                            db.runSQLTemplate("sequences.sql", false);
103                    }
104                    catch (Exception e) {
105                            _log.error(e, e);
106    
107                            throw new SystemException(e);
108                    }
109            }
110    
111            public int getBuildNumberOrCreate()
112                    throws PortalException, SystemException {
113    
114                    // Get release build number
115    
116                    Connection con = null;
117                    PreparedStatement ps = null;
118                    ResultSet rs = null;
119    
120                    try {
121                            con = DataAccess.getConnection();
122    
123                            ps = con.prepareStatement(_GET_BUILD_NUMBER);
124    
125                            ps.setLong(1, ReleaseConstants.DEFAULT_ID);
126    
127                            rs = ps.executeQuery();
128    
129                            if (rs.next()) {
130                                    int buildNumber = rs.getInt("buildNumber");
131    
132                                    if (_log.isDebugEnabled()) {
133                                            _log.debug("Build number " + buildNumber);
134                                    }
135    
136                                    testSupportsStringCaseSensitiveQuery();
137    
138                                    return buildNumber;
139                            }
140                    }
141                    catch (Exception e) {
142                            if (_log.isWarnEnabled()) {
143                                    _log.warn(e.getMessage());
144                            }
145                    }
146                    finally {
147                            DataAccess.cleanUp(con, ps, rs);
148                    }
149    
150                    // Create tables and populate with default data
151    
152                    if (GetterUtil.getBoolean(
153                                    PropsUtil.get(PropsKeys.SCHEMA_RUN_ENABLED))) {
154    
155                            releaseLocalService.createTablesAndPopulate();
156    
157                            testSupportsStringCaseSensitiveQuery();
158    
159                            Release release = getRelease(
160                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
161                                    ReleaseInfo.getBuildNumber());
162    
163                            return release.getBuildNumber();
164                    }
165                    else {
166                            throw new NoSuchReleaseException(
167                                    "The database needs to be populated");
168                    }
169            }
170    
171            public Release getRelease(String servletContextName, int buildNumber)
172                    throws PortalException, SystemException {
173    
174                    if (Validator.isNull(servletContextName)) {
175                            throw new IllegalArgumentException(
176                                    "Servlet context name cannot be null");
177                    }
178    
179                    servletContextName = servletContextName.toLowerCase();
180    
181                    Release release = null;
182    
183                    if (servletContextName.equals(
184                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
185    
186                            release = releasePersistence.findByPrimaryKey(
187                                    ReleaseConstants.DEFAULT_ID);
188                    }
189                    else {
190                            release = releasePersistence.findByServletContextName(
191                                    servletContextName);
192                    }
193    
194                    return release;
195            }
196    
197            public Release updateRelease(
198                            long releaseId, int buildNumber, Date buildDate, boolean verified)
199                    throws PortalException, SystemException {
200    
201                    Release release = releasePersistence.findByPrimaryKey(releaseId);
202    
203                    release.setModifiedDate(new Date());
204                    release.setBuildNumber(buildNumber);
205                    release.setBuildDate(buildDate);
206                    release.setVerified(verified);
207    
208                    releasePersistence.update(release, false);
209    
210                    return release;
211            }
212    
213            protected void testSupportsStringCaseSensitiveQuery()
214                    throws SystemException {
215    
216                    DB db = DBFactoryUtil.getDB();
217    
218                    int count = testSupportsStringCaseSensitiveQuery(
219                            ReleaseConstants.TEST_STRING);
220    
221                    if (count == 0) {
222                            try {
223                                    db.runSQL(
224                                            "alter table Release_ add testString VARCHAR(1024) null");
225                            }
226                            catch (Exception e) {
227                                    if (_log.isDebugEnabled()) {
228                                            _log.debug(e.getMessage());
229                                    }
230                            }
231    
232                            try {
233                                    db.runSQL(
234                                            "update Release_ set testString = '" +
235                                                    ReleaseConstants.TEST_STRING + "'");
236                            }
237                            catch (Exception e) {
238                                    if (_log.isDebugEnabled()) {
239                                            _log.debug(e.getMessage());
240                                    }
241                            }
242    
243                            count = testSupportsStringCaseSensitiveQuery(
244                                    ReleaseConstants.TEST_STRING);
245                    }
246    
247                    if (count == 0) {
248                            throw new SystemException(
249                                    "Release_ table was not initialized properly");
250                    }
251    
252                    count = testSupportsStringCaseSensitiveQuery(
253                            ReleaseConstants.TEST_STRING.toUpperCase());
254    
255                    if (count == 0) {
256                            db.setSupportsStringCaseSensitiveQuery(true);
257                    }
258                    else {
259                            db.setSupportsStringCaseSensitiveQuery(false);
260                    }
261            }
262    
263            protected int testSupportsStringCaseSensitiveQuery(String testString) {
264                    int count = 0;
265    
266                    Connection con = null;
267                    PreparedStatement ps = null;
268                    ResultSet rs = null;
269    
270                    try {
271                            con = DataAccess.getConnection();
272    
273                            ps = con.prepareStatement(_TEST_DATABASE_STRING_CASE_SENSITIVITY);
274    
275                            ps.setLong(1, ReleaseConstants.DEFAULT_ID);
276                            ps.setString(2, testString);
277    
278                            rs = ps.executeQuery();
279    
280                            if (rs.next()) {
281                                    count = rs.getInt(1);
282                            }
283                    }
284                    catch (Exception e) {
285                            if (_log.isWarnEnabled()) {
286                                    _log.warn(e.getMessage());
287                            }
288                    }
289                    finally {
290                            DataAccess.cleanUp(con, ps, rs);
291                    }
292    
293                    return count;
294            }
295    
296            private static final String _GET_BUILD_NUMBER =
297                    "select buildNumber from Release_ where releaseId = ?";
298    
299            private static final String _TEST_DATABASE_STRING_CASE_SENSITIVITY =
300                    "select count(*) from Release_ where releaseId = ? and testString = ?";
301    
302            private static Log _log = LogFactoryUtil.getLog(
303                    ReleaseLocalServiceImpl.class);
304    
305    }