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