1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
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.model.DLFileShortcut;
32  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="EditFileShortcutAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
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         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
125             WebKeys.THEME_DISPLAY);
126 
127         long fileShortcutId = ParamUtil.getLong(
128             actionRequest, "fileShortcutId");
129 
130         long groupId = themeDisplay.getScopeGroupId();
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         ServiceContext serviceContext = ServiceContextFactory.getInstance(
137             DLFileShortcut.class.getName(), actionRequest);
138 
139         if (fileShortcutId <= 0) {
140 
141             // Add file shortcut
142 
143             DLFileShortcut fileShortcut =
144                 DLFileShortcutServiceUtil.addFileShortcut(
145                     groupId, folderId, toFolderId, toName, serviceContext);
146 
147             AssetPublisherUtil.addAndStoreSelection(
148                 actionRequest, DLFileShortcut.class.getName(),
149                 fileShortcut.getFileShortcutId(), -1);
150         }
151         else {
152 
153             // Update file shortcut
154 
155             DLFileShortcutServiceUtil.updateFileShortcut(
156                 fileShortcutId, folderId, toFolderId, toName, serviceContext);
157 
158             AssetPublisherUtil.addRecentFolderId(
159                 actionRequest, DLFileShortcut.class.getName(), folderId);
160         }
161     }
162 
163 }