1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.bookmarks.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.ParamUtil;
28  import com.liferay.portal.model.LayoutTypePortlet;
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.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
36  import com.liferay.portlet.bookmarks.EntryURLException;
37  import com.liferay.portlet.bookmarks.NoSuchEntryException;
38  import com.liferay.portlet.bookmarks.NoSuchFolderException;
39  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
40  import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
41  import com.liferay.portlet.tags.TagsEntryException;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionForward;
51  import org.apache.struts.action.ActionMapping;
52  
53  /**
54   * <a href="EditEntryAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   *
58   */
59  public class EditEntryAction extends PortletAction {
60  
61      public void processAction(
62              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
63              ActionRequest actionRequest, ActionResponse actionResponse)
64          throws Exception {
65  
66          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
67  
68          try {
69              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
70                  updateEntry(actionRequest);
71              }
72              else if (cmd.equals(Constants.DELETE)) {
73                  deleteEntry(actionRequest);
74              }
75  
76              ThemeDisplay themeDisplay =
77                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
78  
79              LayoutTypePortlet layoutTypePortlet =
80                  themeDisplay.getLayoutTypePortlet();
81  
82              if (layoutTypePortlet.hasPortletId(
83                      portletConfig.getPortletName())) {
84  
85                  sendRedirect(actionRequest, actionResponse);
86              }
87              else {
88                  String redirect = ParamUtil.getString(
89                      actionRequest, "redirect");
90  
91                  actionResponse.sendRedirect(redirect);
92              }
93          }
94          catch (Exception e) {
95              if (e instanceof NoSuchEntryException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(actionRequest, e.getClass().getName());
99  
100                 setForward(actionRequest, "portlet.bookmarks.error");
101             }
102             else if (e instanceof EntryURLException ||
103                      e instanceof NoSuchFolderException) {
104 
105                 SessionErrors.add(actionRequest, e.getClass().getName());
106             }
107             else if (e instanceof TagsEntryException) {
108                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
109             }
110             else {
111                 throw e;
112             }
113         }
114     }
115 
116     public ActionForward render(
117             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
118             RenderRequest renderRequest, RenderResponse renderResponse)
119         throws Exception {
120 
121         try {
122             ActionUtil.getEntry(renderRequest);
123         }
124         catch (Exception e) {
125             if (e instanceof NoSuchEntryException ||
126                 e instanceof PrincipalException) {
127 
128                 SessionErrors.add(renderRequest, e.getClass().getName());
129 
130                 return mapping.findForward("portlet.bookmarks.error");
131             }
132             else {
133                 throw e;
134             }
135         }
136 
137         return mapping.findForward(
138             getForward(renderRequest, "portlet.bookmarks.edit_entry"));
139     }
140 
141     protected void deleteEntry(ActionRequest actionRequest) throws Exception {
142         long entryId = ParamUtil.getLong(actionRequest, "entryId");
143 
144         BookmarksEntryServiceUtil.deleteEntry(entryId);
145     }
146 
147     protected void updateEntry(ActionRequest actionRequest) throws Exception {
148         long entryId = ParamUtil.getLong(actionRequest, "entryId");
149 
150         long folderId = ParamUtil.getLong(actionRequest, "folderId");
151         String name = ParamUtil.getString(actionRequest, "name");
152         String url = ParamUtil.getString(actionRequest, "url");
153         String comments = ParamUtil.getString(actionRequest, "comments");
154 
155         ServiceContext serviceContext = ServiceContextFactory.getInstance(
156             BookmarksEntry.class.getName(), actionRequest);
157 
158         if (entryId <= 0) {
159 
160             // Add entry
161 
162             BookmarksEntry entry = BookmarksEntryServiceUtil.addEntry(
163                 folderId, name, url, comments, serviceContext);
164 
165             AssetPublisherUtil.addAndStoreSelection(
166                 actionRequest, BookmarksEntry.class.getName(),
167                 entry.getEntryId(), -1);
168         }
169         else {
170 
171             // Update entry
172 
173             BookmarksEntryServiceUtil.updateEntry(
174                 entryId, folderId, name, url, comments, serviceContext);
175         }
176 
177         AssetPublisherUtil.addRecentFolderId(
178             actionRequest, BookmarksEntry.class.getName(), folderId);
179     }
180 
181 }