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