KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.tigris.scarab.actions;
2
3 /* ================================================================
4  * Copyright (c) 2000-2003 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.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52
53 import org.apache.commons.collections.MapIterator;
54 import org.apache.commons.collections.map.LinkedMap;
55 import org.apache.commons.lang.StringUtils;
56 import org.apache.fulcrum.intake.model.Group;
57 import org.apache.torque.om.NumberKey;
58 import org.apache.turbine.RunData;
59 import org.apache.turbine.TemplateContext;
60 import org.apache.turbine.tool.IntakeTool;
61 import org.tigris.scarab.actions.base.RequireLoginFirstAction;
62 import org.tigris.scarab.attribute.OptionAttribute;
63 import org.tigris.scarab.om.ActivitySet;
64 import org.tigris.scarab.om.ActivitySetManager;
65 import org.tigris.scarab.om.ActivitySetTypePeer;
66 import org.tigris.scarab.om.AttributeOption;
67 import org.tigris.scarab.om.AttributeOptionManager;
68 import org.tigris.scarab.om.AttributeValue;
69 import org.tigris.scarab.om.Issue;
70 import org.tigris.scarab.om.IssueManager;
71 import org.tigris.scarab.om.IssueTemplateInfo;
72 import org.tigris.scarab.om.IssueTemplateInfoPeer;
73 import org.tigris.scarab.om.IssueType;
74 import org.tigris.scarab.om.Module;
75 import org.tigris.scarab.om.ScarabUser;
76 import org.tigris.scarab.om.Scope;
77 import org.tigris.scarab.tools.ScarabLocalizationTool;
78 import org.tigris.scarab.tools.ScarabRequestTool;
79 import org.tigris.scarab.tools.localization.L10NKeySet;
80 import org.tigris.scarab.util.ScarabConstants;
81 import org.tigris.scarab.util.ScarabException;
82
83
84 /**
85  * This class is responsible for report managing issue entry
86  * templates.
87  *
88  * @author <a HREF="mailto:elicia@collab.net">Elicia David</a>
89  * @version $Id: TemplateList.java 9290 2004-12-03 15:57:52Z dep4b $
90  */

