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.messageboards.action;
022:
023: import com.liferay.portal.captcha.CaptchaTextException;
024: import com.liferay.portal.captcha.CaptchaUtil;
025: import com.liferay.portal.kernel.util.Constants;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.model.Layout;
028: import com.liferay.portal.security.auth.PrincipalException;
029: import com.liferay.portal.struts.PortletAction;
030: import com.liferay.portal.util.WebKeys;
031: import com.liferay.portlet.messageboards.CategoryNameException;
032: import com.liferay.portlet.messageboards.NoSuchCategoryException;
033: import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
034: import com.liferay.util.servlet.SessionErrors;
035:
036: import javax.portlet.ActionRequest;
037: import javax.portlet.ActionResponse;
038: import javax.portlet.PortletConfig;
039: import javax.portlet.RenderRequest;
040: import javax.portlet.RenderResponse;
041:
042: import org.apache.struts.action.ActionForm;
043: import org.apache.struts.action.ActionForward;
044: import org.apache.struts.action.ActionMapping;
045:
046: /**
047: * <a href="EditCategoryAction.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: *
051: */
052: public class EditCategoryAction extends PortletAction {
053:
054: public void processAction(ActionMapping mapping, ActionForm form,
055: PortletConfig config, ActionRequest req, ActionResponse res)
056: throws Exception {
057:
058: String cmd = ParamUtil.getString(req, Constants.CMD);
059:
060: try {
061: if (cmd.equals(Constants.ADD)
062: || cmd.equals(Constants.UPDATE)) {
063: updateCategory(req);
064: } else if (cmd.equals(Constants.DELETE)) {
065: deleteCategory(req);
066: } else if (cmd.equals(Constants.SUBSCRIBE)) {
067: subscribeCategory(req);
068: } else if (cmd.equals(Constants.UNSUBSCRIBE)) {
069: unsubscribeCategory(req);
070: }
071:
072: sendRedirect(req, res);
073: } catch (Exception e) {
074: if (e instanceof NoSuchCategoryException
075: || e instanceof PrincipalException) {
076:
077: SessionErrors.add(req, e.getClass().getName());
078:
079: setForward(req, "portlet.message_boards.error");
080: } else if (e instanceof CaptchaTextException
081: || e instanceof CategoryNameException) {
082:
083: SessionErrors.add(req, e.getClass().getName());
084: } else {
085: throw e;
086: }
087: }
088: }
089:
090: public ActionForward render(ActionMapping mapping, ActionForm form,
091: PortletConfig config, RenderRequest req, RenderResponse res)
092: throws Exception {
093:
094: try {
095: ActionUtil.getCategory(req);
096: } catch (Exception e) {
097: if (e instanceof NoSuchCategoryException
098: || e instanceof PrincipalException) {
099:
100: SessionErrors.add(req, e.getClass().getName());
101:
102: return mapping
103: .findForward("portlet.message_boards.error");
104: } else {
105: throw e;
106: }
107: }
108:
109: return mapping.findForward(getForward(req,
110: "portlet.message_boards.edit_category"));
111: }
112:
113: protected void deleteCategory(ActionRequest req) throws Exception {
114: long categoryId = ParamUtil.getLong(req, "categoryId");
115:
116: MBCategoryServiceUtil.deleteCategory(categoryId);
117: }
118:
119: protected void subscribeCategory(ActionRequest req)
120: throws Exception {
121: long categoryId = ParamUtil.getLong(req, "categoryId");
122:
123: MBCategoryServiceUtil.subscribeCategory(categoryId);
124: }
125:
126: protected void unsubscribeCategory(ActionRequest req)
127: throws Exception {
128: long categoryId = ParamUtil.getLong(req, "categoryId");
129:
130: MBCategoryServiceUtil.unsubscribeCategory(categoryId);
131: }
132:
133: protected void updateCategory(ActionRequest req) throws Exception {
134: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
135:
136: long categoryId = ParamUtil.getLong(req, "categoryId");
137:
138: long parentCategoryId = ParamUtil.getLong(req,
139: "parentCategoryId");
140: String name = ParamUtil.getString(req, "name");
141: String description = ParamUtil.getString(req, "description");
142:
143: boolean mergeWithParentCategory = ParamUtil.getBoolean(req,
144: "mergeWithParentCategory");
145:
146: String[] communityPermissions = req
147: .getParameterValues("communityPermissions");
148: String[] guestPermissions = req
149: .getParameterValues("guestPermissions");
150:
151: if (categoryId <= 0) {
152: CaptchaUtil.check(req);
153:
154: // Add category
155:
156: MBCategoryServiceUtil.addCategory(layout.getPlid(),
157: parentCategoryId, name, description,
158: communityPermissions, guestPermissions);
159: } else {
160:
161: // Update category
162:
163: MBCategoryServiceUtil.updateCategory(categoryId,
164: parentCategoryId, name, description,
165: mergeWithParentCategory);
166: }
167: }
168:
169: }
|