1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.bookmarks.EntryURLException;
36  import com.liferay.portlet.bookmarks.NoSuchEntryException;
37  import com.liferay.portlet.bookmarks.NoSuchFolderException;
38  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
39  import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
40  import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
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  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 = PortalUtil.getCommunityPermissions(
158             actionRequest);
159         String[] guestPermissions = PortalUtil.getGuestPermissions(
160             actionRequest);
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 }