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 mapping, ActionForm form, PortletConfig portletConfig,
055                            ActionRequest actionRequest, ActionResponse actionResponse)
056                    throws Exception {
057    
058                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
059    
060                    if (Validator.isNull(cmd)) {
061                            return;
062                    }
063    
064                    try {
065                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
066                                    updateFeed(actionRequest);
067                            }
068                            else if (cmd.equals(Constants.DELETE)) {
069                                    deleteFeeds(actionRequest);
070                            }
071    
072                            sendRedirect(actionRequest, actionResponse);
073                    }
074                    catch (Exception e) {
075                            if (e instanceof NoSuchFeedException ||
076                                    e instanceof PrincipalException) {
077    
078                                    SessionErrors.add(actionRequest, e.getClass());
079    
080                                    setForward(actionRequest, "portlet.journal.error");
081                            }
082                            else if (e instanceof DuplicateFeedIdException ||
083                                             e instanceof FeedContentFieldException ||
084                                             e instanceof FeedIdException ||
085                                             e instanceof FeedNameException ||
086                                             e instanceof FeedTargetLayoutFriendlyUrlException ||
087                                             e instanceof FeedTargetPortletIdException) {
088    
089                                    SessionErrors.add(actionRequest, e.getClass());
090                            }
091                            else {
092                                    throw e;
093                            }
094                    }
095            }
096    
097            @Override
098            public ActionForward render(
099                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
100                            RenderRequest renderRequest, RenderResponse renderResponse)
101                    throws Exception {
102    
103                    try {
104                            String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
105    
106                            if (!cmd.equals(Constants.ADD)) {
107                                    ActionUtil.getFeed(renderRequest);
108                            }
109                    }
110                    catch (NoSuchFeedException nsfe) {
111    
112                            // Let this slide because the user can manually input a feed id for
113                            // a new syndicated feed that does not yet exist.
114    
115                    }
116                    catch (Exception e) {
117                            if (e instanceof PrincipalException) {
118                                    SessionErrors.add(renderRequest, e.getClass());
119    
120                                    return mapping.findForward("portlet.journal.error");
121                            }
122                            else {
123                                    throw e;
124                            }
125                    }
126    
127                    return mapping.findForward(
128                            getForward(renderRequest, "portlet.journal.edit_feed"));
129            }
130    
131            protected void deleteFeeds(ActionRequest actionRequest) throws Exception {
132                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
133    
134                    String[] deleteFeedIds = StringUtil.split(
135                            ParamUtil.getString(actionRequest, "deleteFeedIds"));
136    
137                    for (int i = 0; i < deleteFeedIds.length; i++) {
138                            JournalFeedServiceUtil.deleteFeed(groupId, deleteFeedIds[i]);
139                    }
140            }
141    
142            protected void updateFeed(ActionRequest actionRequest) throws Exception {
143                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
144    
145                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
146    
147                    String feedId = ParamUtil.getString(actionRequest, "feedId");
148                    boolean autoFeedId = ParamUtil.getBoolean(actionRequest, "autoFeedId");
149    
150                    String name = ParamUtil.getString(actionRequest, "name");
151                    String description = ParamUtil.getString(actionRequest, "description");
152                    String type = ParamUtil.getString(actionRequest, "type");
153                    String structureId = ParamUtil.getString(actionRequest, "structureId");
154                    String templateId = ParamUtil.getString(actionRequest, "templateId");
155                    String rendererTemplateId = ParamUtil.getString(
156                            actionRequest, "rendererTemplateId");
157                    int delta = ParamUtil.getInteger(actionRequest, "delta");
158                    String orderByCol = ParamUtil.getString(actionRequest, "orderByCol");
159                    String orderByType = ParamUtil.getString(actionRequest, "orderByType");
160                    String targetLayoutFriendlyUrl = ParamUtil.getString(
161                            actionRequest, "targetLayoutFriendlyUrl");
162                    String targetPortletId = ParamUtil.getString(
163                            actionRequest, "targetPortletId");
164                    String contentField = ParamUtil.getString(
165                            actionRequest, "contentField");
166                    String feedType = ParamUtil.getString(
167                            actionRequest, "feedType", RSSUtil.FEED_TYPE_DEFAULT);
168    
169                    String feedFormat = RSSUtil.getFeedTypeFormat(feedType);
170                    double feedVersion = RSSUtil.getFeedTypeVersion(feedType);
171    
172                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
173                            JournalFeed.class.getName(), actionRequest);
174    
175                    if (cmd.equals(Constants.ADD)) {
176    
177                            // Add feed
178    
179                            JournalFeedServiceUtil.addFeed(
180                                    groupId, feedId, autoFeedId, name, description, type,
181                                    structureId, templateId, rendererTemplateId, delta, orderByCol,
182                                    orderByType, targetLayoutFriendlyUrl, targetPortletId,
183                                    contentField, feedFormat, feedVersion, serviceContext);
184                    }
185                    else {
186    
187                            // Update feed
188    
189                            JournalFeedServiceUtil.updateFeed(
190                                    groupId, feedId, name, description, type, structureId,
191                                    templateId, rendererTemplateId, delta, orderByCol, orderByType,
192                                    targetLayoutFriendlyUrl, targetPortletId, contentField,
193                                    feedFormat, feedVersion, serviceContext);
194                    }
195            }
196    
197    }