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.util.GetterUtil;
29 import com.liferay.portal.kernel.util.ReleaseInfo;
30 import com.liferay.portal.model.Release;
31 import com.liferay.portal.model.impl.ReleaseImpl;
32 import com.liferay.portal.service.base.ReleaseLocalServiceBaseImpl;
33 import com.liferay.portal.spring.hibernate.HibernateUtil;
34 import com.liferay.portal.tools.sql.DBUtil;
35 import com.liferay.portal.util.PropsUtil;
36 import com.liferay.util.dao.DataAccess;
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 = HibernateUtil.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(PropsUtil.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 PortalException, SystemException {
110 Release release = null;
111
112 try {
113 release = releasePersistence.findByPrimaryKey(
114 ReleaseImpl.DEFAULT_ID);
115 }
116 catch (NoSuchReleaseException nsre) {
117 release = releasePersistence.create(ReleaseImpl.DEFAULT_ID);
118
119 Date now = new Date();
120
121 release.setCreateDate(now);
122 release.setModifiedDate(now);
123
124 releasePersistence.update(release, false);
125 }
126
127 return release;
128 }
129
130 public Release updateRelease(boolean verified)
131 throws PortalException, SystemException {
132
133 Release release = getRelease();
134
135 release.setModifiedDate(new Date());
136 release.setBuildNumber(ReleaseInfo.getBuildNumber());
137 release.setBuildDate(ReleaseInfo.getBuildDate());
138 release.setVerified(verified);
139
140 releasePersistence.update(release, false);
141
142 return release;
143 }
144
145 protected void createTablesAndPopulate() throws SystemException {
146 try {
147 DBUtil dbUtil = DBUtil.getInstance();
148
149 dbUtil.runSQLTemplate("portal-tables.sql", false);
150 dbUtil.runSQLTemplate("portal-data-common.sql", false);
151 dbUtil.runSQLTemplate("portal-data-counter.sql", false);
152
153 if (!GetterUtil.getBoolean(
154 PropsUtil.get(PropsUtil.SCHEMA_RUN_MINIMAL))) {
155
156 dbUtil.runSQLTemplate("portal-data-sample.vm", false);
157 }
158
159 dbUtil.runSQLTemplate("portal-data-release.sql", false);
160 dbUtil.runSQLTemplate("indexes.sql", false);
161 dbUtil.runSQLTemplate("sequences.sql", false);
162 }
163 catch (Exception e) {
164 _log.error(e, e);
165
166 throw new SystemException(e);
167 }
168 }
169
170 private static final String _GET_BUILD_NUMBER =
171 "select buildNumber from Release_";
172
173 private static Log _log = LogFactory.getLog(ReleaseLocalServiceImpl.class);
174
175 }