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.service.ServiceContext;
34  import com.liferay.portal.service.ServiceContextFactory;
35  import com.liferay.portal.struts.PortletAction;
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.model.JournalFeed;
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         long groupId = ParamUtil.getLong(actionRequest, "groupId");
155 
156         String feedId = ParamUtil.getString(actionRequest, "feedId");
157         boolean autoFeedId = ParamUtil.getBoolean(actionRequest, "autoFeedId");
158 
159         String name = ParamUtil.getString(actionRequest, "name");
160         String description = ParamUtil.getString(actionRequest, "description");
161         String type = ParamUtil.getString(actionRequest, "type");
162         String structureId = ParamUtil.getString(actionRequest, "structureId");
163         String templateId = ParamUtil.getString(actionRequest, "templateId");
164         String rendererTemplateId = ParamUtil.getString(
165             actionRequest, "rendererTemplateId");
166         int delta = ParamUtil.getInteger(actionRequest, "delta");
167         String orderByCol = ParamUtil.getString(actionRequest, "orderByCol");
168         String orderByType = ParamUtil.getString(actionRequest, "orderByType");
169         String targetLayoutFriendlyUrl = ParamUtil.getString(
170             actionRequest, "targetLayoutFriendlyUrl");
171         String targetPortletId = ParamUtil.getString(
172             actionRequest, "targetPortletId");
173         String contentField = ParamUtil.getString(
174             actionRequest, "contentField");
175 
176         String feedType = RSSUtil.DEFAULT_TYPE;
177         double feedVersion = RSSUtil.DEFAULT_VERSION;
178 
179         String feedTypeAndVersion = ParamUtil.getString(
180             actionRequest, "feedTypeAndVersion");
181 
182         if (Validator.isNotNull(feedTypeAndVersion)) {
183             String[] parts = feedTypeAndVersion.split(StringPool.COLON);
184 
185             try {
186                 feedType = parts[0];
187                 feedVersion = GetterUtil.getDouble(parts[1]);
188             }
189             catch (Exception e) {
190             }
191         }
192         else {
193             feedType = ParamUtil.getString(actionRequest, "feedType", feedType);
194             feedVersion = ParamUtil.getDouble(
195                 actionRequest, "feedVersion", feedVersion);
196         }
197 
198         ServiceContext serviceContext = ServiceContextFactory.getInstance(
199             JournalFeed.class.getName(), actionRequest);
200 
201         if (cmd.equals(Constants.ADD)) {
202 
203             // Add feed
204 
205             JournalFeedServiceUtil.addFeed(
206                 groupId, feedId, autoFeedId, name, description, type,
207                 structureId, templateId, rendererTemplateId, delta, orderByCol,
208                 orderByType, targetLayoutFriendlyUrl, targetPortletId,
209                 contentField, feedType, feedVersion, serviceContext);
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, serviceContext);
220         }
221     }
222 
223 }