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