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.ParamUtil;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.kernel.util.Validator;
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.journal.DuplicateStructureIdException;
032: import com.liferay.portlet.journal.NoSuchStructureException;
033: import com.liferay.portlet.journal.RequiredStructureException;
034: import com.liferay.portlet.journal.StructureDescriptionException;
035: import com.liferay.portlet.journal.StructureIdException;
036: import com.liferay.portlet.journal.StructureNameException;
037: import com.liferay.portlet.journal.StructureXsdException;
038: import com.liferay.portlet.journal.model.JournalStructure;
039: import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
040: import com.liferay.portlet.journal.util.JournalUtil;
041: import com.liferay.util.servlet.SessionErrors;
042:
043: import javax.portlet.ActionRequest;
044: import javax.portlet.ActionResponse;
045: import javax.portlet.PortletConfig;
046: import javax.portlet.RenderRequest;
047: import javax.portlet.RenderResponse;
048:
049: import org.apache.struts.action.ActionForm;
050: import org.apache.struts.action.ActionForward;
051: import org.apache.struts.action.ActionMapping;
052:
053: /**
054: * <a href="EditStructureAction.java.html"><b><i>View Source</i></b></a>
055: *
056: * @author Brian Wing Shun Chan
057: *
058: */
059: public class EditStructureAction extends PortletAction {
060:
061: public void processAction(ActionMapping mapping, ActionForm form,
062: PortletConfig config, ActionRequest req, ActionResponse res)
063: throws Exception {
064:
065: String cmd = ParamUtil.getString(req, Constants.CMD);
066:
067: try {
068: if (cmd.equals(Constants.ADD)
069: || cmd.equals(Constants.UPDATE)) {
070: updateStructure(req);
071: } else if (cmd.equals(Constants.DELETE)) {
072: deleteStructures(req);
073: }
074:
075: if (Validator.isNotNull(cmd)) {
076: sendRedirect(req, res);
077: }
078: } catch (Exception e) {
079: if (e instanceof NoSuchStructureException
080: || e instanceof PrincipalException) {
081:
082: SessionErrors.add(req, e.getClass().getName());
083:
084: setForward(req, "portlet.journal.error");
085: } else if (e instanceof DuplicateStructureIdException
086: || e instanceof RequiredStructureException
087: || e instanceof StructureDescriptionException
088: || e instanceof StructureIdException
089: || e instanceof StructureNameException
090: || e instanceof StructureXsdException) {
091:
092: SessionErrors.add(req, e.getClass().getName());
093:
094: if (e instanceof RequiredStructureException) {
095: res.sendRedirect(ParamUtil.getString(req,
096: "redirect"));
097: }
098: } else {
099: throw e;
100: }
101: }
102: }
103:
104: public ActionForward render(ActionMapping mapping, ActionForm form,
105: PortletConfig config, RenderRequest req, RenderResponse res)
106: throws Exception {
107:
108: try {
109: String cmd = ParamUtil.getString(req, Constants.CMD);
110:
111: if (!cmd.equals(Constants.ADD)) {
112: ActionUtil.getStructure(req);
113: }
114: } catch (NoSuchStructureException nsse) {
115:
116: // Let this slide because the user can manually input a structure id
117: // for a new structure that does not yet exist.
118:
119: } catch (Exception e) {
120: if (//e instanceof NoSuchStructureException ||
121: e instanceof PrincipalException) {
122:
123: SessionErrors.add(req, e.getClass().getName());
124:
125: return mapping.findForward("portlet.journal.error");
126: } else {
127: throw e;
128: }
129: }
130:
131: return mapping.findForward(getForward(req,
132: "portlet.journal.edit_structure"));
133: }
134:
135: protected void deleteStructures(ActionRequest req) throws Exception {
136: long groupId = ParamUtil.getLong(req, "groupId");
137:
138: String[] deleteStructureIds = StringUtil.split(ParamUtil
139: .getString(req, "deleteStructureIds"));
140:
141: for (int i = 0; i < deleteStructureIds.length; i++) {
142: JournalStructureServiceUtil.deleteStructure(groupId,
143: deleteStructureIds[i]);
144:
145: JournalUtil.removeRecentStructure(req,
146: deleteStructureIds[i]);
147: }
148: }
149:
150: protected void updateStructure(ActionRequest req) throws Exception {
151: String cmd = ParamUtil.getString(req, Constants.CMD);
152:
153: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
154:
155: long groupId = ParamUtil.getLong(req, "groupId");
156:
157: String structureId = ParamUtil.getString(req, "structureId");
158: boolean autoStructureId = ParamUtil.getBoolean(req,
159: "autoStructureId");
160:
161: String name = ParamUtil.getString(req, "name");
162: String description = ParamUtil.getString(req, "description");
163: String xsd = ParamUtil.getString(req, "xsd");
164:
165: String[] communityPermissions = req
166: .getParameterValues("communityPermissions");
167: String[] guestPermissions = req
168: .getParameterValues("guestPermissions");
169:
170: JournalStructure structure = null;
171:
172: if (cmd.equals(Constants.ADD)) {
173:
174: // Add structure
175:
176: structure = JournalStructureServiceUtil.addStructure(
177: structureId, autoStructureId, layout.getPlid(),
178: name, description, xsd, communityPermissions,
179: guestPermissions);
180: } else {
181:
182: // Update structure
183:
184: structure = JournalStructureServiceUtil.updateStructure(
185: groupId, structureId, name, description, xsd);
186: }
187:
188: // Recent structures
189:
190: JournalUtil.addRecentStructure(req, structure);
191: }
192:
193: }
|