91 public class TemplateList extends RequireLoginFirstAction
92 {
93     /**
94      * Creates new template.
95      */

96     public void doCreatenew(RunData data, TemplateContext context)
97          throws Exception JavaDoc
98     {
99         IntakeTool intake = getIntakeTool(context);
100         ScarabRequestTool scarabR = getScarabRequestTool(context);
101         ScarabLocalizationTool l10n = getLocalizationTool(context);
102         ScarabUser user = (ScarabUser)data.getUser();
103         Issue issue = scarabR.getIssueTemplate();
104         if (issue == null)
105         {
106             scarabR.setAlertMessage(L10NKeySet.IssueTypeNotAvailable);
107             return;
108         }
109
110         LinkedMap avMap = issue.getModuleAttributeValuesMap();
111         AttributeValue aval = null;
112         Group group = null;
113         
114         IssueTemplateInfo info = scarabR.getIssueTemplateInfo();
115         Group infoGroup = intake.get("IssueTemplateInfo", info.getQueryKey());
116         Group issueGroup = intake.get("Issue", issue.getQueryKey());
117         ActivitySet activitySet = null;
118
119         if (intake.isAllValid())
120         {
121             issueGroup.setProperties(issue);
122             infoGroup.setProperties(info);
123             if (checkForDupes(info, infoGroup.get("Name").toString(),
124                               user, issue))
125             {
126                 scarabR.setAlertMessage(L10NKeySet.DuplicateTemplateName);
127             }
128             else
129             {
130                 boolean atLeastOne = false;
131                 MapIterator iter = avMap.mapIterator();
132                 if (iter.hasNext())
133                 {
134                     // Save activitySet record
135
activitySet = ActivitySetManager
136                         .getInstance(ActivitySetTypePeer.CREATE_ISSUE__PK, user);
137                     activitySet.save();
138                     while (iter.hasNext())
139                     {
140                         aval = (AttributeValue)avMap.get(iter.next());
141                         group = intake.get("AttributeValue", aval.getQueryKey(),false);
142                         String JavaDoc value = null;
143                         if (group != null)
144                         {
145                             if (aval instanceof OptionAttribute)
146                             {
147                                 value = group.get("OptionId").toString();
148                             }
149                             else
150                             {
151                                 value = group.get("Value").toString();
152                             }
153                             if (StringUtils.isNotEmpty(value))
154                             {
155                                 atLeastOne = true;
156                                 aval.startActivitySet(activitySet);
157                                 group.setProperties(aval);
158                             }
159                         }
160                     }
161                 }
162                 if (atLeastOne)
163                 {
164                     issue.setCreatedTransId(activitySet.getActivitySetId());
165                     issue.save();
166                     info.setIssueId(issue.getIssueId());
167
168                     // Save template info
169
info.saveAndSendEmail(user,
170                         scarabR.getCurrentModule(),
171                         context);
172
173                     // For a module-scoped template which is now
174
// pending approval, the user may not have
175
// permission to edit the new issue template.
176
if (info.canEdit(user))
177                     {
178                         data.getParameters().add
179                             ("templateId", issue.getIssueId().toString());
180                         scarabR.setConfirmMessage(L10NKeySet.NewTemplateCreated);
181                     }
182                     else
183                     {
184                         // Display the list of issue templates.
185
scarabR.setInfoMessage(
186                             l10n.format("NotifyPendingApproval",
187                             l10n.get("Template").toLowerCase()));
188                         setTarget(data, "TemplateList.vm");
189                         doPerform(data, context);
190                     }
191                 }
192                 else
193                 {
194                     scarabR.setAlertMessage(
195                                 L10NKeySet.AtLeastOneAttributeForTemplate);
196                 }
197             }
198         }
199         else
200         {
201             scarabR.setAlertMessage(ERROR_MESSAGE);
202         }
203     }
204
205     /**
206      * Edits template's attribute values.
207      */

208     public void doEditvalues(RunData data, TemplateContext context)
209          throws Exception JavaDoc
210     {
211         IntakeTool intake = getIntakeTool(context);
212         ScarabRequestTool scarabR = getScarabRequestTool(context);
213         ScarabLocalizationTool l10n = getLocalizationTool(context);
214         ScarabUser user = (ScarabUser)data.getUser();
215         Issue issue = scarabR.getIssueTemplate();
216
217         LinkedMap avMap = issue.getModuleAttributeValuesMap();
218         AttributeValue aval = null;
219         Group group = null;
220         Group issueGroup = intake.get("Issue", issue.getQueryKey());
221         issueGroup.setProperties(issue);
222
223         if (intake.isAllValid())
224         {
225             AttributeValue aval2 = null;
226             HashMap JavaDoc newAttVals = new HashMap JavaDoc();
227             boolean modifiedAttribute = false;
228
229             Iterator JavaDoc iter = avMap.mapIterator();
230             while (iter.hasNext())
231             {
232                 aval = (AttributeValue)avMap.get(iter.next());
233                 aval2 = AttributeValue.getNewInstance(aval.getAttributeId(),
234                                                       aval.getIssue());
235                 aval2.setProperties(aval);
236                 group = intake.get("AttributeValue", aval.getQueryKey(),false);
237                 if (group != null)
238                 {
239                     String JavaDoc newValue = "";
240                     String JavaDoc oldValue = "";
241                     if (aval instanceof OptionAttribute)
242                     {
243                         newValue = group.get("OptionId").toString();
244                         oldValue = aval.getOptionIdAsString();
245                     
246                         if (!newValue.equals(""))
247                         {
248                             AttributeOption newAttributeOption =
249                               AttributeOptionManager
250                               .getInstance(new Integer JavaDoc(newValue));
251                             newValue = newAttributeOption.getName();
252                         }
253                         if (!oldValue.equals(""))
254                         {
255                             Integer JavaDoc oldOptionId = aval.getOptionId();
256                             AttributeOption oldAttributeOption =
257                               AttributeOptionManager
258                               .getInstance(oldOptionId);
259                             oldValue = oldAttributeOption.getName();
260                         }
261                         
262                     }
263                     else
264                     {
265                         newValue = group.get("Value").toString();
266                         oldValue = aval.getValue();
267                     }
268                     // A value has been entered for the attribute.
269
// The old value is different from the new, or is unset:
270
// Set new value.
271
if (newValue.trim().length() > 0 && (oldValue == null ||
272                           !newValue.trim().equals(oldValue.trim())))
273                     {
274                         group.setProperties(aval2);
275                         newAttVals.put(aval.getAttributeId(), aval2);
276                         modifiedAttribute = true;
277                     }
278                     // The attribute is being undefined.
279
else if (oldValue != null && newValue.trim().length() == 0 &&
280                              oldValue.trim().length() != 0)
281                     {
282                         aval2.setValue(null);
283                         newAttVals.put(aval.getAttributeId(), aval2);
284                         modifiedAttribute = true;
285                     }
286                 }
287             }
288             //save data only if there is a change
289
if (modifiedAttribute)
290             {
291                 issue.setAttributeValues(null, newAttVals, null, user);
292                 intake.removeAll();
293             }
294             scarabR.setConfirmMessage(L10NKeySet.TemplateModified);
295         }
296         else
297         {
298             scarabR.setAlertMessage(ERROR_MESSAGE);
299         }
300     }
301
302     /**
303      * Edits templates's basic information.
304      */

305     public boolean doEdittemplateinfo(RunData data, TemplateContext context)
306          throws Exception JavaDoc
307     {
308         IntakeTool intake = getIntakeTool(context);
309         ScarabRequestTool scarabR = getScarabRequestTool(context);
310         ScarabLocalizationTool l10n = getLocalizationTool(context);
311         ScarabUser user = (ScarabUser)data.getUser();
312         Issue issue = scarabR.getIssueTemplate();
313         boolean success = true;
314
315         IssueTemplateInfo info = scarabR.getIssueTemplateInfo();
316         Group infoGroup = intake.get("IssueTemplateInfo", info.getQueryKey());
317
318         if (intake.isAllValid())
319         {
320             infoGroup.setProperties(info);
321             info.setIssueId(issue.getIssueId());
322             if (checkForDupes(info, infoGroup.get("Name").toString(),
323                               user, issue))
324             {
325                 success = false;
326                 scarabR.setAlertMessage(L10NKeySet.DuplicateTemplateName);
327             }
328             else
329             {
330                 // Save template info
331
info.saveAndSendEmail(user, scarabR.getCurrentModule(), context);
332                 data.getParameters().add("templateId", issue.getIssueId().toString());
333                 if (info.canEdit(user))
334                 {
335                     scarabR.setConfirmMessage(L10NKeySet.TemplateModified);
336                 }
337                 else
338                 {
339                     scarabR.setInfoMessage(
340                                     l10n.format("NotifyPendingApproval",
341                                     l10n.get("Template").toLowerCase()));
342                     setTarget(data, data.getParameters().getString(
343                                     ScarabConstants.CANCEL_TEMPLATE));
344                 }
345             }
346         }
347         else
348         {
349             success = false;
350             scarabR.setAlertMessage(ERROR_MESSAGE);
351         }
352         return success;
353     }
354
355     public void doDeletetemplates(RunData data, TemplateContext context)
356         throws Exception JavaDoc
357     {
358         ScarabLocalizationTool l10n = getLocalizationTool(context);
359         ScarabRequestTool scarabR = getScarabRequestTool(context);
360         Object JavaDoc[] keys = data.getParameters().getKeys();
361         String JavaDoc key;
362         String JavaDoc templateId;
363         ScarabUser user = (ScarabUser)data.getUser();
364         boolean atLeastOne = false;
365         boolean success = true;
366
367         for (int i =0; i<keys.length; i++)
368         {
369             key = keys[i].toString();
370             if (key.startsWith("delete_"))
371             {
372                 atLeastOne = true;
373                 templateId = key.substring(7);
374                 try
375                 {
376                     Issue issue = IssueManager
377                        .getInstance(new NumberKey(templateId), false);
378                     if (issue == null)
379                     {
380                         throw new Exception JavaDoc(
381                             l10n.get("CouldNotLocateTemplateToDelete")); //EXCEPTION
382
}
383                     issue.delete(user);
384                 }
385                 catch (ScarabException e)
386                 {
387                     success = false;
388                     scarabR.setAlertMessage(NO_PERMISSION_MESSAGE);
389                 }
390                 catch (Exception JavaDoc e)
391                 {
392                     success = false;
393                     scarabR.setAlertMessage(l10n.getMessage(e));
394                 }
395             }
396         }
397         if (!atLeastOne)
398         {
399             scarabR.setAlertMessage(L10NKeySet.NoTemplateSelected);
400         }
401         else if (success)
402         {
403             scarabR.setConfirmMessage(L10NKeySet.TemplateDeleted);
404         }
405     }
406
407     public void doUsetemplate(RunData data, TemplateContext context)
408         throws Exception JavaDoc
409     {
410         IntakeTool intake = getIntakeTool(context);
411         intake.removeAll();
412         ScarabRequestTool scarabR = getScarabRequestTool(context);
413         String JavaDoc templateId = data.getParameters().getString("templateId");
414         if (templateId != null && templateId.length() > 0)
415         {
416             IssueType templateType = IssueManager.getInstance(
417                 new Long JavaDoc(templateId)).getIssueType();
418             setTarget(data, scarabR.getNextEntryTemplate(
419                 templateType.getIssueTypeForTemplateType()));
420             ReportIssue.cleanOutStaleIssue(data, context);
421         }
422     }
423     
424     public void doSave(RunData data, TemplateContext context)
425         throws Exception JavaDoc
426     {
427         doEditvalues(data, context);
428         doEdittemplateinfo(data, context);
429     }
430
431     private boolean checkForDupes(IssueTemplateInfo template,
432                                   String JavaDoc newName, ScarabUser user,
433                                   Issue issueTemplate)
434         throws Exception JavaDoc
435     {
436         boolean areThereDupes = false;
437         Module module = issueTemplate.getModule();
438         List JavaDoc prevTemplates = IssueTemplateInfoPeer.getUserTemplates(user,
439             module, issueTemplate.getIssueType().getIssueTypeForTemplateType());
440         if (template.getScopeId().equals(Scope.MODULE__PK))
441         {
442             prevTemplates.addAll(IssueTemplateInfoPeer.getModuleTemplates(module));
443         }
444         if (prevTemplates != null && !prevTemplates.isEmpty())
445         {
446             Long JavaDoc pk = template.getIssueId();
447             for (Iterator JavaDoc i = prevTemplates.iterator();
448                  i.hasNext() && !areThereDupes;)
449             {
450                 IssueTemplateInfo t = (IssueTemplateInfo)i.next();
451                 areThereDupes = ((pk == null || !pk.equals(t.getIssueId())) &&
452                     newName.trim().toLowerCase().equals(
453                         t.getName().trim().toLowerCase()));
454             }
455         }
456         return areThereDupes;
457     }
458
459     /**
460         Overrides base class.
461     */

462     public void doDone(RunData data, TemplateContext context)
463         throws Exception JavaDoc
464     {
465         boolean success = doEdittemplateinfo(data, context);
466         if (success)
467         {
468             doEditvalues(data, context);
469             doCancel(data, context);
470         }
471     }
472 }
473
Popular Tags