001    /**
002     * Copyright (c) 2000-2010 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.FeedDescriptionException;
031    import com.liferay.portlet.journal.FeedIdException;
032    import com.liferay.portlet.journal.FeedNameException;
033    import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
034    import com.liferay.portlet.journal.FeedTargetPortletIdException;
035    import com.liferay.portlet.journal.NoSuchFeedException;
036    import com.liferay.portlet.journal.model.JournalFeed;
037    import com.liferay.portlet.journal.service.JournalFeedServiceUtil;
038    import com.liferay.util.RSSUtil;
039    
040    import javax.portlet.ActionRequest;
041    import javax.portlet.ActionResponse;
042    import javax.portlet.PortletConfig;
043    import javax.portlet.RenderRequest;
044    import javax.portlet.RenderResponse;
045    
046    import org.apache.struts.action.ActionForm;
047    import org.apache.struts.action.ActionForward;
048    import org.apache.struts.action.ActionMapping;
049    
050    /**
051     * @author Raymond Augé
052     */
053    public class EditFeedAction extends PortletAction {
054    
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                    try {
063                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
064                                    updateFeed(actionRequest);
065                            }
066                            else if (cmd.equals(Constants.DELETE)) {
067                                    deleteFeeds(actionRequest);
068                            }
069    
070                            sendRedirect(actionRequest, actionResponse);
071                    }
072                    catch (Exception e) {
073                            if (e instanceof NoSuchFeedException ||
074                                    e instanceof PrincipalException) {
075    
076                                    SessionErrors.add(actionRequest, e.getClass().getName());
077    
078                                    setForward(actionRequest, "portlet.journal.error");
079                            }
080                            else if (e instanceof DuplicateFeedIdException ||
081                                             e instanceof FeedContentFieldException ||
082                                             e instanceof FeedDescriptionException ||
083                                             e instanceof FeedIdException ||
084                                             e instanceof FeedNameException ||
085                                             e instanceof FeedTargetLayoutFriendlyUrlException ||
086                                             e instanceof FeedTargetPortletIdException) {
087    
088                                    SessionErrors.add(actionRequest, e.getClass().getName());
089                            }
090                            else {
091                                    throw e;
092                            }
093                    }
094            }
095    
096            public ActionForward render(
097                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
098                            RenderRequest renderRequest, RenderResponse renderResponse)
099                    throws Exception {
100    
101                    try {
102                            String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
103    
104                            if (!cmd.equals(Constants.ADD)) {
105                                    ActionUtil.getFeed(renderRequest);
106                            }
107                    }
108                    catch (NoSuchFeedException nssfe) {
109    
110                            // Let this slide because the user can manually input a feed id for
111                            // a new syndicated feed that does not yet exist.
112    
113                    }
114                    catch (Exception e) {
115                            if (e instanceof PrincipalException) {
116                                    SessionErrors.add(renderRequest, e.getClass().getName());
117    
118                                    return mapping.findForward("portlet.journal.error");
119                            }
120                            else {
121                                    throw e;
122                            }
123                    }
124    
125                    return mapping.findForward(
126                            getForward(renderRequest, "portlet.journal.edit_feed"));
127            }
128    
129            protected void deleteFeeds(ActionRequest actionRequest) throws Exception {
130                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
131    
132                    String[] deleteFeedIds = StringUtil.split(
133                            ParamUtil.getString(actionRequest, "deleteFeedIds"));
134    
135                    for (int i = 0; i < deleteFeedIds.length; i++) {
136                            JournalFeedServiceUtil.deleteFeed(groupId, deleteFeedIds[i]);
137                    }
138            }
139    
140            protected void updateFeed(ActionRequest actionRequest) throws Exception {
141                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
142    
143                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
144    
145                    String feedId = ParamUtil.getString(actionRequest, "feedId");
146                    boolean autoFeedId = ParamUtil.getBoolean(actionRequest, "autoFeedId");
147    
148                    String name = ParamUtil.getString(actionRequest, "name");
149                    String description = ParamUtil.getString(actionRequest, "description");
150                    String type = ParamUtil.getString(actionRequest, "type");
151                    String structureId = ParamUtil.getString(actionRequest, "structureId");
152                    String templateId = ParamUtil.getString(actionRequest, "templateId");
153                    String rendererTemplateId = ParamUtil.getString(
154                            actionRequest, "rendererTemplateId");
155                    int delta = ParamUtil.getInteger(actionRequest, "delta");
156                    String orderByCol = ParamUtil.getString(actionRequest, "orderByCol");
157                    String orderByType = ParamUtil.getString(actionRequest, "orderByType");
158                    String targetLayoutFriendlyUrl = ParamUtil.getString(
159                            actionRequest, "targetLayoutFriendlyUrl");
160                    String targetPortletId = ParamUtil.getString(
161                            actionRequest, "targetPortletId");
162                    String contentField = ParamUtil.getString(
163                            actionRequest, "contentField");
164    
165                    String feedType = RSSUtil.DEFAULT_TYPE;
166                    double feedVersion = RSSUtil.DEFAULT_VERSION;
167    
168                    String feedTypeAndVersion = ParamUtil.getString(
169                            actionRequest, "feedTypeAndVersion");
170    
171                    if (Validator.isNotNull(feedTypeAndVersion)) {
172                            String[] parts = feedTypeAndVersion.split(StringPool.COLON);
173    
174                            try {
175                                    feedType = parts[0];
176                                    feedVersion = GetterUtil.getDouble(parts[1]);
177                            }
178                            catch (Exception e) {
179                            }
180                    }
181                    else {
182                            feedType = ParamUtil.getString(actionRequest, "feedType", feedType);
183                            feedVersion = ParamUtil.getDouble(
184                                    actionRequest, "feedVersion", feedVersion);
185                    }
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, feedType, 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                                    feedType, feedVersion, serviceContext);
209                    }
210            }
211    
212    }