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