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.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.upgrade.v6_2_0.util.JournalFeedTable;
027 import com.liferay.portal.util.PortalUtil;
028 import com.liferay.portal.util.PortletKeys;
029 import com.liferay.portal.util.PropsValues;
030 import com.liferay.portlet.PortletPreferencesFactoryUtil;
031 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
032 import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
033 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
034 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
035 import com.liferay.portlet.journal.model.JournalArticle;
036 import com.liferay.portlet.journal.model.JournalStructure;
037 import com.liferay.portlet.journal.model.JournalTemplate;
038 import com.liferay.portlet.journal.util.JournalConverterUtil;
039
040 import java.sql.Connection;
041 import java.sql.PreparedStatement;
042 import java.sql.ResultSet;
043 import java.sql.SQLException;
044 import java.sql.Timestamp;
045
046 import java.util.HashMap;
047 import java.util.Map;
048
049 import javax.portlet.PortletPreferences;
050
051
057 public class UpgradeJournal extends BaseUpgradePortletPreferences {
058
059 protected void addDDMStructure(
060 String uuid_, long ddmStructureId, long groupId, long companyId,
061 long userId, String userName, Timestamp createDate,
062 Timestamp modifiedDate, long parentDDMStructureId, long classNameId,
063 String ddmStructureKey, String name, String description, String xsd,
064 String storageType, int type)
065 throws Exception {
066
067 Connection con = null;
068 PreparedStatement ps = null;
069
070 try {
071 con = DataAccess.getUpgradeOptimizedConnection();
072
073 StringBundler sb = new StringBundler(6);
074
075 sb.append("insert into DDMStructure (uuid_, structureId, ");
076 sb.append("groupId, companyId, userId, userName, createDate, ");
077 sb.append("modifiedDate, parentStructureId, classNameId, ");
078 sb.append("structureKey, name, description, xsd, storageType, ");
079 sb.append("type_) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
080 sb.append("?, ?, ?)");
081
082 String sql = sb.toString();
083
084 ps = con.prepareStatement(sql);
085
086 ps.setString(1, uuid_);
087 ps.setLong(2, ddmStructureId);
088 ps.setLong(3, groupId);
089 ps.setLong(4, companyId);
090 ps.setLong(5, userId);
091 ps.setString(6, userName);
092 ps.setTimestamp(7, createDate);
093 ps.setTimestamp(8, modifiedDate);
094 ps.setLong(9, parentDDMStructureId);
095 ps.setLong(10, classNameId);
096 ps.setString(11, ddmStructureKey);
097 ps.setString(12, name);
098 ps.setString(13, description);
099 ps.setString(14, JournalConverterUtil.getDDMXSD(xsd));
100 ps.setString(15, storageType);
101 ps.setInt(16, type);
102
103 ps.executeUpdate();
104 }
105 catch (Exception e) {
106 _log.error(
107 "Unable to upgrade dynamic data mapping structure with UUID " +
108 uuid_);
109
110 throw e;
111 }
112 finally {
113 DataAccess.cleanUp(con, ps);
114 }
115 }
116
117 protected void addDDMStructure(
118 String uuid_, long ddmStructureId, long groupId, long companyId,
119 long userId, String userName, Timestamp createDate,
120 Timestamp modifiedDate, String parentStructureId,
121 String ddmStructureKey, String name, String description, String xsd)
122 throws Exception {
123
124 long parentDDMStructureId = 0;
125
126 if (Validator.isNotNull(parentStructureId)) {
127 parentDDMStructureId = updateStructure(parentStructureId);
128 }
129
130 addDDMStructure(
131 uuid_, ddmStructureId, groupId, companyId, userId, userName,
132 createDate, modifiedDate, parentDDMStructureId,
133 PortalUtil.getClassNameId(JournalArticle.class.getName()),
134 ddmStructureKey, name, description, xsd,
135 PropsValues.JOURNAL_ARTICLE_STORAGE_TYPE,
136 DDMStructureConstants.TYPE_DEFAULT);
137 }
138
139 protected void addDDMTemplate(
140 String uuid_, long ddmTemplateId, long groupId, long companyId,
141 long userId, String userName, Timestamp createDate,
142 Timestamp modifiedDate, long classNameId, long classPK,
143 String templateKey, String name, String description, String type,
144 String mode, String language, String script, boolean cacheable,
145 boolean smallImage, long smallImageId, String smallImageURL)
146 throws Exception {
147
148 Connection con = null;
149 PreparedStatement ps = null;
150
151 try {
152 con = DataAccess.getUpgradeOptimizedConnection();
153
154 StringBundler sb = new StringBundler(6);
155
156 sb.append("insert into DDMTemplate (uuid_, templateId, groupId, ");
157 sb.append("companyId, userId, userName, createDate, modifiedDate,");
158 sb.append("classNameId, classPK , templateKey, name, description,");
159 sb.append("type_, mode_, language, script, cacheable, smallImage,");
160 sb.append("smallImageId, smallImageURL) values (?, ?, ?, ?, ?, ?,");
161 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
162
163 String sql = sb.toString();
164
165 ps = con.prepareStatement(sql);
166
167 ps.setString(1, uuid_);
168 ps.setLong(2, ddmTemplateId);
169 ps.setLong(3, groupId);
170 ps.setLong(4, companyId);
171 ps.setLong(5, userId);
172 ps.setString(6, userName);
173 ps.setTimestamp(7, createDate);
174 ps.setTimestamp(8, modifiedDate);
175 ps.setLong(9, classNameId);
176 ps.setLong(10, classPK);
177 ps.setString(11, templateKey);
178 ps.setString(12, name);
179 ps.setString(13, description);
180 ps.setString(14, type);
181 ps.setString(15, mode);
182 ps.setString(16, language);
183 ps.setString(17, script);
184 ps.setBoolean(18, cacheable);
185 ps.setBoolean(19, smallImage);
186 ps.setLong(20, smallImageId);
187 ps.setString(21, smallImageURL);
188
189 ps.executeUpdate();
190 }
191 catch (Exception e) {
192 _log.error(
193 "Unable to upgrade dynamic data mapping template with UUID " +
194 uuid_);
195
196 throw e;
197 }
198 finally {
199 DataAccess.cleanUp(con, ps);
200 }
201 }
202
203 @Override
204 protected void doUpgrade() throws Exception {
205 try {
206 runSQL(
207 "alter_column_name JournalFeed feedType feedFormat " +
208 "VARCHAR(75) null");
209 }
210 catch (SQLException sqle) {
211 upgradeTable(
212 JournalFeedTable.TABLE_NAME, JournalFeedTable.TABLE_COLUMNS,
213 JournalFeedTable.TABLE_SQL_CREATE,
214 JournalFeedTable.TABLE_SQL_ADD_INDEXES);
215 }
216
217 updateStructures();
218 updateTemplates();
219
220 super.doUpgrade();
221 }
222
223 protected long getDDMStructureId(long groupId, String structureId) {
224 if (Validator.isNull(structureId)) {
225 return 0;
226 }
227
228 Long ddmStructureId = _ddmStructureIds.get(groupId + "#" + structureId);
229
230 if (ddmStructureId == null) {
231 if (_log.isWarnEnabled()) {
232 _log.warn(
233 "Unable to get the DDM structure ID for group " +
234 groupId + " and journal structure ID " + structureId);
235 }
236
237 return 0;
238 }
239
240 return ddmStructureId;
241 }
242
243 @Override
244 protected String[] getPortletIds() {
245 return new String[] {
246 "56_INSTANCE_%", "62_INSTANCE_%", "101_INSTANCE_%"
247 };
248 }
249
250 protected void updatePreferencesClassPKs(
251 PortletPreferences preferences, String key)
252 throws Exception {
253
254 String[] oldValues = preferences.getValues(key, null);
255
256 if (oldValues == null) {
257 return;
258 }
259
260 String[] newValues = new String[oldValues.length];
261
262 for (int i = 0; i < oldValues.length; i++) {
263 String oldValue = oldValues[i];
264
265 String newValue = oldValue;
266
267 String[] oldPrimaryKeys = StringUtil.split(oldValue);
268
269 for (String oldPrimaryKey : oldPrimaryKeys) {
270 if (!Validator.isNumber(oldPrimaryKey)) {
271 break;
272 }
273
274 Long newPrimaryKey = _ddmStructurePKs.get(
275 GetterUtil.getLong(oldPrimaryKey));
276
277 if (Validator.isNotNull(newPrimaryKey)) {
278 newValue = StringUtil.replace(
279 newValue, oldPrimaryKey, String.valueOf(newPrimaryKey));
280 }
281 }
282
283 newValues[i] = newValue;
284 }
285
286 preferences.setValues(key, newValues);
287 }
288
289 protected void updateResourcePermission(
290 long companyId, String oldClassName, String newClassName,
291 long oldPrimKey, long newPrimKey)
292 throws Exception {
293
294 StringBundler sb = new StringBundler(11);
295
296 sb.append("update ResourcePermission set name = '");
297 sb.append(newClassName);
298 sb.append("', primKey = '");
299 sb.append(newPrimKey);
300 sb.append("' where companyId = ");
301 sb.append(companyId);
302 sb.append(" and name = '");
303 sb.append(oldClassName);
304 sb.append("' and primKey = '");
305 sb.append(oldPrimKey);
306 sb.append("'");
307
308 runSQL(sb.toString());
309 }
310
311 protected long updateStructure(String structureId) throws Exception {
312 Connection con = null;
313 PreparedStatement ps = null;
314 ResultSet rs = null;
315
316 try {
317 con = DataAccess.getUpgradeOptimizedConnection();
318
319 ps = con.prepareStatement(
320 "select * from JournalStructure where structureId = " +
321 structureId);
322
323 rs = ps.executeQuery();
324
325 if (rs.next()) {
326 String uuid_ = rs.getString("uuid_");
327 long id_ = rs.getLong("id_");
328 long groupId = rs.getLong("groupId");
329 long companyId = rs.getLong("companyId");
330 long userId = rs.getLong("userId");
331 String userName = rs.getString("userName");
332 Timestamp createDate = rs.getTimestamp("createDate");
333 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
334 String parentStructureId = rs.getString("parentStructureId");
335 String name = rs.getString("name");
336 String description = rs.getString("description");
337 String xsd = rs.getString("xsd");
338
339 Long ddmStructureId = _ddmStructureIds.get(
340 groupId + "#" + structureId);
341
342 if (ddmStructureId != null) {
343 return ddmStructureId;
344 }
345
346 ddmStructureId = increment();
347
348 addDDMStructure(
349 uuid_, ddmStructureId, groupId, companyId, userId, userName,
350 createDate, modifiedDate, parentStructureId, structureId,
351 name, description, xsd);
352
353 updateResourcePermission(
354 companyId, JournalStructure.class.getName(),
355 DDMStructure.class.getName(), id_, ddmStructureId);
356
357 _ddmStructureIds.put(
358 groupId + "#" + structureId, ddmStructureId);
359 }
360
361 return 0;
362 }
363 catch (Exception e) {
364 _log.error(
365 "Unable to update journal structure with structure ID " +
366 structureId);
367
368 throw e;
369 }
370 finally {
371 DataAccess.cleanUp(con, ps, rs);
372 }
373 }
374
375 protected void updateStructures() throws Exception {
376 Connection con = null;
377 PreparedStatement ps = null;
378 ResultSet rs = null;
379
380 try {
381 con = DataAccess.getUpgradeOptimizedConnection();
382
383 ps = con.prepareStatement("select * from JournalStructure");
384
385 rs = ps.executeQuery();
386
387 while (rs.next()) {
388 String uuid_ = rs.getString("uuid_");
389 long id_ = rs.getLong("id_");
390 long groupId = rs.getLong("groupId");
391 long companyId = rs.getLong("companyId");
392 long userId = rs.getLong("userId");
393 String userName = rs.getString("userName");
394 Timestamp createDate = rs.getTimestamp("createDate");
395 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
396 String structureId = rs.getString("structureId");
397 String parentStructureId = rs.getString("parentStructureId");
398 String name = rs.getString("name");
399 String description = rs.getString("description");
400 String xsd = rs.getString("xsd");
401
402 long ddmStructureId = increment();
403
404 addDDMStructure(
405 uuid_, ddmStructureId, groupId, companyId, userId, userName,
406 createDate, modifiedDate, parentStructureId, structureId,
407 name, description, xsd);
408
409 updateResourcePermission(
410 companyId, JournalStructure.class.getName(),
411 DDMStructure.class.getName(), id_, ddmStructureId);
412
413 _ddmStructureIds.put(
414 groupId + "#" + structureId, ddmStructureId);
415 _ddmStructurePKs.put(id_, ddmStructureId);
416 }
417 }
418 finally {
419 DataAccess.cleanUp(con, ps, rs);
420 }
421
422 runSQL("drop table JournalStructure");
423 }
424
425 protected void updateTemplates() throws Exception {
426 Connection con = null;
427 PreparedStatement ps = null;
428 ResultSet rs = null;
429
430 try {
431 con = DataAccess.getUpgradeOptimizedConnection();
432
433 ps = con.prepareStatement("select * from JournalTemplate");
434
435 rs = ps.executeQuery();
436
437 while (rs.next()) {
438 String uuid_ = rs.getString("uuid_");
439 long id_ = rs.getLong("id_");
440 long groupId = rs.getLong("groupId");
441 long companyId = rs.getLong("companyId");
442 long userId = rs.getLong("userId");
443 String userName = rs.getString("userName");
444 Timestamp createDate = rs.getTimestamp("createDate");
445 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
446 String templateId = rs.getString("templateId");
447 String structureId = rs.getString("structureId");
448 String name = rs.getString("name");
449 String description = rs.getString("description");
450 String language = rs.getString("langType");
451 String script = rs.getString("xsl");
452 boolean cacheable = rs.getBoolean("cacheable");
453 boolean smallImage = rs.getBoolean("smallImage");
454 long smallImageId = rs.getLong("smallImageId");
455 String smallImageURL = rs.getString("smallImageURL");
456
457 long ddmTemplateId = increment();
458
459 long classNameId = PortalUtil.getClassNameId(
460 DDMStructure.class.getName());
461
462 long classPK = getDDMStructureId(groupId, structureId);
463
464 addDDMTemplate(
465 uuid_, ddmTemplateId, groupId, companyId, userId, userName,
466 createDate, modifiedDate, classNameId, classPK, templateId,
467 name, description,
468 DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY,
469 DDMTemplateConstants.TEMPLATE_MODE_CREATE, language, script,
470 cacheable, smallImage, smallImageId, smallImageURL);
471
472 updateResourcePermission(
473 companyId, JournalTemplate.class.getName(),
474 DDMTemplate.class.getName(), id_, ddmTemplateId);
475 }
476 }
477 finally {
478 DataAccess.cleanUp(con, ps, rs);
479 }
480
481 runSQL("drop table JournalTemplate");
482 }
483
484 @Override
485 protected String upgradePreferences(
486 long companyId, long ownerId, int ownerType, long plid,
487 String portletId, String xml)
488 throws Exception {
489
490 PortletPreferences preferences = PortletPreferencesFactoryUtil.fromXML(
491 companyId, ownerId, ownerType, plid, portletId, xml);
492
493 if (portletId.startsWith(PortletKeys.ASSET_PUBLISHER)) {
494 updatePreferencesClassPKs(
495 preferences, "anyClassTypeJournalArticleAssetRendererFactory");
496 updatePreferencesClassPKs(preferences, "classTypeIds");
497 updatePreferencesClassPKs(
498 preferences, "classTypeIdsJournalArticleAssetRendererFactory");
499 }
500 else if (portletId.startsWith(PortletKeys.JOURNAL_CONTENT)) {
501 String templateId = preferences.getValue(
502 "templateId", StringPool.BLANK);
503
504 if (Validator.isNotNull(templateId)) {
505 preferences.reset("templateId");
506
507 preferences.setValue("ddmTemplateKey", templateId);
508 }
509 }
510 else if (portletId.startsWith(PortletKeys.JOURNAL_CONTENT_LIST)) {
511 String structureId = preferences.getValue(
512 "structureId", StringPool.BLANK);
513
514 if (Validator.isNotNull(structureId)) {
515 preferences.reset("structureId");
516
517 preferences.setValue("ddmStructureKey", structureId);
518 }
519 }
520
521 return PortletPreferencesFactoryUtil.toXML(preferences);
522 }
523
524 private static Log _log = LogFactoryUtil.getLog(UpgradeJournal.class);
525
526 private Map<String, Long> _ddmStructureIds = new HashMap<String, Long>();
527 private Map<Long, Long> _ddmStructurePKs = new HashMap<Long, Long>();
528
529 }