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