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