KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
50
51 import org.apache.fulcrum.intake.model.Field;
52 import org.apache.fulcrum.intake.model.Group;
53 import org.apache.fulcrum.parser.ParameterParser;
54 import org.apache.torque.om.NumberKey;
55 import org.apache.turbine.RunData;
56 import org.apache.turbine.TemplateContext;
57 import org.apache.turbine.tool.IntakeTool;
58 import org.tigris.scarab.actions.base.RequireLoginFirstAction;
59 import org.tigris.scarab.om.Attribute;
60 import org.tigris.scarab.om.AttributeGroup;
61 import org.tigris.scarab.om.AttributeGroupManager;
62 import org.tigris.scarab.om.AttributeManager;
63 import org.tigris.scarab.om.IssueType;
64 import org.tigris.scarab.om.IssueTypePeer;
65 import org.tigris.scarab.om.RIssueTypeAttribute;
66 import org.tigris.scarab.services.cache.ScarabCache;
67 import org.tigris.scarab.tools.ScarabLocalizationTool;
68 import org.tigris.scarab.tools.ScarabRequestTool;
69 import org.tigris.scarab.util.Log;
70
71 /**
72  * This class deals with modifying Global Artifact Types.
73  *
74  * @author <a HREF="mailto:elicia@collab.net">Elicia David</a>
75  * @version $Id: GlobalArtifactTypeCreate.java 9255 2004-11-14 21:07:04Z dep4b $
76  */

