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