001: package org.tigris.scarab.actions;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: // Turbine Stuff
050: import java.util.List;
051:
052: import org.apache.fulcrum.intake.model.Group;
053: import org.apache.fulcrum.parser.ParameterParser;
054: import org.apache.torque.TorqueException;
055: import org.apache.torque.oid.IDBroker;
056: import org.apache.torque.util.BasePeer;
057: import org.apache.turbine.RunData;
058: import org.apache.turbine.TemplateContext;
059: import org.apache.turbine.tool.IntakeTool;
060: import org.tigris.scarab.actions.base.RequireLoginFirstAction;
061: import org.tigris.scarab.om.GlobalParameter;
062: import org.tigris.scarab.om.GlobalParameterManager;
063: import org.tigris.scarab.om.Issue;
064: import org.tigris.scarab.om.Module;
065: import org.tigris.scarab.om.ModuleManager;
066: import org.tigris.scarab.om.ScarabModule;
067: import org.tigris.scarab.om.ScarabUser;
068: import org.tigris.scarab.services.security.ScarabSecurity;
069: import org.tigris.scarab.tools.ScarabLocalizationTool;
070: import org.tigris.scarab.tools.ScarabRequestTool;
071: import org.tigris.scarab.tools.localization.L10NKeySet;
072: import org.tigris.scarab.util.Log;
073:
074: /**
075: * This class is responsible for creating / updating Scarab Modules
076: *
077: * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
078: * @version $Id: ModifyModule.java 10198 2006-07-03 23:33:30Z dabbous $
079: */
080: public class ModifyModule extends RequireLoginFirstAction {
081: private static final String[] EMAIL_PARAMS = {
082: GlobalParameter.EMAIL_ENABLED,
083: GlobalParameter.EMAIL_INCLUDE_ISSUE_DETAILS };
084:
085: /**
086: * Process Update button which updates a Module
087: */
088: public void doUpdate(RunData data, TemplateContext context)
089: throws Exception {
090: String template = getCurrentTemplate(data, null);
091: String nextTemplate = getNextTemplate(data, template);
092:
093: IntakeTool intake = getIntakeTool(context);
094: ScarabRequestTool scarabR = getScarabRequestTool(context);
095:
096: if (intake.isAllValid()) {
097: Module me = null;
098: try {
099: me = scarabR.getModule();
100: } catch (Exception e) {
101: throw new Exception("Could not locate module"); //EXCEPTION
102: }
103:
104: Group moduleGroup = intake.get("Module", me.getQueryKey(),
105: false);
106: if (moduleGroup == null) {
107: setTarget(data, template);
108: scarabR
109: .setAlertMessage(L10NKeySet.CouldNotLocateModuleGroup);
110: return;
111: } else {
112: ScarabUser user = (ScarabUser) data.getUser();
113:
114: // make sure that the user has Edit permission
115: // in the module.
116: if (!user
117: .hasPermission(ScarabSecurity.MODULE__EDIT, me)) {
118: scarabR.setAlertMessage(NO_PERMISSION_MESSAGE);
119: intake.remove(moduleGroup);
120: setTarget(data, nextTemplate);
121: return;
122: }
123:
124: Module origParent = me.getParent();
125: String origCode = me.getCode();
126: moduleGroup.setProperties(me);
127: Module newParent = me.getParent();
128: String newCode = me.getCode();
129:
130: if (newParent.getParent() == me && origParent != me) {
131: scarabR
132: .setAlertMessage(L10NKeySet.CircularParentChildRelationship);
133: intake.remove(moduleGroup);
134: setTarget(data, template);
135: return;
136: } else if (!user.hasPermission(
137: ScarabSecurity.MODULE__EDIT, origParent)
138: && origParent.getModuleId() != newParent
139: .getModuleId()) {
140: scarabR
141: .setAlertMessage(L10NKeySet.NoPermissionInParentModule);
142: setTarget(data, template);
143: return;
144: }
145:
146: // Cascade update the code to the (denormalized) issue prefix
147: if (!newCode.equals(origCode)) {
148: if (me instanceof ScarabModule) {
149: ScarabModule sm = (ScarabModule) me;
150: List issues = sm.getIssues();
151: for (int i = 0; i < issues.size(); i++) {
152: Issue issue = (Issue) issues.get(i);
153: if (!issue.getIdPrefix().equals(
154: me.getCode())) {
155: issue.setIdPrefix(me.getCode());
156: issue.save();
157: }
158: }
159: //Update the ID table to reflect the module code r
160: // FIXME: Using SQL because IDBroker doesn't have a Peer yet.
161: String idTable = IDBroker.TABLE_NAME.substring(
162: 0, IDBroker.TABLE_NAME.indexOf('.'));
163: String sql = "update " + idTable
164: + " SET TABLE_NAME='" + newCode
165: + "' WHERE TABLE_NAME='" + origCode
166: + "'";
167: BasePeer.executeStatement(sql);
168: } else {
169: throw new Exception(
170: "Did not get a ScarabModule"); //EXCEPTION
171: }
172: }
173:
174: me.save();
175:
176: updateModuleParameters(data, me);
177:
178: intake.remove(moduleGroup);
179: setTarget(data, nextTemplate);
180: scarabR.setConfirmMessage(L10NKeySet.ModuleUpdated);
181: }
182: }
183: }
184:
185: /**
186: * This method will search for email-configuration in the received form, and will
187: * update globalparameters acordingly.
188: *
189: * @param data
190: * @param me
191: * @throws TorqueException
192: */
193: private void updateModuleParameters(RunData data, Module me)
194: throws TorqueException {
195: // Set email overrides
196: if (GlobalParameterManager.getBoolean(
197: GlobalParameter.EMAIL_ALLOW_MODULE_OVERRIDE, me)) {
198: ParameterParser pp = data.getParameters();
199: String name;
200: for (int i = 0; i < EMAIL_PARAMS.length; i++) {
201: name = EMAIL_PARAMS[i];
202: GlobalParameterManager.setBoolean(name, me, pp
203: .getBoolean(name));
204: }
205: }
206:
207: ParameterParser pp = data.getParameters();
208: storeGlobalParameter(GlobalParameter.ISSUE_REASON_REQUIRED, me,
209: pp);
210: storeGlobalParameter(
211: GlobalParameter.REQUIRED_ROLE_FOR_REQUESTING_ACCESS,
212: me, pp);
213: storeGlobalParameter(GlobalParameter.COMMENT_RENDER_ENGINE, me,
214: pp);
215:
216: }
217:
218: /**
219: * @param me
220: * @param pp
221: * @throws TorqueException
222: */
223: private void storeGlobalParameter(String name, Module me,
224: ParameterParser pp) throws TorqueException {
225: String requiredRole = pp.getString(name);
226: if (null == requiredRole) {
227: requiredRole = "";
228: }
229: GlobalParameterManager.setString(name, me, requiredRole);
230: }
231:
232: /**
233: * Process Create button which creates a new Module
234: */
235: public void doCreate(RunData data, TemplateContext context)
236: throws Exception {
237: String template = getCurrentTemplate(data, null);
238: String nextTemplate = getNextTemplate(data, template);
239:
240: IntakeTool intake = getIntakeTool(context);
241: ScarabRequestTool scarabR = getScarabRequestTool(context);
242:
243: if (intake.isAllValid()) {
244: Group moduleGroup = intake.get("Module",
245: IntakeTool.DEFAULT_KEY, false);
246: Module me = ModuleManager.getInstance();
247: if (moduleGroup == null) {
248: throw new Exception("Could not locate module"); //EXCEPTION
249: }
250: moduleGroup.setProperties(me);
251: ScarabUser user = (ScarabUser) data.getUser();
252:
253: // make sure that the user has Edit permission
254: // in the parent module.
255: if (!user.hasPermission(ScarabSecurity.MODULE__EDIT, me
256: .getParent())) {
257: scarabR
258: .setAlertMessage(L10NKeySet.NoPermissionInParentModule);
259: } else {
260: me.setOwnerId(user.getUserId());
261: me.save();
262:
263: updateModuleParameters(data, me);
264:
265: data.setACL(((ScarabUser) data.getUser()).getACL());
266: data.save();
267:
268: scarabR.setConfirmMessage(L10NKeySet.NewModuleCreated);
269: intake.remove(moduleGroup);
270: }
271: } else {
272: setTarget(data, template);
273: return;
274: }
275: setTarget(data, nextTemplate);
276: }
277: }
|