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