001
014
015 package com.liferay.portal.upgrade.v6_2_0;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.json.JSONFactoryUtil;
019 import com.liferay.portal.kernel.json.JSONObject;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
023 import com.liferay.portal.kernel.util.StringBundler;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.workflow.WorkflowConstants;
026 import com.liferay.portal.util.PortalUtil;
027 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
028 import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
029 import com.liferay.portlet.journal.model.JournalArticle;
030 import com.liferay.portlet.wiki.model.WikiPage;
031 import com.liferay.portlet.wiki.social.WikiActivityKeys;
032
033 import java.sql.Connection;
034 import java.sql.PreparedStatement;
035 import java.sql.ResultSet;
036 import java.sql.Timestamp;
037
038 import java.util.HashSet;
039 import java.util.Set;
040
041
045 public class UpgradeSocial extends UpgradeProcess {
046
047 protected void addActivity(
048 long activityId, long groupId, long companyId, long userId,
049 Timestamp createDate, long mirrorActivityId, long classNameId,
050 long classPK, int type, String extraData, long receiverUserId)
051 throws Exception {
052
053 Connection con = null;
054 PreparedStatement ps = null;
055 ResultSet rs = null;
056
057 try {
058 con = DataAccess.getUpgradeOptimizedConnection();
059
060 StringBundler sb = new StringBundler(5);
061
062 sb.append("insert into SocialActivity (activityId, groupId, ");
063 sb.append("companyId, userId, createDate, mirrorActivityId, ");
064 sb.append("classNameId, classPK, type_, extraData, ");
065 sb.append("receiverUserId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
066 sb.append("?)");
067
068 ps = con.prepareStatement(sb.toString());
069
070 ps.setLong(1, activityId);
071 ps.setLong(2, groupId);
072 ps.setLong(3, companyId);
073 ps.setLong(4, userId);
074 ps.setLong(5, createDate.getTime());
075 ps.setLong(6, mirrorActivityId);
076 ps.setLong(7, classNameId);
077 ps.setLong(8, classPK);
078 ps.setInt(9, type);
079 ps.setString(10, extraData);
080 ps.setLong(11, receiverUserId);
081
082 ps.executeUpdate();
083 }
084 catch (Exception e) {
085 if (_log.isWarnEnabled()) {
086 _log.warn("Unable to add activity " + activityId, e);
087 }
088 }
089 finally {
090 DataAccess.cleanUp(con, ps, rs);
091 }
092 }
093
094 @Override
095 protected void doUpgrade() throws Exception {
096 updateDLFileVersionActivities();
097 updateJournalActivities();
098 updateSOSocialActivities();
099 updateWikiPageActivities();
100 }
101
102 protected Timestamp getUniqueModifiedDate(
103 Set<String> keys, long groupId, long userId, Timestamp modifiedDate,
104 long classNameId, long resourcePrimKey, double type) {
105
106 while (true) {
107 StringBundler sb = new StringBundler(11);
108
109 sb.append(groupId);
110 sb.append(StringPool.DASH);
111 sb.append(userId);
112 sb.append(StringPool.DASH);
113 sb.append(modifiedDate);
114 sb.append(StringPool.DASH);
115 sb.append(classNameId);
116 sb.append(StringPool.DASH);
117 sb.append(resourcePrimKey);
118 sb.append(StringPool.DASH);
119 sb.append(type);
120
121 String key = sb.toString();
122
123 modifiedDate = new Timestamp(modifiedDate.getTime() + 1);
124
125 if (!keys.contains(key)) {
126 keys.add(key);
127
128 return modifiedDate;
129 }
130 }
131 }
132
133 protected void updateDLFileVersionActivities() throws Exception {
134 long classNameId = PortalUtil.getClassNameId(DLFileEntry.class);
135
136 runSQL("delete from SocialActivity where classNameId = " + classNameId);
137
138 Connection con = null;
139 PreparedStatement ps = null;
140 ResultSet rs = null;
141
142 try {
143 Set<String> keys = new HashSet<String>();
144
145 con = DataAccess.getUpgradeOptimizedConnection();
146
147 ps = con.prepareStatement(
148 "select groupId, companyId, userId, modifiedDate, " +
149 "fileEntryId, title, version from DLFileVersion " +
150 "where status = ?");
151
152 ps.setInt(1, WorkflowConstants.STATUS_APPROVED);
153
154 rs = ps.executeQuery();
155
156 while (rs.next()) {
157 long groupId = rs.getLong("groupId");
158 long companyId = rs.getLong("companyId");
159 long userId = rs.getLong("userId");
160 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
161 long fileEntryId = rs.getLong("fileEntryId");
162 String title = rs.getString("title");
163 double version = rs.getDouble("version");
164
165 int type = DLActivityKeys.ADD_FILE_ENTRY;
166
167 if (version > 1.0) {
168 type = DLActivityKeys.UPDATE_FILE_ENTRY;
169 }
170
171 modifiedDate = getUniqueModifiedDate(
172 keys, groupId, userId, modifiedDate, classNameId,
173 fileEntryId, type);
174
175 JSONObject extraDataJSONObject =
176 JSONFactoryUtil.createJSONObject();
177
178 extraDataJSONObject.put("title", title);
179
180 addActivity(
181 increment(), groupId, companyId, userId, modifiedDate, 0,
182 classNameId, fileEntryId, type,
183 extraDataJSONObject.toString(), 0);
184 }
185 }
186 finally {
187 DataAccess.cleanUp(con, ps, rs);
188 }
189 }
190
191 protected void updateJournalActivities() throws Exception {
192 long classNameId = PortalUtil.getClassNameId(JournalArticle.class);
193
194 String[] tableNames = {"SocialActivity", "SocialActivityCounter"};
195
196 for (String tableName : tableNames) {
197 StringBundler sb = new StringBundler(7);
198
199 sb.append("update ");
200 sb.append(tableName);
201 sb.append(" set classPK = (select resourcePrimKey ");
202 sb.append("from JournalArticle where id_ = ");
203 sb.append(tableName);
204 sb.append(".classPK) where classNameId = ");
205 sb.append(classNameId);
206
207 runSQL(sb.toString());
208 }
209 }
210
211 protected void updateSOSocialActivities() throws Exception {
212 if (!hasTable("SO_SocialActivity")) {
213 return;
214 }
215
216 Connection con = null;
217 PreparedStatement ps = null;
218 ResultSet rs = null;
219
220 try {
221 con = DataAccess.getUpgradeOptimizedConnection();
222
223 ps = con.prepareStatement(
224 "select activityId, activitySetId from SO_SocialActivity");
225
226 rs = ps.executeQuery();
227
228 while (rs.next()) {
229 long activityId = rs.getLong("activityId");
230 long activitySetId = rs.getLong("activitySetId");
231
232 StringBundler sb = new StringBundler(4);
233
234 sb.append("update SocialActivity set activitySetId = ");
235 sb.append(activitySetId);
236 sb.append(" where activityId = ");
237 sb.append(activityId);
238
239 runSQL(sb.toString());
240 }
241 }
242 finally {
243 DataAccess.cleanUp(con, ps, rs);
244 }
245
246 runSQL("drop table SO_SocialActivity");
247 }
248
249 protected void updateWikiPageActivities() throws Exception {
250 long classNameId = PortalUtil.getClassNameId(WikiPage.class);
251
252 runSQL("delete from SocialActivity where classNameId = " + classNameId);
253
254 Connection con = null;
255 PreparedStatement ps = null;
256 ResultSet rs = null;
257
258 try {
259 Set<String> keys = new HashSet<String>();
260
261 con = DataAccess.getUpgradeOptimizedConnection();
262
263 ps = con.prepareStatement(
264 "select groupId, companyId, userId, modifiedDate, " +
265 "resourcePrimKey, version from WikiPage");
266
267 rs = ps.executeQuery();
268
269 while (rs.next()) {
270 long groupId = rs.getLong("groupId");
271 long companyId = rs.getLong("companyId");
272 long userId = rs.getLong("userId");
273 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
274 long resourcePrimKey = rs.getLong("resourcePrimKey");
275 double version = rs.getDouble("version");
276
277 int type = WikiActivityKeys.ADD_PAGE;
278
279 if (version > 1.0) {
280 type = WikiActivityKeys.UPDATE_PAGE;
281 }
282
283 modifiedDate = getUniqueModifiedDate(
284 keys, groupId, userId, modifiedDate, classNameId,
285 resourcePrimKey, type);
286
287 JSONObject extraDataJSONObject =
288 JSONFactoryUtil.createJSONObject();
289
290 extraDataJSONObject.put("version", version);
291
292 addActivity(
293 increment(), groupId, companyId, userId, modifiedDate, 0,
294 classNameId, resourcePrimKey, type,
295 extraDataJSONObject.toString(), 0);
296 }
297 }
298 finally {
299 DataAccess.cleanUp(con, ps, rs);
300 }
301 }
302
303 private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
304
305 }