1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.security.auth.PrincipalException;
25  import com.liferay.portal.service.ServiceContext;
26  import com.liferay.portal.service.ServiceContextFactory;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portlet.journal.DuplicateFeedIdException;
29  import com.liferay.portlet.journal.FeedContentFieldException;
30  import com.liferay.portlet.journal.FeedDescriptionException;
31  import com.liferay.portlet.journal.FeedIdException;
32  import com.liferay.portlet.journal.FeedNameException;
33  import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
34  import com.liferay.portlet.journal.FeedTargetPortletIdException;
35  import com.liferay.portlet.journal.NoSuchFeedException;
36  import com.liferay.portlet.journal.model.JournalFeed;
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         long groupId = ParamUtil.getLong(actionRequest, "groupId");
146 
147         String feedId = ParamUtil.getString(actionRequest, "feedId");
148         boolean autoFeedId = ParamUtil.getBoolean(actionRequest, "autoFeedId");
149 
150         String name = ParamUtil.getString(actionRequest, "name");
151         String description = ParamUtil.getString(actionRequest, "description");
152         String type = ParamUtil.getString(actionRequest, "type");
153         String structureId = ParamUtil.getString(actionRequest, "structureId");
154         String templateId = ParamUtil.getString(actionRequest, "templateId");
155         String rendererTemplateId = ParamUtil.getString(
156             actionRequest, "rendererTemplateId");
157         int delta = ParamUtil.getInteger(actionRequest, "delta");
158         String orderByCol = ParamUtil.getString(actionRequest, "orderByCol");
159         String orderByType = ParamUtil.getString(actionRequest, "orderByType");
160         String targetLayoutFriendlyUrl = ParamUtil.getString(
161             actionRequest, "targetLayoutFriendlyUrl");
162         String targetPortletId = ParamUtil.getString(
163             actionRequest, "targetPortletId");
164         String contentField = ParamUtil.getString(
165             actionRequest, "contentField");
166 
167         String feedType = RSSUtil.DEFAULT_TYPE;
168         double feedVersion = RSSUtil.DEFAULT_VERSION;
169 
170         String feedTypeAndVersion = ParamUtil.getString(
171             actionRequest, "feedTypeAndVersion");
172 
173         if (Validator.isNotNull(feedTypeAndVersion)) {
174             String[] parts = feedTypeAndVersion.split(StringPool.COLON);
175 
176             try {
177                 feedType = parts[0];
178                 feedVersion = GetterUtil.getDouble(parts[1]);
179             }
180             catch (Exception e) {
181             }
182         }
183         else {
184             feedType = ParamUtil.getString(actionRequest, "feedType", feedType);
185             feedVersion = ParamUtil.getDouble(
186                 actionRequest, "feedVersion", feedVersion);
187         }
188 
189         ServiceContext serviceContext = ServiceContextFactory.getInstance(
190             JournalFeed.class.getName(), actionRequest);
191 
192         if (cmd.equals(Constants.ADD)) {
193 
194             // Add feed
195 
196             JournalFeedServiceUtil.addFeed(
197                 groupId, feedId, autoFeedId, name, description, type,
198                 structureId, templateId, rendererTemplateId, delta, orderByCol,
199                 orderByType, targetLayoutFriendlyUrl, targetPortletId,
200                 contentField, feedType, feedVersion, serviceContext);
201         }
202         else {
203 
204             // Update feed
205 
206             JournalFeedServiceUtil.updateFeed(
207                 groupId, feedId, name, description, type, structureId,
208                 templateId, rendererTemplateId, delta, orderByCol, orderByType,
209                 targetLayoutFriendlyUrl, targetPortletId, contentField,
210                 feedType, feedVersion, serviceContext);
211         }
212     }
213 
214 }