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