1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.HtmlUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portlet.documentlibrary.FileShortcutPermissionException;
33  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
34  import com.liferay.portlet.documentlibrary.NoSuchFileShortcutException;
35  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditFileShortcutAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class EditFileShortcutAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
61  
62          try {
63              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
64                  updateFileShortcut(actionRequest);
65              }
66              else if (cmd.equals(Constants.DELETE)) {
67                  deleteFileShortcut(actionRequest);
68              }
69  
70              sendRedirect(actionRequest, actionResponse);
71          }
72          catch (Exception e) {
73              if (e instanceof NoSuchFileShortcutException ||
74                  e instanceof PrincipalException) {
75  
76                  SessionErrors.add(actionRequest, e.getClass().getName());
77  
78                  setForward(actionRequest, "portlet.document_library.error");
79              }
80              else if (e instanceof FileShortcutPermissionException ||
81                       e instanceof NoSuchFileEntryException) {
82  
83                  SessionErrors.add(actionRequest, e.getClass().getName());
84              }
85              else {
86                  throw e;
87              }
88          }
89      }
90  
91      public ActionForward render(
92              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
93              RenderRequest renderRequest, RenderResponse renderResponse)
94          throws Exception {
95  
96          try {
97              ActionUtil.getFileShortcut(renderRequest);
98          }
99          catch (Exception e) {
100             if (e instanceof NoSuchFileShortcutException ||
101                 e instanceof PrincipalException) {
102 
103                 SessionErrors.add(renderRequest, e.getClass().getName());
104 
105                 return mapping.findForward("portlet.document_library.error");
106             }
107             else {
108                 throw e;
109             }
110         }
111 
112         return mapping.findForward(getForward(
113             renderRequest, "portlet.document_library.edit_file_shortcut"));
114     }
115 
116     protected void deleteFileShortcut(ActionRequest actionRequest)
117         throws Exception {
118 
119         long fileShortcutId = ParamUtil.getLong(
120             actionRequest, "fileShortcutId");
121 
122         DLFileShortcutServiceUtil.deleteFileShortcut(fileShortcutId);
123     }
124 
125     protected void updateFileShortcut(ActionRequest actionRequest)
126         throws Exception {
127 
128         long fileShortcutId = ParamUtil.getLong(
129             actionRequest, "fileShortcutId");
130 
131         long folderId = ParamUtil.getLong(actionRequest, "folderId");
132         long toFolderId = ParamUtil.getLong(actionRequest, "toFolderId");
133         String toName = HtmlUtil.unescape(
134             ParamUtil.getString(actionRequest, "toName"));
135 
136         String[] communityPermissions = PortalUtil.getCommunityPermissions(
137             actionRequest);
138         String[] guestPermissions = PortalUtil.getGuestPermissions(
139             actionRequest);
140 
141         if (fileShortcutId <= 0) {
142 
143             // Add file shortcut
144 
145             DLFileShortcutServiceUtil.addFileShortcut(
146                 folderId, toFolderId, toName, communityPermissions,
147                 guestPermissions);
148         }
149         else {
150 
151             // Update file shortcut
152 
153             DLFileShortcutServiceUtil.updateFileShortcut(
154                 fileShortcutId, folderId, toFolderId, toName);
155         }
156     }
157 
158 }