001
014
015 package com.liferay.portlet.documentlibrary.action;
016
017 import com.liferay.portal.kernel.lock.DuplicateLockException;
018 import com.liferay.portal.kernel.portlet.LiferayWindowState;
019 import com.liferay.portal.kernel.repository.model.FileEntry;
020 import com.liferay.portal.kernel.repository.model.FileShortcut;
021 import com.liferay.portal.kernel.repository.model.Folder;
022 import com.liferay.portal.kernel.servlet.ServletResponseConstants;
023 import com.liferay.portal.kernel.servlet.SessionErrors;
024 import com.liferay.portal.kernel.util.Constants;
025 import com.liferay.portal.kernel.util.ParamUtil;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.model.TrashedModel;
030 import com.liferay.portal.security.auth.PrincipalException;
031 import com.liferay.portal.service.ServiceContext;
032 import com.liferay.portal.service.ServiceContextFactory;
033 import com.liferay.portal.struts.PortletAction;
034 import com.liferay.portal.util.PortalUtil;
035 import com.liferay.portlet.asset.AssetCategoryException;
036 import com.liferay.portlet.asset.AssetTagException;
037 import com.liferay.portlet.documentlibrary.DuplicateFileException;
038 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
039 import com.liferay.portlet.documentlibrary.InvalidFolderException;
040 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
041 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
042 import com.liferay.portlet.documentlibrary.SourceFileNameException;
043 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
044 import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
045 import com.liferay.portlet.trash.service.TrashEntryServiceUtil;
046 import com.liferay.portlet.trash.util.TrashUtil;
047
048 import java.util.ArrayList;
049 import java.util.List;
050
051 import javax.portlet.ActionRequest;
052 import javax.portlet.ActionResponse;
053 import javax.portlet.PortletConfig;
054 import javax.portlet.RenderRequest;
055 import javax.portlet.RenderResponse;
056 import javax.portlet.WindowState;
057
058 import javax.servlet.http.HttpServletResponse;
059
060 import org.apache.struts.action.ActionForm;
061 import org.apache.struts.action.ActionForward;
062 import org.apache.struts.action.ActionMapping;
063
064
070 public class EditEntryAction extends PortletAction {
071
072 @Override
073 public void processAction(
074 ActionMapping actionMapping, ActionForm actionForm,
075 PortletConfig portletConfig, ActionRequest actionRequest,
076 ActionResponse actionResponse)
077 throws Exception {
078
079 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
080
081 try {
082 if (cmd.equals(Constants.CANCEL_CHECKOUT)) {
083 cancelCheckedOutEntries(actionRequest);
084 }
085 else if (cmd.equals(Constants.CHECKIN)) {
086 checkInEntries(actionRequest);
087 }
088 else if (cmd.equals(Constants.CHECKOUT)) {
089 checkOutEntries(actionRequest);
090 }
091 else if (cmd.equals(Constants.DELETE)) {
092 deleteEntries(actionRequest, false);
093 }
094 else if (cmd.equals(Constants.MOVE)) {
095 moveEntries(actionRequest);
096 }
097 else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
098 deleteEntries(actionRequest, true);
099 }
100 else if (cmd.equals(Constants.RESTORE)) {
101 restoreTrashEntries(actionRequest);
102 }
103
104 WindowState windowState = actionRequest.getWindowState();
105
106 if (!windowState.equals(LiferayWindowState.POP_UP)) {
107 sendRedirect(actionRequest, actionResponse);
108 }
109 else {
110 String redirect = PortalUtil.escapeRedirect(
111 ParamUtil.getString(actionRequest, "redirect"));
112
113 if (Validator.isNotNull(redirect)) {
114 actionResponse.sendRedirect(redirect);
115 }
116 }
117 }
118 catch (Exception e) {
119 if (e instanceof DuplicateLockException ||
120 e instanceof NoSuchFileEntryException ||
121 e instanceof NoSuchFolderException ||
122 e instanceof PrincipalException) {
123
124 if (e instanceof DuplicateLockException) {
125 DuplicateLockException dle = (DuplicateLockException)e;
126
127 SessionErrors.add(
128 actionRequest, dle.getClass(), dle.getLock());
129 }
130 else {
131 SessionErrors.add(actionRequest, e.getClass());
132 }
133
134 setForward(actionRequest, "portlet.document_library.error");
135 }
136 else if (e instanceof DuplicateFileException ||
137 e instanceof DuplicateFolderNameException ||
138 e instanceof SourceFileNameException) {
139
140 if (e instanceof DuplicateFileException) {
141 HttpServletResponse response =
142 PortalUtil.getHttpServletResponse(actionResponse);
143
144 response.setStatus(
145 ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
146 }
147
148 SessionErrors.add(actionRequest, e.getClass());
149 }
150 else if (e instanceof AssetCategoryException ||
151 e instanceof AssetTagException ||
152 e instanceof InvalidFolderException) {
153
154 SessionErrors.add(actionRequest, e.getClass(), e);
155 }
156 else {
157 throw e;
158 }
159 }
160 }
161
162 @Override
163 public ActionForward render(
164 ActionMapping actionMapping, ActionForm actionForm,
165 PortletConfig portletConfig, RenderRequest renderRequest,
166 RenderResponse renderResponse)
167 throws Exception {
168
169 try {
170 ActionUtil.getFileEntries(renderRequest);
171 ActionUtil.getFileShortcuts(renderRequest);
172 ActionUtil.getFolders(renderRequest);
173 }
174 catch (Exception e) {
175 if (e instanceof NoSuchFileEntryException ||
176 e instanceof PrincipalException) {
177
178 SessionErrors.add(renderRequest, e.getClass());
179
180 return actionMapping.findForward(
181 "portlet.document_library.error");
182 }
183 else {
184 throw e;
185 }
186 }
187
188 String forward = "portlet.document_library.edit_entry";
189
190 return actionMapping.findForward(getForward(renderRequest, forward));
191 }
192
193 protected void cancelCheckedOutEntries(ActionRequest actionRequest)
194 throws Exception {
195
196 long[] fileEntryIds = StringUtil.split(
197 ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
198
199 for (long fileEntryId : fileEntryIds) {
200 DLAppServiceUtil.cancelCheckOut(fileEntryId);
201 }
202 }
203
204 protected void checkInEntries(ActionRequest actionRequest)
205 throws Exception {
206
207 long[] fileEntryIds = StringUtil.split(
208 ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
209
210 ServiceContext serviceContext = ServiceContextFactory.getInstance(
211 actionRequest);
212
213 for (long fileEntryId : fileEntryIds) {
214 DLAppServiceUtil.checkInFileEntry(
215 fileEntryId, false, StringPool.BLANK, serviceContext);
216 }
217 }
218
219 protected void checkOutEntries(ActionRequest actionRequest)
220 throws Exception {
221
222 long[] fileEntryIds = StringUtil.split(
223 ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
224
225 ServiceContext serviceContext = ServiceContextFactory.getInstance(
226 actionRequest);
227
228 for (long fileEntryId : fileEntryIds) {
229 DLAppServiceUtil.checkOutFileEntry(fileEntryId, serviceContext);
230 }
231 }
232
233 protected void deleteEntries(
234 ActionRequest actionRequest, boolean moveToTrash)
235 throws Exception {
236
237 long[] deleteFolderIds = StringUtil.split(
238 ParamUtil.getString(actionRequest, "folderIds"), 0L);
239
240 List<TrashedModel> trashedModels = new ArrayList<>();
241
242 for (int i = 0; i < deleteFolderIds.length; i++) {
243 long deleteFolderId = deleteFolderIds[i];
244
245 if (moveToTrash) {
246 Folder folder = DLAppServiceUtil.moveFolderToTrash(
247 deleteFolderId);
248
249 if (folder.getModel() instanceof TrashedModel) {
250 trashedModels.add((TrashedModel)folder.getModel());
251 }
252 }
253 else {
254 DLAppServiceUtil.deleteFolder(deleteFolderId);
255 }
256 }
257
258
259
260 long[] deleteFileShortcutIds = StringUtil.split(
261 ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
262
263 for (int i = 0; i < deleteFileShortcutIds.length; i++) {
264 long deleteFileShortcutId = deleteFileShortcutIds[i];
265
266 if (moveToTrash) {
267 FileShortcut fileShortcut =
268 DLAppServiceUtil.moveFileShortcutToTrash(
269 deleteFileShortcutId);
270
271 if (fileShortcut.getModel() instanceof TrashedModel) {
272 trashedModels.add((TrashedModel)fileShortcut.getModel());
273 }
274 }
275 else {
276 DLAppServiceUtil.deleteFileShortcut(deleteFileShortcutId);
277 }
278 }
279
280 long[] deleteFileEntryIds = StringUtil.split(
281 ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
282
283 for (long deleteFileEntryId : deleteFileEntryIds) {
284 if (moveToTrash) {
285 FileEntry fileEntry = DLAppServiceUtil.moveFileEntryToTrash(
286 deleteFileEntryId);
287
288 if (fileEntry.getModel() instanceof TrashedModel) {
289 trashedModels.add((TrashedModel)fileEntry.getModel());
290 }
291 }
292 else {
293 DLAppServiceUtil.deleteFileEntry(deleteFileEntryId);
294 }
295 }
296
297 if (moveToTrash && !trashedModels.isEmpty()) {
298 TrashUtil.addTrashSessionMessages(actionRequest, trashedModels);
299
300 hideDefaultSuccessMessage(actionRequest);
301 }
302 }
303
304 protected void moveEntries(ActionRequest actionRequest) throws Exception {
305 long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
306
307 ServiceContext serviceContext = ServiceContextFactory.getInstance(
308 DLFileEntry.class.getName(), actionRequest);
309
310 long[] folderIds = StringUtil.split(
311 ParamUtil.getString(actionRequest, "folderIds"), 0L);
312
313 for (long folderId : folderIds) {
314 DLAppServiceUtil.moveFolder(folderId, newFolderId, serviceContext);
315 }
316
317 long[] fileEntryIds = StringUtil.split(
318 ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
319
320 for (long fileEntryId : fileEntryIds) {
321 DLAppServiceUtil.moveFileEntry(
322 fileEntryId, newFolderId, serviceContext);
323 }
324
325 long[] fileShortcutIds = StringUtil.split(
326 ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
327
328 for (long fileShortcutId : fileShortcutIds) {
329 if (fileShortcutId == 0) {
330 continue;
331 }
332
333 FileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(
334 fileShortcutId);
335
336 DLAppServiceUtil.updateFileShortcut(
337 fileShortcutId, newFolderId, fileShortcut.getToFileEntryId(),
338 serviceContext);
339 }
340 }
341
342 protected void restoreTrashEntries(ActionRequest actionRequest)
343 throws Exception {
344
345 long[] restoreTrashEntryIds = StringUtil.split(
346 ParamUtil.getString(actionRequest, "restoreTrashEntryIds"), 0L);
347
348 for (long restoreTrashEntryId : restoreTrashEntryIds) {
349 TrashEntryServiceUtil.restoreEntry(restoreTrashEntryId);
350 }
351 }
352
353 }