1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portlet.documentlibrary.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.HtmlUtil;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portlet.documentlibrary.FileShortcutPermissionException;
25  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
26  import com.liferay.portlet.documentlibrary.NoSuchFileShortcutException;
27  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.ActionResponse;
31  import javax.portlet.PortletConfig;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionForward;
37  import org.apache.struts.action.ActionMapping;
38  
39  /**
40   * <a href="EditFileShortcutAction.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class EditFileShortcutAction extends PortletAction {
45  
46      public void processAction(
47              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
48              ActionRequest actionRequest, ActionResponse actionResponse)
49          throws Exception {
50  
51          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
52  
53          try {
54              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
55                  updateFileShortcut(actionRequest);
56              }
57              else if (cmd.equals(Constants.DELETE)) {
58                  deleteFileShortcut(actionRequest);
59              }
60  
61              sendRedirect(actionRequest, actionResponse);
62          }
63          catch (Exception e) {
64              if (e instanceof NoSuchFileShortcutException ||
65                  e instanceof PrincipalException) {
66  
67                  SessionErrors.add(actionRequest, e.getClass().getName());
68  
69                  setForward(actionRequest, "portlet.document_library.error");
70              }
71              else if (e instanceof FileShortcutPermissionException ||
72                       e instanceof NoSuchFileEntryException) {
73  
74                  SessionErrors.add(actionRequest, e.getClass().getName());
75              }
76              else {
77                  throw e;
78              }
79          }
80      }
81  
82      public ActionForward render(
83              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
84              RenderRequest renderRequest, RenderResponse renderResponse)
85          throws Exception {
86  
87          try {
88              ActionUtil.getFileShortcut(renderRequest);
89          }
90          catch (Exception e) {
91              if (e instanceof NoSuchFileShortcutException ||
92                  e instanceof PrincipalException) {
93  
94                  SessionErrors.add(renderRequest, e.getClass().getName());
95  
96                  return mapping.findForward("portlet.document_library.error");
97              }
98              else {
99                  throw e;
100             }
101         }
102 
103         return mapping.findForward(getForward(
104             renderRequest, "portlet.document_library.edit_file_shortcut"));
105     }
106 
107     protected void deleteFileShortcut(ActionRequest actionRequest)
108         throws Exception {
109 
110         long fileShortcutId = ParamUtil.getLong(
111             actionRequest, "fileShortcutId");
112 
113         DLFileShortcutServiceUtil.deleteFileShortcut(fileShortcutId);
114     }
115 
116     protected void updateFileShortcut(ActionRequest actionRequest)
117         throws Exception {
118 
119         long fileShortcutId = ParamUtil.getLong(
120             actionRequest, "fileShortcutId");
121 
122         long folderId = ParamUtil.getLong(actionRequest, "folderId");
123         long toFolderId = ParamUtil.getLong(actionRequest, "toFolderId");
124         String toName = HtmlUtil.unescape(
125             ParamUtil.getString(actionRequest, "toName"));
126 
127         String[] communityPermissions = PortalUtil.getCommunityPermissions(
128             actionRequest);
129         String[] guestPermissions = PortalUtil.getGuestPermissions(
130             actionRequest);
131 
132         if (fileShortcutId <= 0) {
133 
134             // Add file shortcut
135 
136             DLFileShortcutServiceUtil.addFileShortcut(
137                 folderId, toFolderId, toName, communityPermissions,
138                 guestPermissions);
139         }
140         else {
141 
142             // Update file shortcut
143 
144             DLFileShortcutServiceUtil.updateFileShortcut(
145                 fileShortcutId, folderId, toFolderId, toName);
146         }
147     }
148 
149 }