KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > actions > admin > ArtifactTypeEdit


1 package org.tigris.scarab.actions.admin;
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 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51
52 import org.apache.fulcrum.intake.model.Field;
53 import org.apache.fulcrum.intake.model.Group;
54 import org.apache.fulcrum.parser.ParameterParser;
55 import org.apache.torque.om.NumberKey;
56 import org.apache.turbine.RunData;
57 import org.apache.turbine.TemplateContext;
58 import org.apache.turbine.tool.IntakeTool;
59 import org.tigris.scarab.actions.base.RequireLoginFirstAction;
60 import org.tigris.scarab.om.Attribute;
61 import org.tigris.scarab.om.AttributeGroup;
62 import org.tigris.scarab.om.AttributeGroupManager;
63 import org.tigris.scarab.om.AttributeManager;
64 import org.tigris.scarab.om.IssueType;
65 import org.tigris.scarab.om.Module;
66 import org.tigris.scarab.om.RModuleAttribute;
67 import org.tigris.scarab.om.RModuleIssueType;
68 import org.tigris.scarab.services.cache.ScarabCache;
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.workflow.WorkflowFactory;
73
74 /**
75  * action methods on RModuleAttribute table
76  *
77  * @author <a HREF="mailto:elicia@collab.net">Elicia David</a>
78  * @version $Id: ArtifactTypeEdit.java 9255 2004-11-14 21:07:04Z dep4b $
79  */

