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