001    /**
002     * Copyright (c) 2000-2012 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.dynamicdatamapping.model.DDMStructure;
027    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
028    import com.liferay.portlet.journal.DuplicateFeedIdException;
029    import com.liferay.portlet.journal.FeedContentFieldException;
030    import com.liferay.portlet.journal.FeedIdException;
031    import com.liferay.portlet.journal.FeedNameException;
032    import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
033    import com.liferay.portlet.journal.FeedTargetPortletIdException;
034    import com.liferay.portlet.journal.NoSuchFeedException;
035    import com.liferay.portlet.journal.model.JournalFeed;
036    import com.liferay.portlet.journal.service.JournalFeedServiceUtil;
037    import com.liferay.util.RSSUtil;
038    
039    import javax.portlet.ActionRequest;
040    import javax.portlet.ActionResponse;
041    import javax.portlet.PortletConfig;
042    import javax.portlet.RenderRequest;
043    import javax.portlet.RenderResponse;
044    
045    import org.apache.struts.action.ActionForm;
046    import org.apache.struts.action.ActionForward;
047    import org.apache.struts.action.ActionMapping;
048    
049    /**
050     * @author Raymond Augé
051     */
052    public class EditFeedAction extends PortletAction {
053    
054            @Override
055            public void processAction(
056                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
057                            ActionRequest actionRequest, ActionResponse actionResponse)
058                    throws Exception {
059    
060                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
061    
062                    if (Validator.isNull(cmd)) {
063                            return;
064                    }
065    
066                    try {
067                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
068                                    updateFeed(actionRequest);
069                            }
070                            else if (cmd.equals(Constants.DELETE)) {
071                                    deleteFeeds(actionRequest);
072                            }
073    
074                            sendRedirect(actionRequest, actionResponse);
075                    }
076                    catch (Exception e) {
077                            if (e instanceof NoSuchFeedException ||
078                                    e instanceof PrincipalException) {
079    
080                                    SessionErrors.add(actionRequest, e.getClass());
081    
082                                    setForward(actionRequest, "portlet.journal.error");
083                            }
084                            else if (e instanceof DuplicateFeedIdException ||
085                                             e instanceof FeedContentFieldException ||
086                                             e instanceof FeedIdException ||
087                                             e instanceof FeedNameException ||
088                                             e instanceof FeedTargetLayoutFriendlyUrlException ||
089                                             e instanceof FeedTargetPortletIdException) {
090    
091                                    SessionErrors.add(actionRequest, e.getClass());
092                            }
093                            else {
094                                    throw e;
095                            }
096                    }
097            }
098    
099            @Override
100            public ActionForward render(
101                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
102                            RenderRequest renderRequest, 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 mapping.findForward("portlet.journal.error");
123                            }
124                            else {
125                                    throw e;
126                            }
127                    }
128    
129                    return mapping.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    
169                    long ddmStructureId = ParamUtil.getLong(
170                            actionRequest, "ddmStructureId");
171    
172                    if (ddmStructureId > 0) {
173                            DDMStructure ddmStructure =
174                                    DDMStructureLocalServiceUtil.fetchDDMStructure(ddmStructureId);
175    
176                            if (ddmStructure != null) {
177                                    structureId = ddmStructure.getStructureKey();
178                            }
179                    }
180    
181                    String feedType = ParamUtil.getString(
182                            actionRequest, "feedType", RSSUtil.FEED_TYPE_DEFAULT);
183    
184                    String feedFormat = RSSUtil.getFeedTypeFormat(feedType);
185                    double feedVersion = RSSUtil.getFeedTypeVersion(feedType);
186    
187                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
188                            JournalFeed.class.getName(), actionRequest);
189    
190                    if (cmd.equals(Constants.ADD)) {
191    
192                            // Add feed
193    
194                            JournalFeedServiceUtil.addFeed(
195                                    groupId, feedId, autoFeedId, name, description, type,
196                                    structureId, templateId, rendererTemplateId, delta, orderByCol,
197                                    orderByType, targetLayoutFriendlyUrl, targetPortletId,
198                                    contentField, feedFormat, feedVersion, serviceContext);
199                    }
200                    else {
201    
202                            // Update feed
203    
204                            JournalFeedServiceUtil.updateFeed(
205                                    groupId, feedId, name, description, type, structureId,
206                                    templateId, rendererTemplateId, delta, orderByCol, orderByType,
207                                    targetLayoutFriendlyUrl, targetPortletId, contentField,
208                                    feedFormat, feedVersion, serviceContext);
209                    }
210            }
211    
212    }