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