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