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.GetterUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextFactory;
027    import com.liferay.portal.struts.PortletAction;
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                    String feedType = RSSUtil.TYPE_DEFAULT;
170                    double feedVersion = RSSUtil.VERSION_DEFAULT;
171    
172                    String feedTypeAndVersion = ParamUtil.getString(
173                            actionRequest, "feedTypeAndVersion");
174    
175                    if (Validator.isNotNull(feedTypeAndVersion)) {
176                            String[] parts = feedTypeAndVersion.split(StringPool.COLON);
177    
178                            try {
179                                    feedType = parts[0];
180                                    feedVersion = GetterUtil.getDouble(parts[1]);
181                            }
182                            catch (Exception e) {
183                            }
184                    }
185                    else {
186                            feedType = ParamUtil.getString(actionRequest, "feedType", feedType);
187                            feedVersion = ParamUtil.getDouble(
188                                    actionRequest, "feedVersion", feedVersion);
189                    }
190    
191                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
192                            JournalFeed.class.getName(), actionRequest);
193    
194                    if (cmd.equals(Constants.ADD)) {
195    
196                            // Add feed
197    
198                            JournalFeedServiceUtil.addFeed(
199                                    groupId, feedId, autoFeedId, name, description, type,
200                                    structureId, templateId, rendererTemplateId, delta, orderByCol,
201                                    orderByType, targetLayoutFriendlyUrl, targetPortletId,
202                                    contentField, feedType, feedVersion, serviceContext);
203                    }
204                    else {
205    
206                            // Update feed
207    
208                            JournalFeedServiceUtil.updateFeed(
209                                    groupId, feedId, name, description, type, structureId,
210                                    templateId, rendererTemplateId, delta, orderByCol, orderByType,
211                                    targetLayoutFriendlyUrl, targetPortletId, contentField,
212                                    feedType, feedVersion, serviceContext);
213                    }
214            }
215    
216    }