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