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