001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.ServiceContextFactory;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portlet.journal.DuplicateFeedIdException;
027    import com.liferay.portlet.journal.FeedContentFieldException;
028    import com.liferay.portlet.journal.FeedIdException;
029    import com.liferay.portlet.journal.FeedNameException;
030    import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
031    import com.liferay.portlet.journal.FeedTargetPortletIdException;
032    import com.liferay.portlet.journal.NoSuchFeedException;
033    import com.liferay.portlet.journal.model.JournalFeed;
034    import com.liferay.portlet.journal.service.JournalFeedServiceUtil;
035    import com.liferay.util.RSSUtil;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.RenderRequest;
041    import javax.portlet.RenderResponse;
042    
043    import org.apache.struts.action.ActionForm;
044    import org.apache.struts.action.ActionForward;
045    import org.apache.struts.action.ActionMapping;
046    
047    /**
048     * @author Raymond Aug??
049     */
050    public class EditFeedAction extends PortletAction {
051    
052            @Override
053            public void processAction(
054                            ActionMapping actionMapping, ActionForm actionForm,
055                            PortletConfig portletConfig, ActionRequest actionRequest,
056                            ActionResponse actionResponse)
057                    throws Exception {
058    
059                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
060    
061                    if (Validator.isNull(cmd)) {
062                            return;
063                    }
064    
065                    try {
066                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
067                                    updateFeed(actionRequest);
068                            }
069                            else if (cmd.equals(Constants.DELETE)) {
070                                    deleteFeeds(actionRequest);
071                            }
072    
073                            sendRedirect(actionRequest, actionResponse);
074                    }
075                    catch (Exception e) {
076                            if (e instanceof NoSuchFeedException ||
077                                    e instanceof PrincipalException) {
078    
079                                    SessionErrors.add(actionRequest, e.getClass());
080    
081                                    setForward(actionRequest, "portlet.journal.error");
082                            }
083                            else if (e instanceof DuplicateFeedIdException ||
084                                             e instanceof FeedContentFieldException ||
085                                             e instanceof FeedIdException ||
086                                             e instanceof FeedNameException ||
087                                             e instanceof FeedTargetLayoutFriendlyUrlException ||
088                                             e instanceof FeedTargetPortletIdException) {
089    
090                                    SessionErrors.add(actionRequest, e.getClass());
091                            }
092                            else {
093                                    throw e;
094                            }
095                    }
096            }
097    
098            @Override
099            public ActionForward render(
100                            ActionMapping actionMapping, ActionForm actionForm,
101                            PortletConfig portletConfig, RenderRequest renderRequest,
102                            RenderResponse renderResponse)
103                    throws Exception {
104    
105                    try {
106                            String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
107    
108                            if (!cmd.equals(Constants.ADD)) {
109                                    ActionUtil.getFeed(renderRequest);
110                            }
111                    }
112                    catch (NoSuchFeedException nsfe) {
113    
114                            // Let this slide because the user can manually input a feed id for
115                            // a new syndicated feed that does not yet exist.
116    
117                    }
118                    catch (Exception e) {
119                            if (e instanceof PrincipalException) {
120                                    SessionErrors.add(renderRequest, e.getClass());
121    
122                                    return actionMapping.findForward("portlet.journal.error");
123                            }
124                            else {
125                                    throw e;
126                            }
127                    }
128    
129                    return actionMapping.findForward(
130                            getForward(renderRequest, "portlet.journal.edit_feed"));
131            }
132    
133            protected void deleteFeeds(ActionRequest actionRequest) throws Exception {
134                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
135    
136                    String[] deleteFeedIds = StringUtil.split(
137                            ParamUtil.getString(actionRequest, "deleteFeedIds"));
138    
139                    for (int i = 0; i < deleteFeedIds.length; i++) {
140                            JournalFeedServiceUtil.deleteFeed(groupId, deleteFeedIds[i]);
141                    }
142            }
143    
144            protected void updateFeed(ActionRequest actionRequest) throws Exception {
145                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
146    
147                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
148    
149                    String feedId = ParamUtil.getString(actionRequest, "feedId");
150                    boolean autoFeedId = ParamUtil.getBoolean(actionRequest, "autoFeedId");
151    
152                    String name = ParamUtil.getString(actionRequest, "name");
153                    String description = ParamUtil.getString(actionRequest, "description");
154                    String type = ParamUtil.getString(actionRequest, "type");
155                    String structureId = ParamUtil.getString(actionRequest, "structureId");
156                    String templateId = ParamUtil.getString(actionRequest, "templateId");
157                    String rendererTemplateId = ParamUtil.getString(
158                            actionRequest, "rendererTemplateId");
159                    int delta = ParamUtil.getInteger(actionRequest, "delta");
160                    String orderByCol = ParamUtil.getString(actionRequest, "orderByCol");
161                    String orderByType = ParamUtil.getString(actionRequest, "orderByType");
162                    String targetLayoutFriendlyUrl = ParamUtil.getString(
163                            actionRequest, "targetLayoutFriendlyUrl");
164                    String targetPortletId = ParamUtil.getString(
165                            actionRequest, "targetPortletId");
166                    String contentField = ParamUtil.getString(
167                            actionRequest, "contentField");
168                    String feedType = ParamUtil.getString(
169                            actionRequest, "feedType", RSSUtil.FEED_TYPE_DEFAULT);
170    
171                    String feedFormat = RSSUtil.getFeedTypeFormat(feedType);
172                    double feedVersion = RSSUtil.getFeedTypeVersion(feedType);
173    
174                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
175                            JournalFeed.class.getName(), actionRequest);
176    
177                    if (cmd.equals(Constants.ADD)) {
178    
179                            // Add feed
180    
181                            JournalFeedServiceUtil.addFeed(
182                                    groupId, feedId, autoFeedId, name, description, type,
183                                    structureId, templateId, rendererTemplateId, delta, orderByCol,
184                                    orderByType, targetLayoutFriendlyUrl, targetPortletId,
185                                    contentField, feedFormat, feedVersion, serviceContext);
186                    }
187                    else {
188    
189                            // Update feed
190    
191                            JournalFeedServiceUtil.updateFeed(
192                                    groupId, feedId, name, description, type, structureId,
193                                    templateId, rendererTemplateId, delta, orderByCol, orderByType,
194                                    targetLayoutFriendlyUrl, targetPortletId, contentField,
195                                    feedFormat, feedVersion, serviceContext);
196                    }
197            }
198    
199    }