001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.NoSuchReleaseException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.ReleaseInfo;
028: import com.liferay.portal.model.Release;
029: import com.liferay.portal.model.impl.ReleaseImpl;
030: import com.liferay.portal.service.base.ReleaseLocalServiceBaseImpl;
031: import com.liferay.portal.spring.hibernate.HibernateUtil;
032: import com.liferay.portal.tools.sql.DBUtil;
033: import com.liferay.portal.util.PropsUtil;
034: import com.liferay.util.dao.DataAccess;
035:
036: import java.sql.Connection;
037: import java.sql.PreparedStatement;
038: import java.sql.ResultSet;
039:
040: import java.util.Date;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044:
045: /**
046: * <a href="ReleaseLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: *
050: */
051: public class ReleaseLocalServiceImpl extends
052: ReleaseLocalServiceBaseImpl {
053:
054: public int getBuildNumberOrCreate() throws PortalException,
055: SystemException {
056:
057: // Get release build number
058:
059: Connection con = null;
060: PreparedStatement ps = null;
061: ResultSet rs = null;
062:
063: try {
064: con = HibernateUtil.getConnection();
065:
066: ps = con.prepareStatement(_GET_BUILD_NUMBER);
067:
068: rs = ps.executeQuery();
069:
070: if (rs.next()) {
071: int buildNumber = rs.getInt("buildNumber");
072:
073: if (_log.isDebugEnabled()) {
074: _log.debug("Build number " + buildNumber);
075: }
076:
077: return buildNumber;
078: }
079: } catch (Exception e) {
080: if (_log.isWarnEnabled()) {
081: _log.warn(e.getMessage());
082: }
083: } finally {
084: DataAccess.cleanUp(con, ps, rs);
085: }
086:
087: // Create tables and populate with default data
088:
089: if (GetterUtil.getBoolean(PropsUtil
090: .get(PropsUtil.SCHEMA_RUN_ENABLED))) {
091:
092: if (_log.isInfoEnabled()) {
093: _log
094: .info("Create tables and populate with default data");
095: }
096:
097: createTablesAndPopulate();
098:
099: return getRelease().getBuildNumber();
100: } else {
101: throw new NoSuchReleaseException(
102: "The database needs to be populated");
103: }
104: }
105:
106: public Release getRelease() throws PortalException, SystemException {
107: Release release = null;
108:
109: try {
110: release = releasePersistence
111: .findByPrimaryKey(ReleaseImpl.DEFAULT_ID);
112: } catch (NoSuchReleaseException nsre) {
113: release = releasePersistence.create(ReleaseImpl.DEFAULT_ID);
114:
115: Date now = new Date();
116:
117: release.setCreateDate(now);
118: release.setModifiedDate(now);
119:
120: releasePersistence.update(release);
121: }
122:
123: return release;
124: }
125:
126: public Release updateRelease(boolean verified)
127: throws PortalException, SystemException {
128:
129: Release release = getRelease();
130:
131: release.setModifiedDate(new Date());
132: release.setBuildNumber(ReleaseInfo.getBuildNumber());
133: release.setBuildDate(ReleaseInfo.getBuildDate());
134: release.setVerified(verified);
135:
136: releasePersistence.update(release);
137:
138: return release;
139: }
140:
141: protected void createTablesAndPopulate() throws SystemException {
142: try {
143: DBUtil dbUtil = DBUtil.getInstance();
144:
145: dbUtil.runSQLTemplate("portal-tables.sql", false);
146: dbUtil.runSQLTemplate("portal-data-common.sql", false);
147: dbUtil.runSQLTemplate("portal-data-counter.sql", false);
148:
149: if (!GetterUtil.getBoolean(PropsUtil
150: .get(PropsUtil.SCHEMA_RUN_MINIMAL))) {
151:
152: dbUtil.runSQLTemplate("portal-data-sample.vm", false);
153: }
154:
155: dbUtil.runSQLTemplate("portal-data-release.sql", false);
156: dbUtil.runSQLTemplate("indexes.sql", false);
157: dbUtil.runSQLTemplate("sequences.sql", false);
158: } catch (Exception e) {
159: _log.error(e, e);
160:
161: throw new SystemException(e);
162: }
163: }
164:
165: private static final String _GET_BUILD_NUMBER = "select buildNumber from Release_";
166:
167: private static Log _log = LogFactory
168: .getLog(ReleaseLocalServiceImpl.class);
169:
170: }
|