1
22
23 package com.liferay.portal.service.impl;
24
25 import com.liferay.portal.NoSuchReleaseException;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.ReleaseInfo;
31 import com.liferay.portal.model.Release;
32 import com.liferay.portal.model.impl.ReleaseImpl;
33 import com.liferay.portal.service.base.ReleaseLocalServiceBaseImpl;
34 import com.liferay.portal.tools.sql.DBUtil;
35 import com.liferay.portal.util.PropsKeys;
36 import com.liferay.portal.util.PropsUtil;
37
38 import java.sql.Connection;
39 import java.sql.PreparedStatement;
40 import java.sql.ResultSet;
41
42 import java.util.Date;
43
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47
53 public class ReleaseLocalServiceImpl extends ReleaseLocalServiceBaseImpl {
54
55 public int getBuildNumberOrCreate()
56 throws PortalException, SystemException {
57
58
60 Connection con = null;
61 PreparedStatement ps = null;
62 ResultSet rs = null;
63
64 try {
65 con = DataAccess.getConnection();
66
67 ps = con.prepareStatement(_GET_BUILD_NUMBER);
68
69 rs = ps.executeQuery();
70
71 if (rs.next()) {
72 int buildNumber = rs.getInt("buildNumber");
73
74 if (_log.isDebugEnabled()) {
75 _log.debug("Build number " + buildNumber);
76 }
77
78 return buildNumber;
79 }
80 }
81 catch (Exception e) {
82 if (_log.isWarnEnabled()) {
83 _log.warn(e.getMessage());
84 }
85 }
86 finally {
87 DataAccess.cleanUp(con, ps, rs);
88 }
89
90
92 if (GetterUtil.getBoolean(
93 PropsUtil.get(PropsKeys.SCHEMA_RUN_ENABLED))) {
94
95 if (_log.isInfoEnabled()) {
96 _log.info("Create tables and populate with default data");
97 }
98
99 createTablesAndPopulate();
100
101 return getRelease().getBuildNumber();
102 }
103 else {
104 throw new NoSuchReleaseException(
105 "The database needs to be populated");
106 }
107 }
108
109 public Release getRelease() throws SystemException {
110 Release release = releasePersistence.fetchByPrimaryKey(
111 ReleaseImpl.DEFAULT_ID);
112
113 if (release == null) {
114 release = releasePersistence.create(ReleaseImpl.DEFAULT_ID);
115
116 Date now = new Date();
117
118 release.setCreateDate(now);
119 release.setModifiedDate(now);
120
121 releasePersistence.update(release, false);
122 }
123
124 return release;
125 }
126
127 public Release updateRelease(boolean verified) throws SystemException {
128 Release release = getRelease();
129
130 release.setModifiedDate(new Date());
131 release.setBuildNumber(ReleaseInfo.getBuildNumber());
132 release.setBuildDate(ReleaseInfo.getBuildDate());
133 release.setVerified(verified);
134
135 releasePersistence.update(release, false);
136
137 return release;
138 }
139
140 protected void createTablesAndPopulate() throws SystemException {
141 try {
142 DBUtil dbUtil = DBUtil.getInstance();
143
144 dbUtil.runSQLTemplate("portal-tables.sql", false);
145 dbUtil.runSQLTemplate("portal-data-common.sql", false);
146 dbUtil.runSQLTemplate("portal-data-counter.sql", false);
147
148 if (!GetterUtil.getBoolean(
149 PropsUtil.get(PropsKeys.SCHEMA_RUN_MINIMAL))) {
150
151 dbUtil.runSQLTemplate("portal-data-sample.vm", false);
152 }
153
154 dbUtil.runSQLTemplate("portal-data-release.sql", false);
155 dbUtil.runSQLTemplate("indexes.sql", false);
156 dbUtil.runSQLTemplate("sequences.sql", false);
157 dbUtil.runSQLTemplate("quartz-tables.sql", false);
158 }
159 catch (Exception e) {
160 _log.error(e, e);
161
162 throw new SystemException(e);
163 }
164 }
165
166 private static final String _GET_BUILD_NUMBER =
167 "select buildNumber from Release_";
168
169 private static Log _log = LogFactory.getLog(ReleaseLocalServiceImpl.class);
170
171 }