1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.documentlibrary.action;
24  
25  import com.liferay.documentlibrary.DuplicateFileException;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portal.service.ServiceContextFactory;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
36  import com.liferay.portlet.documentlibrary.FolderNameException;
37  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
38  import com.liferay.portlet.documentlibrary.model.DLFolder;
39  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
40  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.ActionResponse;
43  import javax.portlet.PortletConfig;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="EditFolderAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Alexander Chow
56   */
57  public class EditFolderAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
65  
66          try {
67              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
68                  updateFolder(actionRequest);
69              }
70              else if (cmd.equals(Constants.DELETE)) {
71                  deleteFolder(actionRequest);
72              }
73  
74              sendRedirect(actionRequest, actionResponse);
75          }
76          catch (Exception e) {
77              if (e instanceof NoSuchFolderException ||
78                  e instanceof PrincipalException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81  
82                  setForward(actionRequest, "portlet.document_library.error");
83              }
84              else if (e instanceof DuplicateFileException ||
85                       e instanceof DuplicateFolderNameException ||
86                       e instanceof FolderNameException) {
87  
88                  SessionErrors.add(actionRequest, e.getClass().getName());
89              }
90              else {
91                  throw e;
92              }
93          }
94      }
95  
96      public ActionForward render(
97              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
98              RenderRequest renderRequest, RenderResponse renderResponse)
99          throws Exception {
100 
101         try {
102             ActionUtil.getFolder(renderRequest);
103         }
104         catch (Exception e) {
105             if (e instanceof NoSuchFolderException ||
106                 e instanceof PrincipalException) {
107 
108                 SessionErrors.add(renderRequest, e.getClass().getName());
109 
110                 return mapping.findForward("portlet.document_library.error");
111             }
112             else {
113                 throw e;
114             }
115         }
116 
117         return mapping.findForward(
118             getForward(renderRequest, "portlet.document_library.edit_folder"));
119     }
120 
121     protected void deleteFolder(ActionRequest actionRequest) throws Exception {
122         long folderId = ParamUtil.getLong(actionRequest, "folderId");
123 
124         DLFolderServiceUtil.deleteFolder(folderId);
125     }
126 
127     protected void updateFolder(ActionRequest actionRequest) throws Exception {
128         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
129             WebKeys.THEME_DISPLAY);
130 
131         long folderId = ParamUtil.getLong(actionRequest, "folderId");
132 
133         long parentFolderId = ParamUtil.getLong(
134             actionRequest, "parentFolderId");
135         String name = ParamUtil.getString(actionRequest, "name");
136         String description = ParamUtil.getString(actionRequest, "description");
137 
138         ServiceContext serviceContext = ServiceContextFactory.getInstance(
139             DLFolder.class.getName(), actionRequest);
140 
141         if (folderId <= 0) {
142 
143             // Add folder
144 
145             DLFolderServiceUtil.addFolder(
146                 themeDisplay.getScopeGroupId(), parentFolderId, name,
147                 description, serviceContext);
148         }
149         else {
150 
151             // Update folder
152 
153             DLFolderServiceUtil.updateFolder(
154                 folderId, parentFolderId, name, description, serviceContext);
155         }
156     }
157 
158 }