001
014
015 package com.liferay.portlet.documentlibrary.action;
016
017 import com.liferay.portal.DuplicateLockException;
018 import com.liferay.portal.NoSuchRepositoryEntryException;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.json.JSONArray;
021 import com.liferay.portal.kernel.json.JSONFactoryUtil;
022 import com.liferay.portal.kernel.json.JSONObject;
023 import com.liferay.portal.kernel.language.LanguageUtil;
024 import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
025 import com.liferay.portal.kernel.portlet.LiferayWindowState;
026 import com.liferay.portal.kernel.repository.model.FileEntry;
027 import com.liferay.portal.kernel.repository.model.Folder;
028 import com.liferay.portal.kernel.servlet.ServletResponseConstants;
029 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
030 import com.liferay.portal.kernel.servlet.SessionErrors;
031 import com.liferay.portal.kernel.servlet.SessionMessages;
032 import com.liferay.portal.kernel.upload.UploadException;
033 import com.liferay.portal.kernel.upload.UploadPortletRequest;
034 import com.liferay.portal.kernel.util.Constants;
035 import com.liferay.portal.kernel.util.ContentTypes;
036 import com.liferay.portal.kernel.util.KeyValuePair;
037 import com.liferay.portal.kernel.util.MimeTypesUtil;
038 import com.liferay.portal.kernel.util.ParamUtil;
039 import com.liferay.portal.kernel.util.PropsKeys;
040 import com.liferay.portal.kernel.util.StreamUtil;
041 import com.liferay.portal.kernel.util.StringPool;
042 import com.liferay.portal.kernel.util.StringUtil;
043 import com.liferay.portal.kernel.util.TempFileUtil;
044 import com.liferay.portal.kernel.util.Validator;
045 import com.liferay.portal.kernel.workflow.WorkflowConstants;
046 import com.liferay.portal.security.auth.PrincipalException;
047 import com.liferay.portal.service.ServiceContext;
048 import com.liferay.portal.service.ServiceContextFactory;
049 import com.liferay.portal.struts.ActionConstants;
050 import com.liferay.portal.struts.PortletAction;
051 import com.liferay.portal.theme.ThemeDisplay;
052 import com.liferay.portal.util.PortalUtil;
053 import com.liferay.portal.util.PortletKeys;
054 import com.liferay.portal.util.PrefsPropsUtil;
055 import com.liferay.portal.util.WebKeys;
056 import com.liferay.portlet.PortletPreferencesFactoryUtil;
057 import com.liferay.portlet.PortletURLImpl;
058 import com.liferay.portlet.asset.AssetCategoryException;
059 import com.liferay.portlet.asset.AssetTagException;
060 import com.liferay.portlet.asset.model.AssetVocabulary;
061 import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
062 import com.liferay.portlet.documentlibrary.DuplicateFileException;
063 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
064 import com.liferay.portlet.documentlibrary.FileExtensionException;
065 import com.liferay.portlet.documentlibrary.FileMimeTypeException;
066 import com.liferay.portlet.documentlibrary.FileNameException;
067 import com.liferay.portlet.documentlibrary.FileSizeException;
068 import com.liferay.portlet.documentlibrary.InvalidFileEntryTypeException;
069 import com.liferay.portlet.documentlibrary.InvalidFileVersionException;
070 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
071 import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
072 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
073 import com.liferay.portlet.documentlibrary.SourceFileNameException;
074 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
075 import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
076 import com.liferay.portlet.documentlibrary.util.DLUtil;
077 import com.liferay.portlet.dynamicdatamapping.StorageFieldRequiredException;
078
079 import java.io.InputStream;
080
081 import java.util.ArrayList;
082 import java.util.Arrays;
083 import java.util.HashMap;
084 import java.util.List;
085 import java.util.Map;
086
087 import javax.portlet.ActionRequest;
088 import javax.portlet.ActionResponse;
089 import javax.portlet.PortletConfig;
090 import javax.portlet.PortletContext;
091 import javax.portlet.PortletPreferences;
092 import javax.portlet.PortletRequest;
093 import javax.portlet.PortletRequestDispatcher;
094 import javax.portlet.RenderRequest;
095 import javax.portlet.RenderResponse;
096 import javax.portlet.ResourceRequest;
097 import javax.portlet.ResourceResponse;
098 import javax.portlet.WindowState;
099
100 import javax.servlet.http.HttpServletResponse;
101
102 import org.apache.commons.fileupload.FileUploadBase.IOFileUploadException;
103 import org.apache.struts.action.ActionForm;
104 import org.apache.struts.action.ActionForward;
105 import org.apache.struts.action.ActionMapping;
106
107
115 public class EditFileEntryAction extends PortletAction {
116
117 @Override
118 public void processAction(
119 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
120 ActionRequest actionRequest, ActionResponse actionResponse)
121 throws Exception {
122
123 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
124
125 FileEntry fileEntry = null;
126
127 try {
128 if (Validator.isNull(cmd)) {
129 UploadException uploadException =
130 (UploadException)actionRequest.getAttribute(
131 WebKeys.UPLOAD_EXCEPTION);
132
133 if (uploadException != null) {
134 if (uploadException.isExceededSizeLimit()) {
135 throw new FileSizeException(uploadException.getCause());
136 }
137
138 throw new PortalException(uploadException.getCause());
139 }
140 }
141 else if (cmd.equals(Constants.ADD) ||
142 cmd.equals(Constants.ADD_DYNAMIC) ||
143 cmd.equals(Constants.UPDATE) ||
144 cmd.equals(Constants.UPDATE_AND_CHECKIN)) {
145
146 fileEntry = updateFileEntry(
147 portletConfig, actionRequest, actionResponse);
148 }
149 else if (cmd.equals(Constants.ADD_MULTIPLE)) {
150 addMultipleFileEntries(actionRequest, actionResponse);
151 }
152 else if (cmd.equals(Constants.ADD_TEMP)) {
153 addTempFileEntry(actionRequest);
154 }
155 else if (cmd.equals(Constants.DELETE)) {
156 deleteFileEntry(
157 (LiferayPortletConfig)portletConfig, actionRequest, false);
158 }
159 else if (cmd.equals(Constants.DELETE_TEMP)) {
160 deleteTempFileEntry(actionRequest, actionResponse);
161 }
162 else if (cmd.equals(Constants.CANCEL_CHECKOUT)) {
163 cancelFileEntriesCheckOut(actionRequest);
164 }
165 else if (cmd.equals(Constants.CHECKIN)) {
166 checkInFileEntries(actionRequest);
167 }
168 else if (cmd.equals(Constants.CHECKOUT)) {
169 checkOutFileEntries(actionRequest);
170 }
171 else if (cmd.equals(Constants.MOVE)) {
172 moveFileEntries(actionRequest, false);
173 }
174 else if (cmd.equals(Constants.MOVE_FROM_TRASH)) {
175 moveFileEntries(actionRequest, true);
176 }
177 else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
178 deleteFileEntry(
179 (LiferayPortletConfig)portletConfig, actionRequest, true);
180 }
181 else if (cmd.equals(Constants.REVERT)) {
182 revertFileEntry(actionRequest);
183 }
184
185 if (cmd.equals(Constants.ADD_TEMP) ||
186 cmd.equals(Constants.DELETE_TEMP)) {
187
188 setForward(actionRequest, ActionConstants.COMMON_NULL);
189 }
190 else if (cmd.equals(Constants.PREVIEW)) {
191 }
192 else if (!cmd.equals(Constants.MOVE_FROM_TRASH)) {
193 sendRedirect(actionRequest, actionResponse);
194 }
195 else {
196 String redirect = ParamUtil.getString(
197 actionRequest, "redirect");
198 int workflowAction = ParamUtil.getInteger(
199 actionRequest, "workflowAction",
200 WorkflowConstants.ACTION_SAVE_DRAFT);
201
202 if ((fileEntry != null) &&
203 (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT)) {
204
205 redirect = getSaveAndContinueRedirect(
206 portletConfig, actionRequest, fileEntry, redirect);
207
208 sendRedirect(actionRequest, actionResponse, redirect);
209 }
210 else {
211 WindowState windowState = actionRequest.getWindowState();
212
213 if (!windowState.equals(LiferayWindowState.POP_UP)) {
214 sendRedirect(actionRequest, actionResponse);
215 }
216 else {
217 redirect = PortalUtil.escapeRedirect(
218 ParamUtil.getString(actionRequest, "redirect"));
219
220 if (Validator.isNotNull(redirect)) {
221 actionResponse.sendRedirect(redirect);
222 }
223 }
224 }
225 }
226 }
227 catch (Exception e) {
228 handleUploadException(actionRequest, actionResponse, cmd, e);
229 }
230 }
231
232 @Override
233 public ActionForward render(
234 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
235 RenderRequest renderRequest, RenderResponse renderResponse)
236 throws Exception {
237
238 try {
239 ActionUtil.getFileEntry(renderRequest);
240 }
241 catch (Exception e) {
242 if (e instanceof NoSuchFileEntryException ||
243 e instanceof NoSuchFileVersionException ||
244 e instanceof NoSuchRepositoryEntryException ||
245 e instanceof PrincipalException) {
246
247 SessionErrors.add(renderRequest, e.getClass());
248
249 return mapping.findForward("portlet.document_library.error");
250 }
251 else {
252 throw e;
253 }
254 }
255
256 String forward = "portlet.document_library.edit_file_entry";
257
258 return mapping.findForward(getForward(renderRequest, forward));
259 }
260
261 @Override
262 public void serveResource(
263 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
264 ResourceRequest resourceRequest, ResourceResponse resourceResponse)
265 throws Exception {
266
267 PortletContext portletContext = portletConfig.getPortletContext();
268
269 PortletRequestDispatcher portletRequestDispatcher =
270 portletContext.getRequestDispatcher(
271 "/html/portlet/document_library/" +
272 "upload_multiple_file_entries_resources.jsp");
273
274 portletRequestDispatcher.include(resourceRequest, resourceResponse);
275 }
276
277 protected void addMultipleFileEntries(
278 ActionRequest actionRequest, ActionResponse actionResponse)
279 throws Exception {
280
281 List<String> validFileNames = new ArrayList<String>();
282 List<KeyValuePair> invalidFileNameKVPs = new ArrayList<KeyValuePair>();
283
284 String[] selectedFileNames = ParamUtil.getParameterValues(
285 actionRequest, "selectedFileName");
286
287 for (String selectedFileName : selectedFileNames) {
288 addMultipleFileEntries(
289 actionRequest, actionResponse, selectedFileName, validFileNames,
290 invalidFileNameKVPs);
291 }
292
293 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
294
295 for (String validFileName : validFileNames) {
296 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
297
298 jsonObject.put("added", Boolean.TRUE);
299 jsonObject.put("fileName", validFileName);
300
301 jsonArray.put(jsonObject);
302 }
303
304 for (KeyValuePair invalidFileNameKVP : invalidFileNameKVPs) {
305 String fileName = invalidFileNameKVP.getKey();
306 String errorMessage = invalidFileNameKVP.getValue();
307
308 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
309
310 jsonObject.put("added", Boolean.FALSE);
311 jsonObject.put("errorMessage", errorMessage);
312 jsonObject.put("fileName", fileName);
313
314 jsonArray.put(jsonObject);
315 }
316
317 writeJSON(actionRequest, actionResponse, jsonArray);
318 }
319
320 protected void addMultipleFileEntries(
321 ActionRequest actionRequest, ActionResponse actionResponse,
322 String selectedFileName, List<String> validFileNames,
323 List<KeyValuePair> invalidFileNameKVPs)
324 throws Exception {
325
326 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
327 WebKeys.THEME_DISPLAY);
328
329 long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
330 long folderId = ParamUtil.getLong(actionRequest, "folderId");
331 String description = ParamUtil.getString(actionRequest, "description");
332 String changeLog = ParamUtil.getString(actionRequest, "changeLog");
333
334 FileEntry tempFileEntry = null;
335
336 try {
337 tempFileEntry = TempFileUtil.getTempFile(
338 themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
339 selectedFileName, _TEMP_FOLDER_NAME);
340
341 String mimeType = tempFileEntry.getMimeType();
342 InputStream inputStream = tempFileEntry.getContentStream();
343 long size = tempFileEntry.getSize();
344
345 ServiceContext serviceContext = ServiceContextFactory.getInstance(
346 DLFileEntry.class.getName(), actionRequest);
347
348 FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
349 repositoryId, folderId, selectedFileName, mimeType,
350 selectedFileName, description, changeLog, inputStream, size,
351 serviceContext);
352
353 AssetPublisherUtil.addAndStoreSelection(
354 actionRequest, DLFileEntry.class.getName(),
355 fileEntry.getFileEntryId(), -1);
356
357 AssetPublisherUtil.addRecentFolderId(
358 actionRequest, DLFileEntry.class.getName(), folderId);
359
360 validFileNames.add(selectedFileName);
361
362 return;
363 }
364 catch (Exception e) {
365 String errorMessage = getAddMultipleFileEntriesErrorMessage(
366 themeDisplay, e);
367
368 invalidFileNameKVPs.add(
369 new KeyValuePair(selectedFileName, errorMessage));
370 }
371 finally {
372 if (tempFileEntry != null) {
373 TempFileUtil.deleteTempFile(tempFileEntry.getFileEntryId());
374 }
375 }
376 }
377
378 protected void addTempFileEntry(ActionRequest actionRequest)
379 throws Exception {
380
381 UploadPortletRequest uploadPortletRequest =
382 PortalUtil.getUploadPortletRequest(actionRequest);
383
384 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
385 WebKeys.THEME_DISPLAY);
386
387 long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
388 String sourceFileName = uploadPortletRequest.getFileName("file");
389
390 InputStream inputStream = null;
391
392 try {
393 inputStream = uploadPortletRequest.getFileAsStream("file");
394
395 String contentType = uploadPortletRequest.getContentType("file");
396
397 DLAppServiceUtil.addTempFileEntry(
398 themeDisplay.getScopeGroupId(), folderId, sourceFileName,
399 _TEMP_FOLDER_NAME, inputStream, contentType);
400 }
401 catch (Exception e) {
402 UploadException uploadException =
403 (UploadException)actionRequest.getAttribute(
404 WebKeys.UPLOAD_EXCEPTION);
405
406 if ((uploadException != null) &&
407 (uploadException.getCause() instanceof IOFileUploadException)) {
408
409
410
411 }
412 else if ((uploadException != null) &&
413 uploadException.isExceededSizeLimit()) {
414
415 throw new FileSizeException(uploadException.getCause());
416 }
417 else {
418 throw e;
419 }
420 }
421 finally {
422 StreamUtil.cleanUp(inputStream);
423 }
424 }
425
426 protected void cancelFileEntriesCheckOut(ActionRequest actionRequest)
427 throws Exception {
428
429 long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
430
431 if (fileEntryId > 0) {
432 DLAppServiceUtil.cancelCheckOut(fileEntryId);
433 }
434 else {
435 long[] fileEntryIds = StringUtil.split(
436 ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
437
438 for (int i = 0; i < fileEntryIds.length; i++) {
439 DLAppServiceUtil.cancelCheckOut(fileEntryIds[i]);
440 }
441 }
442 }
443
444 protected void checkInFileEntries(ActionRequest actionRequest)
445 throws Exception {
446
447 long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
448
449 ServiceContext serviceContext = ServiceContextFactory.getInstance(
450 actionRequest);
451
452 if (fileEntryId > 0) {
453 DLAppServiceUtil.checkInFileEntry(
454 fileEntryId, false, StringPool.BLANK, serviceContext);
455 }
456 else {
457 long[] fileEntryIds = StringUtil.split(
458 ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
459
460 for (int i = 0; i < fileEntryIds.length; i++) {
461 DLAppServiceUtil.checkInFileEntry(
462 fileEntryIds[i], false, StringPool.BLANK, serviceContext);
463 }
464 }
465 }
466
467 protected void checkOutFileEntries(ActionRequest actionRequest)
468 throws Exception {
469
470 long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
471
472 ServiceContext serviceContext = ServiceContextFactory.getInstance(
473 actionRequest);
474
475 if (fileEntryId > 0) {
476 DLAppServiceUtil.checkOutFileEntry(fileEntryId, serviceContext);
477 }
478 else {
479 long[] fileEntryIds = StringUtil.split(
480 ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
481
482 for (int i = 0; i < fileEntryIds.length; i++) {
483 DLAppServiceUtil.checkOutFileEntry(
484 fileEntryIds[i], serviceContext);
485 }
486 }
487 }
488
489 protected void deleteFileEntry(
490 LiferayPortletConfig liferayPortletConfig,
491 ActionRequest actionRequest, boolean moveToTrash)
492 throws Exception {
493
494 long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
495
496 if (fileEntryId == 0) {
497 return;
498 }
499
500 String version = ParamUtil.getString(actionRequest, "version");
501
502 if (Validator.isNotNull(version)) {
503 DLAppServiceUtil.deleteFileVersion(fileEntryId, version);
504
505 return;
506 }
507
508 if (!moveToTrash) {
509 DLAppServiceUtil.deleteFileEntry(fileEntryId);
510
511 return;
512 }
513
514 DLAppServiceUtil.moveFileEntryToTrash(fileEntryId);
515
516 Map<String, String[]> data = new HashMap<String, String[]>();
517
518 data.put(
519 "restoreFileEntryIds", new String[] {String.valueOf(fileEntryId)});
520
521 SessionMessages.add(
522 actionRequest,
523 liferayPortletConfig.getPortletId() +
524 SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA,
525 data);
526
527 SessionMessages.add(
528 actionRequest,
529 liferayPortletConfig.getPortletId() +
530 SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
531 }
532
533 protected void deleteTempFileEntry(
534 ActionRequest actionRequest, ActionResponse actionResponse)
535 throws Exception {
536
537 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
538 WebKeys.THEME_DISPLAY);
539
540 long folderId = ParamUtil.getLong(actionRequest, "folderId");
541 String fileName = ParamUtil.getString(actionRequest, "fileName");
542
543 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
544
545 try {
546 DLAppServiceUtil.deleteTempFileEntry(
547 themeDisplay.getScopeGroupId(), folderId, fileName,
548 _TEMP_FOLDER_NAME);
549
550 jsonObject.put("deleted", Boolean.TRUE);
551 }
552 catch (Exception e) {
553 String errorMessage = LanguageUtil.get(
554 themeDisplay.getLocale(),
555 "an-unexpected-error-occurred-while-deleting-the-file");
556
557 jsonObject.put("deleted", Boolean.FALSE);
558 jsonObject.put("errorMessage", errorMessage);
559 }
560
561 writeJSON(actionRequest, actionResponse, jsonObject);
562 }
563
564 protected String getAddMultipleFileEntriesErrorMessage(
565 ThemeDisplay themeDisplay, Exception e)
566 throws Exception {
567
568 String errorMessage = null;
569
570 if (e instanceof AssetCategoryException) {
571 AssetCategoryException ace = (AssetCategoryException)e;
572
573 AssetVocabulary assetVocabulary = ace.getVocabulary();
574
575 String vocabularyTitle = StringPool.BLANK;
576
577 if (assetVocabulary != null) {
578 vocabularyTitle = assetVocabulary.getTitle(
579 themeDisplay.getLocale());
580 }
581
582 if (ace.getType() == AssetCategoryException.AT_LEAST_ONE_CATEGORY) {
583 errorMessage = LanguageUtil.format(
584 themeDisplay.getLocale(),
585 "please-select-at-least-one-category-for-x",
586 vocabularyTitle);
587 }
588 else if (ace.getType() ==
589 AssetCategoryException.TOO_MANY_CATEGORIES) {
590
591 errorMessage = LanguageUtil.format(
592 themeDisplay.getLocale(),
593 "you-cannot-select-more-than-one-category-for-x",
594 vocabularyTitle);
595 }
596 }
597 else if (e instanceof DuplicateFileException) {
598 errorMessage = LanguageUtil.get(
599 themeDisplay.getLocale(),
600 "the-folder-you-selected-already-has-an-entry-with-this-name." +
601 "-please-select-a-different-folder");
602 }
603 else if (e instanceof FileExtensionException) {
604 errorMessage = LanguageUtil.format(
605 themeDisplay.getLocale(),
606 "please-enter-a-file-with-a-valid-extension-x",
607 StringUtil.merge(
608 PrefsPropsUtil.getStringArray(
609 PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA)));
610 }
611 else if (e instanceof FileNameException) {
612 errorMessage = LanguageUtil.get(
613 themeDisplay.getLocale(),
614 "please-enter-a-file-with-a-valid-file-name");
615 }
616 else if (e instanceof FileSizeException) {
617 long maxSizeMB = PrefsPropsUtil.getLong(
618 PropsKeys.DL_FILE_MAX_SIZE) / 1024 / 1024;
619
620 errorMessage = LanguageUtil.format(
621 themeDisplay.getLocale(),
622 "file-size-is-larger-than-x-megabytes", maxSizeMB);
623 }
624 else if (e instanceof InvalidFileEntryTypeException) {
625 errorMessage = LanguageUtil.get(
626 themeDisplay.getLocale(),
627 "the-document-type-you-selected-is-not-valid-for-this-folder");
628 }
629 else {
630 errorMessage = LanguageUtil.get(
631 themeDisplay.getLocale(),
632 "an-unexpected-error-occurred-while-saving-your-document");
633 }
634
635 return errorMessage;
636 }
637
638 protected String getSaveAndContinueRedirect(
639 PortletConfig portletConfig, ActionRequest actionRequest,
640 FileEntry fileEntry, String redirect)
641 throws Exception {
642
643 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
644 WebKeys.THEME_DISPLAY);
645
646 PortletURLImpl portletURL = new PortletURLImpl(
647 actionRequest, portletConfig.getPortletName(),
648 themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
649
650 portletURL.setParameter(
651 "struts_action", "/document_library/edit_file_entry");
652 portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
653 portletURL.setParameter("redirect", redirect, false);
654
655 String referringPortletResource = ParamUtil.getString(
656 actionRequest, "referringPortletResource");
657
658 portletURL.setParameter(
659 "referringPortletResource", referringPortletResource, false);
660
661 portletURL.setParameter(
662 "groupId", String.valueOf(fileEntry.getGroupId()), false);
663 portletURL.setParameter(
664 "fileEntryId", String.valueOf(fileEntry.getFileEntryId()), false);
665 portletURL.setParameter(
666 "version", String.valueOf(fileEntry.getVersion()), false);
667 portletURL.setWindowState(actionRequest.getWindowState());
668
669 return portletURL.toString();
670 }
671
672 protected void handleUploadException(
673 ActionRequest actionRequest, ActionResponse actionResponse,
674 String cmd, Exception e)
675 throws Exception {
676
677 if (e instanceof AssetCategoryException ||
678 e instanceof AssetTagException) {
679
680 SessionErrors.add(actionRequest, e.getClass(), e);
681 }
682 else if (e instanceof DuplicateFileException ||
683 e instanceof DuplicateFolderNameException ||
684 e instanceof FileExtensionException ||
685 e instanceof FileMimeTypeException ||
686 e instanceof FileNameException ||
687 e instanceof FileSizeException ||
688 e instanceof NoSuchFolderException ||
689 e instanceof SourceFileNameException ||
690 e instanceof StorageFieldRequiredException) {
691
692 if (!cmd.equals(Constants.ADD_DYNAMIC) &&
693 !cmd.equals(Constants.ADD_MULTIPLE) &&
694 !cmd.equals(Constants.ADD_TEMP)) {
695
696 SessionErrors.add(actionRequest, e.getClass());
697
698 return;
699 }
700
701 if (e instanceof DuplicateFileException ||
702 e instanceof FileExtensionException ||
703 e instanceof FileNameException ||
704 e instanceof FileSizeException) {
705
706 HttpServletResponse response =
707 PortalUtil.getHttpServletResponse(actionResponse);
708
709 response.setContentType(ContentTypes.TEXT_HTML);
710 response.setStatus(HttpServletResponse.SC_OK);
711
712 int errorType = 0;
713
714 if (e instanceof DuplicateFileException) {
715 errorType =
716 ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION;
717 }
718 else if (e instanceof FileExtensionException) {
719 errorType =
720 ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION;
721 }
722 else if (e instanceof FileNameException) {
723 errorType = ServletResponseConstants.SC_FILE_NAME_EXCEPTION;
724 }
725 else if (e instanceof FileSizeException) {
726 errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
727 }
728
729 ServletResponseUtil.write(response, String.valueOf(errorType));
730 }
731
732 SessionErrors.add(actionRequest, e.getClass());
733 }
734 else if (e instanceof DuplicateLockException ||
735 e instanceof InvalidFileVersionException ||
736 e instanceof NoSuchFileEntryException ||
737 e instanceof PrincipalException) {
738
739 if (e instanceof DuplicateLockException) {
740 DuplicateLockException dle = (DuplicateLockException)e;
741
742 SessionErrors.add(actionRequest, dle.getClass(), dle.getLock());
743 }
744 else {
745 SessionErrors.add(actionRequest, e.getClass());
746 }
747
748 setForward(actionRequest, "portlet.document_library.error");
749 }
750 else {
751 throw e;
752 }
753 }
754
755 protected void moveFileEntries(
756 ActionRequest actionRequest, boolean moveFromTrash)
757 throws Exception {
758
759 long[] fileEntryIds = null;
760
761 long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
762
763 if (fileEntryId > 0) {
764 fileEntryIds = new long[] {fileEntryId};
765 }
766 else {
767 fileEntryIds = StringUtil.split(
768 ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
769 }
770
771 long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
772
773 ServiceContext serviceContext = ServiceContextFactory.getInstance(
774 DLFileEntry.class.getName(), actionRequest);
775
776 for (long moveFileEntryId : fileEntryIds) {
777 if (moveFromTrash) {
778 DLAppServiceUtil.moveFileEntryFromTrash(
779 moveFileEntryId, newFolderId, serviceContext);
780 }
781
782 else {
783 DLAppServiceUtil.moveFileEntry(
784 moveFileEntryId, newFolderId, serviceContext);
785 }
786 }
787 }
788
789 protected void revertFileEntry(ActionRequest actionRequest)
790 throws Exception {
791
792 long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
793 String version = ParamUtil.getString(actionRequest, "version");
794
795 ServiceContext serviceContext = ServiceContextFactory.getInstance(
796 DLFileEntry.class.getName(), actionRequest);
797
798 DLAppServiceUtil.revertFileEntry(fileEntryId, version, serviceContext);
799 }
800
801 protected FileEntry updateFileEntry(
802 PortletConfig portletConfig, ActionRequest actionRequest,
803 ActionResponse actionResponse)
804 throws Exception {
805
806 UploadPortletRequest uploadPortletRequest =
807 PortalUtil.getUploadPortletRequest(actionRequest);
808
809 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
810 WebKeys.THEME_DISPLAY);
811
812 String cmd = ParamUtil.getString(uploadPortletRequest, Constants.CMD);
813
814 long fileEntryId = ParamUtil.getLong(
815 uploadPortletRequest, "fileEntryId");
816
817 long repositoryId = ParamUtil.getLong(
818 uploadPortletRequest, "repositoryId");
819 long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
820 String sourceFileName = uploadPortletRequest.getFileName("file");
821 String title = ParamUtil.getString(uploadPortletRequest, "title");
822 String description = ParamUtil.getString(
823 uploadPortletRequest, "description");
824 String changeLog = ParamUtil.getString(
825 uploadPortletRequest, "changeLog");
826 boolean majorVersion = ParamUtil.getBoolean(
827 uploadPortletRequest, "majorVersion");
828
829 if (folderId > 0) {
830 Folder folder = DLAppServiceUtil.getFolder(folderId);
831
832 if (folder.getGroupId() != themeDisplay.getScopeGroupId()) {
833 throw new NoSuchFolderException();
834 }
835 }
836
837 InputStream inputStream = null;
838
839 try {
840 String contentType = uploadPortletRequest.getContentType("file");
841
842 long size = uploadPortletRequest.getSize("file");
843
844 if ((cmd.equals(Constants.ADD) ||
845 cmd.equals(Constants.ADD_DYNAMIC)) &&
846 (size == 0)) {
847
848 contentType = MimeTypesUtil.getContentType(title);
849 }
850
851 if (cmd.equals(Constants.ADD) ||
852 cmd.equals(Constants.ADD_DYNAMIC) || (size > 0)) {
853
854 String portletName = portletConfig.getPortletName();
855
856 if (portletName.equals(PortletKeys.MEDIA_GALLERY_DISPLAY)) {
857 String portletResource = ParamUtil.getString(
858 actionRequest, "portletResource");
859
860 PortletPreferences portletPreferences = null;
861
862 if (Validator.isNotNull(portletResource)) {
863 PortletPreferencesFactoryUtil.getPortletSetup(
864 actionRequest, portletResource);
865 }
866 else {
867 portletPreferences = actionRequest.getPreferences();
868 }
869
870 String[] mimeTypes = DLUtil.getMediaGalleryMimeTypes(
871 portletPreferences, actionRequest);
872
873 if (Arrays.binarySearch(mimeTypes, contentType) < 0) {
874 throw new FileMimeTypeException(contentType);
875 }
876 }
877 }
878
879 inputStream = uploadPortletRequest.getFileAsStream("file");
880
881 ServiceContext serviceContext = ServiceContextFactory.getInstance(
882 DLFileEntry.class.getName(), uploadPortletRequest);
883
884 FileEntry fileEntry = null;
885
886 if (cmd.equals(Constants.ADD) ||
887 cmd.equals(Constants.ADD_DYNAMIC)) {
888
889
890
891 fileEntry = DLAppServiceUtil.addFileEntry(
892 repositoryId, folderId, sourceFileName, contentType, title,
893 description, changeLog, inputStream, size, serviceContext);
894
895 AssetPublisherUtil.addAndStoreSelection(
896 actionRequest, DLFileEntry.class.getName(),
897 fileEntry.getFileEntryId(), -1);
898
899 if (cmd.equals(Constants.ADD_DYNAMIC)) {
900 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
901
902 jsonObject.put("fileEntryId", fileEntry.getFileEntryId());
903
904 writeJSON(actionRequest, actionResponse, jsonObject);
905 }
906 }
907 else if (cmd.equals(Constants.UPDATE_AND_CHECKIN)) {
908
909
910
911 fileEntry = DLAppServiceUtil.updateFileEntryAndCheckIn(
912 fileEntryId, sourceFileName, contentType, title,
913 description, changeLog, majorVersion, inputStream, size,
914 serviceContext);
915 }
916 else {
917
918
919
920 fileEntry = DLAppServiceUtil.updateFileEntry(
921 fileEntryId, sourceFileName, contentType, title,
922 description, changeLog, majorVersion, inputStream, size,
923 serviceContext);
924 }
925
926 AssetPublisherUtil.addRecentFolderId(
927 actionRequest, DLFileEntry.class.getName(), folderId);
928
929 return fileEntry;
930 }
931 catch (Exception e) {
932 UploadException uploadException =
933 (UploadException)actionRequest.getAttribute(
934 WebKeys.UPLOAD_EXCEPTION);
935
936 if ((uploadException != null) &&
937 uploadException.isExceededSizeLimit()) {
938
939 throw new FileSizeException(uploadException.getCause());
940 }
941
942 throw e;
943 }
944 finally {
945 StreamUtil.cleanUp(inputStream);
946 }
947 }
948
949 private static final String _TEMP_FOLDER_NAME =
950 EditFileEntryAction.class.getName();
951
952 }