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