001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.Disjunction;
019    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
020    import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
021    import com.liferay.portal.kernel.dao.orm.Projection;
022    import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
023    import com.liferay.portal.kernel.dao.orm.Property;
024    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
027    import com.liferay.portal.kernel.exception.PortalException;
028    import com.liferay.portal.kernel.exception.SystemException;
029    import com.liferay.portal.kernel.json.JSONFactoryUtil;
030    import com.liferay.portal.kernel.json.JSONObject;
031    import com.liferay.portal.kernel.log.Log;
032    import com.liferay.portal.kernel.log.LogFactoryUtil;
033    import com.liferay.portal.kernel.repository.model.FileEntry;
034    import com.liferay.portal.kernel.repository.model.FileVersion;
035    import com.liferay.portal.kernel.repository.model.Folder;
036    import com.liferay.portal.kernel.util.GetterUtil;
037    import com.liferay.portal.kernel.util.MimeTypesUtil;
038    import com.liferay.portal.kernel.util.StringBundler;
039    import com.liferay.portal.kernel.util.StringPool;
040    import com.liferay.portal.kernel.util.StringUtil;
041    import com.liferay.portal.kernel.util.Validator;
042    import com.liferay.portal.kernel.xml.Attribute;
043    import com.liferay.portal.kernel.xml.Document;
044    import com.liferay.portal.kernel.xml.DocumentException;
045    import com.liferay.portal.kernel.xml.Element;
046    import com.liferay.portal.kernel.xml.Node;
047    import com.liferay.portal.kernel.xml.SAXReaderUtil;
048    import com.liferay.portal.kernel.xml.XPath;
049    import com.liferay.portal.model.AuditedModel;
050    import com.liferay.portal.model.BaseModel;
051    import com.liferay.portal.model.CompanyConstants;
052    import com.liferay.portal.model.User;
053    import com.liferay.portal.service.ServiceContext;
054    import com.liferay.portal.service.UserLocalServiceUtil;
055    import com.liferay.portal.util.PortalUtil;
056    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
057    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
058    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
059    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
060    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
061    import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
062    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
063    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
064    import com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalServiceUtil;
065    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
066    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
067    import com.liferay.portlet.dynamicdatalists.model.DDLRecordModel;
068    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
069    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
070    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
071    import com.liferay.portlet.dynamicdatamapping.model.DDMContent;
072    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
073    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureLink;
074    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
075    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
076    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLinkLocalServiceUtil;
077    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
078    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
079    import com.liferay.portlet.dynamicdatamapping.service.persistence.DDMContentActionableDynamicQuery;
080    import com.liferay.portlet.dynamicdatamapping.service.persistence.DDMContentUtil;
081    import com.liferay.portlet.dynamicdatamapping.service.persistence.DDMTemplateActionableDynamicQuery;
082    import com.liferay.portlet.dynamicdatamapping.storage.Field;
083    import com.liferay.portlet.dynamicdatamapping.storage.FieldConstants;
084    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
085    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
086    import com.liferay.portlet.dynamicdatamapping.util.DDMImpl;
087    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
088    
089    import java.io.File;
090    import java.io.Serializable;
091    
092    import java.util.HashMap;
093    import java.util.HashSet;
094    import java.util.List;
095    import java.util.Map;
096    import java.util.Set;
097    
098    /**
099     * @author Marcellus Tavares
100     */
101    public class VerifyDynamicDataMapping extends VerifyProcess {
102    
103            protected void addFieldsDisplay(Element rootElement) {
104                    List<Element> dynamicElementElements = rootElement.elements(
105                            "dynamic-element");
106    
107                    StringBundler sb = new StringBundler(4 * dynamicElementElements.size());
108    
109                    for (Element dynamicElementElement : dynamicElementElements) {
110                            String fieldName = dynamicElementElement.attributeValue("name");
111    
112                            sb.append(fieldName);
113                            sb.append(DDMImpl.INSTANCE_SEPARATOR);
114                            sb.append(StringUtil.randomId());
115    
116                            if (dynamicElementElements.indexOf(dynamicElementElement) !=
117                                            (dynamicElementElements.size() - 1)) {
118    
119                                    sb.append(StringPool.COMMA);
120                            }
121                    }
122    
123                    Element element = rootElement.addElement("dynamic-element");
124    
125                    element.addAttribute("name", DDMImpl.FIELDS_DISPLAY_NAME);
126    
127                    Element dynamicContentElement = element.addElement("dynamic-content");
128    
129                    dynamicContentElement.addCDATA(sb.toString());
130            }
131    
132            protected FileEntry addFileEntry(
133                            long companyId, long userId, long groupId, long folderId,
134                            String fileName, String filePath, int status)
135                    throws Exception {
136    
137                    String contentType = MimeTypesUtil.getContentType(fileName);
138    
139                    String title = fileName;
140    
141                    try {
142                            return DLAppLocalServiceUtil.getFileEntry(groupId, folderId, title);
143                    }
144                    catch (NoSuchFileEntryException nsfe) {
145                            if (_log.isDebugEnabled()) {
146                                    _log.debug(nsfe, nsfe);
147                            }
148                    }
149    
150                    try {
151                            File file = DLStoreUtil.getFile(
152                                    companyId, CompanyConstants.SYSTEM, filePath);
153    
154                            ServiceContext serviceContext = createServiceContext();
155    
156                            FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(
157                                    userId, groupId, folderId, fileName, contentType, title,
158                                    StringPool.BLANK, StringPool.BLANK, file, serviceContext);
159    
160                            updateFileEntryStatus(fileEntry, status, serviceContext);
161    
162                            return fileEntry;
163                    }
164                    catch (Exception e) {
165                            if (_log.isWarnEnabled()) {
166                                    _log.warn("Unable to add file entry " + fileName, e);
167                            }
168    
169                            return null;
170                    }
171            }
172    
173            protected Folder addFolder(
174                            long userId, long groupId, long primaryKey, String fieldName)
175                    throws Exception {
176    
177                    Folder ddmFolder = addFolder(
178                            userId, groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, "DDM",
179                            StringPool.BLANK);
180    
181                    Folder primaryKeyFolder = addFolder(
182                            userId, groupId, ddmFolder.getFolderId(),
183                            String.valueOf(primaryKey), StringPool.BLANK);
184    
185                    return addFolder(
186                            userId, groupId, primaryKeyFolder.getFolderId(), fieldName,
187                            StringPool.BLANK);
188            }
189    
190            protected Folder addFolder(
191                            long userId, long groupId, long parentFolderId, String name,
192                            String description)
193                    throws Exception {
194    
195                    try {
196                            return DLAppLocalServiceUtil.getFolder(
197                                    groupId, parentFolderId, name);
198                    }
199                    catch (NoSuchFolderException nsfe) {
200                            return DLAppLocalServiceUtil.addFolder(
201                                    userId, groupId, parentFolderId, name, description,
202                                    createServiceContext());
203                    }
204            }
205    
206            protected boolean checkDuplicateNames(DDMStructure structure)
207                    throws Exception {
208    
209                    String xml =
210                            "<root>" + getFullStructureXML(structure, StringPool.BLANK) +
211                                    "</root>";
212    
213                    Document document = SAXReaderUtil.read(xml);
214    
215                    Set<String> duplicateElementNames =
216                            getDuplicateElementNames(
217                                    document.getRootElement(), new HashSet<String>(),
218                                    new HashSet<String>());
219    
220                    if (duplicateElementNames.isEmpty()) {
221                            return false;
222                    }
223    
224                    if (!_log.isWarnEnabled()) {
225                            return true;
226                    }
227    
228                    StringBundler sb = new StringBundler(
229                            duplicateElementNames.size() * 2 + 7);
230    
231                    sb.append("Structure with class name ID ");
232                    sb.append(structure.getClassNameId());
233                    sb.append(" and structure key = ");
234                    sb.append(structure.getStructureKey());
235                    sb.append(" contains more than one element that is identified by the ");
236                    sb.append("same name either within itself or within any of its ");
237                    sb.append("parent structures. The duplicate element names are: ");
238    
239                    for (String duplicateElementName : duplicateElementNames) {
240                            sb.append(duplicateElementName);
241                            sb.append(StringPool.COMMA_AND_SPACE);
242                    }
243    
244                    sb.setIndex(sb.index() - 1);
245    
246                    _log.warn(sb.toString());
247    
248                    return true;
249            }
250    
251            protected boolean createDefaultMetadataElement(
252                    Element dynamicElementElement, String defaultLanguageId) {
253    
254                    boolean hasDefaultMetadataElement = hasDefaultMetadataElement(
255                            dynamicElementElement, defaultLanguageId);
256    
257                    if (hasDefaultMetadataElement) {
258                            return false;
259                    }
260    
261                    Element metadataElement = dynamicElementElement.addElement("meta-data");
262    
263                    metadataElement.addAttribute("locale", defaultLanguageId);
264    
265                    Element entryElement = metadataElement.addElement("entry");
266    
267                    entryElement.addAttribute("name", "label");
268                    entryElement.addCDATA(StringPool.BLANK);
269    
270                    return true;
271            }
272    
273            protected ServiceContext createServiceContext() {
274                    ServiceContext serviceContext = new ServiceContext();
275    
276                    serviceContext.setAddGroupPermissions(true);
277                    serviceContext.setAddGuestPermissions(true);
278    
279                    return serviceContext;
280            }
281    
282            @Override
283            protected void doVerify() throws Exception {
284                    setUpClassNameIds();
285    
286                    List<DDMStructure> structures =
287                            DDMStructureLocalServiceUtil.getStructures();
288    
289                    boolean duplicateExists = false;
290    
291                    for (DDMStructure structure : structures) {
292                            if (checkDuplicateNames(structure)) {
293                                    duplicateExists = true;
294                            }
295                    }
296    
297                    if (duplicateExists) {
298                            throw new VerifyException(
299                                    "Duplicate element name found in structures");
300                    }
301    
302                    for (DDMStructure structure : structures) {
303                            verifyStructure(structure);
304                    }
305    
306                    verifyDDMContent();
307    
308                    verifyDDMTemplateWithDate();
309            }
310    
311            protected Set<String> getDuplicateElementNames(
312                    Element element, Set<String> elementNames,
313                    Set<String> duplicateElementNames) {
314    
315                    String elementName = element.attributeValue("name");
316    
317                    if (!elementNames.add(elementName)) {
318                            duplicateElementNames.add(elementName);
319                    }
320    
321                    List<Element> dynamicElements = element.elements("dynamic-element");
322    
323                    for (Element dynamicElement : dynamicElements) {
324                            duplicateElementNames = getDuplicateElementNames(
325                                    dynamicElement, elementNames, duplicateElementNames);
326                    }
327    
328                    return duplicateElementNames;
329            }
330    
331            protected String getFileUploadPath(BaseModel<?> baseModel)
332                    throws Exception {
333    
334                    StringBundler sb = new StringBundler(7);
335    
336                    long primaryKey = 0;
337    
338                    String version = StringPool.BLANK;
339    
340                    if (baseModel instanceof DDLRecordModel) {
341                            DDLRecord ddlRecord = (DDLRecord)baseModel;
342    
343                            primaryKey = ddlRecord.getPrimaryKey();
344    
345                            DDLRecordVersion ddlRecordVersion = ddlRecord.getRecordVersion();
346    
347                            version = ddlRecordVersion.getVersion();
348                    }
349                    else {
350                            DLFileEntryMetadata dlFileEntryMetadata =
351                                    (DLFileEntryMetadata)baseModel;
352    
353                            primaryKey = dlFileEntryMetadata.getPrimaryKey();
354    
355                            DLFileVersion dlFileVersion = dlFileEntryMetadata.getFileVersion();
356    
357                            version = dlFileVersion.getVersion();
358                    }
359    
360                    sb.append("ddm");
361                    sb.append(StringPool.SLASH);
362                    sb.append(baseModel.getModelClassName());
363                    sb.append(StringPool.SLASH);
364                    sb.append(primaryKey);
365                    sb.append(StringPool.SLASH);
366                    sb.append(version);
367    
368                    return sb.toString();
369            }
370    
371            protected String getFullStructureXML(DDMStructure structure, String xml)
372                    throws Exception {
373    
374                    if (structure.getParentStructureId() != 0) {
375                            DDMStructure parentStructure =
376                                    DDMStructureLocalServiceUtil.getStructure(
377                                            structure.getParentStructureId());
378    
379                            xml = getFullStructureXML(parentStructure, xml);
380                    }
381    
382                    Document document = SAXReaderUtil.read(structure.getXsd());
383    
384                    Element rootElement = document.getRootElement();
385    
386                    List<Element> dynamicElements = rootElement.elements("dynamic-element");
387    
388                    for (Element dynamicElement : dynamicElements) {
389                            xml += dynamicElement.asXML();
390                    }
391    
392                    return xml;
393            }
394    
395            protected String getJSON(FileEntry fileEntry) {
396                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
397    
398                    jsonObject.put("groupId", fileEntry.getGroupId());
399                    jsonObject.put("uuid", fileEntry.getUuid());
400    
401                    return jsonObject.toString();
402            }
403    
404            protected long getUserId(AuditedModel auditedModel) throws Exception {
405                    User user = UserLocalServiceUtil.fetchUser(auditedModel.getUserId());
406    
407                    if (user != null) {
408                            return user.getUserId();
409                    }
410    
411                    User defaultUser = UserLocalServiceUtil.getDefaultUser(
412                            auditedModel.getCompanyId());
413    
414                    if (_log.isWarnEnabled()) {
415                            _log.warn(
416                                    "Using default user " + defaultUser.getUserId() +
417                                            " for audited model " + auditedModel.getModelClassName() +
418                                                    " with primary key " + auditedModel.getPrimaryKeyObj());
419                    }
420    
421                    return defaultUser.getUserId();
422            }
423    
424            protected boolean hasDefaultMetadataElement(
425                    Element dynamicElementElement, String defaultLanguageId) {
426    
427                    List<Element> metadataElements = dynamicElementElement.elements(
428                            "meta-data");
429    
430                    for (Element metadataElement : metadataElements) {
431                            String languageId = metadataElement.attributeValue("locale");
432    
433                            if (languageId.equals(defaultLanguageId)) {
434                                    return true;
435                            }
436                    }
437    
438                    return false;
439            }
440    
441            protected boolean hasFieldsDisplayElement(Element rootElement) {
442                    List<Element> dynamicElementElements = rootElement.elements(
443                            "dynamic-element");
444    
445                    for (Element dynamicElementElement : dynamicElementElements) {
446                            String fieldName = dynamicElementElement.attributeValue("name");
447    
448                            if (fieldName.equals(DDMImpl.FIELDS_DISPLAY_NAME)) {
449                                    return true;
450                            }
451                    }
452    
453                    return false;
454            }
455    
456            protected boolean hasFileUploadFields(DDMStructure structure)
457                    throws Exception {
458    
459                    Map<String, Map<String, String>> fieldsMap = structure.getFieldsMap();
460    
461                    for (Map<String, String> field : fieldsMap.values()) {
462                            String dataType = field.get(FieldConstants.DATA_TYPE);
463    
464                            if (dataType.equals("document-library") ||
465                                    dataType.equals("file-upload")) {
466    
467                                    return true;
468                            }
469                    }
470    
471                    return false;
472            }
473    
474            protected void setUpClassNameIds() {
475                    _ddlRecordSetClassNameId = PortalUtil.getClassNameId(
476                            DDLRecordSet.class);
477                    _ddmStructureClassNameId = PortalUtil.getClassNameId(
478                            DDMStructure.class);
479                    _dlFileEntryMetadataClassNameId = PortalUtil.getClassNameId(
480                            DLFileEntryMetadata.class);
481            }
482    
483            protected void updateDDLFileUploadReferences(long ddlRecordSetId)
484                    throws Exception {
485    
486                    List<DDLRecord> ddlRecords = DDLRecordLocalServiceUtil.getRecords(
487                            ddlRecordSetId);
488    
489                    for (DDLRecord ddlRecord : ddlRecords) {
490                            updateFileUploadReferences(
491                                    ddlRecord.getCompanyId(), ddlRecord.getDDMStorageId(),
492                                    getUserId(ddlRecord), ddlRecord.getGroupId(), ddlRecord,
493                                    ddlRecord.getStatus());
494                    }
495            }
496    
497            protected void updateDLFileUploadReferences(long dlFileEntryMetadataId)
498                    throws Exception {
499    
500                    DLFileEntryMetadata dlFileEntryMetadata =
501                            DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(
502                                    dlFileEntryMetadataId);
503    
504                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
505                            dlFileEntryMetadata.getFileEntryId());
506    
507                    FileVersion fileVersion = fileEntry.getFileVersion();
508    
509                    updateFileUploadReferences(
510                            fileEntry.getCompanyId(), dlFileEntryMetadata.getDDMStorageId(),
511                            getUserId(fileEntry), fileEntry.getGroupId(), dlFileEntryMetadata,
512                            fileVersion.getStatus());
513            }
514    
515            protected void updateFileEntryStatus(
516                            FileEntry fileEntry, int status, ServiceContext serviceContext)
517                    throws Exception {
518    
519                    FileVersion fileVersion = fileEntry.getFileVersion();
520    
521                    Map<String, Serializable> workflowContext =
522                            new HashMap<String, Serializable>();
523    
524                    workflowContext.put("event", DLSyncConstants.EVENT_ADD);
525    
526                    DLFileEntryLocalServiceUtil.updateStatus(
527                            fileVersion.getUserId(), fileVersion.getFileVersionId(), status,
528                            workflowContext, serviceContext);
529            }
530    
531            protected void updateFileUploadReferences(DDMStructure structure)
532                    throws Exception {
533    
534                    if (!hasFileUploadFields(structure)) {
535                            return;
536                    }
537    
538                    List<DDMStructureLink> structureLinks =
539                            DDMStructureLinkLocalServiceUtil.getStructureLinks(
540                                    structure.getStructureId(), QueryUtil.ALL_POS,
541                                    QueryUtil.ALL_POS);
542    
543                    for (DDMStructureLink structureLink : structureLinks) {
544                            updateFileUploadReferences(structureLink);
545                    }
546    
547                    List<DDMTemplate> templates = DDMTemplateLocalServiceUtil.getTemplates(
548                            structure.getGroupId(), _ddmStructureClassNameId,
549                            structure.getStructureId(),
550                            DDMTemplateConstants.TEMPLATE_TYPE_FORM);
551    
552                    for (DDMTemplate template : templates) {
553                            updateTemplate(template, updateXSD(template.getScript()));
554                    }
555            }
556    
557            protected void updateFileUploadReferences(DDMStructureLink structureLink)
558                    throws Exception {
559    
560                    long classNameId = structureLink.getClassNameId();
561    
562                    if (classNameId == _ddlRecordSetClassNameId) {
563                            updateDDLFileUploadReferences(structureLink.getClassPK());
564                    }
565                    else if (classNameId == _dlFileEntryMetadataClassNameId) {
566                            updateDLFileUploadReferences(structureLink.getClassPK());
567                    }
568            }
569    
570            protected void updateFileUploadReferences(
571                            long companyId, long storageId, long userId, long groupId,
572                            BaseModel<?> baseModel, int status)
573                    throws Exception {
574    
575                    Fields fields = StorageEngineUtil.getFields(storageId);
576    
577                    for (Field field : fields) {
578                            String dataType = field.getDataType();
579    
580                            if (!(dataType.equals("file-upload") ||
581                                      dataType.equals("document-library")) ||
582                                    Validator.isNull(field.getValue())) {
583    
584                                    continue;
585                            }
586    
587                            long primaryKey = GetterUtil.getLong(baseModel.getPrimaryKeyObj());
588    
589                            Folder folder = addFolder(
590                                    userId, groupId, primaryKey, field.getName());
591    
592                            String valueString = String.valueOf(field.getValue());
593    
594                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
595                                    valueString);
596    
597                            String filePath =
598                                    getFileUploadPath(baseModel) + StringPool.SLASH +
599                                            field.getName();
600    
601                            FileEntry fileEntry = addFileEntry(
602                                    companyId, userId, groupId, folder.getFolderId(),
603                                    jsonObject.getString("name"), filePath, status);
604    
605                            if (fileEntry != null) {
606                                    field.setValue(getJSON(fileEntry));
607                            }
608                    }
609    
610                    ServiceContext serviceContext = new ServiceContext();
611    
612                    StorageEngineUtil.update(storageId, fields, false, serviceContext);
613            }
614    
615            protected void updateStructure(DDMStructure structure, String xsd)
616                    throws Exception {
617    
618                    xsd = DDMXMLUtil.formatXML(xsd);
619    
620                    structure.setXsd(xsd);
621    
622                    DDMStructureLocalServiceUtil.updateDDMStructure(structure);
623            }
624    
625            protected void updateTemplate(DDMTemplate template, String script)
626                    throws Exception {
627    
628                    script = DDMXMLUtil.formatXML(script);
629    
630                    template.setScript(script);
631    
632                    DDMTemplateLocalServiceUtil.updateDDMTemplate(template);
633            }
634    
635            protected String updateTemplateScriptDateGetDateStatement(
636                            String language, String script) {
637    
638                    StringBundler oldTemplateScriptSB = new StringBundler(3);
639                    StringBundler newTemplateScriptSB = new StringBundler(4);
640    
641                    if (language.equals("ftl")) {
642                            oldTemplateScriptSB.append("dateUtil.getDate\\(");
643                            oldTemplateScriptSB.append("(.*)");
644                            oldTemplateScriptSB.append("locale\\s*\\)");
645    
646                            newTemplateScriptSB.append("dateUtil.getDate(");
647                            newTemplateScriptSB.append("$1");
648                            newTemplateScriptSB.append("locale, timeZoneUtil.");
649                            newTemplateScriptSB.append("getTimeZone(\"UTC\"))");
650                    }
651                    else if (language.equals("vm")) {
652                            oldTemplateScriptSB.append("dateUtil.getDate\\(");
653                            oldTemplateScriptSB.append("(.*)");
654                            oldTemplateScriptSB.append("\\$locale\\s*\\)");
655    
656                            newTemplateScriptSB.append("dateUtil.getDate(");
657                            newTemplateScriptSB.append("$1");
658                            newTemplateScriptSB.append("\\$locale, \\$timeZoneUtil.");
659                            newTemplateScriptSB.append("getTimeZone(\"UTC\"))");
660                    }
661    
662                    return script.replaceAll(
663                            oldTemplateScriptSB.toString(), newTemplateScriptSB.toString());
664            }
665    
666            protected String updateXSD(String xsd) throws Exception {
667                    Document document = SAXReaderUtil.read(xsd);
668    
669                    Element rootElement = document.getRootElement();
670    
671                    List<Element> dynamicElementElements = rootElement.elements(
672                            "dynamic-element");
673    
674                    for (Element dynamicElementElement : dynamicElementElements) {
675                            updateXSDDynamicElement(dynamicElementElement);
676                    }
677    
678                    return document.asXML();
679            }
680    
681            protected void updateXSDDynamicElement(Element element) {
682                    String dataType = element.attributeValue("dataType");
683    
684                    if (Validator.equals(dataType, "file-upload")) {
685                            element.addAttribute("dataType", "document-library");
686                            element.addAttribute("type", "ddm-documentlibrary");
687                    }
688    
689                    List<Element> dynamicElementElements = element.elements(
690                            "dynamic-element");
691    
692                    for (Element dynamicElementElement : dynamicElementElements) {
693                            updateXSDDynamicElement(dynamicElementElement);
694                    }
695    
696                    Attribute attribute = element.attribute("autoGeneratedName");
697    
698                    if (attribute != null) {
699                            element.remove(attribute);
700                    }
701            }
702    
703            protected void verifyDDMContent() throws Exception {
704                    ActionableDynamicQuery actionableDynamicQuery =
705                            new DDMContentActionableDynamicQuery() {
706    
707                                    @Override
708                                    protected void addCriteria(DynamicQuery dynamicQuery) {
709                                            Disjunction disjunction =
710                                                    RestrictionsFactoryUtil.disjunction();
711    
712                                            DynamicQuery ddlRecordDynamicQuery =
713                                                    DDLRecordLocalServiceUtil.dynamicQuery();
714    
715                                            Projection projection = ProjectionFactoryUtil.property(
716                                                    "DDMStorageId");
717    
718                                            ddlRecordDynamicQuery.setProjection(projection);
719    
720                                            Property contentIdProperty = PropertyFactoryUtil.forName(
721                                                    "contentId");
722    
723                                            disjunction.add(
724                                                    contentIdProperty.in(ddlRecordDynamicQuery));
725    
726                                            DynamicQuery dlFileEntryMetadataDynamicQuery =
727                                                    DLFileEntryMetadataLocalServiceUtil.dynamicQuery();
728    
729                                            Property fileEntryTypeIdProperty =
730                                                    PropertyFactoryUtil.forName("fileEntryTypeId");
731    
732                                            dlFileEntryMetadataDynamicQuery.add(
733                                                    fileEntryTypeIdProperty.ne(0L));
734    
735                                            dlFileEntryMetadataDynamicQuery.setProjection(projection);
736    
737                                            disjunction.add(
738                                                    contentIdProperty.in(dlFileEntryMetadataDynamicQuery));
739    
740                                            dynamicQuery.add(disjunction);
741                                    }
742    
743                                    @Override
744                                    protected void performAction(Object object)
745                                            throws PortalException, SystemException {
746    
747                                            DDMContent ddmContent = (DDMContent)object;
748    
749                                            try {
750                                                    Document document = SAXReaderUtil.read(
751                                                            ddmContent.getXml());
752    
753                                                    Element rootElement = document.getRootElement();
754    
755                                                    if (!hasFieldsDisplayElement(
756                                                                    document.getRootElement())) {
757    
758                                                            addFieldsDisplay(rootElement);
759    
760                                                            String xml = document.asXML();
761    
762                                                            ddmContent.setXml(DDMXMLUtil.formatXML(xml));
763    
764                                                            DDMContentUtil.update(ddmContent);
765                                                    }
766                                            }
767                                            catch (DocumentException de) {
768                                                    throw new PortalException(de);
769                                            }
770                                    }
771                    };
772    
773                    actionableDynamicQuery.performActions();
774            }
775    
776            protected void verifyDDMTemplateWithDate() throws Exception {
777                    ActionableDynamicQuery actionableDynamicQuery =
778                            new DDMTemplateActionableDynamicQuery() {
779    
780                                    @Override
781                                    protected void addCriteria(DynamicQuery dynamicQuery) {
782    
783                                            DynamicQuery ddmStructureDynamicQuery =
784                                                    DynamicQueryFactoryUtil.forClass(DDMStructure.class);
785    
786                                                    Property storageTypeProperty =
787                                                            PropertyFactoryUtil.forName("storageType");
788    
789                                                    ddmStructureDynamicQuery.add(
790                                                            storageTypeProperty.eq("xml"));
791    
792                                                    Property xsdProperty = PropertyFactoryUtil.forName(
793                                                            "xsd");
794    
795                                                    ddmStructureDynamicQuery.add(
796                                                            xsdProperty.like("%dataType=\"date\"%"));
797    
798                                                    Projection projection = ProjectionFactoryUtil.property(
799                                                            "structureId");
800    
801                                                    ddmStructureDynamicQuery.setProjection(projection);
802    
803                                                    Property typeProperty = PropertyFactoryUtil.forName(
804                                                            "type");
805    
806                                                    dynamicQuery.add(
807                                                            typeProperty.eq(
808                                                                    DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY));
809    
810                                                    Property classPKProperty = PropertyFactoryUtil.forName(
811                                                                    "classPK");
812    
813                                                    dynamicQuery.add(
814                                                            classPKProperty.in(ddmStructureDynamicQuery));
815                                    }
816    
817                                    @Override
818                                    protected void performAction(Object object)
819                                            throws PortalException, SystemException {
820    
821                                            DDMTemplate ddmTemplate = (DDMTemplate)object;
822    
823                                            String script = updateTemplateScriptDateGetDateStatement(
824                                                    ddmTemplate.getLanguage(), ddmTemplate.getScript());
825    
826                                            ddmTemplate.setScript(script);
827    
828                                            DDMTemplateLocalServiceUtil.updateDDMTemplate(ddmTemplate);
829                                    }
830                    };
831    
832                    actionableDynamicQuery.performActions();
833            }
834    
835            protected void verifyStructure(DDMStructure structure) throws Exception {
836                    updateFileUploadReferences(structure);
837    
838                    updateStructure(structure, updateXSD(structure.getXsd()));
839    
840                    boolean modified = false;
841    
842                    String defaultLanguageId = structure.getDefaultLanguageId();
843    
844                    XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element");
845    
846                    Document document = structure.getDocument();
847    
848                    List<Node> nodes = xPathSelector.selectNodes(document);
849    
850                    for (Node node : nodes) {
851                            Element dynamicElementElement = (Element)node;
852    
853                            if (createDefaultMetadataElement(
854                                            dynamicElementElement, defaultLanguageId)) {
855    
856                                    modified = true;
857                            }
858                    }
859    
860                    if (modified) {
861                            updateStructure(structure, document.asXML());
862                    }
863            }
864    
865            private static Log _log = LogFactoryUtil.getLog(
866                    VerifyDynamicDataMapping.class);
867    
868            private long _ddlRecordSetClassNameId;
869            private long _ddmStructureClassNameId;
870            private long _dlFileEntryMetadataClassNameId;
871    
872    }