1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.service.ServiceContext;
31  import com.liferay.portal.service.ServiceContextFactory;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
34  import com.liferay.portlet.documentlibrary.FileShortcutPermissionException;
35  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
36  import com.liferay.portlet.documentlibrary.NoSuchFileShortcutException;
37  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
38  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="EditFileShortcutAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   */
55  public class EditFileShortcutAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
63  
64          try {
65              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
66                  updateFileShortcut(actionRequest);
67              }
68              else if (cmd.equals(Constants.DELETE)) {
69                  deleteFileShortcut(actionRequest);
70              }
71  
72              sendRedirect(actionRequest, actionResponse);
73          }
74          catch (Exception e) {
75              if (e instanceof NoSuchFileShortcutException ||
76                  e instanceof PrincipalException) {
77  
78                  SessionErrors.add(actionRequest, e.getClass().getName());
79  
80                  setForward(actionRequest, "portlet.document_library.error");
81              }
82              else if (e instanceof FileShortcutPermissionException ||
83                       e instanceof NoSuchFileEntryException) {
84  
85                  SessionErrors.add(actionRequest, e.getClass().getName());
86              }
87              else {
88                  throw e;
89              }
90          }
91      }
92  
93      public ActionForward render(
94              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
95              RenderRequest renderRequest, RenderResponse renderResponse)
96          throws Exception {
97  
98          try {
99              ActionUtil.getFileShortcut(renderRequest);
100         }
101         catch (Exception e) {
102             if (e instanceof NoSuchFileShortcutException ||
103                 e instanceof PrincipalException) {
104 
105                 SessionErrors.add(renderRequest, e.getClass().getName());
106 
107                 return mapping.findForward("portlet.document_library.error");
108             }
109             else {
110                 throw e;
111             }
112         }
113 
114         return mapping.findForward(getForward(
115             renderRequest, "portlet.document_library.edit_file_shortcut"));
116     }
117 
118     protected void deleteFileShortcut(ActionRequest actionRequest)
119         throws Exception {
120 
121         long fileShortcutId = ParamUtil.getLong(
122             actionRequest, "fileShortcutId");
123 
124         DLFileShortcutServiceUtil.deleteFileShortcut(fileShortcutId);
125     }
126 
127     protected void updateFileShortcut(ActionRequest actionRequest)
128         throws Exception {
129 
130         long fileShortcutId = ParamUtil.getLong(
131             actionRequest, "fileShortcutId");
132 
133         long folderId = ParamUtil.getLong(actionRequest, "folderId");
134         long toFolderId = ParamUtil.getLong(actionRequest, "toFolderId");
135         String toName = HtmlUtil.unescape(
136             ParamUtil.getString(actionRequest, "toName"));
137 
138         ServiceContext serviceContext = ServiceContextFactory.getInstance(
139             DLFileShortcut.class.getName(), actionRequest);
140 
141         if (fileShortcutId <= 0) {
142 
143             // Add file shortcut
144 
145             DLFileShortcut fileShortcut =
146                 DLFileShortcutServiceUtil.addFileShortcut(
147                     folderId, toFolderId, toName, serviceContext);
148 
149             AssetPublisherUtil.addAndStoreSelection(
150                 actionRequest, DLFileShortcut.class.getName(),
151                 fileShortcut.getFileShortcutId(), -1);
152         }
153         else {
154 
155             // Update file shortcut
156 
157             DLFileShortcutServiceUtil.updateFileShortcut(
158                 fileShortcutId, folderId, toFolderId, toName, serviceContext);
159 
160             AssetPublisherUtil.addRecentFolderId(
161                 actionRequest, DLFileShortcut.class.getName(), folderId);
162         }
163     }
164 
165 }