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