001    /**
002     * Copyright (c) 2000-2010 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.documentlibrary.DuplicateFileException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.WebKeys;
027    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
028    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
029    import com.liferay.portlet.documentlibrary.FolderNameException;
030    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
031    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
032    import com.liferay.portlet.documentlibrary.model.DLFolder;
033    import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Alexander Chow
048     */
049    public class EditFolderAction extends PortletAction {
050    
051            public void processAction(
052                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
053                            ActionRequest actionRequest, ActionResponse actionResponse)
054                    throws Exception {
055    
056                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
057    
058                    try {
059                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
060                                    updateFolder(actionRequest);
061                            }
062                            else if (cmd.equals(Constants.DELETE)) {
063                                    deleteFolder(actionRequest);
064                            }
065    
066                            sendRedirect(actionRequest, actionResponse);
067                    }
068                    catch (Exception e) {
069                            if (e instanceof NoSuchFolderException ||
070                                    e instanceof PrincipalException) {
071    
072                                    SessionErrors.add(actionRequest, e.getClass().getName());
073    
074                                    setForward(actionRequest, "portlet.document_library.error");
075                            }
076                            else if (e instanceof DuplicateFileException ||
077                                             e instanceof DuplicateFolderNameException ||
078                                             e instanceof FolderNameException) {
079    
080                                    SessionErrors.add(actionRequest, e.getClass().getName());
081                            }
082                            else {
083                                    throw e;
084                            }
085                    }
086            }
087    
088            public ActionForward render(
089                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
090                            RenderRequest renderRequest, RenderResponse renderResponse)
091                    throws Exception {
092    
093                    try {
094                            ActionUtil.getFolder(renderRequest);
095                    }
096                    catch (Exception e) {
097                            if (e instanceof NoSuchFolderException ||
098                                    e instanceof PrincipalException) {
099    
100                                    SessionErrors.add(renderRequest, e.getClass().getName());
101    
102                                    return mapping.findForward("portlet.document_library.error");
103                            }
104                            else {
105                                    throw e;
106                            }
107                    }
108    
109                    return mapping.findForward(
110                            getForward(renderRequest, "portlet.document_library.edit_folder"));
111            }
112    
113            protected void deleteFolder(ActionRequest actionRequest) throws Exception {
114                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
115    
116                    DLFolderServiceUtil.deleteFolder(folderId);
117    
118                    AssetPublisherUtil.removeRecentFolderId(
119                            actionRequest, DLFileEntry.class.getName(), folderId);
120            }
121    
122            protected void updateFolder(ActionRequest actionRequest) throws Exception {
123                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
124                            WebKeys.THEME_DISPLAY);
125    
126                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
127    
128                    long parentFolderId = ParamUtil.getLong(
129                            actionRequest, "parentFolderId");
130                    String name = ParamUtil.getString(actionRequest, "name");
131                    String description = ParamUtil.getString(actionRequest, "description");
132    
133                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
134                            DLFolder.class.getName(), actionRequest);
135    
136                    if (folderId <= 0) {
137    
138                            // Add folder
139    
140                            DLFolderServiceUtil.addFolder(
141                                    themeDisplay.getScopeGroupId(), parentFolderId, name,
142                                    description, serviceContext);
143                    }
144                    else {
145    
146                            // Update folder
147    
148                            DLFolderServiceUtil.updateFolder(
149                                    folderId, parentFolderId, name, description, serviceContext);
150                    }
151            }
152    
153    }