001    /**
002     * Copyright (c) 2000-2013 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.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.PortletDataContext;
022    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.repository.model.FileEntry;
026    import com.liferay.portal.kernel.repository.model.Folder;
027    import com.liferay.portal.kernel.util.MapUtil;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
031    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
032    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
033    import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
034    
035    import java.util.Map;
036    
037    /**
038     * @author Mate Thurzo
039     */
040    public class DLFileShortcutStagedModelDataHandler
041            extends BaseStagedModelDataHandler<DLFileShortcut> {
042    
043            public static final String[] CLASS_NAMES = {DLFileShortcut.class.getName()};
044    
045            @Override
046            public void deleteStagedModel(
047                            String uuid, long groupId, String className, String extraData)
048                    throws PortalException, SystemException {
049    
050                    DLFileShortcut dlFileShortcut =
051                            DLFileShortcutLocalServiceUtil.fetchDLFileShortcutByUuidAndGroupId(
052                                    uuid, groupId);
053    
054                    if (dlFileShortcut != null) {
055                            DLFileShortcutLocalServiceUtil.deleteFileShortcut(dlFileShortcut);
056                    }
057            }
058    
059            @Override
060            public String[] getClassNames() {
061                    return CLASS_NAMES;
062            }
063    
064            @Override
065            public String getDisplayName(DLFileShortcut shortcut) {
066                    return shortcut.getToTitle();
067            }
068    
069            @Override
070            protected void doExportStagedModel(
071                            PortletDataContext portletDataContext, DLFileShortcut fileShortcut)
072                    throws Exception {
073    
074                    if (fileShortcut.getFolderId() !=
075                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
076    
077                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
078                                    portletDataContext, fileShortcut, fileShortcut.getFolder(),
079                                    PortletDataContext.REFERENCE_TYPE_PARENT);
080                    }
081    
082                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
083                            fileShortcut.getToFileEntryId());
084    
085                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
086                            portletDataContext, fileShortcut, DLFileShortcut.class, fileEntry,
087                            FileEntry.class, PortletDataContext.REFERENCE_TYPE_STRONG);
088    
089                    Element fileShortcutElement = portletDataContext.getExportDataElement(
090                            fileShortcut);
091    
092                    fileShortcutElement.addAttribute(
093                            "file-entry-uuid", fileEntry.getUuid());
094    
095                    portletDataContext.addClassedModel(
096                            fileShortcutElement,
097                            ExportImportPathUtil.getModelPath(fileShortcut), fileShortcut,
098                            DLPortletDataHandler.NAMESPACE);
099            }
100    
101            @Override
102            protected void doImportStagedModel(
103                            PortletDataContext portletDataContext, DLFileShortcut fileShortcut)
104                    throws Exception {
105    
106                    long userId = portletDataContext.getUserId(fileShortcut.getUserUuid());
107    
108                    if (fileShortcut.getFolderId() !=
109                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
110    
111                            String folderPath = ExportImportPathUtil.getModelPath(
112                                    portletDataContext, Folder.class.getName(),
113                                    fileShortcut.getFolderId());
114    
115                            Folder folder = (Folder)portletDataContext.getZipEntryAsObject(
116                                    folderPath);
117    
118                            StagedModelDataHandlerUtil.importStagedModel(
119                                    portletDataContext, folder);
120                    }
121    
122                    Map<Long, Long> folderIds =
123                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
124                                    Folder.class);
125    
126                    long folderId = MapUtil.getLong(
127                            folderIds, fileShortcut.getFolderId(), fileShortcut.getFolderId());
128    
129                    long groupId = portletDataContext.getScopeGroupId();
130    
131                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
132                            Folder folder = FolderUtil.findByPrimaryKey(folderId);
133    
134                            groupId = folder.getRepositoryId();
135                    }
136    
137                    String fileEntryPath = ExportImportPathUtil.getModelPath(
138                            portletDataContext, FileEntry.class.getName(),
139                            fileShortcut.getToFileEntryId());
140    
141                    FileEntry fileEntry = (FileEntry)portletDataContext.getZipEntryAsObject(
142                            fileEntryPath);
143    
144                    StagedModelDataHandlerUtil.importStagedModel(
145                            portletDataContext, fileEntry);
146    
147                    Element fileShortcutElement =
148                            portletDataContext.getImportDataStagedModelElement(fileShortcut);
149    
150                    String fileEntryUuid = fileShortcutElement.attributeValue(
151                            "file-entry-uuid");
152    
153                    FileEntry importedFileEntry = FileEntryUtil.fetchByUUID_R(
154                            fileEntryUuid, groupId);
155    
156                    if (importedFileEntry == null) {
157                            if (_log.isWarnEnabled()) {
158                                    _log.warn(
159                                            "Unable to fetch file entry {uuid=" + fileEntryUuid +
160                                                    ", groupId=" + groupId + "}");
161                            }
162    
163                            return;
164                    }
165    
166                    ServiceContext serviceContext = portletDataContext.createServiceContext(
167                            fileShortcut, DLPortletDataHandler.NAMESPACE);
168    
169                    DLFileShortcut importedFileShortcut = null;
170    
171                    if (portletDataContext.isDataStrategyMirror()) {
172                            DLFileShortcut existingFileShortcut =
173                                    DLFileShortcutLocalServiceUtil.
174                                            fetchDLFileShortcutByUuidAndGroupId(
175                                                    fileShortcut.getUuid(),
176                                                    portletDataContext.getScopeGroupId());
177    
178                            if (existingFileShortcut == null) {
179                                    serviceContext.setUuid(fileShortcut.getUuid());
180    
181                                    importedFileShortcut = DLAppLocalServiceUtil.addFileShortcut(
182                                            userId, groupId, folderId,
183                                            importedFileEntry.getFileEntryId(), serviceContext);
184                            }
185                            else {
186                                    importedFileShortcut = DLAppLocalServiceUtil.updateFileShortcut(
187                                            userId, existingFileShortcut.getFileShortcutId(), folderId,
188                                            importedFileEntry.getFileEntryId(), serviceContext);
189                            }
190                    }
191                    else {
192                            importedFileShortcut = DLAppLocalServiceUtil.addFileShortcut(
193                                    userId, groupId, folderId, importedFileEntry.getFileEntryId(),
194                                    serviceContext);
195                    }
196    
197                    portletDataContext.importClassedModel(
198                            fileShortcut, importedFileShortcut, DLPortletDataHandler.NAMESPACE);
199            }
200    
201            private static Log _log = LogFactoryUtil.getLog(
202                    DLFileShortcutStagedModelDataHandler.class);
203    
204    }