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.bookmarks.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.ParamUtil;
20  import com.liferay.portal.model.LayoutTypePortlet;
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.asset.AssetTagException;
28  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
29  import com.liferay.portlet.bookmarks.EntryURLException;
30  import com.liferay.portlet.bookmarks.NoSuchEntryException;
31  import com.liferay.portlet.bookmarks.NoSuchFolderException;
32  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
33  import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="EditEntryAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   */
50  public class EditEntryAction extends PortletAction {
51  
52      public void processAction(
53              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54              ActionRequest actionRequest, ActionResponse actionResponse)
55          throws Exception {
56  
57          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
58  
59          try {
60              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
61                  updateEntry(actionRequest);
62              }
63              else if (cmd.equals(Constants.DELETE)) {
64                  deleteEntry(actionRequest);
65              }
66  
67              ThemeDisplay themeDisplay =
68                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
69  
70              LayoutTypePortlet layoutTypePortlet =
71                  themeDisplay.getLayoutTypePortlet();
72  
73              if (layoutTypePortlet.hasPortletId(
74                      portletConfig.getPortletName())) {
75  
76                  sendRedirect(actionRequest, actionResponse);
77              }
78              else {
79                  String redirect = ParamUtil.getString(
80                      actionRequest, "redirect");
81  
82                  actionResponse.sendRedirect(redirect);
83              }
84          }
85          catch (Exception e) {
86              if (e instanceof NoSuchEntryException ||
87                  e instanceof PrincipalException) {
88  
89                  SessionErrors.add(actionRequest, e.getClass().getName());
90  
91                  setForward(actionRequest, "portlet.bookmarks.error");
92              }
93              else if (e instanceof EntryURLException ||
94                       e instanceof NoSuchFolderException) {
95  
96                  SessionErrors.add(actionRequest, e.getClass().getName());
97              }
98              else if (e instanceof AssetTagException) {
99                  SessionErrors.add(actionRequest, e.getClass().getName(), e);
100             }
101             else {
102                 throw e;
103             }
104         }
105     }
106 
107     public ActionForward render(
108             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
109             RenderRequest renderRequest, RenderResponse renderResponse)
110         throws Exception {
111 
112         try {
113             ActionUtil.getEntry(renderRequest);
114         }
115         catch (Exception e) {
116             if (e instanceof NoSuchEntryException ||
117                 e instanceof PrincipalException) {
118 
119                 SessionErrors.add(renderRequest, e.getClass().getName());
120 
121                 return mapping.findForward("portlet.bookmarks.error");
122             }
123             else {
124                 throw e;
125             }
126         }
127 
128         return mapping.findForward(
129             getForward(renderRequest, "portlet.bookmarks.edit_entry"));
130     }
131 
132     protected void deleteEntry(ActionRequest actionRequest) throws Exception {
133         long entryId = ParamUtil.getLong(actionRequest, "entryId");
134 
135         BookmarksEntryServiceUtil.deleteEntry(entryId);
136     }
137 
138     protected void updateEntry(ActionRequest actionRequest) throws Exception {
139         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
140             WebKeys.THEME_DISPLAY);
141 
142         long entryId = ParamUtil.getLong(actionRequest, "entryId");
143 
144         long groupId = themeDisplay.getScopeGroupId();
145         long folderId = ParamUtil.getLong(actionRequest, "folderId");
146         String name = ParamUtil.getString(actionRequest, "name");
147         String url = ParamUtil.getString(actionRequest, "url");
148         String comments = ParamUtil.getString(actionRequest, "comments");
149 
150         ServiceContext serviceContext = ServiceContextFactory.getInstance(
151             BookmarksEntry.class.getName(), actionRequest);
152 
153         if (entryId <= 0) {
154 
155             // Add entry
156 
157             BookmarksEntry entry = BookmarksEntryServiceUtil.addEntry(
158                 groupId, folderId, name, url, comments, serviceContext);
159 
160             AssetPublisherUtil.addAndStoreSelection(
161                 actionRequest, BookmarksEntry.class.getName(),
162                 entry.getEntryId(), -1);
163         }
164         else {
165 
166             // Update entry
167 
168             BookmarksEntryServiceUtil.updateEntry(
169                 entryId, groupId, folderId, name, url, comments,
170                 serviceContext);
171         }
172 
173         AssetPublisherUtil.addRecentFolderId(
174             actionRequest, BookmarksEntry.class.getName(), folderId);
175     }
176 
177 }