KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > actions > ModifyModule


1 package org.tigris.scarab.actions;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 // Turbine Stuff
50
import java.util.List JavaDoc;
51
52 import org.apache.fulcrum.intake.model.Group;
53 import org.apache.fulcrum.parser.ParameterParser;
54 import org.apache.fulcrum.security.TurbineSecurity;
55 import org.apache.torque.oid.IDBroker;
56 import org.apache.torque.util.BasePeer;
57 import org.apache.turbine.RunData;
58 import org.apache.turbine.TemplateContext;
59 import org.apache.turbine.tool.IntakeTool;
60 import org.tigris.scarab.actions.base.RequireLoginFirstAction;
61 import org.tigris.scarab.om.GlobalParameter;
62 import org.tigris.scarab.om.GlobalParameterManager;
63 import org.tigris.scarab.om.Issue;
64 import org.tigris.scarab.om.Module;
65 import org.tigris.scarab.om.ModuleManager;
66 import org.tigris.scarab.om.ScarabModule;
67 import org.tigris.scarab.om.ScarabUser;
68 import org.tigris.scarab.services.security.ScarabSecurity;
69 import org.tigris.scarab.tools.ScarabLocalizationTool;
70 import org.tigris.scarab.tools.ScarabRequestTool;
71 import org.tigris.scarab.tools.localization.L10NKeySet;
72 import org.tigris.scarab.util.Log;
73
74 /**
75  * This class is responsible for creating / updating Scarab Modules
76  *
77  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
78  * @version $Id: ModifyModule.java 9516 2005-03-25 01:12:25Z dabbous $
79  */

