001
014
015 package com.liferay.portal.upgrade.v7_0_0;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.json.JSONFactoryUtil;
020 import com.liferay.portal.kernel.json.JSONObject;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.upgrade.UpgradeException;
024 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
025 import com.liferay.portal.kernel.util.CharPool;
026 import com.liferay.portal.kernel.util.LocaleUtil;
027 import com.liferay.portal.kernel.util.MimeTypesUtil;
028 import com.liferay.portal.kernel.util.StringBundler;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.StringUtil;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
033 import com.liferay.portal.kernel.workflow.WorkflowConstants;
034 import com.liferay.portal.kernel.xml.Document;
035 import com.liferay.portal.kernel.xml.DocumentException;
036 import com.liferay.portal.kernel.xml.Element;
037 import com.liferay.portal.kernel.xml.Node;
038 import com.liferay.portal.kernel.xml.SAXReaderUtil;
039 import com.liferay.portal.kernel.xml.XPath;
040 import com.liferay.portal.model.CompanyConstants;
041 import com.liferay.portal.model.ResourceConstants;
042 import com.liferay.portal.model.RoleConstants;
043 import com.liferay.portal.upgrade.v7_0_0.util.DDMContentTable;
044 import com.liferay.portal.upgrade.v7_0_0.util.DDMStructureTable;
045 import com.liferay.portal.util.PortalUtil;
046 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
047 import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
048 import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
049 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
050 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
051 import com.liferay.portlet.dynamicdatamapping.io.DDMFormJSONSerializerUtil;
052 import com.liferay.portlet.dynamicdatamapping.io.DDMFormLayoutJSONSerializerUtil;
053 import com.liferay.portlet.dynamicdatamapping.io.DDMFormValuesJSONDeserializerUtil;
054 import com.liferay.portlet.dynamicdatamapping.io.DDMFormValuesJSONSerializerUtil;
055 import com.liferay.portlet.dynamicdatamapping.io.DDMFormXSDDeserializerUtil;
056 import com.liferay.portlet.dynamicdatamapping.model.DDMContent;
057 import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
058 import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
059 import com.liferay.portlet.dynamicdatamapping.model.DDMFormLayout;
060 import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
061 import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
062 import com.liferay.portlet.dynamicdatamapping.model.UnlocalizedValue;
063 import com.liferay.portlet.dynamicdatamapping.model.Value;
064 import com.liferay.portlet.dynamicdatamapping.storage.DDMFormFieldValue;
065 import com.liferay.portlet.dynamicdatamapping.storage.DDMFormValues;
066 import com.liferay.portlet.dynamicdatamapping.util.DDMFieldsCounter;
067 import com.liferay.portlet.dynamicdatamapping.util.DDMFormFieldValueTransformer;
068 import com.liferay.portlet.dynamicdatamapping.util.DDMFormValuesTransformer;
069 import com.liferay.portlet.dynamicdatamapping.util.DDMImpl;
070 import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
071
072 import java.io.File;
073
074 import java.sql.Connection;
075 import java.sql.PreparedStatement;
076 import java.sql.ResultSet;
077 import java.sql.SQLException;
078 import java.sql.Timestamp;
079
080 import java.util.ArrayList;
081 import java.util.HashMap;
082 import java.util.LinkedHashSet;
083 import java.util.List;
084 import java.util.Locale;
085 import java.util.Map;
086 import java.util.Set;
087
088
092 public class UpgradeDynamicDataMapping extends UpgradeProcess {
093
094 protected void addStructureLayout(
095 String uuid_, long structureLayoutId, long groupId, long companyId,
096 long userId, String userName, Timestamp createDate,
097 Timestamp modifiedDate, long structureVersionId, String definition)
098 throws Exception {
099
100 Connection con = null;
101 PreparedStatement ps = null;
102
103 try {
104 con = DataAccess.getUpgradeOptimizedConnection();
105
106 StringBundler sb = new StringBundler(5);
107
108 sb.append("insert into DDMStructureLayout (uuid_, ");
109 sb.append("structureLayoutId, groupId, companyId, userId, ");
110 sb.append("userName, createDate, modifiedDate, ");
111 sb.append("structureVersionId, definition) values (?, ?, ?, ?, ");
112 sb.append("?, ?, ?, ?, ?, ?)");
113
114 String sql = sb.toString();
115
116 ps = con.prepareStatement(sql);
117
118 ps.setString(1, uuid_);
119 ps.setLong(2, structureLayoutId);
120 ps.setLong(3, groupId);
121 ps.setLong(4, companyId);
122 ps.setLong(5, userId);
123 ps.setString(6, userName);
124 ps.setTimestamp(7, createDate);
125 ps.setTimestamp(8, modifiedDate);
126 ps.setLong(9, structureVersionId);
127 ps.setString(10, definition);
128
129 ps.executeUpdate();
130 }
131 catch (Exception e) {
132 _log.error(
133 "Unable to upgrade dynamic data mapping structure layout " +
134 "with structure version ID " + structureVersionId);
135
136 throw e;
137 }
138 finally {
139 DataAccess.cleanUp(con, ps);
140 }
141 }
142
143 protected void addStructureVersion(
144 long structureVersionId, long groupId, long companyId, long userId,
145 String userName, Timestamp createDate, long structureId,
146 long parentStructureId, String name, String description,
147 String definition, String storageType, int type, int status,
148 long statusByUserId, String statusByUserName, Timestamp statusDate)
149 throws Exception {
150
151 Connection con = null;
152 PreparedStatement ps = null;
153
154 try {
155 con = DataAccess.getUpgradeOptimizedConnection();
156
157 StringBundler sb = new StringBundler(6);
158
159 sb.append("insert into DDMStructureVersion (structureVersionId, ");
160 sb.append("groupId, companyId, userId, userName, createDate, ");
161 sb.append("structureId, version, parentStructureId, name, ");
162 sb.append("description, definition, storageType, type_, status, ");
163 sb.append("statusByUserId, statusByUserName, statusDate) values ");
164 sb.append("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
165
166 String sql = sb.toString();
167
168 ps = con.prepareStatement(sql);
169
170 ps.setLong(1, structureVersionId);
171 ps.setLong(2, groupId);
172 ps.setLong(3, companyId);
173 ps.setLong(4, userId);
174 ps.setString(5, userName);
175 ps.setTimestamp(6, createDate);
176 ps.setLong(7, structureId);
177 ps.setString(8, DDMStructureConstants.VERSION_DEFAULT);
178 ps.setLong(9, parentStructureId);
179 ps.setString(10, name);
180 ps.setString(11, description);
181 ps.setString(12, definition);
182 ps.setString(13, storageType);
183 ps.setInt(14, type);
184 ps.setInt(15, status);
185 ps.setLong(16, statusByUserId);
186 ps.setString(17, statusByUserName);
187 ps.setTimestamp(18, statusDate);
188
189 ps.executeUpdate();
190 }
191 catch (Exception e) {
192 _log.error(
193 "Unable to upgrade dynamic data mapping structure version " +
194 "with structure ID " + structureId);
195
196 throw e;
197 }
198 finally {
199 DataAccess.cleanUp(con, ps);
200 }
201 }
202
203 protected void addTemplateVersion(
204 long templateVersionId, long groupId, long companyId, long userId,
205 String userName, Timestamp createDate, long classNameId,
206 long classPK, long templateId, String name, String description,
207 String language, String script, int status, long statusByUserId,
208 String statusByUserName, Timestamp statusDate)
209 throws Exception {
210
211 Connection con = null;
212 PreparedStatement ps = null;
213
214 try {
215 con = DataAccess.getUpgradeOptimizedConnection();
216
217 StringBundler sb = new StringBundler(5);
218
219 sb.append("insert into DDMTemplateVersion (templateVersionId, ");
220 sb.append("groupId, companyId, userId, userName, createDate, ");
221 sb.append("classNameId, classPK, templateId, version, name, ");
222 sb.append("description, language, script, status, ");
223 sb.append("statusByUserId, statusByUserName, statusDate) values (");
224 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
225
226 String sql = sb.toString();
227
228 ps = con.prepareStatement(sql);
229
230 ps.setLong(1, templateVersionId);
231 ps.setLong(2, groupId);
232 ps.setLong(3, companyId);
233 ps.setLong(4, userId);
234 ps.setString(5, userName);
235 ps.setTimestamp(6, createDate);
236 ps.setLong(7, classNameId);
237 ps.setLong(8, classPK);
238 ps.setLong(9, templateId);
239 ps.setString(10, DDMStructureConstants.VERSION_DEFAULT);
240 ps.setString(11, name);
241 ps.setString(12, description);
242 ps.setString(13, language);
243 ps.setString(14, script);
244 ps.setInt(15, status);
245 ps.setLong(16, statusByUserId);
246 ps.setString(17, statusByUserName);
247 ps.setTimestamp(18, statusDate);
248
249 ps.executeUpdate();
250 }
251 catch (Exception e) {
252 _log.error(
253 "Unable to upgrade dynamic data mapping template version " +
254 "with template ID " + templateId);
255
256 throw e;
257 }
258 finally {
259 DataAccess.cleanUp(con, ps);
260 }
261 }
262
263 @Override
264 protected void doUpgrade() throws Exception {
265 upgradeDDMSchema();
266
267 upgradeStructuresAndAddStructureVersionsAndLayouts();
268 upgradeTemplatesAndAddTemplateVersions();
269 upgradeXMLStorageAdapter();
270
271 upgradeFileUploadReferences();
272 }
273
274 protected DDMForm getDDMForm(long structureId) throws Exception {
275 DDMForm ddmForm = _ddmForms.get(structureId);
276
277 if (ddmForm != null) {
278 return ddmForm;
279 }
280
281 Connection con = null;
282 PreparedStatement ps = null;
283 ResultSet rs = null;
284
285 try {
286 con = DataAccess.getUpgradeOptimizedConnection();
287
288 ps = con.prepareStatement(
289 "select parentStructureId, definition from DDMStructure " +
290 "where structureId = ?" );
291
292 ps.setLong(1, structureId);
293
294 rs = ps.executeQuery();
295
296 if (rs.next()) {
297 long parentStructureId = rs.getLong("parentStructureId");
298 String definition = rs.getString("definition");
299
300 ddmForm = DDMFormXSDDeserializerUtil.deserialize(definition);
301
302 if (parentStructureId > 0) {
303 DDMForm parentDDMForm = getDDMForm(parentStructureId);
304
305 List<DDMFormField> ddmFormFields =
306 ddmForm.getDDMFormFields();
307
308 ddmFormFields.addAll(parentDDMForm.getDDMFormFields());
309 }
310
311 _ddmForms.put(structureId, ddmForm);
312
313 return ddmForm;
314 }
315
316 throw new UpgradeException(
317 "Unable to find dynamic data mapping structure with ID " +
318 structureId);
319 }
320 finally {
321 DataAccess.cleanUp(con, ps, rs);
322 }
323 }
324
325 protected DDMFormValues getDDMFormValues(DDMForm ddmForm, String xml)
326 throws Exception {
327
328 DDMFormValuesXSDDeserializer ddmFormValuesXSDDeserializer =
329 new DDMFormValuesXSDDeserializer();
330
331 return ddmFormValuesXSDDeserializer.deserialize(ddmForm, xml);
332 }
333
334 protected String getDefaultDDMFormLayoutDefinition(DDMForm ddmForm) {
335 DDMFormLayout ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(ddmForm);
336
337 return DDMFormLayoutJSONSerializerUtil.serialize(ddmFormLayout);
338 }
339
340 protected String toJSON(DDMForm ddmForm) {
341 return DDMFormJSONSerializerUtil.serialize(ddmForm);
342 }
343
344 protected String toJSON(DDMFormValues ddmFormValues) {
345 return DDMFormValuesJSONSerializerUtil.serialize(ddmFormValues);
346 }
347
348 protected void transformFileUploadDDMFormFields(
349 long groupId, long companyId, long userId, String userName,
350 Timestamp createDate, long entryId, String entryVersion,
351 DDMFormValues ddmFormValues)
352 throws Exception {
353
354 DDMFormValuesTransformer ddmFormValuesTransformer =
355 new DDMFormValuesTransformer(ddmFormValues);
356
357 ddmFormValuesTransformer.addTransformer(
358 new FileUploadDDMFormFieldValueTransformer(
359 groupId, companyId, userId, userName, createDate, entryId,
360 entryVersion));
361
362 ddmFormValuesTransformer.transform();
363 }
364
365 protected void updateContent(DDMForm ddmForm, long contentId)
366 throws Exception {
367
368 Connection con = null;
369 PreparedStatement ps = null;
370 ResultSet rs = null;
371
372 try {
373 con = DataAccess.getUpgradeOptimizedConnection();
374
375 ps = con.prepareStatement(
376 "select data_ from DDMContent where contentId = ?");
377
378 ps.setLong(1, contentId);
379
380 rs = ps.executeQuery();
381
382 if (rs.next()) {
383 String xml = rs.getString("data_");
384
385 DDMFormValues ddmFormValues = getDDMFormValues(ddmForm, xml);
386
387 updateContent(contentId, toJSON(ddmFormValues));
388 }
389 }
390 finally {
391 DataAccess.cleanUp(con, ps, rs);
392 }
393 }
394
395 protected void updateContent(long contentId, String data_)
396 throws Exception {
397
398 Connection con = null;
399 PreparedStatement ps = null;
400 ResultSet rs = null;
401
402 try {
403 con = DataAccess.getUpgradeOptimizedConnection();
404
405 ps = con.prepareStatement(
406 "update DDMContent set data_= ? where contentId = ?");
407
408 ps.setString(1, data_);
409 ps.setLong(2, contentId);
410
411 ps.executeUpdate();
412 }
413 finally {
414 DataAccess.cleanUp(con, ps, rs);
415 }
416 }
417
418 protected DDMForm updateDDMFormFields(DDMForm ddmForm) {
419 DDMForm copyDDMForm = new DDMForm(ddmForm);
420
421 Map<String, DDMFormField> ddmFormFieldsMap =
422 copyDDMForm.getDDMFormFieldsMap(true);
423
424 for (DDMFormField ddmFormField : ddmFormFieldsMap.values()) {
425 String dataType = ddmFormField.getDataType();
426
427 if (Validator.equals(dataType, "file-upload")) {
428 ddmFormField.setDataType("document-library");
429 ddmFormField.setType("ddm-documentlibrary");
430 }
431 else if (Validator.equals(dataType, "image")) {
432 ddmFormField.setFieldNamespace("ddm");
433 ddmFormField.setType("ddm-image");
434 }
435 }
436
437 return copyDDMForm;
438 }
439
440 protected void updateStructureStorageType() throws Exception {
441 runSQL(
442 "update DDMStructure set storageType='json' where " +
443 "storageType = 'xml'");
444 }
445
446 protected void updateStructureVersionStorageType() throws Exception {
447 runSQL(
448 "update DDMStructureVersion set storageType='json' where " +
449 "storageType = 'xml'");
450 }
451
452 protected void upgradeDDLFileUploadReferences() throws Exception {
453 Connection con = null;
454 PreparedStatement ps = null;
455 ResultSet rs = null;
456
457 try {
458 con = DataAccess.getUpgradeOptimizedConnection();
459
460 StringBundler sb = new StringBundler(7);
461
462 sb.append("select DDLRecordVersion.*, DDMContent.data_, ");
463 sb.append("DDMStructure.structureId from DDLRecordVersion inner ");
464 sb.append("join DDLRecordSet on DDLRecordVersion.recordSetId = ");
465 sb.append("DDLRecordSet.recordSetId inner join DDMContent on ");
466 sb.append("DDLRecordVersion.DDMStorageId = DDMContent.contentId ");
467 sb.append("inner join DDMStructure on DDLRecordSet.");
468 sb.append("DDMStructureId = DDMStructure.structureId");
469
470 ps = con.prepareStatement(sb.toString());
471
472 rs = ps.executeQuery();
473
474 while (rs.next()) {
475 long groupId = rs.getLong("groupId");
476 long companyId = rs.getLong("companyId");
477 long userId = rs.getLong("userId");
478 String userName = rs.getString("userName");
479 Timestamp createDate = rs.getTimestamp("createDate");
480 long entryId = rs.getLong("recordId");
481 String entryVersion = rs.getString("version");
482 long contentId = rs.getLong("ddmStorageId");
483 String data_ = rs.getString("data_");
484 long ddmStructureId = rs.getLong("structureId");
485
486 DDMForm ddmForm = getDDMForm(ddmStructureId);
487
488 DDMFormValues ddmFormValues =
489 DDMFormValuesJSONDeserializerUtil.deserialize(
490 ddmForm, data_);
491
492 transformFileUploadDDMFormFields(
493 groupId, companyId, userId, userName, createDate, entryId,
494 entryVersion, ddmFormValues);
495
496 updateContent(contentId, toJSON(ddmFormValues));
497 }
498 }
499 finally {
500 DataAccess.cleanUp(con, ps, rs);
501 }
502 }
503
504 protected void upgradeDDMSchema() throws Exception {
505 try {
506 runSQL("alter_column_name DDMContent xml data_ TEXT null");
507 }
508 catch (SQLException sqle) {
509 upgradeTable(
510 DDMContentTable.TABLE_NAME, DDMContentTable.TABLE_COLUMNS,
511 DDMContentTable.TABLE_SQL_CREATE,
512 DDMContentTable.TABLE_SQL_ADD_INDEXES);
513 }
514
515 try {
516 runSQL("alter_column_name DDMStructure xsd definition TEXT null");
517 }
518 catch (SQLException sqle) {
519 upgradeTable(
520 DDMStructureTable.TABLE_NAME, DDMStructureTable.TABLE_COLUMNS,
521 DDMStructureTable.TABLE_SQL_CREATE,
522 DDMStructureTable.TABLE_SQL_ADD_INDEXES);
523 }
524 }
525
526 protected void upgradeDLFileUploadReferences() throws Exception {
527 Connection con = null;
528 PreparedStatement ps = null;
529 ResultSet rs = null;
530
531 try {
532 con = DataAccess.getUpgradeOptimizedConnection();
533
534 StringBundler sb = new StringBundler(10);
535
536 sb.append("select DLFileVersion.*, DDMContent.contentId, ");
537 sb.append("DDMContent.data_, DDMStructure.structureId from ");
538 sb.append("DLFileEntryMetadata inner join DDMContent on ");
539 sb.append("DLFileEntryMetadata.DDMStorageId = DDMContent.");
540 sb.append("contentId inner join DDMStructure on ");
541 sb.append("DLFileEntryMetadata.DDMStructureId = DDMStructure.");
542 sb.append("structureId inner join DLFileVersion on ");
543 sb.append("DLFileEntryMetadata.fileVersionId = DLFileVersion.");
544 sb.append("fileVersionId and DLFileEntryMetadata.fileEntryId = ");
545 sb.append("DLFileVersion.fileEntryId");
546
547 ps = con.prepareStatement(sb.toString());
548
549 rs = ps.executeQuery();
550
551 while (rs.next()) {
552 long groupId = rs.getLong("groupId");
553 long companyId = rs.getLong("companyId");
554 long userId = rs.getLong("userId");
555 String userName = rs.getString("userName");
556 Timestamp createDate = rs.getTimestamp("createDate");
557 long entryId = rs.getLong("fileEntryId");
558 String entryVersion = rs.getString("version");
559 long contentId = rs.getLong("contentId");
560 String data_ = rs.getString("data_");
561 long ddmStructureId = rs.getLong("structureId");
562
563 DDMForm ddmForm = getDDMForm(ddmStructureId);
564
565 DDMFormValues ddmFormValues =
566 DDMFormValuesJSONDeserializerUtil.deserialize(
567 ddmForm, data_);
568
569 transformFileUploadDDMFormFields(
570 groupId, companyId, userId, userName, createDate, entryId,
571 entryVersion, ddmFormValues);
572
573 updateContent(contentId, toJSON(ddmFormValues));
574 }
575 }
576 finally {
577 DataAccess.cleanUp(con, ps, rs);
578 }
579 }
580
581 protected void upgradeFileUploadReferences() throws Exception {
582 upgradeDDLFileUploadReferences();
583 upgradeDLFileUploadReferences();
584 }
585
586 protected void upgradeStructureDefinition(
587 long structureId, String definition)
588 throws Exception {
589
590 Connection con = null;
591 PreparedStatement ps = null;
592
593 try {
594 con = DataAccess.getUpgradeOptimizedConnection();
595
596 ps = con.prepareStatement(
597 "update DDMStructure set definition = ? where structureId = ?");
598
599 ps.setString(1, definition);
600 ps.setLong(2, structureId);
601
602 ps.executeUpdate();
603 }
604 catch (Exception e) {
605 _log.error(
606 "Unable to upgrade dynamic data mapping structure with " +
607 "structure ID " + structureId);
608
609 throw e;
610 }
611 finally {
612 DataAccess.cleanUp(con, ps);
613 }
614 }
615
616 protected void upgradeStructuresAndAddStructureVersionsAndLayouts()
617 throws Exception {
618
619 Connection con = null;
620 PreparedStatement ps = null;
621 ResultSet rs = null;
622
623 try {
624 con = DataAccess.getUpgradeOptimizedConnection();
625
626 ps = con.prepareStatement("select * from DDMStructure");
627
628 rs = ps.executeQuery();
629
630 while (rs.next()) {
631 long structureId = rs.getLong("structureId");
632 long groupId = rs.getLong("groupId");
633 long companyId = rs.getLong("companyId");
634 long userId = rs.getLong("userId");
635 String userName = rs.getString("userName");
636 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
637 long parentStructureId = rs.getLong("parentStructureId");
638 String name = rs.getString("name");
639 String description = rs.getString("description");
640 String storageType = rs.getString("storageType");
641 int type = rs.getInt("type_");
642
643 DDMForm ddmForm = getDDMForm(structureId);
644
645 ddmForm = updateDDMFormFields(ddmForm);
646
647 String definition = toJSON(ddmForm);
648
649 upgradeStructureDefinition(structureId, definition);
650
651 long structureVersionId = increment();
652
653 addStructureVersion(
654 structureVersionId, groupId, companyId, userId, userName,
655 modifiedDate, structureId, parentStructureId, name,
656 description, definition, storageType, type,
657 WorkflowConstants.STATUS_APPROVED, userId, userName,
658 modifiedDate);
659
660 String ddmFormLayoutDefinition =
661 getDefaultDDMFormLayoutDefinition(ddmForm);
662
663 addStructureLayout(
664 PortalUUIDUtil.generate(), increment(), groupId, companyId,
665 userId, userName, modifiedDate, modifiedDate,
666 structureVersionId, ddmFormLayoutDefinition);
667 }
668 }
669 finally {
670 DataAccess.cleanUp(con, ps, rs);
671 }
672 }
673
674 protected void upgradeTemplatesAndAddTemplateVersions() throws Exception {
675 Connection con = null;
676 PreparedStatement ps = null;
677 ResultSet rs = null;
678
679 try {
680 con = DataAccess.getUpgradeOptimizedConnection();
681
682 ps = con.prepareStatement("select * from DDMTemplate");
683
684 rs = ps.executeQuery();
685
686 while (rs.next()) {
687 long groupId = rs.getLong("groupId");
688 long companyId = rs.getLong("companyId");
689 long userId = rs.getLong("userId");
690 String userName = rs.getString("userName");
691 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
692 long classNameId = rs.getLong("classNameId");
693 long classPK = rs.getLong("classPK");
694 long templateId = rs.getLong("templateId");
695 String name = rs.getString("name");
696 String description = rs.getString("description");
697 String language = rs.getString("language");
698 String script = rs.getString("script");
699
700 if (language.equals("xsd")) {
701 DDMForm ddmForm = DDMFormXSDDeserializerUtil.deserialize(
702 script);
703
704 ddmForm = updateDDMFormFields(ddmForm);
705
706 script = toJSON(ddmForm);
707
708 upgradeTemplateScript(templateId, script);
709 }
710
711 addTemplateVersion(
712 increment(), groupId, companyId, userId, userName,
713 modifiedDate, classNameId, classPK, templateId, name,
714 description, language, script,
715 WorkflowConstants.STATUS_APPROVED, userId, userName,
716 modifiedDate);
717 }
718 }
719 finally {
720 DataAccess.cleanUp(con, ps, rs);
721 }
722 }
723
724 protected void upgradeTemplateScript(long templateId, String script)
725 throws Exception {
726
727 Connection con = null;
728 PreparedStatement ps = null;
729
730 try {
731 con = DataAccess.getUpgradeOptimizedConnection();
732
733 ps = con.prepareStatement(
734 "update DDMTemplate set language = ?, script = ? where " +
735 "templateId = ?");
736
737 ps.setString(1, "json");
738 ps.setString(2, script);
739 ps.setLong(3, templateId);
740
741 ps.executeUpdate();
742 }
743 catch (Exception e) {
744 _log.error(
745 "Unable to upgrade dynamic data mapping template with " +
746 "template ID " + templateId);
747
748 throw e;
749 }
750 finally {
751 DataAccess.cleanUp(con, ps);
752 }
753 }
754
755 protected void upgradeXMLStorageAdapter() throws Exception {
756 Connection con = null;
757 PreparedStatement ps = null;
758 ResultSet rs = null;
759
760 try {
761 con = DataAccess.getUpgradeOptimizedConnection();
762
763 StringBundler sb = new StringBundler(5);
764
765 sb.append("select DDMStorageLink.classPK, DDMStorageLink.");
766 sb.append("structureId from DDMStorageLink inner join ");
767 sb.append("DDMStructure on (DDMStorageLink.structureId = ");
768 sb.append("DDMStructure.structureId) where DDMStorageLink.");
769 sb.append("classNameId = ? and DDMStructure.storageType = ?");
770
771 ps = con.prepareStatement(sb.toString());
772
773 ps.setLong(1, PortalUtil.getClassNameId(DDMContent.class));
774 ps.setString(2, "xml");
775
776 rs = ps.executeQuery();
777
778 while (rs.next()) {
779 long structureId = rs.getLong("structureId");
780 long classPK = rs.getLong("classPK");
781
782 DDMForm ddmForm = getDDMForm(structureId);
783
784 updateContent(ddmForm, classPK);
785 }
786
787 updateStructureStorageType();
788 updateStructureVersionStorageType();
789 }
790 finally {
791 DataAccess.cleanUp(con, ps, rs);
792 }
793 }
794
795 private static final Log _log = LogFactoryUtil.getLog(
796 UpgradeDynamicDataMapping.class);
797
798 private final Map<Long, DDMForm> _ddmForms = new HashMap<>();
799
800 private class DDMFormValuesXSDDeserializer {
801
802 public DDMFormValues deserialize(DDMForm ddmForm, String xml)
803 throws PortalException {
804
805 try {
806 DDMFormValues ddmFormValues = new DDMFormValues(ddmForm);
807
808 Document document = SAXReaderUtil.read(xml);
809
810 Element rootElement = document.getRootElement();
811
812 setDDMFormValuesAvailableLocales(ddmFormValues, rootElement);
813 setDDMFormValuesDefaultLocale(ddmFormValues, rootElement);
814
815 DDMFieldsCounter ddmFieldsCounter = new DDMFieldsCounter();
816
817 for (DDMFormField ddmFormField : ddmForm.getDDMFormFields()) {
818 String fieldName = ddmFormField.getName();
819
820 int repetitions = countDDMFieldRepetitions(
821 rootElement, fieldName, null, -1);
822
823 for (int i = 0; i < repetitions; i++) {
824 DDMFormFieldValue ddmFormFieldValue =
825 createDDMFormFieldValue(fieldName);
826
827 setDDMFormFieldValueProperties(
828 ddmFormFieldValue, ddmFormField, rootElement,
829 ddmFieldsCounter);
830
831 ddmFormValues.addDDMFormFieldValue(ddmFormFieldValue);
832 }
833 }
834
835 return ddmFormValues;
836 }
837 catch (DocumentException de) {
838 throw new UpgradeException(de);
839 }
840 }
841
842 protected int countDDMFieldRepetitions(
843 Element rootElement, String fieldName, String parentFieldName,
844 int parentOffset) {
845
846 String[] ddmFieldsDisplayValues = getDDMFieldsDisplayValues(
847 rootElement, true);
848
849 if (ddmFieldsDisplayValues.length != 0) {
850 return countDDMFieldRepetitions(
851 ddmFieldsDisplayValues, fieldName, parentFieldName,
852 parentOffset);
853 }
854
855 Element dynamicElementElement = getDynamicElementElementByName(
856 rootElement, fieldName);
857
858 if (dynamicElementElement != null) {
859 return 1;
860 }
861
862 return 0;
863 }
864
865 protected int countDDMFieldRepetitions(
866 String[] fieldsDisplayValues, String fieldName,
867 String parentFieldName, int parentOffset) {
868
869 int offset = -1;
870
871 int repetitions = 0;
872
873 for (int i = 0; i < fieldsDisplayValues.length; i++) {
874 String fieldDisplayName = fieldsDisplayValues[i];
875
876 if (offset > parentOffset) {
877 break;
878 }
879
880 if (fieldDisplayName.equals(parentFieldName)) {
881 offset++;
882 }
883
884 if (fieldDisplayName.equals(fieldName) &&
885 (offset == parentOffset)) {
886
887 repetitions++;
888 }
889 }
890
891 return repetitions;
892 }
893
894 protected DDMFormFieldValue createDDMFormFieldValue(String name) {
895 DDMFormFieldValue ddmFormFieldValue = new DDMFormFieldValue();
896
897 ddmFormFieldValue.setName(name);
898
899 return ddmFormFieldValue;
900 }
901
902 protected Set<Locale> getAvailableLocales(
903 Element dynamicElementElement) {
904
905 List<Element> dynamicContentElements =
906 dynamicElementElement.elements("dynamic-content");
907
908 Set<Locale> availableLocales = new LinkedHashSet<>();
909
910 for (Element dynamicContentElement : dynamicContentElements) {
911 String languageId = dynamicContentElement.attributeValue(
912 "language-id");
913
914 availableLocales.add(LocaleUtil.fromLanguageId(languageId));
915 }
916
917 return availableLocales;
918 }
919
920 protected Set<Locale> getAvailableLocales(
921 List<Element> dynamicElementElements) {
922
923 Set<Locale> availableLocales = new LinkedHashSet<>();
924
925 for (Element dynamicElementElement : dynamicElementElements) {
926 availableLocales.addAll(
927 getAvailableLocales(dynamicElementElement));
928 }
929
930 return availableLocales;
931 }
932
933 protected String getDDMFieldInstanceId(
934 Element rootElement, String fieldName, int index) {
935
936 String[] ddmFieldsDisplayValues = getDDMFieldsDisplayValues(
937 rootElement, false);
938
939 if (ddmFieldsDisplayValues.length == 0) {
940 return StringUtil.randomString();
941 }
942
943 String prefix = fieldName.concat(DDMImpl.INSTANCE_SEPARATOR);
944
945 for (String ddmFieldsDisplayValue : ddmFieldsDisplayValues) {
946 if (ddmFieldsDisplayValue.startsWith(prefix)) {
947 index--;
948
949 if (index < 0) {
950 return StringUtil.extractLast(
951 ddmFieldsDisplayValue, DDMImpl.INSTANCE_SEPARATOR);
952 }
953 }
954 }
955
956 return null;
957 }
958
959 protected String[] getDDMFieldsDisplayValues(
960 Element rootElement, boolean extractFieldName) {
961
962 Element fieldsDisplayElement = getDynamicElementElementByName(
963 rootElement, "_fieldsDisplay");
964
965 List<String> ddmFieldsDisplayValues = new ArrayList<>();
966
967 if (fieldsDisplayElement != null) {
968 Element fieldsDisplayDynamicContent =
969 fieldsDisplayElement.element("dynamic-content");
970
971 String fieldsDisplayText =
972 fieldsDisplayDynamicContent.getText();
973
974 for (String fieldDisplayValue :
975 StringUtil.split(fieldsDisplayText)) {
976
977 if (extractFieldName) {
978 fieldDisplayValue = StringUtil.extractFirst(
979 fieldDisplayValue, DDMImpl.INSTANCE_SEPARATOR);
980 }
981
982 ddmFieldsDisplayValues.add(fieldDisplayValue);
983 }
984 }
985
986 return ddmFieldsDisplayValues.toArray(
987 new String[ddmFieldsDisplayValues.size()]);
988 }
989
990 protected DDMFormFieldValue getDDMFormFieldValue(
991 Element dynamicElementElement) {
992
993 DDMFormFieldValue ddmFormFieldValue = new DDMFormFieldValue();
994
995 ddmFormFieldValue.setName(
996 dynamicElementElement.attributeValue("name"));
997
998 List<Element> dynamicContentElements =
999 dynamicElementElement.elements("dynamic-content");
1000
1001 ddmFormFieldValue.setValue(getValue(dynamicContentElements));
1002
1003 ddmFormFieldValue.setNestedDDMFormFields(
1004 getDDMFormFieldValues(
1005 dynamicElementElement.elements("dynamic-element")));
1006
1007 return ddmFormFieldValue;
1008 }
1009
1010 protected List<DDMFormFieldValue> getDDMFormFieldValues(
1011 List<Element> dynamicElementElements) {
1012
1013 if (dynamicElementElements == null) {
1014 return null;
1015 }
1016
1017 List<DDMFormFieldValue> ddmFormFieldValues = new ArrayList<>();
1018
1019 for (Element dynamicElement : dynamicElementElements) {
1020 ddmFormFieldValues.add(getDDMFormFieldValue(dynamicElement));
1021 }
1022
1023 return ddmFormFieldValues;
1024 }
1025
1026 protected String getDDMFormFieldValueValueString(
1027 Element dynamicElementElement, Locale locale, int index) {
1028
1029 Element dynamicContentElement = getDynamicContentElement(
1030 dynamicElementElement, locale, index);
1031
1032 return dynamicContentElement.getTextTrim();
1033 }
1034
1035 protected Locale getDefaultLocale(Element dynamicElementElement) {
1036 String defaultLanguageId = dynamicElementElement.attributeValue(
1037 "default-language-id");
1038
1039 return LocaleUtil.fromLanguageId(defaultLanguageId);
1040 }
1041
1042 protected Locale getDefaultLocale(
1043 List<Element> dynamicElementElements) {
1044
1045 for (Element dynamicElement : dynamicElementElements) {
1046 String defaultLanguageId = dynamicElement.attributeValue(
1047 "default-language-id");
1048
1049 if (defaultLanguageId != null) {
1050 return LocaleUtil.fromLanguageId(defaultLanguageId);
1051 }
1052 }
1053
1054 return null;
1055 }
1056
1057 protected Element getDynamicContentElement(
1058 Element dynamicElementElement, Locale locale, int index) {
1059
1060 String languageId = LocaleUtil.toLanguageId(locale);
1061
1062 XPath dynamicContentXPath = SAXReaderUtil.createXPath(
1063 "dynamic-content[(@language-id='" + languageId + "')]");
1064
1065 List<Node> nodes = dynamicContentXPath.selectNodes(
1066 dynamicElementElement);
1067
1068 return (Element)nodes.get(index);
1069 }
1070
1071 protected Element getDynamicElementElementByName(
1072 Element rootElement, String fieldName) {
1073
1074 XPath dynamicElementXPath = SAXReaderUtil.createXPath(
1075 "
1076
1077 if (dynamicElementXPath.booleanValueOf(rootElement)) {
1078 return (Element)dynamicElementXPath.evaluate(rootElement);
1079 }
1080
1081 return null;
1082 }
1083
1084 protected Value getValue(List<Element> dynamicContentElements) {
1085 Value value = new LocalizedValue();
1086
1087 for (Element dynamicContentElement : dynamicContentElements) {
1088 String fieldValue = dynamicContentElement.getText();
1089
1090 String languageId = dynamicContentElement.attributeValue(
1091 "language-id");
1092
1093 Locale locale = LocaleUtil.fromLanguageId(languageId);
1094
1095 value.addString(locale, fieldValue);
1096 }
1097
1098 return value;
1099 }
1100
1101 protected void setDDMFormFieldValueInstanceId(
1102 DDMFormFieldValue ddmFormFieldValue, Element rootElement,
1103 DDMFieldsCounter ddmFieldsCounter) {
1104
1105 String name = ddmFormFieldValue.getName();
1106
1107 String instanceId = getDDMFieldInstanceId(
1108 rootElement, name, ddmFieldsCounter.get(name));
1109
1110 ddmFormFieldValue.setInstanceId(instanceId);
1111 }
1112
1113 protected void setDDMFormFieldValueLocalizedValue(
1114 DDMFormFieldValue ddmFormFieldValue, Element dynamicElementElement,
1115 int index) {
1116
1117 Value value = new LocalizedValue(
1118 getDefaultLocale(dynamicElementElement));
1119
1120 Map<String, Integer> dynamicContentValuesMap = new HashMap<>();
1121
1122 for (Element dynamicContentElement : dynamicElementElement.elements(
1123 "dynamic-content")) {
1124
1125 String languageId = dynamicContentElement.attributeValue(
1126 "language-id");
1127
1128 int localizedContentIndex = 0;
1129
1130 if (dynamicContentValuesMap.containsKey(languageId)) {
1131 localizedContentIndex = dynamicContentValuesMap.get(
1132 languageId);
1133 }
1134
1135 if (localizedContentIndex == index) {
1136 Locale locale = LocaleUtil.fromLanguageId(languageId);
1137
1138 String content = dynamicContentElement.getText();
1139
1140 value.addString(locale, content);
1141 }
1142
1143 dynamicContentValuesMap.put(
1144 languageId, localizedContentIndex+1);
1145 }
1146
1147 ddmFormFieldValue.setValue(value);
1148 }
1149
1150 protected void setDDMFormFieldValueProperties(
1151 DDMFormFieldValue ddmFormFieldValue, DDMFormField ddmFormField,
1152 Element rootElement, DDMFieldsCounter ddmFieldsCounter)
1153 throws PortalException {
1154
1155 setDDMFormFieldValueInstanceId(
1156 ddmFormFieldValue, rootElement, ddmFieldsCounter);
1157
1158 setNestedDDMFormFieldValues(
1159 ddmFormFieldValue, ddmFormField, rootElement, ddmFieldsCounter);
1160
1161 setDDMFormFieldValueValues(
1162 ddmFormFieldValue, ddmFormField, rootElement, ddmFieldsCounter);
1163 }
1164
1165 protected void setDDMFormFieldValueUnlocalizedValue(
1166 DDMFormFieldValue ddmFormFieldValue, Element dynamicElement,
1167 int index) {
1168
1169 String valueString = getDDMFormFieldValueValueString(
1170 dynamicElement, getDefaultLocale(dynamicElement), index);
1171
1172 Value value = new UnlocalizedValue(valueString);
1173
1174 ddmFormFieldValue.setValue(value);
1175 }
1176
1177 protected void setDDMFormFieldValueValues(
1178 DDMFormFieldValue ddmFormFieldValue, DDMFormField ddmFormField,
1179 Element rootElement, DDMFieldsCounter ddmFieldsCounter) {
1180
1181 String fieldName = ddmFormFieldValue.getName();
1182
1183 Element dynamicElement = getDynamicElementElementByName(
1184 rootElement, fieldName);
1185
1186 if (Validator.isNotNull(ddmFormField.getDataType())) {
1187 if (ddmFormField.isLocalizable()) {
1188 setDDMFormFieldValueLocalizedValue(
1189 ddmFormFieldValue, dynamicElement,
1190 ddmFieldsCounter.get(fieldName));
1191 }
1192 else {
1193 setDDMFormFieldValueUnlocalizedValue(
1194 ddmFormFieldValue, dynamicElement,
1195 ddmFieldsCounter.get(fieldName));
1196 }
1197 }
1198
1199 ddmFieldsCounter.incrementKey(fieldName);
1200 }
1201
1202 protected void setDDMFormValuesAvailableLocales(
1203 DDMFormValues ddmFormValues, Element rootElement) {
1204
1205 Set<Locale> availableLocales = getAvailableLocales(
1206 rootElement.elements("dynamic-element"));
1207
1208 ddmFormValues.setAvailableLocales(availableLocales);
1209 }
1210
1211 protected void setDDMFormValuesDefaultLocale(
1212 DDMFormValues ddmFormValues, Element rootElement) {
1213
1214 Locale defaultLocale = getDefaultLocale(
1215 rootElement.elements("dynamic-element"));
1216
1217 ddmFormValues.setDefaultLocale(defaultLocale);
1218 }
1219
1220 protected void setNestedDDMFormFieldValues(
1221 DDMFormFieldValue ddmFormFieldValue, DDMFormField ddmFormField,
1222 Element rootElement, DDMFieldsCounter ddmFieldsCounter)
1223 throws PortalException {
1224
1225 String fieldName = ddmFormFieldValue.getName();
1226
1227 int parentOffset = ddmFieldsCounter.get(fieldName);
1228
1229 Map<String, DDMFormField> nestedDDMFormFieldsMap =
1230 ddmFormField.getNestedDDMFormFieldsMap();
1231
1232 String[] ddmFieldsDisplayValues = getDDMFieldsDisplayValues(
1233 rootElement, true);
1234
1235 for (Map.Entry<String, DDMFormField> nestedDDMFormFieldEntry :
1236 nestedDDMFormFieldsMap.entrySet()) {
1237
1238 String nestedDDMFormFieldName =
1239 nestedDDMFormFieldEntry.getKey();
1240
1241 DDMFormField nestedDDMFormField =
1242 nestedDDMFormFieldEntry.getValue();
1243
1244 int repetitions = countDDMFieldRepetitions(
1245 ddmFieldsDisplayValues, nestedDDMFormFieldName, fieldName,
1246 parentOffset);
1247
1248 for (int i = 0; i < repetitions; i++) {
1249 DDMFormFieldValue nestedDDMFormFieldValue =
1250 createDDMFormFieldValue(nestedDDMFormFieldName);
1251
1252 setDDMFormFieldValueProperties(
1253 nestedDDMFormFieldValue, nestedDDMFormField,
1254 rootElement, ddmFieldsCounter);
1255
1256 ddmFormFieldValue.addNestedDDMFormFieldValue(
1257 nestedDDMFormFieldValue);
1258 }
1259 }
1260 }
1261
1262 }
1263
1264 private class FileUploadDDMFormFieldValueTransformer
1265 implements DDMFormFieldValueTransformer {
1266
1267 public FileUploadDDMFormFieldValueTransformer(
1268 long groupId, long companyId, long userId, String userName,
1269 Timestamp createDate, long entryId, String entryVersion) {
1270
1271 _groupId = groupId;
1272 _companyId = companyId;
1273 _userId = userId;
1274 _userName = userName;
1275 _createDate = createDate;
1276 _entryId = entryId;
1277 _entryVersion = entryVersion;
1278 }
1279
1280 @Override
1281 public String getFieldType() {
1282 return "ddm-fileupload";
1283 }
1284
1285 @Override
1286 public void transform(DDMFormFieldValue ddmFormFieldValue)
1287 throws PortalException {
1288
1289 Value value = ddmFormFieldValue.getValue();
1290
1291 for (Locale locale : value.getAvailableLocales()) {
1292 String valueString = value.getString(locale);
1293
1294 if (Validator.isNull(valueString)) {
1295 continue;
1296 }
1297
1298 String fileEntryUuid = PortalUUIDUtil.generate();
1299
1300 upgradeFileUploadReference(
1301 fileEntryUuid, ddmFormFieldValue.getName(), valueString);
1302
1303 value.addString(locale, toJSON(_groupId, fileEntryUuid));
1304 }
1305 }
1306
1307 protected void addAssetEntry(
1308 long entryId, long groupId, long companyId, long userId,
1309 String userName, Timestamp createDate, Timestamp modifiedDate,
1310 long classNameId, long classPK, String classUuid,
1311 long classTypeId, boolean visible, Timestamp startDate,
1312 Timestamp endDate, Timestamp publishDate,
1313 Timestamp expirationDate, String mimeType, String title,
1314 String description, String summary, String url,
1315 String layoutUuid, int height, int width, double priority,
1316 int viewCount)
1317 throws Exception {
1318
1319 Connection con = null;
1320 PreparedStatement ps = null;
1321
1322 try {
1323 con = DataAccess.getUpgradeOptimizedConnection();
1324
1325 StringBundler sb = new StringBundler(9);
1326
1327 sb.append("insert into AssetEntry (entryId, groupId, ");
1328 sb.append("companyId, userId, userName, createDate, ");
1329 sb.append("modifiedDate, classNameId, classPK, classUuid, ");
1330 sb.append("classTypeId, visible, startDate, endDate, ");
1331 sb.append("publishDate, expirationDate, mimeType, title, ");
1332 sb.append("description, summary, url, layoutUuid, height, ");
1333 sb.append("width, priority, viewCount) values (?, ?, ?, ?, ");
1334 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
1335 sb.append("?, ?, ?, ?, ?, ?)");
1336
1337 String sql = sb.toString();
1338
1339 ps = con.prepareStatement(sql);
1340
1341 ps.setLong(1, entryId);
1342 ps.setLong(2, groupId);
1343 ps.setLong(3, companyId);
1344 ps.setLong(4, userId);
1345 ps.setString(5, userName);
1346 ps.setTimestamp(6, createDate);
1347 ps.setTimestamp(7, modifiedDate);
1348 ps.setLong(8, classNameId);
1349 ps.setLong(9, classPK);
1350 ps.setString(10, classUuid);
1351 ps.setLong(11, classTypeId);
1352 ps.setBoolean(12, visible);
1353 ps.setTimestamp(13, startDate);
1354 ps.setTimestamp(14, endDate);
1355 ps.setTimestamp(15, publishDate);
1356 ps.setTimestamp(16, expirationDate);
1357 ps.setString(17, mimeType);
1358 ps.setString(18, title);
1359 ps.setString(19, description);
1360 ps.setString(20, summary);
1361 ps.setString(21, url);
1362 ps.setString(22, layoutUuid);
1363 ps.setInt(23, height);
1364 ps.setInt(24, width);
1365 ps.setDouble(25, priority);
1366 ps.setInt(26, viewCount);
1367
1368 ps.executeUpdate();
1369 }
1370 finally {
1371 DataAccess.cleanUp(con, ps);
1372 }
1373 }
1374
1375 protected long addDDMDLFolder() throws Exception {
1376 long ddmFolderId = getDLFolderId(
1377 _groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, "DDM");
1378
1379 if (ddmFolderId > 0) {
1380 return ddmFolderId;
1381 }
1382
1383 ddmFolderId = increment();
1384
1385 addDLFolder(
1386 PortalUUIDUtil.generate(), ddmFolderId, _groupId, _companyId,
1387 _userId, _userName, _now, _now, _groupId,
1388 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, "DDM",
1389 StringPool.BLANK, _now);
1390
1391 return ddmFolderId;
1392 }
1393
1394 protected void addDLFileEntry(
1395 String uuid, long fileEntryId, long groupId, long companyId,
1396 long userId, String userName, Timestamp createDate,
1397 Timestamp modifiedDate, long classNameId, long classPK,
1398 long repositoryId, long folderId, String treePath, String name,
1399 String fileName, String extension, String mimeType,
1400 String title, String description, String extraSettings,
1401 long fileEntryTypeId, String version, long size, int readCount,
1402 long smallImageId, long largeImageId, long custom1ImageId,
1403 long custom2ImageId, boolean manualCheckInRequired)
1404 throws Exception {
1405
1406 Connection con = null;
1407 PreparedStatement ps = null;
1408
1409 try {
1410 con = DataAccess.getUpgradeOptimizedConnection();
1411
1412 StringBundler sb = new StringBundler(9);
1413
1414 sb.append("insert into DLFileEntry (uuid_, fileEntryId, ");
1415 sb.append("groupId, companyId, userId, userName, createDate, ");
1416 sb.append("modifiedDate, classNameId, classPK, repositoryId, ");
1417 sb.append("folderId, treePath, name, fileName, extension, ");
1418 sb.append("mimeType, title, description, extraSettings, ");
1419 sb.append("fileEntryTypeId, version, size_, readCount, ");
1420 sb.append("smallImageId, largeImageId, custom1ImageId, ");
1421 sb.append("custom2ImageId, manualCheckInRequired) values (?, ");
1422 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
1423 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
1424
1425 String sql = sb.toString();
1426
1427 ps = con.prepareStatement(sql);
1428
1429 ps.setString(1, uuid);
1430 ps.setLong(2, fileEntryId);
1431 ps.setLong(3, groupId);
1432 ps.setLong(4, companyId);
1433 ps.setLong(5, userId);
1434 ps.setString(6, userName);
1435 ps.setTimestamp(7, createDate);
1436 ps.setTimestamp(8, modifiedDate);
1437 ps.setLong(9, classNameId);
1438 ps.setLong(10, classPK);
1439 ps.setLong(11, repositoryId);
1440 ps.setLong(12, folderId);
1441 ps.setString(13, treePath);
1442 ps.setString(14, name);
1443 ps.setString(15, fileName);
1444 ps.setString(16, extension);
1445 ps.setString(17, mimeType);
1446 ps.setString(18, title);
1447 ps.setString(19, description);
1448 ps.setString(20, extraSettings);
1449 ps.setLong(21, fileEntryTypeId);
1450 ps.setString(22, version);
1451 ps.setLong(23, size);
1452 ps.setInt(24, readCount);
1453 ps.setLong(25, smallImageId);
1454 ps.setLong(26, largeImageId);
1455 ps.setLong(27, custom1ImageId);
1456 ps.setLong(28, custom2ImageId);
1457 ps.setBoolean(29, manualCheckInRequired);
1458
1459 ps.executeUpdate();
1460 }
1461 finally {
1462 DataAccess.cleanUp(con, ps);
1463 }
1464 }
1465
1466 protected void addDLFileVersion(
1467 String uuid, long fileVersionId, long groupId, long companyId,
1468 long userId, String userName, Timestamp createDate,
1469 Timestamp modifiedDate, long repositoryId, long folderId,
1470 long fileEntryId, String treePath, String fileName,
1471 String extension, String mimeType, String title,
1472 String description, String changeLog, String extraSettings,
1473 long fileEntryTypeId, String version, long size,
1474 String checksum, int status, long statusByUserId,
1475 String statusByUserName, Timestamp statusDate)
1476 throws Exception {
1477
1478 Connection con = null;
1479 PreparedStatement ps = null;
1480
1481 try {
1482 con = DataAccess.getUpgradeOptimizedConnection();
1483
1484 StringBundler sb = new StringBundler(10);
1485
1486 sb.append("insert into DLFileVersion (uuid_, fileVersionId, ");
1487 sb.append("groupId, companyId, userId, userName, createDate, ");
1488 sb.append("modifiedDate, repositoryId, folderId, ");
1489 sb.append("fileEntryId, treePath, fileName, extension, ");
1490 sb.append("mimeType, title, description, changeLog, ");
1491 sb.append("extraSettings, fileEntryTypeId, version, size_, ");
1492 sb.append("checksum, status, statusByUserId, ");
1493 sb.append("statusByUserName, statusDate) values (?, ?, ?, ?, ");
1494 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
1495 sb.append("?, ?, ?, ?, ?, ?, ?)");
1496
1497 String sql = sb.toString();
1498
1499 ps = con.prepareStatement(sql);
1500
1501 ps.setString(1, uuid);
1502 ps.setLong(2, fileVersionId);
1503 ps.setLong(3, groupId);
1504 ps.setLong(4, companyId);
1505 ps.setLong(5, userId);
1506 ps.setString(6, userName);
1507 ps.setTimestamp(7, createDate);
1508 ps.setTimestamp(8, modifiedDate);
1509 ps.setLong(9, repositoryId);
1510 ps.setLong(10, folderId);
1511 ps.setLong(11, fileEntryId);
1512 ps.setString(12, treePath);
1513 ps.setString(13, fileName);
1514 ps.setString(14, extension);
1515 ps.setString(15, mimeType);
1516 ps.setString(16, title);
1517 ps.setString(17, description);
1518 ps.setString(18, changeLog);
1519 ps.setString(19, extraSettings);
1520 ps.setLong(20, fileEntryTypeId);
1521 ps.setString(21, version);
1522 ps.setLong(22, size);
1523 ps.setString(23, checksum);
1524 ps.setInt(24, status);
1525 ps.setLong(25, statusByUserId);
1526 ps.setString(26, statusByUserName);
1527 ps.setTimestamp(27, statusDate);
1528
1529 ps.executeUpdate();
1530 }
1531 finally {
1532 DataAccess.cleanUp(con, ps);
1533 }
1534 }
1535
1536 protected void addDLFolder(
1537 String uuid, long folderId, long groupId, long companyId,
1538 long userId, String userName, Timestamp createDate,
1539 Timestamp modifiedDate, long repositoryId, long parentFolderId,
1540 String name, String description, Timestamp lastPostDate)
1541 throws Exception {
1542
1543 Connection con = null;
1544 PreparedStatement ps = null;
1545
1546 try {
1547 con = DataAccess.getUpgradeOptimizedConnection();
1548
1549 StringBundler sb = new StringBundler(5);
1550
1551 sb.append("insert into DLFolder (uuid_, folderId, groupId, ");
1552 sb.append("companyId, userId, userName, createDate, ");
1553 sb.append("modifiedDate, repositoryId, mountPoint, ");
1554 sb.append("parentFolderId, name, description, lastPostDate, ");
1555 sb.append("defaultFileEntryTypeId, hidden_, status, ");
1556 sb.append("statusByUserId, statusByUserName, restrictionType");
1557 sb.append(") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
1558 sb.append("?, ?, ?, ?, ?, ?, ?)");
1559
1560 String sql = sb.toString();
1561
1562 ps = con.prepareStatement(sql);
1563
1564 ps.setString(1, uuid);
1565 ps.setLong(2, folderId);
1566 ps.setLong(3, groupId);
1567 ps.setLong(4, companyId);
1568 ps.setLong(5, userId);
1569 ps.setString(6, userName);
1570 ps.setTimestamp(7, createDate);
1571 ps.setTimestamp(8, modifiedDate);
1572 ps.setLong(9, repositoryId);
1573 ps.setBoolean(10, false);
1574 ps.setLong(11, parentFolderId);
1575 ps.setString(12, name);
1576 ps.setString(13, description);
1577 ps.setTimestamp(14, lastPostDate);
1578 ps.setLong(15, 0);
1579 ps.setBoolean(16, false);
1580 ps.setInt(17, WorkflowConstants.STATUS_APPROVED);
1581 ps.setInt(18, 0);
1582 ps.setString(19, StringPool.BLANK);
1583 ps.setInt(20, 0);
1584
1585 ps.executeUpdate();
1586 }
1587 finally {
1588 DataAccess.cleanUp(con, ps);
1589 }
1590 }
1591
1592 protected long addDLFolderTree(String ddmFormFieldName)
1593 throws Exception {
1594
1595 long ddmFolderId = addDDMDLFolder();
1596
1597 long entryIdFolderId = addEntryIdDLFolder(ddmFolderId);
1598
1599 long entryVersionFolderId = addEntryVersionDLFolder(
1600 entryIdFolderId);
1601
1602 long fieldNameFolderId = increment();
1603
1604 addDLFolder(
1605 PortalUUIDUtil.generate(), fieldNameFolderId, _groupId,
1606 _companyId, _userId, _userName, _now, _now, _groupId,
1607 entryVersionFolderId, ddmFormFieldName, StringPool.BLANK, _now);
1608
1609 return fieldNameFolderId;
1610 }
1611
1612 protected long addEntryIdDLFolder(long ddmFolderId) throws Exception {
1613 long entryIdFolderId = getDLFolderId(
1614 _groupId, ddmFolderId, String.valueOf(_entryId));
1615
1616 if (entryIdFolderId > 0) {
1617 return entryIdFolderId;
1618 }
1619
1620 entryIdFolderId = increment();
1621
1622 addDLFolder(
1623 PortalUUIDUtil.generate(), entryIdFolderId, _groupId,
1624 _companyId, _userId, _userName, _now, _now, _groupId,
1625 ddmFolderId, String.valueOf(_entryId), StringPool.BLANK, _now);
1626
1627 return entryIdFolderId;
1628 }
1629
1630 protected long addEntryVersionDLFolder(long entryIdFolderId)
1631 throws Exception {
1632
1633 long entryVersionFolderId = getDLFolderId(
1634 _groupId, entryIdFolderId, _entryVersion);
1635
1636 if (entryVersionFolderId > 0) {
1637 return entryVersionFolderId;
1638 }
1639
1640 entryVersionFolderId = increment();
1641
1642 addDLFolder(
1643 PortalUUIDUtil.generate(), entryVersionFolderId, _groupId,
1644 _companyId, _userId, _userName, _now, _now, _groupId,
1645 entryIdFolderId, _entryVersion, StringPool.BLANK, _now);
1646
1647 return entryVersionFolderId;
1648 }
1649
1650 protected void addResourcePermissions(
1651 int mvccVersion, long resourcePermissionId, long companyId,
1652 String name, long scope, long primKey, long roleId,
1653 long ownerId, long actionIds)
1654 throws Exception {
1655
1656 Connection con = null;
1657 PreparedStatement ps = null;
1658
1659 try {
1660 con = DataAccess.getUpgradeOptimizedConnection();
1661
1662 StringBundler sb = new StringBundler(4);
1663
1664 sb.append("insert into ResourcePermission (mvccVersion, ");
1665 sb.append("resourcePermissionId, companyId, name, scope, ");
1666 sb.append("primKey, roleId, ownerId, actionIds) values (?, ");
1667 sb.append("?, ?, ?, ?, ?, ?, ?, ?)");
1668
1669 String sql = sb.toString();
1670
1671 ps = con.prepareStatement(sql);
1672
1673 ps.setLong(1, mvccVersion);
1674 ps.setLong(2, resourcePermissionId);
1675 ps.setLong(3, companyId);
1676 ps.setString(4, name);
1677 ps.setLong(5, scope);
1678 ps.setLong(6, primKey);
1679 ps.setLong(7, roleId);
1680 ps.setLong(8, ownerId);
1681 ps.setLong(9, actionIds);
1682
1683 ps.executeUpdate();
1684 }
1685 finally {
1686 DataAccess.cleanUp(con, ps);
1687 }
1688 }
1689
1690 protected long getActionBitwiseValue(String action) throws Exception {
1691 Connection con = null;
1692 PreparedStatement ps = null;
1693 ResultSet rs = null;
1694
1695 try {
1696 con = DataAccess.getUpgradeOptimizedConnection();
1697
1698 StringBundler sb = new StringBundler(4);
1699
1700 sb.append("select bitwiseValue from ResourceAction where ");
1701 sb.append("name = ? and actionId = ?");
1702
1703 String sql = sb.toString();
1704
1705 ps = con.prepareStatement(sql);
1706
1707 ps.setString(1, DLFileEntry.class.getName());
1708 ps.setString(2, action);
1709
1710 rs = ps.executeQuery();
1711
1712 if (rs.next()) {
1713 return rs.getLong("bitwiseValue");
1714 }
1715
1716 return 0;
1717 }
1718 finally {
1719 DataAccess.cleanUp(con, ps, rs);
1720 }
1721 }
1722
1723 protected long getActionIdsLong(String[] actions) throws Exception {
1724 long actionIdsLong = 0;
1725
1726 for (String action : actions) {
1727 actionIdsLong |= getActionBitwiseValue(action);
1728 }
1729
1730 return actionIdsLong;
1731 }
1732
1733 protected long getDLFolderId(
1734 long groupId, long parentFolderId, String name)
1735 throws Exception {
1736
1737 Connection con = null;
1738 PreparedStatement ps = null;
1739 ResultSet rs = null;
1740
1741 try {
1742 con = DataAccess.getUpgradeOptimizedConnection();
1743
1744 String sql =
1745 "select folderId from DLFolder where groupId = ? and " +
1746 "parentFolderId = ? and name = ?";
1747
1748 ps = con.prepareStatement(sql);
1749
1750 ps.setLong(1, groupId);
1751 ps.setLong(2, parentFolderId);
1752 ps.setString(3, name);
1753
1754 rs = ps.executeQuery();
1755
1756 if (rs.next()) {
1757 return rs.getLong("folderId");
1758 }
1759 }
1760 finally {
1761 DataAccess.cleanUp(con, ps, rs);
1762 }
1763
1764 return 0;
1765 }
1766
1767 protected String getExtension(String fileName) {
1768 String extension = StringPool.BLANK;
1769
1770 int pos = fileName.lastIndexOf(CharPool.PERIOD);
1771
1772 if (pos > 0) {
1773 extension = fileName.substring(pos + 1, fileName.length());
1774 }
1775
1776 return StringUtil.toLowerCase(extension);
1777 }
1778
1779 protected long getRoleId(String roleName) throws Exception {
1780 Connection con = null;
1781 PreparedStatement ps = null;
1782 ResultSet rs = null;
1783
1784 try {
1785 con = DataAccess.getUpgradeOptimizedConnection();
1786
1787 StringBundler sb = new StringBundler(4);
1788
1789 sb.append("select roleId from role_ where companyId = ? and ");
1790 sb.append("name = ?");
1791
1792 String sql = sb.toString();
1793
1794 ps = con.prepareStatement(sql);
1795
1796 ps.setLong(1, _companyId);
1797 ps.setString(2, roleName);
1798
1799 rs = ps.executeQuery();
1800
1801 if (rs.next()) {
1802 return rs.getLong("roleId");
1803 }
1804
1805 return 0;
1806 }
1807 finally {
1808 DataAccess.cleanUp(con, ps, rs);
1809 }
1810 }
1811
1812 protected String toJSON(long groupId, String fileEntryUuid) {
1813 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
1814
1815 jsonObject.put("groupId", groupId);
1816 jsonObject.put("uuid", fileEntryUuid);
1817
1818 return jsonObject.toString();
1819 }
1820
1821 protected String upgradeFileUploadReference(
1822 String fileEntryUuid, String ddmFormFieldName,
1823 String valueString)
1824 throws PortalException {
1825
1826 try {
1827 long dlFolderId = addDLFolderTree(ddmFormFieldName);
1828
1829 JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
1830 valueString);
1831
1832 String name = String.valueOf(
1833 increment(DLFileEntry.class.getName()));
1834
1835 String fileName = jsonObject.getString("name");
1836 String filePath = jsonObject.getString("path");
1837
1838
1839
1840 long fileEntryId = increment();
1841
1842 String extension = getExtension(fileName);
1843
1844 File file = DLStoreUtil.getFile(
1845 _companyId, CompanyConstants.SYSTEM, filePath);
1846
1847 addDLFileEntry(
1848 fileEntryUuid, fileEntryId, _groupId, _companyId, _userId,
1849 _userName, _createDate, _createDate, 0, 0, _groupId,
1850 dlFolderId, StringPool.BLANK, name, fileName, extension,
1851 MimeTypesUtil.getContentType(fileName), fileName,
1852 StringPool.BLANK, StringPool.BLANK,
1853 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT,
1854 DLFileEntryConstants.VERSION_DEFAULT, file.length(),
1855 DLFileEntryConstants.DEFAULT_READ_COUNT, 0, 0, 0, 0, false);
1856
1857
1858
1859 addResourcePermissions(
1860 0, increment(), _companyId, DLFileEntry.class.getName(),
1861 ResourceConstants.SCOPE_INDIVIDUAL, fileEntryId,
1862 getRoleId(RoleConstants.OWNER), _userId,
1863 getActionIdsLong(_ownerPermissions));
1864
1865 if (_groupId > 0) {
1866 addResourcePermissions(
1867 0, increment(), _companyId, DLFileEntry.class.getName(),
1868 ResourceConstants.SCOPE_INDIVIDUAL, fileEntryId,
1869 getRoleId(RoleConstants.SITE_MEMBER), 0,
1870 getActionIdsLong(_groupPermissions));
1871 }
1872
1873 addResourcePermissions(
1874 0, increment(), _companyId, DLFileEntry.class.getName(),
1875 ResourceConstants.SCOPE_INDIVIDUAL, fileEntryId,
1876 getRoleId(RoleConstants.GUEST), 0,
1877 getActionIdsLong(_guestPermissions));
1878
1879
1880
1881 addDLFileVersion(
1882 fileEntryUuid, increment(), _groupId, _companyId, _userId,
1883 _userName, _createDate, _createDate, _groupId, dlFolderId,
1884 fileEntryId, StringPool.BLANK, fileName, extension,
1885 MimeTypesUtil.getContentType(fileName), fileName,
1886 StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
1887 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT,
1888 DLFileEntryConstants.VERSION_DEFAULT, file.length(),
1889 StringPool.BLANK, WorkflowConstants.STATUS_APPROVED,
1890 _userId, _userName, _createDate);
1891
1892
1893
1894 addAssetEntry(
1895 increment(), _groupId, _companyId, _userId, _userName,
1896 _createDate, _createDate,
1897 PortalUtil.getClassNameId(DLFileEntry.class), fileEntryId,
1898 fileEntryUuid,
1899 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT,
1900 false, null, null, null, null,
1901 MimeTypesUtil.getContentType(fileName), fileName,
1902 StringPool.BLANK, StringPool.BLANK, null, null, 0, 0, 0, 0);
1903
1904
1905
1906 DLStoreUtil.addFile(_companyId, dlFolderId, name, file);
1907
1908 return fileEntryUuid;
1909 }
1910 catch (Exception e) {
1911 throw new UpgradeException(e);
1912 }
1913 }
1914
1915 private final long _companyId;
1916 private final Timestamp _createDate;
1917 private final long _entryId;
1918 private final String _entryVersion;
1919 private final long _groupId;
1920 private final String[] _groupPermissions = {"ADD_DISCUSSION", "VIEW"};
1921 private final String[] _guestPermissions = {"ADD_DISCUSSION", "VIEW"};
1922 private final Timestamp _now = new Timestamp(
1923 System.currentTimeMillis());
1924 private final String[] _ownerPermissions = {
1925 "ADD_DISCUSSION", "DELETE", "DELETE_DISCUSSION",
1926 "OVERRIDE_CHECKOUT", "PERMISSIONS", "UPDATE", "UPDATE_DISCUSSION",
1927 "VIEW"
1928 };
1929 private final long _userId;
1930 private final String _userName;
1931
1932 }
1933
1934 }