77 public class GlobalArtifactTypeCreate extends RequireLoginFirstAction
78 {
79
80     /**
81      * creates or edits global artifact type
82      */

83     public boolean doSaveinfo(RunData data, TemplateContext context)
84         throws Exception JavaDoc
85     {
86         boolean success = true;
87         IntakeTool intake = getIntakeTool(context);
88         ScarabRequestTool scarabR = getScarabRequestTool(context);
89         ScarabLocalizationTool l10n = getLocalizationTool(context);
90         IssueType issueType = getScarabRequestTool(context).getIssueType();
91         if (issueType.isSystemDefined())
92         {
93             scarabR.setAlertMessage(l10n.get("SystemSpecifiedIssueType"));
94             return false;
95         }
96         Group group = intake.get("IssueType", issueType.getQueryKey());
97         Field field = group.get("Name");
98         String JavaDoc name = field.toString();
99         Integer JavaDoc id = issueType.getIssueTypeId();
100
101         if (intake.isAllValid())
102         {
103             if (id == null)
104             {
105                 // Create new issue type
106
// make sure name is unique
107
if (IssueTypePeer.isUnique(name, null))
108                 {
109                     group.setProperties(issueType);
110                     issueType.setParentId(IssueTypePeer.getRootKey());
111                     issueType.save();
112                     
113                     // Create default attribute groups
114
issueType.createDefaultGroups();
115
116                     // Create template type.
117
IssueType template = new IssueType();
118                     template.setName(issueType.getName() + " Template");
119                     template.setParentId(issueType.getIssueTypeId());
120                     template.save();
121                 }
122                 else
123                 {
124                     scarabR.setAlertMessage(l10n.get("IssueTypeNameExists"));
125                 }
126             }
127             else
128             {
129                 // Edit existing issue type
130
if (IssueTypePeer.isUnique(name, id))
131                 {
132                     // Cannot delete an issue type that has issues
133
Field deleted = group.get("Deleted");
134                     if (deleted != null && deleted.toString().equals("true")
135                         && issueType.hasIssues())
136                     {
137                         scarabR.setAlertMessage(l10n.get(ERROR_MESSAGE));
138                         deleted.setMessage("IssueTypeHasIssues");
139                         success = false;
140                     }
141                     else
142                     {
143                         group.setProperties(issueType);
144                         issueType.save();
145                         scarabR.setConfirmMessage(l10n.get(DEFAULT_MSG));
146                     }
147                 }
148                 else
149                 {
150                     success = false;
151                     scarabR.setAlertMessage(l10n.get("IssueTypeNameExists"));
152                 }
153             }
154         }
155         else
156         {
157             scarabR.setAlertMessage(l10n.get(ERROR_MESSAGE));
158             success = false;
159         }
160         return success;
161     }
162
163
164     /**
165      * Adds or modifies an issue type's attribute groups.
166      */

167     public boolean doSavegroups (RunData data, TemplateContext context)
168         throws Exception JavaDoc
169     {
170         boolean success = true;
171         IntakeTool intake = getIntakeTool(context);
172         ScarabRequestTool scarabR = getScarabRequestTool(context);
173         ScarabLocalizationTool l10n = getLocalizationTool(context);
174         IssueType issueType = scarabR.getIssueType();
175         if (issueType.isSystemDefined())
176         {
177             scarabR.setAlertMessage(l10n.get("SystemSpecifiedIssueType"));
178             return false;
179         }
180         List JavaDoc attGroups = issueType.getAttributeGroups(null, false);
181         int nbrAttGroups = attGroups.size();
182         int dupeOrder = 2;
183
184         // Manage attribute groups, only seeking sequence collisions
185
// when there is more than one active group.
186
if (issueType.getAttributeGroups(null, true).size() > 1)
187         {
188             dupeOrder = data.getParameters().getInt("dupe_order");
189
190             // Check that duplicate check is not at the beginning.
191
if (dupeOrder == 1)
192             {
193                 scarabR.setAlertMessage(l10n.get("CannotPositionDuplicateCheckFirst"));
194                 return false;
195             }
196
197             // Check for duplicate sequence numbers
198
if (areThereDupeSequences(attGroups, intake, "AttributeGroup",
199                     "Order", dupeOrder))
200             {
201                 scarabR.setAlertMessage(l10n.format("DuplicateSequenceNumbersFound",
202                     l10n.get("AttributeGroups").toLowerCase()));
203                 return false;
204             }
205         }
206
207         if (intake.isAllValid())
208         {
209             boolean areThereDedupeAttrs = false;
210             // Set properties for attribute groups
211
for (int i = nbrAttGroups - 1; i >= 0; i--)
212             {
213                 AttributeGroup attGroup = (AttributeGroup)attGroups.get(i);
214                 Group agGroup = intake.get("AttributeGroup",
215                                  attGroup.getQueryKey(), false);
216                 agGroup.setProperties(attGroup);
217
218                 // If an attribute group falls before the dedupe
219
// screen, mark it as a dedupe group. Even groups
220
// which are currently empty of attributes should be
221
// marked as such, as attributes may later be added to
222
// them.
223
areThereDedupeAttrs = attGroup.getOrder() < dupeOrder;
224                 attGroup.setDedupe(areThereDedupeAttrs);
225                 attGroup.save();
226             }
227             if (areThereDedupeAttrs)
228             {
229                 Group itGroup = intake.get("IssueType",
230                                         issueType.getQueryKey(), false);
231                 Field dedupe = itGroup.get("Dedupe");
232                 dedupe.setProperty(issueType);
233             }
234             issueType.save();
235             ScarabCache.clear();
236             scarabR.setConfirmMessage(l10n.get(DEFAULT_MSG));
237         }
238         else
239         {
240             scarabR.setAlertMessage(l10n.get(ERROR_MESSAGE));
241             success = false;
242         }
243         return success;
244     }
245
246     /**
247      * Redirects to create new user attribute screen.
248      */

249     public void doCreatenewuserattribute(RunData data,
250                                           TemplateContext context)
251         throws Exception JavaDoc
252     {
253         IntakeTool intake = getIntakeTool(context);
254         ScarabRequestTool scarabR = getScarabRequestTool(context);
255         ScarabLocalizationTool l10n = getLocalizationTool(context);
256         IssueType issueType = scarabR.getIssueType();
257         if (issueType.isSystemDefined())
258         {
259             scarabR.setAlertMessage(l10n.get("SystemSpecifiedIssueType"));
260             return;
261         }
262         Group attGroup = intake.get("Attribute", IntakeTool.DEFAULT_KEY);
263         intake.remove(attGroup);
264         scarabR.setAttribute(null);
265         setTarget(data, getOtherTemplate(data));
266     }
267
268     /**
269      * Creates new attribute group.
270      */

271     public AttributeGroup doCreatenewgroup (RunData data,
272                                              TemplateContext context)
273         throws Exception JavaDoc
274     {
275         ScarabRequestTool scarabR = getScarabRequestTool(context);
276         ScarabLocalizationTool l10n = getLocalizationTool(context);
277         IssueType issueType = scarabR.getIssueType();
278         if (issueType.isSystemDefined())
279         {
280             scarabR.setAlertMessage(l10n.get("SystemSpecifiedIssueType"));
281             return null;
282         }
283         scarabR.setConfirmMessage(l10n.get(DEFAULT_MSG));
284         return issueType.createNewGroup();
285     }
286
287     /**
288      * Deletes an attribute group.
289      */

290     public void doDeletegroup (RunData data, TemplateContext context)
291         throws Exception JavaDoc
292     {
293         ParameterParser params = data.getParameters();
294         Object JavaDoc[] keys = params.getKeys();
295         String JavaDoc key;
296         String JavaDoc groupId;
297         ScarabRequestTool scarabR = getScarabRequestTool(context);
298         ScarabLocalizationTool l10n = getLocalizationTool(context);
299         IssueType issueType = scarabR.getIssueType();
300         if (issueType.isSystemDefined())
301         {
302             scarabR.setAlertMessage(l10n.get("SystemSpecifiedIssueType"));
303             return;
304         }
305         List JavaDoc attributeGroups = issueType.getAttributeGroups(null, false);
306
307         boolean atLeastOne = false;
308         for (int i =0; i<keys.length; i++)
309         {
310             key = keys[i].toString();
311             if (key.startsWith("group_action"))
312             {
313                 atLeastOne = true;
314                 groupId = key.substring(13);
315                 AttributeGroup ag = AttributeGroupManager
316                     .getInstance(new NumberKey(groupId), false);
317                 if (Log.get().isDebugEnabled())
318                 {
319                     Log.get().info("Deleting attribute group: " + groupId);
320                 }
321                 ag.delete();
322                 ScarabCache.clear();
323             }
324         }
325         if (atLeastOne)
326         {
327             scarabR.setConfirmMessage(l10n.get("SelectedGroupDeleted"));
328             // TODO: this logic seems off by one to me (jmcnally), need
329
// to document why this is not so, as it seems to work
330
if (attributeGroups.size() -1 < 2)
331             {
332                 // If there are fewer than 2 attribute groups,
333
// Turn off deduping
334
issueType.setDedupe(false);
335                 issueType.save();
336             }
337         }
338         else
339         {
340             scarabR.setAlertMessage(l10n.get("NoGroupSelected"));
341         }
342     }
343
344     /**
345      * Selects attribute to add to issue type.
346      */

347     public void doSelectuserattribute(RunData data, TemplateContext context)
348         throws Exception JavaDoc
349     {
350         ScarabRequestTool scarabR = getScarabRequestTool(context);
351         ScarabLocalizationTool l10n = getLocalizationTool(context);
352         IssueType issueType = scarabR.getIssueType();
353         if (issueType.isSystemDefined())
354         {
355             scarabR.setAlertMessage(l10n.get("SystemSpecifiedIssueType"));
356             return;
357         }
358         String JavaDoc[] attributeIds = data.getParameters()
359                                     .getStrings("attribute_ids");
360  
361         if (attributeIds == null || attributeIds.length <= 0)
362         {
363             scarabR.setAlertMessage(l10n.get("SelectAttribute"));
364             return;
365         }
366         else
367         {
368             for (int i=0; i < attributeIds.length; i++)
369             {
370                 Attribute attribute =
371                     scarabR.getAttribute(new Integer JavaDoc(attributeIds[i]));
372                 if (attribute != null)
373                 {
374                     // add issuetype-attribute groupings
375
issueType.addRIssueTypeAttribute(attribute);
376                 }
377                 doCancel(data, context);
378                 ScarabCache.clear();
379             }
380         }
381     }
382
383     /**
384      * Unmaps attributes to issue types.
385      */

386     public void doDeleteuserattribute(RunData data, TemplateContext context)
387         throws Exception JavaDoc
388     {
389         ScarabRequestTool scarabR = getScarabRequestTool(context);
390         ScarabLocalizationTool l10n = getLocalizationTool(context);
391         ParameterParser params = data.getParameters();
392         IssueType issueType = scarabR.getIssueType();
393         if (issueType.isSystemDefined())
394         {
395             scarabR.setAlertMessage(l10n.get("SystemSpecifiedIssueType"));
396             return;
397         }
398         Object JavaDoc[] keys = params.getKeys();
399         String JavaDoc key;
400         String JavaDoc attributeId;
401         boolean atLeastOne = false;
402
403         for (int i =0; i<keys.length; i++)
404         {
405             key = keys[i].toString();
406             if (key.startsWith("att_delete_"))
407             {
408                atLeastOne = true;
409                attributeId = key.substring(11);
410                Attribute attribute = AttributeManager
411                    .getInstance(new NumberKey(attributeId), false);
412
413                // Remove attribute - issue type mapping
414
RIssueTypeAttribute ria = issueType
415                    .getRIssueTypeAttribute(attribute);
416                try
417                {
418                    ria.delete();
419                }
420                catch (Exception JavaDoc e)
421                {
422                    scarabR.setAlertMessage(l10n.get(NO_PERMISSION_MESSAGE));
423                }
424
425                scarabR.setConfirmMessage(l10n.get(DEFAULT_MSG));
426                ScarabCache.clear();
427            }
428         }
429         if (!atLeastOne)
430         {
431            scarabR.setAlertMessage(l10n.get("NoAttributesSelected"));
432         }
433     }
434
435     /**
436      * Adds or modifies user attributes' properties
437      */

438     public boolean doSaveuserattributes (RunData data, TemplateContext context)
439         throws Exception JavaDoc
440     {
441         IntakeTool intake = getIntakeTool(context);
442         ScarabRequestTool scarabR = getScarabRequestTool(context);
443         IssueType issueType = scarabR.getIssueType();
444         ScarabLocalizationTool l10n = getLocalizationTool(context);
445         if (issueType.isSystemDefined())
446         {
447             scarabR.setAlertMessage(l10n.get("SystemSpecifiedIssueType"));
448             return false;
449         }
450         if (intake.isAllValid())
451         {
452             List JavaDoc rias = issueType.getRIssueTypeAttributes(false,"user");
453             if (areThereDupeSequences(rias, intake, "RIssueTypeAttribute",
454                                           "Order", 0))
455             {
456                 scarabR.setAlertMessage(l10n.format("DuplicateSequenceNumbersFound",
457                     l10n.get("UserAttributes").toLowerCase()));
458                 return false;
459             }
460             for (int i=0; i < rias.size(); i++)
461             {
462                 // Set properties for issue type-attribute mapping
463
RIssueTypeAttribute ria = (RIssueTypeAttribute)rias.get(i);
464                 Group riaGroup = intake.get("RIssueTypeAttribute",
465                                  ria.getQueryKey(), false);
466                 riaGroup.setProperties(ria);
467                 ria.save();
468             }
469             getScarabRequestTool(context)
470                 .setConfirmMessage(l10n.get(DEFAULT_MSG));
471         }
472         return true;
473     }
474
475     /**
476      * Manages clicking of the AllDone button
477      */

478     public void doDone(RunData data, TemplateContext context)
479         throws Exception JavaDoc
480     {
481         boolean success = doSaveinfo(data, context) &&
482                               doSavegroups(data, context) &&
483                                   doSaveuserattributes(data, context);
484         if (success)
485         {
486             doCancel(data, context);
487         }
488         //Reset confirm message in case some of the changes got saved
489
else
490         {
491             getScarabRequestTool(context).setConfirmMessage(null);
492         }
493     }
494
495     /**
496      * Overridden method to check for system defined issue types
497      * and prevent New attributes from being added to them.
498      */

499
500     public void doGotoothertemplate(RunData data,
501                                      TemplateContext context)
502         throws Exception JavaDoc
503     {
504         ScarabRequestTool scarabR = getScarabRequestTool(context);
505         IssueType issueType = scarabR.getIssueType();
506         ScarabLocalizationTool l10n = getLocalizationTool(context);
507         if (issueType.isSystemDefined())
508         {
509             scarabR.setAlertMessage(l10n.get("SystemSpecifiedIssueType"));
510             return;
511         }
512         super.doGotoothertemplate(data,context);
513     }
514 }
515
Popular Tags