001
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
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, fileShortcut.getFolder(),
080 PortletDataContext.REFERENCE_TYPE_PARENT);
081 }
082
083 FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
084 fileShortcut.getToFileEntryId());
085
086 StagedModelDataHandlerUtil.exportReferenceStagedModel(
087 portletDataContext, fileShortcut, DLFileShortcut.class, fileEntry,
088 FileEntry.class, PortletDataContext.REFERENCE_TYPE_STRONG);
089
090 Element fileShortcutElement = portletDataContext.getExportDataElement(
091 fileShortcut);
092
093 fileShortcutElement.addAttribute(
094 "file-entry-uuid", fileEntry.getUuid());
095
096 portletDataContext.addClassedModel(
097 fileShortcutElement,
098 ExportImportPathUtil.getModelPath(fileShortcut), fileShortcut);
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.importReferenceStagedModel(
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.importReferenceStagedModel(
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);
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);
199 }
200
201 @Override
202 protected void doRestoreStagedModel(
203 PortletDataContext portletDataContext, DLFileShortcut fileShortcut)
204 throws Exception {
205
206 long userId = portletDataContext.getUserId(fileShortcut.getUserUuid());
207
208 DLFileShortcut existingFileShortcut =
209 DLFileShortcutLocalServiceUtil.fetchDLFileShortcutByUuidAndGroupId(
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 Log _log = LogFactoryUtil.getLog(
229 DLFileShortcutStagedModelDataHandler.class);
230
231 }