001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journal.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.Layout;
030: import com.liferay.portal.security.auth.PrincipalException;
031: import com.liferay.portal.struts.PortletAction;
032: import com.liferay.portal.util.WebKeys;
033: import com.liferay.portlet.journal.DuplicateFeedIdException;
034: import com.liferay.portlet.journal.FeedContentFieldException;
035: import com.liferay.portlet.journal.FeedDescriptionException;
036: import com.liferay.portlet.journal.FeedIdException;
037: import com.liferay.portlet.journal.FeedNameException;
038: import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
039: import com.liferay.portlet.journal.FeedTargetPortletIdException;
040: import com.liferay.portlet.journal.NoSuchFeedException;
041: import com.liferay.portlet.journal.service.JournalFeedServiceUtil;
042: import com.liferay.util.RSSUtil;
043: import com.liferay.util.servlet.SessionErrors;
044:
045: import javax.portlet.ActionRequest;
046: import javax.portlet.ActionResponse;
047: import javax.portlet.PortletConfig;
048: import javax.portlet.RenderRequest;
049: import javax.portlet.RenderResponse;
050:
051: import org.apache.struts.action.ActionForm;
052: import org.apache.struts.action.ActionForward;
053: import org.apache.struts.action.ActionMapping;
054:
055: /**
056: * <a href="EditFeedAction.java.html"><b><i>View Source</i></b></a>
057: *
058: * @author Raymond Augé
059: *
060: */
061: public class EditFeedAction extends PortletAction {
062:
063: public void processAction(ActionMapping mapping, ActionForm form,
064: PortletConfig config, ActionRequest req, ActionResponse res)
065: throws Exception {
066:
067: String cmd = ParamUtil.getString(req, Constants.CMD);
068:
069: try {
070: if (cmd.equals(Constants.ADD)
071: || cmd.equals(Constants.UPDATE)) {
072: updateFeed(req);
073: } else if (cmd.equals(Constants.DELETE)) {
074: deleteFeeds(req);
075: }
076:
077: sendRedirect(req, res);
078: } catch (Exception e) {
079: if (e instanceof NoSuchFeedException
080: || e instanceof PrincipalException) {
081:
082: SessionErrors.add(req, e.getClass().getName());
083:
084: setForward(req, "portlet.journal.error");
085: } else if (e instanceof DuplicateFeedIdException
086: || e instanceof FeedContentFieldException
087: || e instanceof FeedDescriptionException
088: || e instanceof FeedIdException
089: || e instanceof FeedNameException
090: || e instanceof FeedTargetLayoutFriendlyUrlException
091: || e instanceof FeedTargetPortletIdException) {
092:
093: SessionErrors.add(req, e.getClass().getName());
094: } else {
095: throw e;
096: }
097: }
098: }
099:
100: public ActionForward render(ActionMapping mapping, ActionForm form,
101: PortletConfig config, RenderRequest req, RenderResponse res)
102: throws Exception {
103:
104: try {
105: String cmd = ParamUtil.getString(req, Constants.CMD);
106:
107: if (!cmd.equals(Constants.ADD)) {
108: ActionUtil.getFeed(req);
109: }
110: } catch (NoSuchFeedException nssfe) {
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: } catch (Exception e) {
116: if (e instanceof PrincipalException) {
117: SessionErrors.add(req, e.getClass().getName());
118:
119: return mapping.findForward("portlet.journal.error");
120: } else {
121: throw e;
122: }
123: }
124:
125: return mapping.findForward(getForward(req,
126: "portlet.journal.edit_feed"));
127: }
128:
129: protected void deleteFeeds(ActionRequest req) throws Exception {
130: long groupId = ParamUtil.getLong(req, "groupId");
131:
132: String[] deleteFeedIds = StringUtil.split(ParamUtil.getString(
133: req, "deleteFeedIds"));
134:
135: for (int i = 0; i < deleteFeedIds.length; i++) {
136: JournalFeedServiceUtil
137: .deleteFeed(groupId, deleteFeedIds[i]);
138: }
139: }
140:
141: protected void updateFeed(ActionRequest req) throws Exception {
142: String cmd = ParamUtil.getString(req, Constants.CMD);
143:
144: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
145:
146: long groupId = ParamUtil.getLong(req, "groupId");
147:
148: String feedId = ParamUtil.getString(req, "feedId");
149: boolean autoFeedId = ParamUtil.getBoolean(req, "autoFeedId");
150:
151: String name = ParamUtil.getString(req, "name");
152: String description = ParamUtil.getString(req, "description");
153: String type = ParamUtil.getString(req, "type");
154: String structureId = ParamUtil.getString(req, "structureId");
155: String templateId = ParamUtil.getString(req, "templateId");
156: String rendererTemplateId = ParamUtil.getString(req,
157: "rendererTemplateId");
158: int delta = ParamUtil.getInteger(req, "delta");
159: String orderByCol = ParamUtil.getString(req, "orderByCol");
160: String orderByType = ParamUtil.getString(req, "orderByType");
161: String targetLayoutFriendlyUrl = ParamUtil.getString(req,
162: "targetLayoutFriendlyUrl");
163: String targetPortletId = ParamUtil.getString(req,
164: "targetPortletId");
165: String contentField = ParamUtil.getString(req, "contentField");
166:
167: String feedType = RSSUtil.DEFAULT_TYPE;
168: double feedVersion = RSSUtil.DEFAULT_VERSION;
169:
170: String feedTypeAndVersion = ParamUtil.getString(req,
171: "feedTypeAndVersion");
172:
173: if (Validator.isNotNull(feedTypeAndVersion)) {
174: String[] parts = feedTypeAndVersion.split(StringPool.COLON);
175:
176: try {
177: feedType = parts[0];
178: feedVersion = GetterUtil.getDouble(parts[1]);
179: } catch (Exception e) {
180: }
181: } else {
182: feedType = ParamUtil.getString(req, "feedType", feedType);
183: feedVersion = ParamUtil.getDouble(req, "feedVersion",
184: feedVersion);
185: }
186:
187: String[] communityPermissions = req
188: .getParameterValues("communityPermissions");
189: String[] guestPermissions = req
190: .getParameterValues("guestPermissions");
191:
192: if (cmd.equals(Constants.ADD)) {
193:
194: // Add feed
195:
196: JournalFeedServiceUtil
197: .addFeed(layout.getPlid(), feedId, autoFeedId,
198: name, description, type, structureId,
199: templateId, rendererTemplateId, delta,
200: orderByCol, orderByType,
201: targetLayoutFriendlyUrl, targetPortletId,
202: contentField, feedType, feedVersion,
203: communityPermissions, guestPermissions);
204: } else {
205:
206: // Update feed
207:
208: JournalFeedServiceUtil.updateFeed(groupId, feedId, name,
209: description, type, structureId, templateId,
210: rendererTemplateId, delta, orderByCol, orderByType,
211: targetLayoutFriendlyUrl, targetPortletId,
212: contentField, feedType, feedVersion);
213: }
214: }
215:
216: }
|