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