1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.journal.DuplicateFeedIdException;
34  import com.liferay.portlet.journal.FeedContentFieldException;
35  import com.liferay.portlet.journal.FeedDescriptionException;
36  import com.liferay.portlet.journal.FeedIdException;
37  import com.liferay.portlet.journal.FeedNameException;
38  import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
39  import com.liferay.portlet.journal.FeedTargetPortletIdException;
40  import com.liferay.portlet.journal.NoSuchFeedException;
41  import com.liferay.portlet.journal.service.JournalFeedServiceUtil;
42  import com.liferay.util.RSSUtil;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.PortletConfig;
47  import javax.portlet.RenderRequest;
48  import javax.portlet.RenderResponse;
49  
50  import org.apache.struts.action.ActionForm;
51  import org.apache.struts.action.ActionForward;
52  import org.apache.struts.action.ActionMapping;
53  
54  /**
55   * <a href="EditFeedAction.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Raymond Augé
58   *
59   */
60  public class EditFeedAction extends PortletAction {
61  
62      public void processAction(
63              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
64              ActionRequest actionRequest, ActionResponse actionResponse)
65          throws Exception {
66  
67          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
68  
69          try {
70              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
71                  updateFeed(actionRequest);
72              }
73              else if (cmd.equals(Constants.DELETE)) {
74                  deleteFeeds(actionRequest);
75              }
76  
77              sendRedirect(actionRequest, actionResponse);
78          }
79          catch (Exception e) {
80              if (e instanceof NoSuchFeedException ||
81                  e instanceof PrincipalException) {
82  
83                  SessionErrors.add(actionRequest, e.getClass().getName());
84  
85                  setForward(actionRequest, "portlet.journal.error");
86              }
87              else if (e instanceof DuplicateFeedIdException ||
88                       e instanceof FeedContentFieldException ||
89                       e instanceof FeedDescriptionException ||
90                       e instanceof FeedIdException ||
91                       e instanceof FeedNameException ||
92                       e instanceof FeedTargetLayoutFriendlyUrlException ||
93                       e instanceof FeedTargetPortletIdException) {
94  
95                  SessionErrors.add(actionRequest, e.getClass().getName());
96              }
97              else {
98                  throw e;
99              }
100         }
101     }
102 
103     public ActionForward render(
104             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105             RenderRequest renderRequest, RenderResponse renderResponse)
106         throws Exception {
107 
108         try {
109             String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
110 
111             if (!cmd.equals(Constants.ADD)) {
112                 ActionUtil.getFeed(renderRequest);
113             }
114         }
115         catch (NoSuchFeedException nssfe) {
116 
117             // Let this slide because the user can manually input a feed id for
118             // a new syndicated feed that does not yet exist.
119 
120         }
121         catch (Exception e) {
122             if (e instanceof PrincipalException) {
123                 SessionErrors.add(renderRequest, e.getClass().getName());
124 
125                 return mapping.findForward("portlet.journal.error");
126             }
127             else {
128                 throw e;
129             }
130         }
131 
132         return mapping.findForward(
133             getForward(renderRequest, "portlet.journal.edit_feed"));
134     }
135 
136     protected void deleteFeeds(ActionRequest actionRequest) throws Exception {
137         long groupId = ParamUtil.getLong(actionRequest, "groupId");
138 
139         String[] deleteFeedIds = StringUtil.split(
140             ParamUtil.getString(actionRequest, "deleteFeedIds"));
141 
142         for (int i = 0; i < deleteFeedIds.length; i++) {
143             JournalFeedServiceUtil.deleteFeed(groupId, deleteFeedIds[i]);
144         }
145     }
146 
147     protected void updateFeed(ActionRequest actionRequest) throws Exception {
148         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
149 
150         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
151 
152         long groupId = ParamUtil.getLong(actionRequest, "groupId");
153 
154         String feedId = ParamUtil.getString(actionRequest, "feedId");
155         boolean autoFeedId = ParamUtil.getBoolean(actionRequest, "autoFeedId");
156 
157         String name = ParamUtil.getString(actionRequest, "name");
158         String description = ParamUtil.getString(actionRequest, "description");
159         String type = ParamUtil.getString(actionRequest, "type");
160         String structureId = ParamUtil.getString(actionRequest, "structureId");
161         String templateId = ParamUtil.getString(actionRequest, "templateId");
162         String rendererTemplateId = ParamUtil.getString(
163             actionRequest, "rendererTemplateId");
164         int delta = ParamUtil.getInteger(actionRequest, "delta");
165         String orderByCol = ParamUtil.getString(actionRequest, "orderByCol");
166         String orderByType = ParamUtil.getString(actionRequest, "orderByType");
167         String targetLayoutFriendlyUrl = ParamUtil.getString(
168             actionRequest, "targetLayoutFriendlyUrl");
169         String targetPortletId = ParamUtil.getString(
170             actionRequest, "targetPortletId");
171         String contentField = ParamUtil.getString(
172             actionRequest, "contentField");
173 
174         String feedType = RSSUtil.DEFAULT_TYPE;
175         double feedVersion = RSSUtil.DEFAULT_VERSION;
176 
177         String feedTypeAndVersion = ParamUtil.getString(
178             actionRequest, "feedTypeAndVersion");
179 
180         if (Validator.isNotNull(feedTypeAndVersion)) {
181             String[] parts = feedTypeAndVersion.split(StringPool.COLON);
182 
183             try {
184                 feedType = parts[0];
185                 feedVersion = GetterUtil.getDouble(parts[1]);
186             }
187             catch (Exception e) {
188             }
189         }
190         else {
191             feedType = ParamUtil.getString(actionRequest, "feedType", feedType);
192             feedVersion = ParamUtil.getDouble(
193                 actionRequest, "feedVersion", feedVersion);
194         }
195 
196         String[] communityPermissions = actionRequest.getParameterValues(
197             "communityPermissions");
198         String[] guestPermissions = actionRequest.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 }