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