80 public class ModifyModule extends RequireLoginFirstAction
81 {
82     private static final String JavaDoc[] EMAIL_PARAMS =
83         {GlobalParameter.EMAIL_ENABLED,
84          GlobalParameter.EMAIL_INCLUDE_ISSUE_DETAILS};
85
86     /**
87      * Process Update button which updates a Module
88      */

89     public void doUpdate(RunData data, TemplateContext context)
90         throws Exception JavaDoc
91     {
92         String JavaDoc template = getCurrentTemplate(data, null);
93         String JavaDoc nextTemplate = getNextTemplate(data, template);
94
95         ScarabLocalizationTool l10n = getLocalizationTool(context);
96         IntakeTool intake = getIntakeTool(context);
97         ScarabRequestTool scarabR = getScarabRequestTool(context);
98         
99         if (intake.isAllValid())
100         {
101             Module me = null;
102             try
103             {
104                 me = scarabR.getModule();
105             }
106             catch (Exception JavaDoc e)
107             {
108                 throw new Exception JavaDoc("Could not locate module"); //EXCEPTION
109
}
110
111             Group moduleGroup = intake.get
112                 ("Module",me.getQueryKey(), false);
113             if (moduleGroup == null)
114             {
115                 setTarget(data, template);
116                 scarabR.setAlertMessage(
117                     L10NKeySet.CouldNotLocateModuleGroup);
118                 return;
119             }
120             else
121             {
122                 ScarabUser user = (ScarabUser) data.getUser();
123
124                 // make sure that the user has Edit permission
125
// in the module.
126
if (!user.hasPermission(ScarabSecurity.MODULE__EDIT, me))
127                 {
128                     scarabR.setAlertMessage(NO_PERMISSION_MESSAGE);
129                     intake.remove(moduleGroup);
130                     setTarget(data, nextTemplate);
131                     return;
132                 }
133
134                 Module origParent = me.getParent();
135                 String JavaDoc origCode = me.getCode();
136                 moduleGroup.setProperties(me);
137                 Module newParent = me.getParent();
138                 String JavaDoc newCode = me.getCode();
139
140                 if (newParent.getParent() == me && origParent!=me)
141                 {
142                     scarabR.setAlertMessage(L10NKeySet.CircularParentChildRelationship);
143                     intake.remove(moduleGroup);
144                     setTarget(data, template);
145                     return;
146                 }
147                 else if (!user.hasPermission(ScarabSecurity.MODULE__EDIT, origParent) &&
148                     origParent.getModuleId() != newParent.getModuleId())
149                 {
150                     scarabR.setAlertMessage(L10NKeySet.NoPermissionInParentModule);
151                     setTarget(data, template);
152                     return;
153                 }
154                 
155                 // Cascade update the code to the (denormalized) issue prefix
156
if (! newCode.equals(origCode))
157                 {
158                     if (me instanceof ScarabModule)
159                     {
160                         ScarabModule sm = (ScarabModule)me;
161                         List JavaDoc issues = sm.getIssues();
162                         for (int i = 0; i < issues.size(); i++)
163                         {
164                             Issue issue = (Issue)issues.get(i);
165                             if (! issue.getIdPrefix().equals(me.getCode()))
166                             {
167                                 issue.setIdPrefix(me.getCode());
168                                 issue.save();
169                             }
170                         }
171                         //Update the ID table to reflect the module code r
172
// FIXME: Using SQL because IDBroker doesn't have a Peer yet.
173
String JavaDoc idTable = IDBroker.TABLE_NAME.substring(0,
174                                 IDBroker.TABLE_NAME.indexOf('.'));
175                         String JavaDoc sql = "update " + idTable
176                          + " SET TABLE_NAME='" + newCode + "' WHERE TABLE_NAME='" +
177                          origCode + "'";
178                         BasePeer.executeStatement(sql);
179                     }
180                     else
181                     {
182                         throw new Exception JavaDoc ("Did not get a ScarabModule"); //EXCEPTION
183
}
184                 }
185                 
186                 me.save();
187
188                 // Set email overrides
189
if (GlobalParameterManager.getBoolean(
190                         GlobalParameter.EMAIL_ALLOW_MODULE_OVERRIDE,me))
191                 {
192                     ParameterParser pp = data.getParameters();
193                     String JavaDoc name;
194                     for (int i=0; i<EMAIL_PARAMS.length; i++)
195                     {
196                         name = EMAIL_PARAMS[i];
197                         GlobalParameterManager
198                             .setBoolean(name, me, pp.getBoolean(name));
199                     }
200                 }
201
202                 ParameterParser pp = data.getParameters();
203                 String JavaDoc name = GlobalParameter.ISSUE_REASON_REQUIRED;
204                 boolean allowEmptyReason = pp.getBoolean(name,false);
205                 GlobalParameterManager.setBoolean(name, me,allowEmptyReason);
206          
207                 intake.remove(moduleGroup);
208                 setTarget(data, nextTemplate);
209                 scarabR.setConfirmMessage(L10NKeySet.ModuleUpdated);
210             }
211         }
212     }
213
214     /**
215      * Process Create button which creates a new Module
216      */

217     public void doCreate(RunData data, TemplateContext context)
218         throws Exception JavaDoc
219     {
220         String JavaDoc template = getCurrentTemplate(data, null);
221         String JavaDoc nextTemplate = getNextTemplate(data, template);
222
223         ScarabLocalizationTool l10n = getLocalizationTool(context);
224         IntakeTool intake = getIntakeTool(context);
225         ScarabRequestTool scarabR = getScarabRequestTool(context);
226         
227         if (intake.isAllValid())
228         {
229             Group moduleGroup = intake.get
230                 ("Module",IntakeTool.DEFAULT_KEY, false);
231             Module me = ModuleManager.getInstance();
232             if (moduleGroup == null)
233             {
234                 throw new Exception JavaDoc("Could not locate module"); //EXCEPTION
235
}
236             try
237             {
238                 moduleGroup.setProperties(me);
239                 ScarabUser user = (ScarabUser)data.getUser();
240                 
241                 // make sure that the user has Edit permission
242
// in the parent module.
243
// FIXME: move this logic into the ScarabModule.save() method
244
if (!user.hasPermission(ScarabSecurity.MODULE__EDIT,
245                     me.getParent()))
246                 {
247                     throw new Exception JavaDoc ("You do not have permission to" +
248                         " assign this module to the requested parent module."); //EXCEPTION
249
}
250                 me.setOwnerId(user.getUserId());
251                 me.save();
252
253                 data.setACL(TurbineSecurity.getACL(data.getUser()));
254                 data.save();
255
256                 scarabR.setConfirmMessage(L10NKeySet.NewModuleCreated);
257             }
258             catch (Exception JavaDoc e)
259             {
260                 setTarget(data, template);
261                 Log.get().error(e);
262                 String JavaDoc msg = l10n.getMessage(e);
263                 scarabR.setAlertMessage(msg);
264                 return;
265             }
266             intake.remove(moduleGroup);
267         }
268         else
269         {
270             setTarget(data, template);
271             return;
272         }
273         setTarget(data, nextTemplate);
274     }
275 }
276
Popular Tags