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.BaseTrashHandler;
023 import com.liferay.portal.kernel.trash.TrashActionKeys;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.repository.liferayrepository.LiferayRepository;
026 import com.liferay.portal.security.permission.ActionKeys;
027 import com.liferay.portal.security.permission.PermissionChecker;
028 import com.liferay.portal.service.RepositoryServiceUtil;
029 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
030 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
031 import com.liferay.portlet.documentlibrary.model.DLFolder;
032 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
033 import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
034 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
035 import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
036 import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
037 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
038 import com.liferay.portlet.documentlibrary.util.DLAppHelperThreadLocal;
039 import com.liferay.portlet.documentlibrary.util.DLUtil;
040 import com.liferay.portlet.trash.DuplicateEntryException;
041 import com.liferay.portlet.trash.TrashEntryConstants;
042 import com.liferay.portlet.trash.model.TrashEntry;
043 import com.liferay.portlet.trash.util.TrashUtil;
044
045 import javax.portlet.PortletRequest;
046
047
054 public class DLFileEntryTrashHandler extends BaseTrashHandler {
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 restoredTitle = dlFileEntry.getTitle();
070
071 if (Validator.isNotNull(newName)) {
072 restoredTitle = newName;
073 }
074
075 String originalTitle = TrashUtil.stripTrashNamespace(restoredTitle);
076
077 DLFileEntry duplicateDLFileEntry =
078 DLFileEntryLocalServiceUtil.fetchFileEntry(
079 dlFileEntry.getGroupId(), containerModelId, originalTitle);
080
081 if (duplicateDLFileEntry != null) {
082 DuplicateEntryException dee = new DuplicateEntryException();
083
084 dee.setDuplicateEntryId(duplicateDLFileEntry.getFileEntryId());
085 dee.setOldName(duplicateDLFileEntry.getTitle());
086 dee.setTrashEntryId(trashEntry.getEntryId());
087
088 throw dee;
089 }
090 }
091
092 public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
093 throws PortalException, SystemException {
094
095 for (long classPK : classPKs) {
096 if (checkPermission) {
097 DLAppServiceUtil.deleteFileEntry(classPK);
098 }
099 else {
100 DLAppLocalServiceUtil.deleteFileEntry(classPK);
101 }
102 }
103 }
104
105 public String getClassName() {
106 return CLASS_NAME;
107 }
108
109 @Override
110 public String getRestoreLink(PortletRequest portletRequest, long classPK)
111 throws PortalException, SystemException {
112
113 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
114
115 return DLUtil.getDLControlPanelLink(
116 portletRequest, dlFileEntry.getFolderId());
117 }
118
119 @Override
120 public String getRestoreMessage(PortletRequest portletRequest, long classPK)
121 throws PortalException, SystemException {
122
123 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
124
125 DLFolder dlFolder = dlFileEntry.getFolder();
126
127 return DLUtil.getAbsolutePath(portletRequest, dlFolder.getFolderId());
128 }
129
130 @Override
131 public boolean hasTrashPermission(
132 PermissionChecker permissionChecker, long groupId, long classPK,
133 String trashActionId)
134 throws PortalException, SystemException {
135
136 if (trashActionId.equals(TrashActionKeys.MOVE)) {
137 return DLFolderPermission.contains(
138 permissionChecker, groupId, classPK, ActionKeys.ADD_DOCUMENT);
139 }
140
141 return super.hasTrashPermission(
142 permissionChecker, groupId, classPK, trashActionId);
143 }
144
145 public boolean isInTrash(long classPK)
146 throws PortalException, SystemException {
147
148 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
149
150 DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
151
152 if (dlFileEntry.isInTrashFolder() || dlFileVersion.isInTrash()) {
153 return true;
154 }
155
156 return false;
157 }
158
159 public void restoreTrashEntries(long[] classPKs)
160 throws PortalException, SystemException {
161
162 for (long classPK : classPKs) {
163 boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
164
165 try {
166 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
167
168 if (dlFileEntry.isInHiddenFolder()) {
169 DLAppHelperThreadLocal.setEnabled(false);
170 }
171
172 DLAppServiceUtil.restoreFileEntryFromTrash(classPK);
173 }
174 finally {
175 DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
176 }
177 }
178 }
179
180 @Override
181 public void updateTitle(long classPK, String name)
182 throws PortalException, SystemException {
183
184 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
185
186 dlFileEntry.setTitle(name);
187
188 DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);
189
190 DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
191
192 dlFileVersion.setTitle(name);
193
194 DLFileVersionLocalServiceUtil.updateDLFileVersion(dlFileVersion);
195 }
196
197 protected DLFileEntry getDLFileEntry(long classPK)
198 throws PortalException, SystemException {
199
200 Repository repository = RepositoryServiceUtil.getRepositoryImpl(
201 0, classPK, 0);
202
203 if (!(repository instanceof LiferayRepository)) {
204 throw new InvalidRepositoryException(
205 "Repository " + repository.getRepositoryId() +
206 " does not support trash operations");
207 }
208
209 FileEntry fileEntry = repository.getFileEntry(classPK);
210
211 return (DLFileEntry)fileEntry.getModel();
212 }
213
214 @Override
215 protected boolean hasPermission(
216 PermissionChecker permissionChecker, long classPK, String actionId)
217 throws PortalException, SystemException {
218
219 DLFileEntry dlFileEntry = getDLFileEntry(classPK);
220
221 if (dlFileEntry.isInHiddenFolder()) {
222 return false;
223 }
224
225 return DLFileEntryPermission.contains(
226 permissionChecker, classPK, actionId);
227 }
228
229 }