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