001
014
015 package com.liferay.portlet.documentlibrary.trash;
016
017 import com.liferay.portal.InvalidRepositoryException;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.repository.Repository;
021 import com.liferay.portal.kernel.repository.model.FileEntry;
022 import com.liferay.portal.kernel.trash.TrashActionKeys;
023 import com.liferay.portal.kernel.trash.TrashHandler;
024 import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.model.ContainerModel;
027 import com.liferay.portal.repository.liferayrepository.LiferayRepository;
028 import com.liferay.portal.security.permission.ActionKeys;
029 import com.liferay.portal.security.permission.PermissionChecker;
030 import com.liferay.portal.service.RepositoryServiceUtil;
031 import com.liferay.portal.service.ServiceContext;
032 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
033 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
034 import com.liferay.portlet.documentlibrary.model.DLFolder;
035 import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil;
036 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
037 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
038 import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
039 import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
040 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
041 import com.liferay.portlet.documentlibrary.util.DLUtil;
042 import com.liferay.portlet.trash.DuplicateEntryException;
043 import com.liferay.portlet.trash.TrashEntryConstants;
044 import com.liferay.portlet.trash.model.TrashEntry;
045
046 import javax.portlet.PortletRequest;
047
048
055 public class DLFileEntryTrashHandler extends DLBaseTrashHandler {
056
057 @Override
058 public void checkDuplicateTrashEntry(
059 TrashEntry trashEntry, long containerModelId, String newName)
060 throws PortalException, SystemException {
061
062 DLFileEntry dlFileEntry = getDLFileEntry(trashEntry.getClassPK());
063
064 if (containerModelId == TrashEntryConstants.DEFAULT_CONTAINER_ID) {
065 containerModelId = dlFileEntry.getFolderId();
066 }
067
068 String originalTitle = trashEntry.getTypeSettingsProperty("title");
069
070 if (Validator.isNotNull(newName)) {
071 originalTitle = newName;
072 }
073
074 DLFileEntry duplicateDLFileEntry =
075 DLFileEntryLocalServiceUtil.fetchFileEntry(
076 dlFileEntry.getGroupId(), containerModelId, originalTitle);
077
078 if (duplicateDLFileEntry != null) {
079 DuplicateEntryException dee = new DuplicateEntryException();
080
081 dee.setDuplicateEntryId(duplicateDLFileEntry.getFileEntryId());
082 dee.setOldName(duplicateDLFileEntry.getTitle());
083 dee.setTrashEntryId(trashEntry.getEntryId());
084
085 throw dee;
086 }
087 }
088
089 public void deleteTrashEntry(long classPK)
090 throws PortalException, SystemException {
091
092 DLAppLocalServiceUtil.deleteFileEntry(classPK);
093 }
094
095 public String getClassName() {
096 return DLFileEntry.class.getName();
097 }
098
099 @Override
100 public ContainerModel getParentContainerModel(long classPK)
101 throws PortalException, SystemException {
102
103 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
104
105 long parentFolderId = dlFileEntry.getFolderId();
106
107 if (parentFolderId <= 0) {
108 return null;
109 }
110
111 return getContainerModel(parentFolderId);
112 }
113
114 @Override
115 public String getRestoreLink(PortletRequest portletRequest, long classPK)
116 throws PortalException, SystemException {
117
118 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
119
120 return DLUtil.getDLControlPanelLink(
121 portletRequest, dlFileEntry.getFolderId());
122 }
123
124 @Override
125 public String getRestoreMessage(PortletRequest portletRequest, long classPK)
126 throws PortalException, SystemException {
127
128 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
129
130 DLFolder dlFolder = dlFileEntry.getFolder();
131
132 return DLUtil.getAbsolutePath(portletRequest, dlFolder.getFolderId());
133 }
134
135 @Override
136 public ContainerModel getTrashContainer(long classPK)
137 throws PortalException, SystemException {
138
139 try {
140 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
141
142 return dlFileEntry.getTrashContainer();
143 }
144 catch (InvalidRepositoryException ire) {
145 return null;
146 }
147 }
148
149 @Override
150 public boolean hasTrashPermission(
151 PermissionChecker permissionChecker, long groupId, long classPK,
152 String trashActionId)
153 throws PortalException, SystemException {
154
155 if (trashActionId.equals(TrashActionKeys.MOVE)) {
156 return DLFolderPermission.contains(
157 permissionChecker, groupId, classPK, ActionKeys.ADD_DOCUMENT);
158 }
159
160 return super.hasTrashPermission(
161 permissionChecker, groupId, classPK, trashActionId);
162 }
163
164 public boolean isInTrash(long classPK)
165 throws PortalException, SystemException {
166
167 try {
168 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
169
170 DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
171
172 return dlFileVersion.isInTrash();
173 }
174 catch (InvalidRepositoryException ire) {
175 return false;
176 }
177 }
178
179 @Override
180 public boolean isInTrashContainer(long classPK)
181 throws PortalException, SystemException {
182
183 try {
184 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
185
186 return dlFileEntry.isInTrashContainer();
187 }
188 catch (InvalidRepositoryException ire) {
189 return false;
190 }
191 }
192
193 @Override
194 public boolean isRestorable(long classPK)
195 throws PortalException, SystemException {
196
197 try {
198 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
199
200 return !dlFileEntry.isInTrashContainer();
201 }
202 catch (InvalidRepositoryException ire) {
203 return false;
204 }
205 }
206
207 @Override
208 public void moveEntry(
209 long userId, long classPK, long containerModelId,
210 ServiceContext serviceContext)
211 throws PortalException, SystemException {
212
213 DLAppLocalServiceUtil.moveFileEntry(
214 userId, classPK, containerModelId, serviceContext);
215 }
216
217 @Override
218 public void moveTrashEntry(
219 long userId, long classPK, long containerModelId,
220 ServiceContext serviceContext)
221 throws PortalException, SystemException {
222
223 Repository repository = getRepository(classPK);
224
225 DLAppHelperLocalServiceUtil.moveFileEntryFromTrash(
226 userId, repository.getFileEntry(classPK), containerModelId,
227 serviceContext);
228 }
229
230 public void restoreTrashEntry(long userId, long classPK)
231 throws PortalException, SystemException {
232
233 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
234
235 if ((dlFileEntry.getClassNameId() > 0) &&
236 (dlFileEntry.getClassPK() > 0)) {
237
238 TrashHandler trashHandler =
239 TrashHandlerRegistryUtil.getTrashHandler(
240 dlFileEntry.getClassName());
241
242 trashHandler.restoreRelatedTrashEntry(getClassName(), classPK);
243
244 return;
245 }
246
247 DLAppLocalServiceUtil.restoreFileEntryFromTrash(userId, classPK);
248 }
249
250 @Override
251 public void updateTitle(long classPK, String name)
252 throws PortalException, SystemException {
253
254 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
255
256 dlFileEntry.setTitle(name);
257
258 DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);
259
260 DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
261
262 dlFileVersion.setTitle(name);
263
264 DLFileVersionLocalServiceUtil.updateDLFileVersion(dlFileVersion);
265 }
266
267 protected DLFileEntry getDLFileEntry(long classPK)
268 throws PortalException, SystemException {
269
270 Repository repository = RepositoryServiceUtil.getRepositoryImpl(
271 0, classPK, 0);
272
273 if (!(repository instanceof LiferayRepository)) {
274 throw new InvalidRepositoryException(
275 "Repository " + repository.getRepositoryId() +
276 " does not support trash operations");
277 }
278
279 FileEntry fileEntry = repository.getFileEntry(classPK);
280
281 return (DLFileEntry)fileEntry.getModel();
282 }
283
284 @Override
285 protected Repository getRepository(long classPK)
286 throws PortalException, SystemException {
287
288 Repository repository = RepositoryServiceUtil.getRepositoryImpl(
289 0, classPK, 0);
290
291 if (!(repository instanceof LiferayRepository)) {
292 throw new InvalidRepositoryException(
293 "Repository " + repository.getRepositoryId() +
294 " does not support trash operations");
295 }
296
297 return repository;
298 }
299
300 @Override
301 protected boolean hasPermission(
302 PermissionChecker permissionChecker, long classPK, String actionId)
303 throws PortalException, SystemException {
304
305 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
306
307 if (dlFileEntry.isInHiddenFolder() &&
308 actionId.equals(ActionKeys.VIEW)) {
309
310 return false;
311 }
312
313 return DLFileEntryPermission.contains(
314 permissionChecker, classPK, actionId);
315 }
316
317 }