001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.FileVersion;
023    import com.liferay.portal.kernel.repository.model.Folder;
024    import com.liferay.portal.kernel.search.Indexer;
025    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
026    import com.liferay.portal.kernel.trash.TrashHandler;
027    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
028    import com.liferay.portal.kernel.util.ArrayUtil;
029    import com.liferay.portal.kernel.util.FileUtil;
030    import com.liferay.portal.kernel.util.MapUtil;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.util.StringUtil;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.kernel.xml.Element;
035    import com.liferay.portal.model.Repository;
036    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
037    import com.liferay.portal.repository.portletrepository.PortletRepository;
038    import com.liferay.portal.service.RepositoryLocalServiceUtil;
039    import com.liferay.portal.service.ServiceContext;
040    import com.liferay.portal.util.PortalUtil;
041    import com.liferay.portlet.documentlibrary.DuplicateFileException;
042    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
043    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
044    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
045    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
046    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
047    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
048    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
049    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
050    import com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalServiceUtil;
051    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
052    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
053    import com.liferay.portlet.documentlibrary.util.DLProcessorRegistryUtil;
054    import com.liferay.portlet.documentlibrary.util.DLProcessorThreadLocal;
055    import com.liferay.portlet.documentlibrary.util.DLUtil;
056    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
057    import com.liferay.portlet.dynamicdatamapping.storage.DDMFormValues;
058    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
059    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
060    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
061    import com.liferay.portlet.exportimport.lar.ExportImportThreadLocal;
062    import com.liferay.portlet.exportimport.lar.PortletDataContext;
063    import com.liferay.portlet.exportimport.lar.PortletDataException;
064    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
065    import com.liferay.portlet.exportimport.lar.StagedModelModifiedDateComparator;
066    import com.liferay.portlet.trash.util.TrashUtil;
067    
068    import java.io.IOException;
069    import java.io.InputStream;
070    
071    import java.util.ArrayList;
072    import java.util.List;
073    import java.util.Map;
074    
075    /**
076     * @author Mate Thurzo
077     */
078    public class FileEntryStagedModelDataHandler
079            extends BaseStagedModelDataHandler<FileEntry> {
080    
081            public static final String[] CLASS_NAMES = {
082                    DLFileEntry.class.getName(), FileEntry.class.getName(),
083                    LiferayFileEntry.class.getName()
084            };
085    
086            @Override
087            public void deleteStagedModel(FileEntry fileEntry) throws PortalException {
088                    DLAppLocalServiceUtil.deleteFileEntry(fileEntry.getFileEntryId());
089            }
090    
091            @Override
092            public void deleteStagedModel(
093                            String uuid, long groupId, String className, String extraData)
094                    throws PortalException {
095    
096                    FileEntry fileEntry = fetchStagedModelByUuidAndGroupId(uuid, groupId);
097    
098                    if (fileEntry != null) {
099                            deleteStagedModel(fileEntry);
100                    }
101            }
102    
103            @Override
104            public FileEntry fetchStagedModelByUuidAndGroupId(
105                    String uuid, long groupId) {
106    
107                    try {
108                            return DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
109                                    uuid, groupId);
110                    }
111                    catch (PortalException pe) {
112                            return null;
113                    }
114            }
115    
116            @Override
117            public List<FileEntry> fetchStagedModelsByUuidAndCompanyId(
118                    String uuid, long companyId) {
119    
120                    List<DLFileEntry> dlFileEntries =
121                            DLFileEntryLocalServiceUtil.getDLFileEntriesByUuidAndCompanyId(
122                                    uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
123                                    new StagedModelModifiedDateComparator<DLFileEntry>());
124    
125                    List<FileEntry> fileEntries = new ArrayList<>();
126    
127                    for (DLFileEntry dlFileEntry : dlFileEntries) {
128                            fileEntries.add(new LiferayFileEntry(dlFileEntry));
129                    }
130    
131                    return fileEntries;
132            }
133    
134            @Override
135            public String[] getClassNames() {
136                    return CLASS_NAMES;
137            }
138    
139            @Override
140            public String getDisplayName(FileEntry fileEntry) {
141                    if (fileEntry.isInTrash()) {
142                            return TrashUtil.getOriginalTitle(fileEntry.getTitle());
143                    }
144    
145                    return fileEntry.getTitle();
146            }
147    
148            @Override
149            public void importStagedModel(
150                            PortletDataContext portletDataContext, FileEntry fileEntry)
151                    throws PortletDataException {
152    
153                    boolean dlProcessorEnabled = DLProcessorThreadLocal.isEnabled();
154    
155                    try {
156                            DLProcessorThreadLocal.setEnabled(false);
157    
158                            super.importStagedModel(portletDataContext, fileEntry);
159                    }
160                    finally {
161                            DLProcessorThreadLocal.setEnabled(dlProcessorEnabled);
162                    }
163            }
164    
165            @Override
166            public void restoreStagedModel(
167                            PortletDataContext portletDataContext, FileEntry stagedModel)
168                    throws PortletDataException {
169    
170                    try {
171                            doRestoreStagedModel(portletDataContext, stagedModel);
172                    }
173                    catch (PortletDataException pde) {
174                            throw pde;
175                    }
176                    catch (Exception e) {
177                            throw new PortletDataException(e);
178                    }
179            }
180    
181            @Override
182            protected void doExportStagedModel(
183                            PortletDataContext portletDataContext, FileEntry fileEntry)
184                    throws Exception {
185    
186                    Element fileEntryElement = portletDataContext.getExportDataElement(
187                            fileEntry);
188    
189                    String fileEntryPath = ExportImportPathUtil.getModelPath(fileEntry);
190    
191                    if (!fileEntry.isDefaultRepository()) {
192                            Repository repository = RepositoryLocalServiceUtil.getRepository(
193                                    fileEntry.getRepositoryId());
194    
195                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
196                                    portletDataContext, fileEntry, repository,
197                                    PortletDataContext.REFERENCE_TYPE_STRONG);
198    
199                            portletDataContext.addClassedModel(
200                                    fileEntryElement, fileEntryPath, fileEntry);
201    
202                            long portletRepositoryClassNameId = PortalUtil.getClassNameId(
203                                    PortletRepository.class.getName());
204    
205                            if (repository.getClassNameId() != portletRepositoryClassNameId) {
206                                    return;
207                            }
208                    }
209    
210                    if (fileEntry.getFolderId() !=
211                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
212    
213                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
214                                    portletDataContext, fileEntry, fileEntry.getFolder(),
215                                    PortletDataContext.REFERENCE_TYPE_PARENT);
216                    }
217    
218                    LiferayFileEntry liferayFileEntry = (LiferayFileEntry)fileEntry;
219    
220                    liferayFileEntry.setCachedFileVersion(fileEntry.getFileVersion());
221    
222                    if (!portletDataContext.isPerformDirectBinaryImport()) {
223                            InputStream is = null;
224    
225                            try {
226                                    is = FileEntryUtil.getContentStream(fileEntry);
227                            }
228                            catch (Exception e) {
229                                    if (_log.isWarnEnabled()) {
230                                            _log.warn(
231                                                    "Unable to retrieve content for file entry " +
232                                                            fileEntry.getFileEntryId(),
233                                                    e);
234                                    }
235                            }
236    
237                            if (is == null) {
238                                    fileEntryElement.detach();
239    
240                                    return;
241                            }
242    
243                            try {
244                                    String binPath = ExportImportPathUtil.getModelPath(
245                                            fileEntry, fileEntry.getVersion());
246    
247                                    portletDataContext.addZipEntry(binPath, is);
248    
249                                    fileEntryElement.addAttribute("bin-path", binPath);
250                            }
251                            finally {
252                                    try {
253                                            is.close();
254                                    }
255                                    catch (IOException ioe) {
256                                            _log.error(ioe, ioe);
257                                    }
258                            }
259                    }
260    
261                    if (portletDataContext.getBooleanParameter(
262                                    DLPortletDataHandler.NAMESPACE, "previews-and-thumbnails")) {
263    
264                            DLProcessorRegistryUtil.exportGeneratedFiles(
265                                    portletDataContext, fileEntry, fileEntryElement);
266                    }
267    
268                    exportMetaData(portletDataContext, fileEntryElement, fileEntry);
269    
270                    portletDataContext.addClassedModel(
271                            fileEntryElement, fileEntryPath, liferayFileEntry,
272                            DLFileEntry.class);
273            }
274    
275            @Override
276            protected void doImportMissingReference(
277                            PortletDataContext portletDataContext, String uuid, long groupId,
278                            long fileEntryId)
279                    throws Exception {
280    
281                    FileEntry existingFileEntry = fetchMissingReference(uuid, groupId);
282    
283                    Map<Long, Long> dlFileEntryIds =
284                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
285                                    DLFileEntry.class);
286    
287                    dlFileEntryIds.put(fileEntryId, existingFileEntry.getFileEntryId());
288    
289                    Map<Long, Long> fileEntryIds =
290                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
291                                    FileEntry.class);
292    
293                    fileEntryIds.put(fileEntryId, existingFileEntry.getFileEntryId());
294            }
295    
296            @Override
297            protected void doImportStagedModel(
298                            PortletDataContext portletDataContext, FileEntry fileEntry)
299                    throws Exception {
300    
301                    long userId = portletDataContext.getUserId(fileEntry.getUserUuid());
302    
303                    if (!fileEntry.isDefaultRepository()) {
304    
305                            // References has been automatically imported, nothing to do here
306    
307                            return;
308                    }
309    
310                    Map<Long, Long> folderIds =
311                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
312                                    Folder.class);
313    
314                    long folderId = MapUtil.getLong(
315                            folderIds, fileEntry.getFolderId(), fileEntry.getFolderId());
316    
317                    long[] assetCategoryIds = portletDataContext.getAssetCategoryIds(
318                            DLFileEntry.class, fileEntry.getFileEntryId());
319                    String[] assetTagNames = portletDataContext.getAssetTagNames(
320                            DLFileEntry.class, fileEntry.getFileEntryId());
321    
322                    ServiceContext serviceContext = portletDataContext.createServiceContext(
323                            fileEntry, DLFileEntry.class);
324    
325                    serviceContext.setAttribute(
326                            "sourceFileName", "A." + fileEntry.getExtension());
327                    serviceContext.setUserId(userId);
328    
329                    Element fileEntryElement = portletDataContext.getImportDataElement(
330                            fileEntry);
331    
332                    String binPath = fileEntryElement.attributeValue("bin-path");
333    
334                    InputStream is = null;
335    
336                    if (Validator.isNull(binPath) &&
337                            portletDataContext.isPerformDirectBinaryImport()) {
338    
339                            try {
340                                    is = FileEntryUtil.getContentStream(fileEntry);
341                            }
342                            catch (Exception e) {
343                                    if (_log.isWarnEnabled()) {
344                                            _log.warn(
345                                                    "Unable to retrieve content for file entry " +
346                                                            fileEntry.getFileEntryId(),
347                                                    e);
348                                    }
349    
350                                    return;
351                            }
352                    }
353                    else {
354                            is = portletDataContext.getZipEntryAsInputStream(binPath);
355                    }
356    
357                    if (is == null) {
358                            if (_log.isWarnEnabled()) {
359                                    _log.warn(
360                                            "No file found for file entry " +
361                                                    fileEntry.getFileEntryId());
362                            }
363    
364                            return;
365                    }
366    
367                    importMetaData(
368                            portletDataContext, fileEntryElement, fileEntry, serviceContext);
369    
370                    FileEntry importedFileEntry = null;
371    
372                    String titleWithExtension = DLUtil.getTitleWithExtension(fileEntry);
373                    String extension = fileEntry.getExtension();
374    
375                    String periodAndExtension = StringPool.PERIOD.concat(extension);
376    
377                    if (portletDataContext.isDataStrategyMirror()) {
378                            FileEntry existingFileEntry = fetchStagedModelByUuidAndGroupId(
379                                    fileEntry.getUuid(), portletDataContext.getScopeGroupId());
380    
381                            FileVersion fileVersion = fileEntry.getFileVersion();
382    
383                            if (existingFileEntry == null) {
384                                    String fileEntryTitle = fileEntry.getTitle();
385    
386                                    FileEntry existingTitleFileEntry = FileEntryUtil.fetchByR_F_T(
387                                            portletDataContext.getScopeGroupId(), folderId,
388                                            fileEntryTitle);
389    
390                                    if (existingTitleFileEntry != null) {
391                                            if ((fileEntry.getGroupId() ==
392                                                            portletDataContext.getSourceGroupId()) &&
393                                                    portletDataContext.
394                                                            isDataStrategyMirrorWithOverwriting()) {
395    
396                                                    DLAppLocalServiceUtil.deleteFileEntry(
397                                                            existingTitleFileEntry.getFileEntryId());
398                                            }
399                                            else {
400                                                    boolean titleHasExtension = false;
401    
402                                                    if (fileEntryTitle.endsWith(periodAndExtension)) {
403                                                            fileEntryTitle = FileUtil.stripExtension(
404                                                                    fileEntryTitle);
405    
406                                                            titleHasExtension = true;
407                                                    }
408    
409                                                    for (int i = 1;; i++) {
410                                                            fileEntryTitle += StringPool.SPACE + i;
411    
412                                                            titleWithExtension =
413                                                                    fileEntryTitle + periodAndExtension;
414    
415                                                            existingTitleFileEntry = FileEntryUtil.fetchByR_F_T(
416                                                                    portletDataContext.getScopeGroupId(), folderId,
417                                                                    titleWithExtension);
418    
419                                                            if (existingTitleFileEntry == null) {
420                                                                    if (titleHasExtension) {
421                                                                            fileEntryTitle += periodAndExtension;
422                                                                    }
423    
424                                                                    break;
425                                                            }
426                                                    }
427                                            }
428                                    }
429    
430                                    serviceContext.setAttribute(
431                                            "fileVersionUuid", fileVersion.getUuid());
432                                    serviceContext.setUuid(fileEntry.getUuid());
433    
434                                    importedFileEntry = DLAppLocalServiceUtil.addFileEntry(
435                                            userId, portletDataContext.getScopeGroupId(), folderId,
436                                            titleWithExtension, fileEntry.getMimeType(), fileEntryTitle,
437                                            fileEntry.getDescription(), null, is, fileEntry.getSize(),
438                                            serviceContext);
439    
440                                    if (fileEntry.isInTrash()) {
441                                            importedFileEntry = DLAppServiceUtil.moveFileEntryToTrash(
442                                                    importedFileEntry.getFileEntryId());
443                                    }
444                            }
445                            else {
446                                    FileVersion latestExistingFileVersion =
447                                            existingFileEntry.getLatestFileVersion(true);
448    
449                                    boolean indexEnabled = serviceContext.isIndexingEnabled();
450    
451                                    boolean deleteFileEntry = false;
452                                    boolean updateFileEntry = false;
453    
454                                    if (!Validator.equals(
455                                                    fileVersion.getUuid(),
456                                                    latestExistingFileVersion.getUuid())) {
457    
458                                            deleteFileEntry = true;
459                                            updateFileEntry = true;
460                                    }
461                                    else {
462                                            InputStream existingFileVersionInputStream = null;
463    
464                                            try {
465                                                    existingFileVersionInputStream =
466                                                            latestExistingFileVersion.getContentStream(false);
467                                            }
468                                            catch (Exception e) {
469                                                    if (_log.isDebugEnabled()) {
470                                                            _log.debug(e, e);
471                                                    }
472                                            }
473                                            finally {
474                                                    if (existingFileVersionInputStream != null) {
475                                                            existingFileVersionInputStream.close();
476                                                    }
477                                            }
478    
479                                            if (existingFileVersionInputStream == null) {
480                                                    updateFileEntry = true;
481                                            }
482                                    }
483    
484                                    try {
485                                            serviceContext.setIndexingEnabled(false);
486    
487                                            if (updateFileEntry) {
488                                                    DLFileVersion alreadyExistingFileVersion =
489                                                            DLFileVersionLocalServiceUtil.
490                                                                    getFileVersionByUuidAndGroupId(
491                                                                            fileVersion.getUuid(),
492                                                                            existingFileEntry.getGroupId());
493    
494                                                    if (alreadyExistingFileVersion != null) {
495                                                            serviceContext.setAttribute(
496                                                                    "existingDLFileVersionId",
497                                                                    alreadyExistingFileVersion.getFileVersionId());
498                                                    }
499    
500                                                    serviceContext.setUuid(fileVersion.getUuid());
501    
502                                                    importedFileEntry =
503                                                            DLAppLocalServiceUtil.updateFileEntry(
504                                                                    userId, existingFileEntry.getFileEntryId(),
505                                                                    titleWithExtension, fileEntry.getMimeType(),
506                                                                    fileEntry.getTitle(),
507                                                                    fileEntry.getDescription(), null, false, is,
508                                                                    fileEntry.getSize(), serviceContext);
509                                            }
510                                            else {
511                                                    DLAppLocalServiceUtil.updateAsset(
512                                                            userId, existingFileEntry,
513                                                            latestExistingFileVersion, assetCategoryIds,
514                                                            assetTagNames, null);
515    
516                                                    importedFileEntry = existingFileEntry;
517                                            }
518    
519                                            if (importedFileEntry.getFolderId() != folderId) {
520                                                    importedFileEntry = DLAppLocalServiceUtil.moveFileEntry(
521                                                            userId, importedFileEntry.getFileEntryId(),
522                                                            folderId, serviceContext);
523                                            }
524    
525                                            if (importedFileEntry instanceof LiferayFileEntry) {
526                                                    LiferayFileEntry liferayFileEntry =
527                                                            (LiferayFileEntry)importedFileEntry;
528    
529                                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
530                                                            DLFileEntry.class);
531    
532                                                    indexer.reindex(liferayFileEntry.getModel());
533                                            }
534    
535                                            if (deleteFileEntry &&
536                                                    ExportImportThreadLocal.isStagingInProcess()) {
537    
538                                                    DLAppServiceUtil.deleteFileVersion(
539                                                            latestExistingFileVersion.getFileEntryId(),
540                                                            latestExistingFileVersion.getVersion());
541                                            }
542                                    }
543                                    finally {
544                                            serviceContext.setIndexingEnabled(indexEnabled);
545                                    }
546                            }
547                    }
548                    else {
549                            try {
550                                    importedFileEntry = DLAppLocalServiceUtil.addFileEntry(
551                                            userId, portletDataContext.getScopeGroupId(), folderId,
552                                            titleWithExtension, fileEntry.getMimeType(),
553                                            fileEntry.getTitle(), fileEntry.getDescription(), null, is,
554                                            fileEntry.getSize(), serviceContext);
555                            }
556                            catch (DuplicateFileException dfe) {
557                                    String title = fileEntry.getTitle();
558    
559                                    String[] titleParts = title.split("\\.", 2);
560    
561                                    title = titleParts[0] + StringUtil.randomString();
562    
563                                    if (titleParts.length > 1) {
564                                            title += StringPool.PERIOD + titleParts[1];
565                                    }
566    
567                                    if (!title.endsWith(periodAndExtension)) {
568                                            title += periodAndExtension;
569                                    }
570    
571                                    importedFileEntry = DLAppLocalServiceUtil.addFileEntry(
572                                            userId, portletDataContext.getScopeGroupId(), folderId,
573                                            title, fileEntry.getMimeType(), title,
574                                            fileEntry.getDescription(), null, is, fileEntry.getSize(),
575                                            serviceContext);
576                            }
577                    }
578    
579                    if (portletDataContext.getBooleanParameter(
580                                    DLPortletDataHandler.NAMESPACE, "previews-and-thumbnails")) {
581    
582                            DLProcessorRegistryUtil.importGeneratedFiles(
583                                    portletDataContext, fileEntry, importedFileEntry,
584                                    fileEntryElement);
585                    }
586    
587                    portletDataContext.importClassedModel(
588                            fileEntry, importedFileEntry, DLFileEntry.class);
589    
590                    Map<Long, Long> fileEntryIds =
591                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
592                                    FileEntry.class);
593    
594                    fileEntryIds.put(
595                            fileEntry.getFileEntryId(), importedFileEntry.getFileEntryId());
596            }
597    
598            @Override
599            protected void doRestoreStagedModel(
600                            PortletDataContext portletDataContext, FileEntry fileEntry)
601                    throws Exception {
602    
603                    long userId = portletDataContext.getUserId(fileEntry.getUserUuid());
604    
605                    FileEntry existingFileEntry = fetchStagedModelByUuidAndGroupId(
606                            fileEntry.getUuid(), portletDataContext.getScopeGroupId());
607    
608                    if ((existingFileEntry == null) || !existingFileEntry.isInTrash()) {
609                            return;
610                    }
611    
612                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
613                            DLFileEntry.class.getName());
614    
615                    if (trashHandler.isRestorable(existingFileEntry.getFileEntryId())) {
616                            trashHandler.restoreTrashEntry(
617                                    userId, existingFileEntry.getFileEntryId());
618                    }
619            }
620    
621            protected void exportMetaData(
622                            PortletDataContext portletDataContext, Element fileEntryElement,
623                            FileEntry fileEntry)
624                    throws Exception {
625    
626                    LiferayFileEntry liferayFileEntry = (LiferayFileEntry)fileEntry;
627    
628                    DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();
629    
630                    long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();
631    
632                    DLFileEntryType dlFileEntryType =
633                            DLFileEntryTypeLocalServiceUtil.fetchFileEntryType(fileEntryTypeId);
634    
635                    if ((dlFileEntryType == null) || !dlFileEntryType.isExportable()) {
636                            return;
637                    }
638    
639                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
640                            portletDataContext, fileEntry, dlFileEntryType,
641                            PortletDataContext.REFERENCE_TYPE_STRONG);
642    
643                    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
644    
645                    for (DDMStructure ddmStructure : ddmStructures) {
646                            FileVersion fileVersion = fileEntry.getFileVersion();
647    
648                            DLFileEntryMetadata dlFileEntryMetadata =
649                                    DLFileEntryMetadataLocalServiceUtil.fetchFileEntryMetadata(
650                                            ddmStructure.getStructureId(),
651                                            fileVersion.getFileVersionId());
652    
653                            if (dlFileEntryMetadata == null) {
654                                    continue;
655                            }
656    
657                            Element structureFields = fileEntryElement.addElement(
658                                    "structure-fields");
659    
660                            String path = ExportImportPathUtil.getModelPath(
661                                    ddmStructure,
662                                    String.valueOf(dlFileEntryMetadata.getDDMStorageId()));
663    
664                            structureFields.addAttribute("path", path);
665    
666                            structureFields.addAttribute(
667                                    "structureUuid", ddmStructure.getUuid());
668    
669                            DDMFormValues ddmFormValues = StorageEngineUtil.getDDMFormValues(
670                                    dlFileEntryMetadata.getDDMStorageId());
671    
672                            portletDataContext.addZipEntry(path, ddmFormValues);
673                    }
674            }
675    
676            protected void importMetaData(
677                            PortletDataContext portletDataContext, Element fileEntryElement,
678                            FileEntry fileEntry, ServiceContext serviceContext)
679                    throws Exception {
680    
681                    LiferayFileEntry liferayFileEntry = (LiferayFileEntry)fileEntry;
682    
683                    DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();
684    
685                    Map<Long, Long> dlFileEntryTypeIds =
686                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
687                                    DLFileEntryType.class);
688    
689                    long dlFileEntryTypeId = MapUtil.getLong(
690                            dlFileEntryTypeIds, dlFileEntry.getFileEntryTypeId(),
691                            dlFileEntry.getFileEntryTypeId());
692    
693                    DLFileEntryType existingDLFileEntryType =
694                            DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryType(
695                                    dlFileEntryTypeId);
696    
697                    if (existingDLFileEntryType == null) {
698                            serviceContext.setAttribute("fileEntryTypeId", -1);
699    
700                            return;
701                    }
702    
703                    serviceContext.setAttribute(
704                            "fileEntryTypeId", existingDLFileEntryType.getFileEntryTypeId());
705    
706                    List<DDMStructure> ddmStructures =
707                            existingDLFileEntryType.getDDMStructures();
708    
709                    for (DDMStructure ddmStructure : ddmStructures) {
710                            Element structureFieldsElement =
711                                    (Element)fileEntryElement.selectSingleNode(
712                                            "structure-fields[@structureUuid='".concat(
713                                                    ddmStructure.getUuid()).concat("']"));
714    
715                            if (structureFieldsElement == null) {
716                                    continue;
717                            }
718    
719                            String path = structureFieldsElement.attributeValue("path");
720    
721                            DDMFormValues ddmFormValues =
722                                    (DDMFormValues)portletDataContext.getZipEntryAsObject(path);
723    
724                            serviceContext.setAttribute(
725                                    DDMFormValues.class.getName() + ddmStructure.getStructureId(),
726                                    ddmFormValues);
727                    }
728            }
729    
730            @Override
731            protected void validateExport(
732                            PortletDataContext portletDataContext, FileEntry fileEntry)
733                    throws PortletDataException {
734    
735                    if ((fileEntry.getGroupId() != portletDataContext.getGroupId()) &&
736                            (fileEntry.getGroupId() != portletDataContext.getScopeGroupId())) {
737    
738                            PortletDataException pde = new PortletDataException(
739                                    PortletDataException.INVALID_GROUP);
740    
741                            pde.setStagedModel(fileEntry);
742    
743                            throw pde;
744                    }
745    
746                    try {
747                            FileVersion fileVersion = fileEntry.getFileVersion();
748    
749                            if (!portletDataContext.isInitialPublication() &&
750                                    !ArrayUtil.contains(
751                                            getExportableStatuses(), fileVersion.getStatus())) {
752    
753                                    throw new PortletDataException(
754                                            PortletDataException.STATUS_UNAVAILABLE);
755                            }
756                    }
757                    catch (PortletDataException pde) {
758                            throw pde;
759                    }
760                    catch (Exception e) {
761                            if (_log.isDebugEnabled()) {
762                                    _log.debug(e, e);
763                            }
764                            else if (_log.isWarnEnabled()) {
765                                    _log.warn(
766                                            "Unable to check workflow status for file entry " +
767                                                    fileEntry.getFileEntryId());
768                            }
769                    }
770    
771                    if (fileEntry.isInTrash() || fileEntry.isInTrashContainer()) {
772                            PortletDataException pde = new PortletDataException(
773                                    PortletDataException.STATUS_IN_TRASH);
774    
775                            pde.setStagedModel(fileEntry);
776    
777                            throw pde;
778                    }
779            }
780    
781            private static final Log _log = LogFactoryUtil.getLog(
782                    FileEntryStagedModelDataHandler.class);
783    
784    }