001    /**
002     * Copyright (c) 2000-2011 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.FileUtil;
032    import com.liferay.portal.kernel.util.KeyValuePair;
033    import com.liferay.portal.kernel.util.MimeTypesUtil;
034    import com.liferay.portal.kernel.util.ParamUtil;
035    import com.liferay.portal.kernel.util.PropsKeys;
036    import com.liferay.portal.kernel.util.StreamUtil;
037    import com.liferay.portal.kernel.util.StringPool;
038    import com.liferay.portal.kernel.util.StringUtil;
039    import com.liferay.portal.kernel.util.TempFileUtil;
040    import com.liferay.portal.kernel.util.Validator;
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.asset.AssetCategoryException;
053    import com.liferay.portlet.asset.AssetTagException;
054    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
055    import com.liferay.portlet.documentlibrary.DuplicateFileException;
056    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
057    import com.liferay.portlet.documentlibrary.FileExtensionException;
058    import com.liferay.portlet.documentlibrary.FileMimeTypeException;
059    import com.liferay.portlet.documentlibrary.FileNameException;
060    import com.liferay.portlet.documentlibrary.FileSizeException;
061    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
062    import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
063    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
064    import com.liferay.portlet.documentlibrary.SourceFileNameException;
065    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
066    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
067    import com.liferay.portlet.documentlibrary.util.DLUtil;
068    
069    import java.io.File;
070    import java.io.InputStream;
071    
072    import java.util.ArrayList;
073    import java.util.Arrays;
074    import java.util.List;
075    
076    import javax.portlet.ActionRequest;
077    import javax.portlet.ActionResponse;
078    import javax.portlet.PortletConfig;
079    import javax.portlet.PortletContext;
080    import javax.portlet.PortletPreferences;
081    import javax.portlet.PortletRequestDispatcher;
082    import javax.portlet.RenderRequest;
083    import javax.portlet.RenderResponse;
084    import javax.portlet.ResourceRequest;
085    import javax.portlet.ResourceResponse;
086    import javax.portlet.WindowState;
087    
088    import javax.servlet.http.HttpServletResponse;
089    
090    import org.apache.struts.action.ActionForm;
091    import org.apache.struts.action.ActionForward;
092    import org.apache.struts.action.ActionMapping;
093    
094    /**
095     * @author Brian Wing Shun Chan
096     * @author Alexander Chow
097     * @author Sergio González
098     */
099    public class EditFileEntryAction extends PortletAction {
100    
101            @Override
102            public void processAction(
103                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
104                            ActionRequest actionRequest, ActionResponse actionResponse)
105                    throws Exception {
106    
107                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
108    
109                    try {
110                            if (Validator.isNull(cmd)) {
111                                    UploadException uploadException =
112                                            (UploadException)actionRequest.getAttribute(
113                                                    WebKeys.UPLOAD_EXCEPTION);
114    
115                                    if (uploadException != null) {
116                                            if (uploadException.isExceededSizeLimit()) {
117                                                    throw new FileSizeException(uploadException.getCause());
118                                            }
119    
120                                            throw new PortalException(uploadException.getCause());
121                                    }
122                            }
123                            else if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)
124                                    || cmd.equals(Constants.UPDATE_AND_CHECKIN)) {
125    
126                                    updateFileEntry(portletConfig, actionRequest, actionResponse);
127                            }
128                            else if (cmd.equals(Constants.ADD_MULTIPLE)) {
129                                    addMultipleFileEntries(actionRequest, actionResponse);
130                            }
131                            else if (cmd.equals(Constants.ADD_TEMP)) {
132                                    addTempFileEntry(actionRequest);
133                            }
134                            else if (cmd.equals(Constants.DELETE)) {
135                                    deleteFileEntries(actionRequest);
136                            }
137                            else if (cmd.equals(Constants.DELETE_TEMP)) {
138                                    deleteTempFileEntry(actionRequest, actionResponse);
139                            }
140                            else if (cmd.equals(Constants.CANCEL_CHECKOUT)) {
141                                    cancelFileEntriesCheckOut(actionRequest);
142                            }
143                            else if (cmd.equals(Constants.CHECKIN)) {
144                                    checkInFileEntries(actionRequest);
145                            }
146                            else if (cmd.equals(Constants.CHECKOUT)) {
147                                    checkOutFileEntries(actionRequest);
148                            }
149                            else if (cmd.equals(Constants.MOVE)) {
150                                    moveFileEntries(actionRequest);
151                            }
152                            else if (cmd.equals(Constants.REVERT)) {
153                                    revertFileEntry(actionRequest);
154                            }
155    
156                            WindowState windowState = actionRequest.getWindowState();
157    
158                            if (cmd.equals(Constants.ADD_TEMP) ||
159                                    cmd.equals(Constants.DELETE_TEMP)) {
160    
161                                    setForward(actionRequest, ActionConstants.COMMON_NULL);
162                            }
163                            else if (cmd.equals(Constants.PREVIEW)) {
164                            }
165                            else if (!windowState.equals(LiferayWindowState.POP_UP)) {
166                                    sendRedirect(actionRequest, actionResponse);
167                            }
168                            else {
169                                    String redirect = PortalUtil.escapeRedirect(
170                                            ParamUtil.getString(actionRequest, "redirect"));
171    
172                                    if (Validator.isNotNull(redirect)) {
173                                            actionResponse.sendRedirect(redirect);
174                                    }
175                            }
176                    }
177                    catch (Exception e) {
178                            if (e instanceof DuplicateLockException ||
179                                    e instanceof NoSuchFileEntryException ||
180                                    e instanceof PrincipalException) {
181    
182                                    if (e instanceof DuplicateLockException) {
183                                            DuplicateLockException dle = (DuplicateLockException)e;
184    
185                                            SessionErrors.add(
186                                                    actionRequest, dle.getClass().getName(), dle.getLock());
187                                    }
188                                    else {
189                                            SessionErrors.add(actionRequest, e.getClass().getName());
190                                    }
191    
192                                    setForward(actionRequest, "portlet.document_library.error");
193                            }
194                            else if (e instanceof DuplicateFileException ||
195                                             e instanceof DuplicateFolderNameException ||
196                                             e instanceof FileExtensionException ||
197                                             e instanceof FileMimeTypeException ||
198                                             e instanceof FileNameException ||
199                                             e instanceof FileSizeException ||
200                                             e instanceof NoSuchFolderException ||
201                                             e instanceof SourceFileNameException) {
202    
203                                    if (!cmd.equals(Constants.ADD_MULTIPLE) &&
204                                            !cmd.equals(Constants.ADD_TEMP)) {
205    
206                                            SessionErrors.add(actionRequest, e.getClass().getName());
207    
208                                            return;
209                                    }
210    
211                                    if (e instanceof DuplicateFileException) {
212                                            HttpServletResponse response =
213                                                    PortalUtil.getHttpServletResponse(actionResponse);
214    
215                                            response.setStatus(
216                                                    ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
217                                    }
218                                    else if (e instanceof FileExtensionException) {
219                                            HttpServletResponse response =
220                                                    PortalUtil.getHttpServletResponse(actionResponse);
221    
222                                            response.setStatus(
223                                                    ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION);
224                                    }
225                                    else if (e instanceof FileNameException) {
226                                            HttpServletResponse response =
227                                                    PortalUtil.getHttpServletResponse(actionResponse);
228    
229                                            response.setStatus(
230                                                    ServletResponseConstants.SC_FILE_NAME_EXCEPTION);
231                                    }
232                                    else if (e instanceof FileSizeException) {
233                                            HttpServletResponse response =
234                                                    PortalUtil.getHttpServletResponse(actionResponse);
235    
236                                            response.setStatus(
237                                                    ServletResponseConstants.SC_FILE_SIZE_EXCEPTION);
238                                    }
239    
240                                    SessionErrors.add(actionRequest, e.getClass().getName());
241                            }
242                            else if (e instanceof AssetCategoryException ||
243                                             e instanceof AssetTagException) {
244    
245                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
246                            }
247                            else {
248                                    throw e;
249                            }
250                    }
251            }
252    
253            @Override
254            public ActionForward render(
255                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
256                            RenderRequest renderRequest, RenderResponse renderResponse)
257                    throws Exception {
258    
259                    try {
260                            ActionUtil.getFileEntry(renderRequest);
261                    }
262                    catch (Exception e) {
263                            if (e instanceof NoSuchFileEntryException ||
264                                    e instanceof NoSuchFileVersionException ||
265                                    e instanceof PrincipalException) {
266    
267                                    SessionErrors.add(renderRequest, e.getClass().getName());
268    
269                                    return mapping.findForward("portlet.document_library.error");
270                            }
271                            else {
272                                    throw e;
273                            }
274                    }
275    
276                    String forward = "portlet.document_library.edit_file_entry";
277    
278                    return mapping.findForward(getForward(renderRequest, forward));
279            }
280    
281            @Override
282            public void serveResource(
283                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
284                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
285                    throws Exception {
286    
287                    PortletContext portletContext = portletConfig.getPortletContext();
288    
289                    PortletRequestDispatcher portletRequestDispatcher =
290                            portletContext.getRequestDispatcher(
291                                    "/html/portlet/document_library/" +
292                                            "upload_multiple_file_entries_resources.jsp");
293    
294                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
295            }
296    
297            protected void addMultipleFileEntries(
298                            ActionRequest actionRequest, ActionResponse actionResponse)
299                    throws Exception {
300    
301                    List<String> validFileNames = new ArrayList<String>();
302                    List<KeyValuePair> invalidFileNameKVPs = new ArrayList<KeyValuePair>();
303    
304                    String[] selectedFileNames = ParamUtil.getParameterValues(
305                            actionRequest, "selectedFileName");
306    
307                    for (String selectedFileName : selectedFileNames) {
308                            addMultipleFileEntries(
309                                    actionRequest, actionResponse, selectedFileName,
310                                    validFileNames, invalidFileNameKVPs);
311                    }
312    
313                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
314    
315                    for (String validFileName : validFileNames) {
316                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
317    
318                            jsonObject.put("added", Boolean.TRUE);
319                            jsonObject.put("fileName", validFileName);
320    
321                            jsonArray.put(jsonObject);
322                    }
323    
324                    for (KeyValuePair invalidFileNameKVP : invalidFileNameKVPs) {
325                            String fileName = invalidFileNameKVP.getKey();
326                            String errorMessage = invalidFileNameKVP.getValue();
327    
328                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
329    
330                            jsonObject.put("added", Boolean.FALSE);
331                            jsonObject.put("errorMessage", errorMessage);
332                            jsonObject.put("fileName", fileName);
333    
334                            jsonArray.put(jsonObject);
335                    }
336    
337                    writeJSON(actionRequest, actionResponse, jsonArray);
338            }
339    
340            protected void addMultipleFileEntries(
341                            ActionRequest actionRequest, ActionResponse actionResponse,
342                            String selectedFileName, List<String> validFileNames,
343                            List<KeyValuePair> invalidFileNameKVPs)
344                    throws Exception {
345    
346                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
347                            WebKeys.THEME_DISPLAY);
348    
349                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
350                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
351                    String contentType = MimeTypesUtil.getContentType(selectedFileName);
352                    String description = ParamUtil.getString(actionRequest, "description");
353                    String changeLog = ParamUtil.getString(actionRequest, "changeLog");
354    
355                    File file = null;
356    
357                    try {
358                            file = TempFileUtil.getTempFile(
359                                    themeDisplay.getUserId(), selectedFileName, _TEMP_FOLDER_NAME);
360    
361                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
362                                    DLFileEntry.class.getName(), actionRequest);
363    
364                            FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
365                                    repositoryId, folderId, selectedFileName, contentType,
366                                    selectedFileName, description, changeLog, file, serviceContext);
367    
368                            AssetPublisherUtil.addAndStoreSelection(
369                                    actionRequest, DLFileEntry.class.getName(),
370                                    fileEntry.getFileEntryId(), -1);
371    
372                            AssetPublisherUtil.addRecentFolderId(
373                                    actionRequest, DLFileEntry.class.getName(), folderId);
374    
375                            validFileNames.add(selectedFileName);
376    
377                            return;
378                    }
379                    catch (Exception e) {
380                            String errorMessage = getAddMultipleFileEntriesErrorMessage(
381                                    themeDisplay, e);
382    
383                            invalidFileNameKVPs.add(
384                                    new KeyValuePair(selectedFileName, errorMessage));
385                    }
386                    finally {
387                            FileUtil.delete(file);
388                    }
389            }
390    
391            protected String getAddMultipleFileEntriesErrorMessage(
392                            ThemeDisplay themeDisplay, Exception e)
393                    throws Exception {
394    
395                    String errorMessage = null;
396    
397                    if (e instanceof DuplicateFileException) {
398                            errorMessage = LanguageUtil.get(
399                                    themeDisplay.getLocale(),
400                                    "the-folder-you-selected-already-has-an-entry-with-this-name." +
401                                            "-please-select-a-different-folder");
402                    }
403                    else if (e instanceof FileExtensionException) {
404                            errorMessage = LanguageUtil.format(
405                                    themeDisplay.getLocale(),
406                                    "please-enter-a-file-with-a-valid-extension-x",
407                                    StringUtil.merge(
408                                            PrefsPropsUtil.getStringArray(
409                                                    PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA)));
410                    }
411                    else if (e instanceof FileNameException) {
412                            errorMessage = LanguageUtil.get(
413                                    themeDisplay.getLocale(),
414                                    "please-enter-a-file-with-a-valid-file-name");
415                    }
416                    else if (e instanceof FileSizeException) {
417                            long maxSizeMB = PrefsPropsUtil.getLong(
418                                    PropsKeys.DL_FILE_MAX_SIZE) / 1024 / 1024;
419    
420                            errorMessage = LanguageUtil.format(
421                                    themeDisplay.getLocale(),
422                                    "file-size-is-larger-than-x-megabytes", maxSizeMB);
423                    }
424                    else {
425                            errorMessage = LanguageUtil.get(
426                                    themeDisplay.getLocale(),
427                                    "an-unexpected-error-occurred-while-saving-your-document");
428                    }
429    
430                    return errorMessage;
431            }
432    
433            protected void addTempFileEntry(ActionRequest actionRequest)
434                    throws Exception {
435    
436                    UploadPortletRequest uploadPortletRequest =
437                            PortalUtil.getUploadPortletRequest(actionRequest);
438    
439                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
440                            WebKeys.THEME_DISPLAY);
441    
442                    long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
443                    String sourceFileName = uploadPortletRequest.getFileName("file");
444    
445                    InputStream inputStream = null;
446    
447                    try {
448                            inputStream = uploadPortletRequest.getFileAsStream("file");
449    
450                            DLAppServiceUtil.addTempFileEntry(
451                                    themeDisplay.getScopeGroupId(), folderId, sourceFileName,
452                                    _TEMP_FOLDER_NAME, inputStream);
453                    }
454                    finally {
455                            StreamUtil.cleanUp(inputStream);
456                    }
457            }
458    
459            protected void cancelFileEntriesCheckOut(ActionRequest actionRequest)
460                    throws Exception {
461    
462                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
463    
464                    if (fileEntryId > 0) {
465                            DLAppServiceUtil.cancelCheckOut(fileEntryId);
466                    }
467                    else {
468                            long[] fileEntryIds = StringUtil.split(
469                                    ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
470    
471                            for (int i = 0; i < fileEntryIds.length; i++) {
472                                    DLAppServiceUtil.cancelCheckOut(fileEntryIds[i]);
473                            }
474                    }
475            }
476    
477            protected void checkInFileEntries(ActionRequest actionRequest)
478                    throws Exception {
479    
480                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
481    
482                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
483                            actionRequest);
484    
485                    if (fileEntryId > 0) {
486                            DLAppServiceUtil.checkInFileEntry(
487                                    fileEntryId, false, StringPool.BLANK, serviceContext);
488                    }
489                    else {
490                            long[] fileEntryIds = StringUtil.split(
491                                    ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
492    
493                            for (int i = 0; i < fileEntryIds.length; i++) {
494                                    DLAppServiceUtil.checkInFileEntry(
495                                            fileEntryIds[i], false, StringPool.BLANK, serviceContext);
496                            }
497                    }
498            }
499    
500            protected void checkOutFileEntries(ActionRequest actionRequest)
501                    throws Exception {
502    
503                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
504    
505                    if (fileEntryId > 0) {
506                            DLAppServiceUtil.checkOutFileEntry(fileEntryId);
507                    }
508                    else {
509                            long[] fileEntryIds = StringUtil.split(
510                                    ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
511    
512                            for (int i = 0; i < fileEntryIds.length; i++) {
513                                    DLAppServiceUtil.checkOutFileEntry(fileEntryIds[i]);
514                            }
515                    }
516            }
517    
518            protected void deleteFileEntries(ActionRequest actionRequest)
519                    throws Exception {
520    
521                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
522    
523                    if (fileEntryId > 0) {
524                            DLAppServiceUtil.deleteFileEntry(fileEntryId);
525                    }
526                    else {
527                            long[] deleteFileEntryIds = StringUtil.split(
528                                    ParamUtil.getString(actionRequest, "deleteEntryIds"), 0L);
529    
530                            for (int i = 0; i < deleteFileEntryIds.length; i++) {
531                                    DLAppServiceUtil.deleteFileEntry(deleteFileEntryIds[i]);
532                            }
533                    }
534            }
535    
536            protected void deleteTempFileEntry(
537                            ActionRequest actionRequest, ActionResponse actionResponse)
538                    throws Exception {
539    
540                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
541                            WebKeys.THEME_DISPLAY);
542    
543                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
544                    String fileName = ParamUtil.getString(actionRequest, "fileName");
545    
546                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
547    
548                    try {
549                            DLAppServiceUtil.deleteTempFileEntry(
550                                    themeDisplay.getScopeGroupId(), folderId, fileName,
551                                    _TEMP_FOLDER_NAME);
552    
553                            jsonObject.put("deleted", Boolean.TRUE);
554                    }
555                    catch (Exception e) {
556                            String errorMessage = LanguageUtil.get(
557                                    themeDisplay.getLocale(),
558                                    "an-unexpected-error-occurred-while-deleting-the-file");
559    
560                            jsonObject.put("deleted", Boolean.FALSE);
561                            jsonObject.put("errorMessage", errorMessage);
562                    }
563    
564                    writeJSON(actionRequest, actionResponse, jsonObject);
565            }
566    
567            protected void moveFileEntries(ActionRequest actionRequest)
568                    throws Exception {
569    
570                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
571                    long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
572    
573                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
574                            DLFileEntry.class.getName(), actionRequest);
575    
576                    if (fileEntryId > 0) {
577                            DLAppServiceUtil.moveFileEntry(
578                                    fileEntryId, newFolderId, serviceContext);
579                    }
580                    else {
581                            long[] fileEntryIds = StringUtil.split(
582                                    ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
583    
584                            for (int i = 0; i < fileEntryIds.length; i++) {
585                                    DLAppServiceUtil.moveFileEntry(
586                                            fileEntryIds[i], newFolderId, serviceContext);
587                            }
588                    }
589            }
590    
591            protected void revertFileEntry(ActionRequest actionRequest)
592                    throws Exception {
593    
594                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
595                    String version = ParamUtil.getString(actionRequest, "version");
596    
597                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
598                            DLFileEntry.class.getName(), actionRequest);
599    
600                    DLAppServiceUtil.revertFileEntry(fileEntryId, version, serviceContext);
601            }
602    
603            protected void updateFileEntry(
604                            PortletConfig portletConfig, ActionRequest actionRequest,
605                            ActionResponse actionResponse)
606                    throws Exception {
607    
608                    UploadPortletRequest uploadPortletRequest =
609                            PortalUtil.getUploadPortletRequest(actionRequest);
610    
611                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
612                            WebKeys.THEME_DISPLAY);
613    
614                    String cmd = ParamUtil.getString(uploadPortletRequest, Constants.CMD);
615    
616                    long fileEntryId = ParamUtil.getLong(
617                            uploadPortletRequest, "fileEntryId");
618    
619                    long repositoryId = ParamUtil.getLong(
620                            uploadPortletRequest, "repositoryId");
621                    long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
622                    String sourceFileName = uploadPortletRequest.getFileName("file");
623                    String title = ParamUtil.getString(uploadPortletRequest, "title");
624                    String description = ParamUtil.getString(
625                            uploadPortletRequest, "description");
626                    String changeLog = ParamUtil.getString(
627                            uploadPortletRequest, "changeLog");
628                    boolean majorVersion = ParamUtil.getBoolean(
629                            uploadPortletRequest, "majorVersion");
630    
631                    if (folderId > 0) {
632                            Folder folder = DLAppServiceUtil.getFolder(folderId);
633    
634                            if (folder.getGroupId() != themeDisplay.getScopeGroupId()) {
635                                    throw new NoSuchFolderException();
636                            }
637                    }
638    
639                    InputStream inputStream = null;
640    
641                    try {
642                            String contentType = uploadPortletRequest.getContentType("file");
643    
644                            long size = uploadPortletRequest.getSize("file");
645    
646                            if (cmd.equals(Constants.ADD) && (size == 0)) {
647                                    contentType = MimeTypesUtil.getContentType(title);
648                            }
649    
650                            if (cmd.equals(Constants.ADD) || (size > 0)) {
651                                    String portletName = portletConfig.getPortletName();
652    
653                                    if (portletName.equals(PortletKeys.MEDIA_GALLERY_DISPLAY)) {
654                                            String portletResource = ParamUtil.getString(
655                                                    actionRequest, "portletResource");
656    
657                                            PortletPreferences portletPreferences = null;
658    
659                                            if (Validator.isNotNull(portletResource)) {
660                                                    PortletPreferencesFactoryUtil.getPortletSetup(
661                                                            actionRequest, portletResource);
662                                            }
663                                            else {
664                                                    portletPreferences = actionRequest.getPreferences();
665                                            }
666    
667                                            String[] mimeTypes = DLUtil.getMediaGalleryMimeTypes(
668                                                    portletPreferences, actionRequest);
669    
670                                            if (Arrays.binarySearch(mimeTypes, contentType) < 0) {
671                                                    throw new FileMimeTypeException(contentType);
672                                            }
673                                    }
674                            }
675    
676                            inputStream = uploadPortletRequest.getFileAsStream("file");
677    
678                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
679                                    DLFileEntry.class.getName(), actionRequest);
680    
681                            FileEntry fileEntry = null;
682    
683                            if (cmd.equals(Constants.ADD)) {
684                                    if (Validator.isNull(title)) {
685                                            title = sourceFileName;
686                                    }
687    
688                                    // Add file entry
689    
690                                    fileEntry = DLAppServiceUtil.addFileEntry(
691                                            repositoryId, folderId, sourceFileName, contentType, title,
692                                            description, changeLog, inputStream, size, serviceContext);
693    
694                                    AssetPublisherUtil.addAndStoreSelection(
695                                            actionRequest, DLFileEntry.class.getName(),
696                                            fileEntry.getFileEntryId(), -1);
697                            }
698                            else if (cmd.equals(Constants.UPDATE_AND_CHECKIN)) {
699    
700                                    // Update file entry and checkin
701    
702                                    fileEntry = DLAppServiceUtil.updateFileEntryAndCheckIn(
703                                            fileEntryId, sourceFileName, contentType, title,
704                                            description, changeLog, majorVersion, inputStream,
705                                            size, serviceContext);
706                            }
707                            else {
708    
709                                    // Update file entry
710    
711                                    fileEntry = DLAppServiceUtil.updateFileEntry(
712                                            fileEntryId, sourceFileName, contentType, title,
713                                            description, changeLog, majorVersion, inputStream,
714                                            size, serviceContext);
715                            }
716    
717                            AssetPublisherUtil.addRecentFolderId(
718                                    actionRequest, DLFileEntry.class.getName(), folderId);
719                    }
720                    finally {
721                            StreamUtil.cleanUp(inputStream);
722                    }
723            }
724    
725            private static final String _TEMP_FOLDER_NAME =
726                    EditFileEntryAction.class.getName();
727    
728    }