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.Validator;
029    import com.liferay.portal.model.Release;
030    import com.liferay.portal.model.ReleaseConstants;
031    import com.liferay.portal.service.base.ReleaseLocalServiceBaseImpl;
032    import com.liferay.portal.util.PropsUtil;
033    import com.liferay.portal.util.PropsValues;
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(ReleaseConstants.DEFAULT_ID);
055                    }
056                    else {
057                            long releaseId = counterLocalService.increment();
058    
059                            release = releasePersistence.create(releaseId);
060                    }
061    
062                    Date now = new Date();
063    
064                    release.setCreateDate(now);
065                    release.setModifiedDate(now);
066                    release.setServletContextName(servletContextName);
067                    release.setBuildNumber(buildNumber);
068    
069                    if (servletContextName.equals(
070                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
071    
072                            release.setTestString(ReleaseConstants.TEST_STRING);
073                    }
074    
075                    releasePersistence.update(release);
076    
077                    return release;
078            }
079    
080            public void createTablesAndPopulate() throws SystemException {
081                    try {
082                            if (_log.isInfoEnabled()) {
083                                    _log.info("Create tables and populate with default data");
084                            }
085    
086                            DB db = DBFactoryUtil.getDB();
087    
088                            db.runSQLTemplate("portal-tables.sql", false);
089                            db.runSQLTemplate("portal-data-common.sql", false);
090                            db.runSQLTemplate("portal-data-counter.sql", false);
091    
092                            if (!PropsValues.SCHEMA_RUN_MINIMAL && !ShardUtil.isEnabled()) {
093                                    db.runSQLTemplate("portal-data-sample.vm", false);
094                            }
095    
096                            db.runSQLTemplate("portal-data-release.sql", false);
097                            db.runSQLTemplate("indexes.sql", false);
098                            db.runSQLTemplate("sequences.sql", false);
099                    }
100                    catch (Exception e) {
101                            _log.error(e, e);
102    
103                            throw new SystemException(e);
104                    }
105            }
106    
107            public int getBuildNumberOrCreate()
108                    throws PortalException, SystemException {
109    
110                    // Get release build number
111    
112                    Connection con = null;
113                    PreparedStatement ps = null;
114                    ResultSet rs = null;
115    
116                    try {
117                            con = DataAccess.getConnection();
118    
119                            ps = con.prepareStatement(_GET_BUILD_NUMBER);
120    
121                            ps.setLong(1, ReleaseConstants.DEFAULT_ID);
122    
123                            rs = ps.executeQuery();
124    
125                            if (rs.next()) {
126                                    int buildNumber = rs.getInt("buildNumber");
127    
128                                    if (_log.isDebugEnabled()) {
129                                            _log.debug("Build number " + buildNumber);
130                                    }
131    
132                                    DB db = DBFactoryUtil.getDB();
133    
134                                    try {
135                                            db.runSQL("alter table Release_ add state_ INTEGER");
136                                    }
137                                    catch (Exception e) {
138                                            if (_log.isDebugEnabled()) {
139                                                    _log.debug(e.getMessage());
140                                            }
141                                    }
142    
143                                    testSupportsStringCaseSensitiveQuery();
144    
145                                    return buildNumber;
146                            }
147                    }
148                    catch (Exception e) {
149                            if (_log.isWarnEnabled()) {
150                                    _log.warn(e.getMessage());
151                            }
152                    }
153                    finally {
154                            DataAccess.cleanUp(con, ps, rs);
155                    }
156    
157                    // Create tables and populate with default data
158    
159                    if (GetterUtil.getBoolean(
160                                    PropsUtil.get(PropsKeys.SCHEMA_RUN_ENABLED))) {
161    
162                            releaseLocalService.createTablesAndPopulate();
163    
164                            testSupportsStringCaseSensitiveQuery();
165    
166                            Release release = getRelease(
167                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME);
168    
169                            return release.getBuildNumber();
170                    }
171                    else {
172                            throw new NoSuchReleaseException(
173                                    "The database needs to be populated");
174                    }
175            }
176    
177            public Release getRelease(String servletContextName)
178                    throws PortalException, SystemException {
179    
180                    if (Validator.isNull(servletContextName)) {
181                            throw new IllegalArgumentException(
182                                    "Servlet context name cannot be null");
183                    }
184    
185                    Release release = null;
186    
187                    if (servletContextName.equals(
188                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME)) {
189    
190                            release = releasePersistence.findByPrimaryKey(
191                                    ReleaseConstants.DEFAULT_ID);
192                    }
193                    else {
194                            release = releasePersistence.findByServletContextName(
195                                    servletContextName);
196                    }
197    
198                    return release;
199            }
200    
201            public Release updateRelease(
202                            long releaseId, int buildNumber, Date buildDate, boolean verified)
203                    throws PortalException, SystemException {
204    
205                    Release release = releasePersistence.findByPrimaryKey(releaseId);
206    
207                    release.setModifiedDate(new Date());
208                    release.setBuildNumber(buildNumber);
209                    release.setBuildDate(buildDate);
210                    release.setVerified(verified);
211    
212                    releasePersistence.update(release);
213    
214                    return release;
215            }
216    
217            protected void testSupportsStringCaseSensitiveQuery()
218                    throws SystemException {
219    
220                    DB db = DBFactoryUtil.getDB();
221    
222                    int count = testSupportsStringCaseSensitiveQuery(
223                            ReleaseConstants.TEST_STRING);
224    
225                    if (count == 0) {
226                            try {
227                                    db.runSQL(
228                                            "alter table Release_ add testString VARCHAR(1024) null");
229                            }
230                            catch (Exception e) {
231                                    if (_log.isDebugEnabled()) {
232                                            _log.debug(e.getMessage());
233                                    }
234                            }
235    
236                            try {
237                                    db.runSQL(
238                                            "update Release_ set testString = '" +
239                                                    ReleaseConstants.TEST_STRING + "'");
240                            }
241                            catch (Exception e) {
242                                    if (_log.isDebugEnabled()) {
243                                            _log.debug(e.getMessage());
244                                    }
245                            }
246    
247                            count = testSupportsStringCaseSensitiveQuery(
248                                    ReleaseConstants.TEST_STRING);
249                    }
250    
251                    if (count == 0) {
252                            throw new SystemException(
253                                    "Release_ table was not initialized properly");
254                    }
255    
256                    count = testSupportsStringCaseSensitiveQuery(
257                            ReleaseConstants.TEST_STRING.toUpperCase());
258    
259                    if (count == 0) {
260                            db.setSupportsStringCaseSensitiveQuery(true);
261                    }
262                    else {
263                            db.setSupportsStringCaseSensitiveQuery(false);
264                    }
265            }
266    
267            protected int testSupportsStringCaseSensitiveQuery(String testString) {
268                    int count = 0;
269    
270                    Connection con = null;
271                    PreparedStatement ps = null;
272                    ResultSet rs = null;
273    
274                    try {
275                            con = DataAccess.getConnection();
276    
277                            ps = con.prepareStatement(_TEST_DATABASE_STRING_CASE_SENSITIVITY);
278    
279                            ps.setLong(1, ReleaseConstants.DEFAULT_ID);
280                            ps.setString(2, testString);
281    
282                            rs = ps.executeQuery();
283    
284                            if (rs.next()) {
285                                    count = rs.getInt(1);
286                            }
287                    }
288                    catch (Exception e) {
289                            if (_log.isWarnEnabled()) {
290                                    _log.warn(e.getMessage());
291                            }
292                    }
293                    finally {
294                            DataAccess.cleanUp(con, ps, rs);
295                    }
296    
297                    return count;
298            }
299    
300            private static final String _GET_BUILD_NUMBER =
301                    "select buildNumber from Release_ where releaseId = ?";
302    
303            private static final String _TEST_DATABASE_STRING_CASE_SENSITIVITY =
304                    "select count(*) from Release_ where releaseId = ? and testString = ?";
305    
306            private static Log _log = LogFactoryUtil.getLog(
307                    ReleaseLocalServiceImpl.class);
308    
309    }