1   /**
2    * Copyright (c) 2000-2009 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.journal.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.GetterUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.struts.PortletAction;
34  import com.liferay.portlet.journal.DuplicateFeedIdException;
35  import com.liferay.portlet.journal.FeedContentFieldException;
36  import com.liferay.portlet.journal.FeedDescriptionException;
37  import com.liferay.portlet.journal.FeedIdException;
38  import com.liferay.portlet.journal.FeedNameException;
39  import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
40  import com.liferay.portlet.journal.FeedTargetPortletIdException;
41  import com.liferay.portlet.journal.NoSuchFeedException;
42  import com.liferay.portlet.journal.service.JournalFeedServiceUtil;
43  import com.liferay.util.RSSUtil;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.RenderRequest;
49  import javax.portlet.RenderResponse;
50  
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="EditFeedAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Raymond Augé
59   *
60   */
61  public class EditFeedAction extends PortletAction {
62  
63      public void processAction(
64              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
65              ActionRequest actionRequest, ActionResponse actionResponse)
66          throws Exception {
67  
68          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
69  
70          try {
71              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
72                  updateFeed(actionRequest);
73              }
74              else if (cmd.equals(Constants.DELETE)) {
75                  deleteFeeds(actionRequest);
76              }
77  
78              sendRedirect(actionRequest, actionResponse);
79          }
80          catch (Exception e) {
81              if (e instanceof NoSuchFeedException ||
82                  e instanceof PrincipalException) {
83  
84                  SessionErrors.add(actionRequest, e.getClass().getName());
85  
86                  setForward(actionRequest, "portlet.journal.error");
87              }
88              else if (e instanceof DuplicateFeedIdException ||
89                       e instanceof FeedContentFieldException ||
90                       e instanceof FeedDescriptionException ||
91                       e instanceof FeedIdException ||
92                       e instanceof FeedNameException ||
93                       e instanceof FeedTargetLayoutFriendlyUrlException ||
94                       e instanceof FeedTargetPortletIdException) {
95  
96                  SessionErrors.add(actionRequest, e.getClass().getName());
97              }
98              else {
99                  throw e;
100             }
101         }
102     }
103 
104     public ActionForward render(
105             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
106             RenderRequest renderRequest, RenderResponse renderResponse)
107         throws Exception {
108 
109         try {
110             String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
111 
112             if (!cmd.equals(Constants.ADD)) {
113                 ActionUtil.getFeed(renderRequest);
114             }
115         }
116         catch (NoSuchFeedException nssfe) {
117 
118             // Let this slide because the user can manually input a feed id for
119             // a new syndicated feed that does not yet exist.
120 
121         }
122         catch (Exception e) {
123             if (e instanceof PrincipalException) {
124                 SessionErrors.add(renderRequest, e.getClass().getName());
125 
126                 return mapping.findForward("portlet.journal.error");
127             }
128             else {
129                 throw e;
130             }
131         }
132 
133         return mapping.findForward(
134             getForward(renderRequest, "portlet.journal.edit_feed"));
135     }
136 
137     protected void deleteFeeds(ActionRequest actionRequest) throws Exception {
138         long groupId = ParamUtil.getLong(actionRequest, "groupId");
139 
140         String[] deleteFeedIds = StringUtil.split(
141             ParamUtil.getString(actionRequest, "deleteFeedIds"));
142 
143         for (int i = 0; i < deleteFeedIds.length; i++) {
144             JournalFeedServiceUtil.deleteFeed(groupId, deleteFeedIds[i]);
145         }
146     }
147 
148     protected void updateFeed(ActionRequest actionRequest) throws Exception {
149         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
150 
151         long groupId = ParamUtil.getLong(actionRequest, "groupId");
152 
153         String feedId = ParamUtil.getString(actionRequest, "feedId");
154         boolean autoFeedId = ParamUtil.getBoolean(actionRequest, "autoFeedId");
155 
156         String name = ParamUtil.getString(actionRequest, "name");
157         String description = ParamUtil.getString(actionRequest, "description");
158         String type = ParamUtil.getString(actionRequest, "type");
159         String structureId = ParamUtil.getString(actionRequest, "structureId");
160         String templateId = ParamUtil.getString(actionRequest, "templateId");
161         String rendererTemplateId = ParamUtil.getString(
162             actionRequest, "rendererTemplateId");
163         int delta = ParamUtil.getInteger(actionRequest, "delta");
164         String orderByCol = ParamUtil.getString(actionRequest, "orderByCol");
165         String orderByType = ParamUtil.getString(actionRequest, "orderByType");
166         String targetLayoutFriendlyUrl = ParamUtil.getString(
167             actionRequest, "targetLayoutFriendlyUrl");
168         String targetPortletId = ParamUtil.getString(
169             actionRequest, "targetPortletId");
170         String contentField = ParamUtil.getString(
171             actionRequest, "contentField");
172 
173         String feedType = RSSUtil.DEFAULT_TYPE;
174         double feedVersion = RSSUtil.DEFAULT_VERSION;
175 
176         String feedTypeAndVersion = ParamUtil.getString(
177             actionRequest, "feedTypeAndVersion");
178 
179         if (Validator.isNotNull(feedTypeAndVersion)) {
180             String[] parts = feedTypeAndVersion.split(StringPool.COLON);
181 
182             try {
183                 feedType = parts[0];
184                 feedVersion = GetterUtil.getDouble(parts[1]);
185             }
186             catch (Exception e) {
187             }
188         }
189         else {
190             feedType = ParamUtil.getString(actionRequest, "feedType", feedType);
191             feedVersion = ParamUtil.getDouble(
192                 actionRequest, "feedVersion", feedVersion);
193         }
194 
195         String[] communityPermissions = actionRequest.getParameterValues(
196             "communityPermissions");
197         String[] guestPermissions = actionRequest.getParameterValues(
198             "guestPermissions");
199 
200         if (cmd.equals(Constants.ADD)) {
201 
202             // Add feed
203 
204             JournalFeedServiceUtil.addFeed(
205                 groupId, feedId, autoFeedId, name, description,
206                 type, structureId, templateId, rendererTemplateId, delta,
207                 orderByCol, orderByType, targetLayoutFriendlyUrl,
208                 targetPortletId, contentField, feedType, feedVersion,
209                 communityPermissions, guestPermissions);
210         }
211         else {
212 
213             // Update feed
214 
215             JournalFeedServiceUtil.updateFeed(
216                 groupId, feedId, name, description, type, structureId,
217                 templateId, rendererTemplateId, delta, orderByCol, orderByType,
218                 targetLayoutFriendlyUrl, targetPortletId, contentField,
219                 feedType, feedVersion);
220         }
221     }
222 
223 }