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