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