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.util.Constants;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Layout;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.struts.PortletAction;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.journal.DuplicateFeedIdException;
36  import com.liferay.portlet.journal.FeedContentFieldException;
37  import com.liferay.portlet.journal.FeedDescriptionException;
38  import com.liferay.portlet.journal.FeedIdException;
39  import com.liferay.portlet.journal.FeedNameException;
40  import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
41  import com.liferay.portlet.journal.FeedTargetPortletIdException;
42  import com.liferay.portlet.journal.NoSuchFeedException;
43  import com.liferay.portlet.journal.service.JournalFeedServiceUtil;
44  import com.liferay.util.RSSUtil;
45  import com.liferay.util.servlet.SessionErrors;
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 config,
67              ActionRequest req, ActionResponse res)
68          throws Exception {
69  
70          String cmd = ParamUtil.getString(req, Constants.CMD);
71  
72          try {
73              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
74                  updateFeed(req);
75              }
76              else if (cmd.equals(Constants.DELETE)) {
77                  deleteFeeds(req);
78              }
79  
80              sendRedirect(req, res);
81          }
82          catch (Exception e) {
83              if (e instanceof NoSuchFeedException ||
84                  e instanceof PrincipalException) {
85  
86                  SessionErrors.add(req, e.getClass().getName());
87  
88                  setForward(req, "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(req, e.getClass().getName());
99              }
100             else {
101                 throw e;
102             }
103         }
104     }
105 
106     public ActionForward render(
107             ActionMapping mapping, ActionForm form, PortletConfig config,
108             RenderRequest req, RenderResponse res)
109         throws Exception {
110 
111         try {
112             String cmd = ParamUtil.getString(req, Constants.CMD);
113 
114             if (!cmd.equals(Constants.ADD)) {
115                 ActionUtil.getFeed(req);
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(req, 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(req, "portlet.journal.edit_feed"));
137     }
138 
139     protected void deleteFeeds(ActionRequest req) throws Exception {
140         long groupId = ParamUtil.getLong(req, "groupId");
141 
142         String[] deleteFeedIds = StringUtil.split(
143             ParamUtil.getString(req, "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 req) throws Exception {
151         String cmd = ParamUtil.getString(req, Constants.CMD);
152 
153         Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
154 
155         long groupId = ParamUtil.getLong(req, "groupId");
156 
157         String feedId = ParamUtil.getString(req, "feedId");
158         boolean autoFeedId = ParamUtil.getBoolean(req, "autoFeedId");
159 
160         String name = ParamUtil.getString(req, "name");
161         String description = ParamUtil.getString(req, "description");
162         String type = ParamUtil.getString(req, "type");
163         String structureId = ParamUtil.getString(req, "structureId");
164         String templateId = ParamUtil.getString(req, "templateId");
165         String rendererTemplateId = ParamUtil.getString(
166             req, "rendererTemplateId");
167         int delta = ParamUtil.getInteger(req, "delta");
168         String orderByCol = ParamUtil.getString(req, "orderByCol");
169         String orderByType = ParamUtil.getString(req, "orderByType");
170         String targetLayoutFriendlyUrl = ParamUtil.getString(
171             req, "targetLayoutFriendlyUrl");
172         String targetPortletId = ParamUtil.getString(req, "targetPortletId");
173         String contentField = ParamUtil.getString(req, "contentField");
174 
175         String feedType = RSSUtil.DEFAULT_TYPE;
176         double feedVersion = RSSUtil.DEFAULT_VERSION;
177 
178         String feedTypeAndVersion = ParamUtil.getString(
179             req, "feedTypeAndVersion");
180 
181         if (Validator.isNotNull(feedTypeAndVersion)) {
182             String[] parts = feedTypeAndVersion.split(StringPool.COLON);
183 
184             try {
185                 feedType = parts[0];
186                 feedVersion = GetterUtil.getDouble(parts[1]);
187             }
188             catch (Exception e) {
189             }
190         }
191         else {
192             feedType = ParamUtil.getString(req, "feedType", feedType);
193             feedVersion = ParamUtil.getDouble(req, "feedVersion", feedVersion);
194         }
195 
196         String[] communityPermissions = req.getParameterValues(
197             "communityPermissions");
198         String[] guestPermissions = req.getParameterValues(
199             "guestPermissions");
200 
201         if (cmd.equals(Constants.ADD)) {
202 
203             // Add feed
204 
205             JournalFeedServiceUtil.addFeed(
206                 layout.getPlid(), feedId, autoFeedId, name, description,
207                 type, structureId, templateId, rendererTemplateId, delta,
208                 orderByCol, orderByType, targetLayoutFriendlyUrl,
209                 targetPortletId, contentField, feedType, feedVersion,
210                 communityPermissions, guestPermissions);
211         }
212         else {
213 
214             // Update feed
215 
216             JournalFeedServiceUtil.updateFeed(
217                 groupId, feedId, name, description, type, structureId,
218                 templateId, rendererTemplateId, delta, orderByCol, orderByType,
219                 targetLayoutFriendlyUrl, targetPortletId, contentField,
220                 feedType, feedVersion);
221         }
222     }
223 
224 }