1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.bookmarks.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.model.LayoutTypePortlet;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.bookmarks.EntryURLException;
32  import com.liferay.portlet.bookmarks.NoSuchEntryException;
33  import com.liferay.portlet.bookmarks.NoSuchFolderException;
34  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
35  import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
36  import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
37  import com.liferay.portlet.tags.TagsEntryException;
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 portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
63  
64          try {
65              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
66                  updateEntry(actionRequest);
67              }
68              else if (cmd.equals(Constants.DELETE)) {
69                  deleteEntry(actionRequest);
70              }
71  
72              ThemeDisplay themeDisplay =
73                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
74  
75              LayoutTypePortlet layoutTypePortlet =
76                  themeDisplay.getLayoutTypePortlet();
77  
78              if (layoutTypePortlet.hasPortletId(
79                      portletConfig.getPortletName())) {
80  
81                  sendRedirect(actionRequest, actionResponse);
82              }
83              else {
84                  String redirect = ParamUtil.getString(
85                      actionRequest, "redirect");
86  
87                  actionResponse.sendRedirect(redirect);
88              }
89          }
90          catch (Exception e) {
91              if (e instanceof NoSuchEntryException ||
92                  e instanceof PrincipalException) {
93  
94                  SessionErrors.add(actionRequest, e.getClass().getName());
95  
96                  setForward(actionRequest, "portlet.bookmarks.error");
97              }
98              else if (e instanceof EntryURLException ||
99                       e instanceof NoSuchFolderException) {
100 
101                 SessionErrors.add(actionRequest, e.getClass().getName());
102             }
103             else if (e instanceof TagsEntryException) {
104                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
105             }
106             else {
107                 throw e;
108             }
109         }
110     }
111 
112     public ActionForward render(
113             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
114             RenderRequest renderRequest, RenderResponse renderResponse)
115         throws Exception {
116 
117         try {
118             ActionUtil.getEntry(renderRequest);
119         }
120         catch (Exception e) {
121             if (e instanceof NoSuchEntryException ||
122                 e instanceof PrincipalException) {
123 
124                 SessionErrors.add(renderRequest, 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(renderRequest, "portlet.bookmarks.edit_entry"));
135     }
136 
137     protected void deleteEntry(ActionRequest actionRequest) throws Exception {
138         long entryId = ParamUtil.getLong(actionRequest, "entryId");
139 
140         BookmarksEntryServiceUtil.deleteEntry(entryId);
141     }
142 
143     protected void updateEntry(ActionRequest actionRequest) throws Exception {
144         long entryId = ParamUtil.getLong(actionRequest, "entryId");
145 
146         long folderId = ParamUtil.getLong(actionRequest, "folderId");
147         String name = ParamUtil.getString(actionRequest, "name");
148         String url = ParamUtil.getString(actionRequest, "url");
149         String comments = ParamUtil.getString(actionRequest, "comments");
150 
151         String[] tagsEntries = StringUtil.split(
152             ParamUtil.getString(actionRequest, "tagsEntries"));
153 
154         String[] communityPermissions = actionRequest.getParameterValues(
155             "communityPermissions");
156         String[] guestPermissions = actionRequest.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                 actionRequest, BookmarksEntry.class.getName(),
169                 entry.getEntryId(), -1);
170         }
171         else {
172 
173             // Update entry
174 
175             BookmarksEntryServiceUtil.updateEntry(
176                 entryId, folderId, name, url, comments, tagsEntries);
177         }
178 
179         AssetPublisherUtil.addRecentFolderId(
180             actionRequest, BookmarksEntry.class.getName(), folderId);
181     }
182 
183 }