80 public class ArtifactTypeEdit extends RequireLoginFirstAction
81 {
82     /**
83      * Adds or modifies an issue type's properties.
84      */

85     public boolean doSaveinfo (RunData data, TemplateContext context)
86         throws Exception JavaDoc
87     {
88         boolean success = true;
89         ScarabRequestTool scarabR = getScarabRequestTool(context);
90         ScarabLocalizationTool l10n = getLocalizationTool(context);
91         IssueType issueType = scarabR.getIssueType();
92         if (issueType.isSystemDefined())
93         {
94             scarabR.setAlertMessage(L10NKeySet.SystemSpecifiedIssueType);
95             return false;
96         }
97         if (issueType.getLocked())
98         {
99             scarabR.setAlertMessage(L10NKeySet.LockedIssueType);
100             return false;
101         }
102         IntakeTool intake = getIntakeTool(context);
103         Module module = scarabR.getCurrentModule();
104         RModuleIssueType rmit = module.getRModuleIssueType(issueType);
105         if (rmit == null)
106         {
107             scarabR.setAlertMessage(L10NKeySet.IssueTypeRemovedFromModule);
108             doCancel(data, context);
109             return false;
110         }
111         // Set properties for module-issue type info
112
Group rmitGroup = intake.get("RModuleIssueType",
113                                         rmit.getQueryKey(), false);
114         if (intake.isAllValid())
115         {
116             boolean nameTaken = false;
117             List JavaDoc issueTypes = module.getRModuleIssueTypes();
118             if (issueTypes != null)
119             {
120                 Field displayName = rmitGroup.get("DisplayName");
121                 if (displayName.toString().trim().length() == 0)
122                 {
123                     displayName.setMessage("intake_IssueTypeNameNotAllowedEmpty");
124                     scarabR.setAlertMessage(ERROR_MESSAGE);
125                     return false;
126                 }
127                 for (int i=0;i<issueTypes.size();i++)
128                 {
129                     RModuleIssueType tmpRmit = ((RModuleIssueType)issueTypes.get(i));
130                     if (tmpRmit.getDisplayName().equals(displayName.toString())
131                         && !tmpRmit.getIssueTypeId().equals(issueType.getIssueTypeId()))
132                     {
133                         nameTaken = true;
134                         break;
135                     }
136                 }
137             }
138          
139             if (nameTaken)
140             {
141                 scarabR.setAlertMessage(ERROR_MESSAGE);
142                 rmitGroup.get("DisplayName").setMessage("IssueTypeNameExists");
143                 return false;
144             }
145             else
146             {
147                 rmitGroup.setProperties(rmit);
148                 rmit.save();
149                 scarabR.setConfirmMessage(DEFAULT_MSG);
150             }
151         }
152         else
153         {
154             scarabR.setAlertMessage(ERROR_MESSAGE);
155             return false;
156         }
157         return success;
158     }
159
160     /**
161      * Adds or modifies an issue type's attribute groups.
162      */

163     public boolean doSavegroups (RunData data, TemplateContext context)
164         throws Exception JavaDoc
165     {
166         IntakeTool intake = getIntakeTool(context);
167         ScarabRequestTool scarabR = getScarabRequestTool(context);
168         ScarabLocalizationTool l10n = getLocalizationTool(context);
169
170         IssueType issueType = scarabR.getIssueType();
171         if (issueType.isSystemDefined())
172         {
173             scarabR.setAlertMessage(L10NKeySet.SystemSpecifiedIssueType);
174             return false;
175         }
176         if (issueType.getLocked())
177         {
178             scarabR.setAlertMessage(L10NKeySet.LockedIssueType);
179             return false;
180         }
181
182         Module module = scarabR.getCurrentModule();
183         RModuleIssueType rmit = module.getRModuleIssueType(issueType);
184         if (rmit == null)
185         {
186             scarabR.setAlertMessage(L10NKeySet.IssueTypeRemovedFromModule);
187             doCancel(data, context);
188             return false;
189         }
190         List JavaDoc attGroups = issueType.getAttributeGroups(module, false);
191
192         int dupeOrder = 2;
193         boolean areThereDedupeAttrs = false;
194
195         // Manage attribute groups
196
// Only have dedupe if there are more than one active group
197
if (issueType.getAttributeGroups(module, true).size() > 1)
198         {
199             dupeOrder = data.getParameters().getInt("dupe_order");
200
201             // Check that duplicate check is not at the beginning.
202
if (dupeOrder == 1)
203             {
204                 scarabR.setAlertMessage(L10NKeySet.CannotPositionDuplicateCheckFirst);
205                 return false;
206             }
207             // Check for duplicate sequence numbers
208
if (areThereDupeSequences(attGroups, intake, "AttributeGroup",
209                    "Order", dupeOrder))
210             {
211                scarabR.setAlertMessage(l10n.format("DuplicateSequenceNumbersFound",
212                    l10n.get("AttributeGroups").toLowerCase()));
213                return false;
214             }
215         }
216
217         // Set properties for attribute groups
218
for (int i=attGroups.size()-1; i>=0; i--)
219         {
220             AttributeGroup attGroup = (AttributeGroup)attGroups.get(i);
221             Group agGroup = intake.get("AttributeGroup",
222                                  attGroup.getQueryKey(), false);
223             agGroup.setProperties(attGroup);
224
225             // If an attribute group falls before the dedupe screen,
226
// Mark it as a dedupe group
227
if (attGroup.getOrder() < dupeOrder)
228             {
229                 areThereDedupeAttrs = true;
230                 attGroup.setDedupe(true);
231                 List JavaDoc dedupeGroups = module.
232                          getDedupeGroupsWithAttributes(issueType);
233                 if (!dedupeGroups.contains(attGroup))
234                 {
235                     dedupeGroups.add(attGroup);
236                 }
237             }
238             else
239             {
240                 attGroup.setDedupe(false);
241             }
242             attGroup.save();
243         }
244
245             // Set dedupe property for module-issueType
246
if (!areThereDedupeAttrs
247                 || issueType.getAttributeGroups(module, true).size() < 2)
248         {
249             rmit.setDedupe(false);
250         }
251         else
252         {
253             Group rmitGroup = intake.get("RModuleIssueType",
254                                         rmit.getQueryKey(), false);
255             Field dedupe = rmitGroup.get("Dedupe");
256             dedupe.setProperty(rmit);
257         }
258         rmit.save();
259         ScarabCache.clear();
260         scarabR.setConfirmMessage(DEFAULT_MSG);
261
262         return true;
263     }
264
265     /**
266      * Adds or modifies user attributes' properties
267      */

268     public boolean doSaveuserattributes (RunData data, TemplateContext context)
269         throws Exception JavaDoc
270     {
271         IntakeTool intake = getIntakeTool(context);
272         ScarabRequestTool scarabR = getScarabRequestTool(context);
273         ScarabLocalizationTool l10n = getLocalizationTool(context);
274         IssueType issueType = scarabR.getIssueType();
275         boolean success = true;
276
277         if (issueType.isSystemDefined())
278         {
279             scarabR.setAlertMessage(L10NKeySet.SystemSpecifiedIssueType);
280             success = false;
281         }
282         else if (issueType.getLocked())
283         {
284             scarabR.setAlertMessage(L10NKeySet.LockedIssueType);
285             success = false;
286         }
287         else
288         {
289             Module module = scarabR.getCurrentModule();
290             List JavaDoc rmas = module.getRModuleAttributes(issueType, false, "user");
291             if (areThereDupeSequences(rmas, intake, "RModuleAttribute",
292                                                              "Order", 0))
293             {
294                 scarabR.setAlertMessage(
295                     l10n.format("DuplicateSequenceNumbersFound",
296                     l10n.get("UserAttributes").toLowerCase()));
297                 success = false;
298             }
299             else
300             {
301                 for (Iterator JavaDoc itr = rmas.iterator(); itr.hasNext(); )
302                 {
303                     // Set properties for module-attribute mapping
304
RModuleAttribute rma = (RModuleAttribute)itr.next();
305                     Group rmaGroup = intake.get("RModuleAttribute",
306                              rma.getQueryKey(), false);
307                     // if attribute gets set to inactive, delete dependencies
308
boolean newActive = Boolean.valueOf(rmaGroup.get("Active").
309                                                     toString()).booleanValue();
310                     boolean oldActive = rma.getActive();
311                     if (!newActive && oldActive)
312                     {
313                         WorkflowFactory.getInstance().
314                             deleteWorkflowsForAttribute(
315                                       rma.getAttribute(), module, issueType);
316                     }
317                     rmaGroup.setProperties(rma);
318                     rma.save();
319                 }
320                 scarabR.setConfirmMessage(DEFAULT_MSG);
321             }
322
323         }
324         return success;
325     }
326
327     /**
328      * Creates new attribute group.
329      */

330     public AttributeGroup doCreatenewgroup (RunData data,
331                                              TemplateContext context)
332         throws Exception JavaDoc
333     {
334         ScarabRequestTool scarabR = getScarabRequestTool(context);
335         ScarabLocalizationTool l10n = getLocalizationTool(context);
336         Module module = scarabR.getCurrentModule();
337         IssueType issueType = scarabR.getIssueType();
338         if (issueType.isSystemDefined())
339         {
340             scarabR.setAlertMessage(L10NKeySet.SystemSpecifiedIssueType);
341             return null;
342         }
343         scarabR.setConfirmMessage(DEFAULT_MSG);
344         return issueType.createNewGroup(module);
345     }
346
347     /**
348      * Deletes an attribute group.
349      */

350     public void doDeletegroup (RunData data, TemplateContext context)
351         throws Exception JavaDoc
352     {
353         ScarabRequestTool scarabR = getScarabRequestTool(context);
354         ScarabLocalizationTool l10n = getLocalizationTool(context);
355         IssueType issueType = scarabR.getIssueType();
356         boolean noAGSelected = true;
357         if (issueType.isSystemDefined())
358         {
359             scarabR.setAlertMessage(L10NKeySet.SystemSpecifiedIssueType);
360             return;
361         }
362         if (issueType.getLocked())
363         {
364             scarabR.setAlertMessage(L10NKeySet.LockedIssueType);
365             return;
366         }
367
368         ParameterParser params = data.getParameters();
369         Object JavaDoc[] keys = params.getKeys();
370         String JavaDoc key;
371         String JavaDoc groupId;
372         Module module = scarabR.getCurrentModule();
373         List JavaDoc attributeGroups = issueType.getAttributeGroups(module, false);
374
375         for (int i =0; i<keys.length; i++)
376         {
377             key = keys[i].toString();
378             if (key.startsWith("group_action"))
379             {
380                 try
381                 {
382                     groupId = key.substring(13);
383                     AttributeGroup ag = AttributeGroupManager
384                        .getInstance(new NumberKey(groupId), false);
385                     ag.delete();
386                     noAGSelected = false;
387                     scarabR.setConfirmMessage(DEFAULT_MSG);
388                     ScarabCache.clear();
389                     getIntakeTool(context).removeAll();
390                 }
391                 catch (Exception JavaDoc e)
392                 {
393                     scarabR.setAlertMessage(NO_PERMISSION_MESSAGE);
394                 }
395                 if (attributeGroups.size() -1 < 2)
396                 {
397                     // If there are fewer than 2 attribute groups,
398
// Turn of deduping
399
RModuleIssueType rmit = module.getRModuleIssueType(issueType);
400                     rmit.setDedupe(false);
401                     rmit.save();
402                 }
403             }
404         }
405         if (noAGSelected)
406         {
407            scarabR.setAlertMessage(L10NKeySet.NoAttributeGroupSelected);
408         }
409     }
410
411
412     /**
413      * Unmaps attributes to modules.
414      */

415     public void doDeleteuserattribute(RunData data, TemplateContext context)
416         throws Exception JavaDoc
417     {
418         ScarabRequestTool scarabR = getScarabRequestTool(context);
419         ScarabLocalizationTool l10n = getLocalizationTool(context);
420         IssueType issueType = scarabR.getIssueType();
421         boolean hasAttributes = false;
422         if (issueType.isSystemDefined())
423         {
424             scarabR.setAlertMessage(L10NKeySet.SystemSpecifiedIssueType);
425             return;
426         }
427         if (issueType.getLocked())
428         {
429             scarabR.setAlertMessage(L10NKeySet.LockedIssueType);
430             return;
431         }
432         Module module = scarabR.getCurrentModule();
433         ParameterParser params = data.getParameters();
434         Object JavaDoc[] keys = params.getKeys();
435         String JavaDoc key;
436         String JavaDoc attributeId;
437
438         for (int i =0; i<keys.length; i++)
439         {
440             key = keys[i].toString();
441             if (key.startsWith("att_delete_"))
442             {
443                hasAttributes = true;
444                attributeId = key.substring(11);
445                Attribute attribute = AttributeManager
446                    .getInstance(new NumberKey(attributeId), false);
447
448                // Remove attribute - module mapping
449
RModuleAttribute rma = module
450                    .getRModuleAttribute(attribute, issueType);
451                rma.delete();
452
453                // Remove attribute - module mapping from template type
454
RModuleAttribute rma2 = module
455                    .getRModuleAttribute(attribute,
456                    scarabR.getIssueType(issueType.getTemplateId().toString()));
457                rma2.delete();
458                scarabR.setConfirmMessage(DEFAULT_MSG);
459                ScarabCache.clear();
460            }
461         }
462         if(!hasAttributes)
463         {
464             scarabR.setAlertMessage(L10NKeySet.NoUserAttributeSelected);
465         }
466     }
467
468
469     public void doCreatenewuserattribute(RunData data,
470                                             TemplateContext context)
471         throws Exception JavaDoc
472     {
473         IntakeTool intake = getIntakeTool(context);
474         ScarabRequestTool scarabR = getScarabRequestTool(context);
475         IssueType issueType = scarabR.getIssueType();
476         if (issueType.isSystemDefined())
477         {
478             scarabR.setAlertMessage(L10NKeySet.SystemSpecifiedIssueType);
479             return;
480         }
481         Group attGroup = intake.get("Attribute", IntakeTool.DEFAULT_KEY);
482         intake.remove(attGroup);
483         scarabR.setAttribute(null);
484         setTarget(data, getOtherTemplate(data));
485     }
486
487     /**
488      * Selects attribute to add to issue type.
489      */

490     public void doSelectuserattribute(RunData data, TemplateContext context)
491         throws Exception JavaDoc
492     {
493         ScarabRequestTool scarabR = getScarabRequestTool(context);
494         ScarabLocalizationTool l10n = getLocalizationTool(context);
495         IssueType issueType = scarabR.getIssueType();
496         if (issueType.isSystemDefined())
497         {
498             scarabR.setAlertMessage(L10NKeySet.SystemSpecifiedIssueType);
499             return;
500         }
501         if (issueType.getLocked())
502         {
503             scarabR.setAlertMessage(L10NKeySet.LockedIssueType);
504             return;
505         }
506
507         Module module = scarabR.getCurrentModule();
508         String JavaDoc[] attributeIds = data.getParameters()
509                                     .getStrings("attribute_ids");
510  
511         if (attributeIds == null || attributeIds.length <= 0)
512         {
513             scarabR.setAlertMessage(L10NKeySet.SelectAttribute);
514             return;
515         }
516         else
517         {
518             for (int i=0; i < attributeIds.length; i++)
519             {
520                 Attribute attribute =
521                     scarabR.getAttribute(new Integer JavaDoc(attributeIds[i]));
522                 if (attribute != null)
523                 {
524                     // add module-attribute groupings
525
module.addRModuleAttribute(issueType,attribute);
526                 }
527                 doCancel(data, context);
528             }
529         }
530     }
531
532     /**
533      * Manages clicking of the AllDone button
534      */

535     public void doDone(RunData data, TemplateContext context)
536         throws Exception JavaDoc
537     {
538         boolean success = doSaveinfo(data, context) &&
539                               doSavegroups(data, context) &&
540                                   doSaveuserattributes(data, context);
541         if (success)
542         {
543             doCancel(data, context);
544         }
545         //Reset confirm message in case some of the changes got saved
546
else
547         {
548             getScarabRequestTool(context).setConfirmMessage(null);
549         }
550     }
551
552 }
553
Popular Tags