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