KickJava   Java API By Example, From Geeks To Geeks.

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


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 CollabNet <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 CollabNet.
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 CollabNet.
47  */

48
49 import java.util.ArrayList JavaDoc;
50 import java.util.HashMap JavaDoc;
51 import java.util.HashSet JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.Set JavaDoc;
56 import java.util.StringTokenizer JavaDoc;
57
58 import org.apache.commons.collections.MapIterator;
59 import org.apache.commons.collections.map.LinkedMap;
60 import org.apache.fulcrum.intake.model.Field;
61 import org.apache.fulcrum.intake.model.Group;
62 import org.apache.fulcrum.parser.ParameterParser;
63 import org.apache.turbine.RunData;
64 import org.apache.turbine.TemplateContext;
65 import org.apache.turbine.tool.IntakeTool;
66 import org.tigris.scarab.actions.base.RequireLoginFirstAction;
67 import org.tigris.scarab.attribute.DateAttribute;
68 import org.tigris.scarab.attribute.OptionAttribute;
69 import org.tigris.scarab.attribute.UserAttribute;
70 import org.tigris.scarab.om.ActivitySet;
71 import org.tigris.scarab.om.Attachment;
72 import org.tigris.scarab.om.AttachmentManager;
73 import org.tigris.scarab.om.Attribute;
74 import org.tigris.scarab.om.AttributeValue;
75 import org.tigris.scarab.om.Condition;
76 import org.tigris.scarab.om.Issue;
77 import org.tigris.scarab.om.IssueType;
78 import org.tigris.scarab.om.Module;
79 import org.tigris.scarab.om.RModuleAttribute;
80 import org.tigris.scarab.om.RModuleIssueType;
81 import org.tigris.scarab.om.ScarabUser;
82 import org.tigris.scarab.services.security.ScarabSecurity;
83 import org.tigris.scarab.tools.ScarabLocalizationTool;
84 import org.tigris.scarab.tools.ScarabRequestTool;
85 import org.tigris.scarab.tools.localization.L10NKeySet;
86 import org.tigris.scarab.tools.localization.L10NMessage;
87 import org.tigris.scarab.util.IteratorWithSize;
88 import org.tigris.scarab.util.Log;
89 import org.tigris.scarab.util.ScarabConstants;
90 import org.tigris.scarab.util.word.ComplexQueryException;
91 import org.tigris.scarab.util.word.IssueSearch;
92 import org.tigris.scarab.util.word.IssueSearchFactory;
93 import org.tigris.scarab.util.word.MaxConcurrentSearchException;
94 import org.tigris.scarab.util.word.QueryResult;
95
96 /**
97  * This class is responsible for report issue forms.
98  *
99  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
100  * @version $Id: ReportIssue.java 9593 2005-04-10 19:11:38Z jorgeuriarte $
101  */

102 public class ReportIssue extends RequireLoginFirstAction
103 {
104     private static final int MAX_RESULTS = 25;
105     
106     /**
107      * Calls do check for duplicates by default.
108      */

109     public void doPerform(RunData data, TemplateContext context)
110         throws Exception JavaDoc
111     {
112         doCheckforduplicates(data, context);
113     }
114
115     private boolean checkIssueTypeStatus(RunData data, TemplateContext context)
116         throws Exception JavaDoc
117     {
118         ScarabRequestTool scarabR = getScarabRequestTool(context);
119         Issue issue = scarabR.getReportingIssue();
120         Module module = issue.getModule();
121         IssueType issueType = issue.getIssueType();
122         boolean isValid = module != null && !module.getDeleted() &&
123             issueType != null;
124         if (isValid)
125         {
126             RModuleIssueType rmit = module.getRModuleIssueType(issueType);
127             isValid = rmit != null && rmit.getActive();
128         }
129
130         if (!isValid)
131         {
132           scarabR.setAlertMessage(L10NKeySet.IssueTypeUnavailable);
133           data.setTarget( ((ScarabUser)data.getUser()).getHomePage());
134           cleanup(data, context);
135         }
136         return isValid;
137     }
138
139     public void doCheckforduplicates(RunData data, TemplateContext context)
140         throws Exception JavaDoc
141     {
142         if (checkIssueTypeStatus(data, context))
143         {
144             checkForDuplicates(data, context);
145         }
146     }
147
148     public void checkForDuplicates(RunData data, TemplateContext context)
149         throws Exception JavaDoc
150     {
151         ScarabLocalizationTool l10n = getLocalizationTool(context);
152         IntakeTool intake = getIntakeTool(context);
153         ScarabRequestTool scarabR = getScarabRequestTool(context);
154         try
155         {
156             Issue issue = scarabR.getReportingIssue();
157             LinkedMap avMap = issue.getModuleAttributeValuesMap();
158
159             // set the values entered so far and if that is successful look
160
// for duplicates
161
if (setAttributeValues(issue, intake, context, avMap))
162             {
163                 // check for duplicates, if there are none skip the dedupe page
164
searchAndSetTemplate(data, context, 0, MAX_RESULTS, issue,
165                                      "entry,Wizard3.vm");
166             }
167         }
168         catch (Exception JavaDoc e)
169         {
170             L10NMessage l10nMessage = new L10NMessage(L10NKeySet.ErrorExceptionMessage,e);
171             scarabR.setAlertMessage(l10nMessage);
172             Log.get().error("Error while checking for duplicates", e);
173             setTarget(data, "entry,Wizard1.vm");
174             return;
175         }
176
177         // we know we started at Wizard1 if we are here, Wizard3 needs
178
// to know where the issue entry process starts because it may
179
// branch back
180
data.getParameters()
181             .add(ScarabConstants.HISTORY_SCREEN, "entry,Wizard1.vm");
182     }
183
184     /**
185      * Common code related to deduping. A search for duplicate issues is
186      * performed and if the number of possible duplicates is greater than
187      * the threshold, the results are placed in the ScarabRequestTool and
188      * the screen is set to entry,Wizard2.vm so that they can be viewed.
189      *
190      * @param data a <code>RunData</code> value
191      * @param context a <code>TemplateContext</code> value
192      * @param threshold an <code>int</code> number of issues that determines
193      * whether "entry,Wizard2.vm" screen or the screen given by
194      * nextTemplate is shown
195      * @param maxResults a <code>int</code> number of issues that are returned
196      * as potential duplicates
197      * @param nextTemplate a <code>String</code> screen name to branch to
198      * if the number of duplicate issues is less than or equal to the threshold
199      * @return true if the number of possible duplicates is greater than the
200      * threshold
201      * @exception Exception if an error occurs
202      */

203     private boolean searchAndSetTemplate(RunData data,
204                                          TemplateContext context,
205                                          int threshold,
206                                          int maxResults,
207                                          Issue issue,
208                                          String JavaDoc nextTemplate)
209         throws Exception JavaDoc
210     {
211         // if all of the attributes are unset, then we don't need to run
212
// this query...just break out of it early...
213
List JavaDoc attributeValues = issue.getAttributeValues();
214         boolean hasSetValues = false;
215         for (Iterator JavaDoc itr = attributeValues.iterator();
216              itr.hasNext() && !hasSetValues;)
217         {
218             AttributeValue attVal = (AttributeValue) itr.next();
219             hasSetValues = attVal.isSet();
220         }
221         if (!hasSetValues)
222         {
223             setTarget(data, nextTemplate);
224             return true;
225         }
226
227         // search on the option attributes and keywords
228
IssueSearch search = null;
229         String JavaDoc template = null;
230         boolean dupThresholdExceeded = false;
231         try
232         {
233             search = IssueSearchFactory.INSTANCE.getInstance(
234                 issue, (ScarabUser)data.getUser());
235         // remove special characters from the text attributes
236
for (Iterator JavaDoc textAVs = search.getTextAttributeValues().iterator();
237              textAVs.hasNext();)
238         {
239             AttributeValue av = (AttributeValue)textAVs.next();
240             if (av.getAttribute().getAttributeType().getName().equals("date"))
241                 av.setValue(DateAttribute.internalDateFormat(av.getValue(), getLocalizationTool(context).get(L10NKeySet.ShortDatePattern)));
242             String JavaDoc s = av.getValue();
243             if (s != null && s.length() > 0)
244             {
245                 StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(s,
246                     ScarabConstants.INVALID_SEARCH_CHARACTERS);
247                 StringBuffer JavaDoc query = new StringBuffer JavaDoc(s.length() + 10);
248                 while (tokens.hasMoreTokens())
249                 {
250                     query.append(' ');
251                     query.append(tokens.nextToken());
252                 }
253                 av.setValue(query.toString().toLowerCase());
254             }
255         }
256         
257         // set the template to dedupe unless none exist, then skip
258
// to final entry screen
259
IteratorWithSize queryResults = search.getQueryResults();
260         dupThresholdExceeded = (queryResults.size() > threshold);
261         if (dupThresholdExceeded)
262         {
263             List JavaDoc matchingIssueIds = new ArrayList JavaDoc(maxResults);
264             // limit the number of matching issues to maxResults
265
for (int i = 0; queryResults.hasNext() && i <= maxResults; i++)
266             {
267                 matchingIssueIds.add(
268                     ((QueryResult)queryResults.next()).getUniqueId());
269             }
270             context.put("issueList", matchingIssueIds);
271             template = "entry,Wizard2.vm";
272         }
273         else
274         {
275             template = nextTemplate;
276         }
277         }
278         catch (MaxConcurrentSearchException e)
279         {
280             getScarabRequestTool(context).setInfoMessage(
281                 L10NKeySet.DupeCheckSkippedForLackOfResources);
282         }
283         catch (ComplexQueryException e)
284         {
285             getScarabRequestTool(context).setInfoMessage(
286                     L10NKeySet.DupeCheckSkippedBecauseComplexity);
287         }
288         finally
289         {
290             if (search != null)
291             {
292                 search.close();
293             }
294             IssueSearchFactory.INSTANCE.notifyDone();
295         }
296         
297         setTarget(data, template);
298         return dupThresholdExceeded;
299     }
300     
301     /**
302      * Checks the Module the issue is being entered into to see what
303      * attributes are required to have values. If a required field was present
304      * and the user did not enter anything, intake is notified that the
305      * field was required.
306      *
307      * @param issue an <code>Issue</code> value
308      * @param intake an <code>IntakeTool</code> value
309      * @exception Exception if an error occurs
310      */

311     private void setRequiredFlags(Issue issue, IntakeTool intake,
312             LinkedMap avMap, TemplateContext context)
313         throws Exception JavaDoc
314     {
315         if (issue == null)
316         {
317             ScarabLocalizationTool l10n = getLocalizationTool(context);
318             throw new Exception JavaDoc(l10n.get("IssueNoLongerValid")); //EXCEPTION
319
}
320         Set JavaDoc selectedOptions = new HashSet JavaDoc();
321         Map JavaDoc conditionallyRequiredFields = new HashMap JavaDoc();
322         IssueType issueType = issue.getIssueType();
323         List JavaDoc requiredAttributes = issueType
324             .getRequiredAttributes(issue.getModule());
325         for (MapIterator iter = avMap.mapIterator(); iter.hasNext();)
326         {
327             AttributeValue aval = (AttributeValue)avMap.get(iter.next());
328             
329             Group group =
330                 intake.get("AttributeValue", aval.getQueryKey(), false);
331             if (group != null)
332             {
333                 Field field = null;
334                 if (aval instanceof OptionAttribute)
335                 {
336                     field = group.get("OptionId");
337                     // Will store the selected optionId, for later query.
338
Object JavaDoc fieldValue = field.getValue();
339                     if (null != fieldValue)
340                     {
341                         selectedOptions.add(fieldValue);
342                     }
343                 }
344                 else if (aval instanceof UserAttribute)
345                 {
346                     field = group.get("UserId");
347                 }
348                 else
349                 {
350                     field = group.get("Value");
351                 }
352
353                 /**
354                  * If the field has any conditional constraint, will be added to the collection
355                  * in the hash.
356                  */

357                 List JavaDoc conditions = aval.getRModuleAttribute().getConditions();
358                 if (conditions.size() > 0)
359                 {
360                     for (Iterator JavaDoc it = conditions.iterator(); it.hasNext(); )
361                     {
362                         Condition cond = (Condition)it.next();
363                         Integer JavaDoc id = cond.getOptionId();
364                         List JavaDoc fields = (List JavaDoc)conditionallyRequiredFields.get(id);
365                         if (fields == null)
366                         {
367                             fields = new ArrayList JavaDoc();
368                         }
369                         fields.add(field);
370                         conditionallyRequiredFields.put(id, fields);
371                     }
372                 }
373                 
374                 for (int j=requiredAttributes.size()-1; j>=0; j--)
375                 {
376                     if (aval.getAttribute().getPrimaryKey().equals(
377                             ((Attribute)requiredAttributes.get(j)).getPrimaryKey())
378                         && !aval.isSet())
379                     {
380                         field.setRequired(true);
381                         break;
382                     }
383                 }
384             }
385         }
386         /**
387          * Now that we have all the info, we will force the 'required' status of any field
388          * whose requiredOptionId has been set in the issue.
389          */

390         for (Iterator JavaDoc requiredIds = conditionallyRequiredFields.keySet().iterator(); requiredIds.hasNext(); )
391         {
392             Integer JavaDoc attributeId= (Integer JavaDoc)requiredIds.next();
393             if (selectedOptions.contains(attributeId))
394             {
395                 List JavaDoc fields = (List JavaDoc)conditionallyRequiredFields.get(attributeId);
396                 for (Iterator JavaDoc iter = fields.iterator(); iter.hasNext(); )
397                 {
398                     Field field = (Field)iter.next();
399                     if (field.getValue().toString().length() == 0)
400                     {
401                        field.setRequired(true);
402                        field.setMessage("ConditionallyRequiredAttribute");
403                     }
404                 }
405             }
406         }
407     }
408     
409     /**
410      * Add/Modify any attribute values that were just entered into intake.
411      *
412      * @param issue the <code>Issue</code> currently being editted
413      * @param intake an <code>IntakeTool</code> containing the fields for the
414      * issue's attribute values.
415      * @exception Exception pass thru
416      */

417     private boolean setAttributeValues(Issue issue, IntakeTool intake,
418                                        TemplateContext context,
419                                        LinkedMap avMap)
420         throws Exception JavaDoc
421     {
422         boolean success = false;
423         // set any required flags on attribute values
424
setRequiredFlags(issue, intake, avMap, context);
425         if (intake.isAllValid())
426         {
427             for (MapIterator i = avMap.mapIterator();i.hasNext();)
428             {
429                 AttributeValue aval = (AttributeValue)avMap.get(i.next());
430                 Group group =
431                     intake.get("AttributeValue", aval.getQueryKey(), false);
432                 if (group != null)
433                 {
434                     Field field = group.get(aval instanceof OptionAttribute ?
435                                             "OptionId" : "Value");
436                     String JavaDoc value = field.toString();
437
438                     if (value != null && value.length() > 0)
439                     {
440                         group.setProperties(aval);
441                     }
442                 }
443             }
444             success = true;
445         }
446         else
447         {
448             getScarabRequestTool(context).setAlertMessage(ERROR_MESSAGE);
449         }
450         return success;
451     }
452
453     /**
454      * handles entering an issue
455      */

456     public void doEnterissue(RunData data, TemplateContext context)
457         throws Exception JavaDoc
458     {
459         if (checkIssueTypeStatus(data, context))
460         {
461             enterIssue(data, context);
462         }
463     }
464
465     /**
466      * handles entering an issue
467      */

468     private void enterIssue(RunData data, TemplateContext context)
469         throws Exception JavaDoc
470     {
471         IntakeTool intake = getIntakeTool(context);
472         ScarabRequestTool scarabR = getScarabRequestTool(context);
473         ScarabLocalizationTool l10n = getLocalizationTool(context);
474         Issue issue = scarabR.getReportingIssue();
475         ScarabUser user = (ScarabUser)data.getUser();
476         LinkedMap avMap = issue.getModuleAttributeValuesMap();
477
478         // set the attribute values and if that was successful save the issue.
479
if (setAttributeValues(issue, intake, context, avMap))
480         {
481             DateAttribute.convertDateAttributes(issue.getAttributeValues(), getLocalizationTool(context).get("ShortDatePattern"));
482             if (issue.containsMinimumAttributeValues())
483             {
484                 // we need to see that the default text was filled out
485
// if necessary. We can
486
// only do this after setting the attributes above.
487
boolean saveIssue = true;
488                 Group reasonGroup = intake.get("Attachment", "_1", false);
489                 Field reasonField = reasonGroup.get("Data");
490                 if (issue.getDefaultTextAttributeValue() == null)
491                 {
492                     reasonField.setRequired(true);
493                     saveIssue = false;
494                 }
495
496                 // if the reason field is to long, then show an error.
497
String JavaDoc reasonString = reasonField.toString();
498                 if (reasonString != null && reasonString.length() > 254)
499                 {
500                     reasonField.setMessage("intake_ReasonMustBeLessThan256Characters");
501                     saveIssue = false;
502                 }
503                 // If there is a default text attribute,or if a comment has
504
// Been provided, proceed.
505
if (reasonField.isValid() || saveIssue)
506                 {
507                     HashMap JavaDoc newValues = new HashMap JavaDoc();
508                     List JavaDoc modAttrs = issue.getModule()
509                         .getRModuleAttributes(issue.getIssueType(), true, "all");
510
511                     // this is used for the workflow stuff...FIXME: it should
512
// be refactored as soon as we possibly can. the reason is
513
// that all of this data can be retrieved by simply using
514
// issue.getModuleAttributeValuesMap() because the call
515
// to setAttributeValues() above already gets the group
516
// information into the module attribute values.
517
for (int i = 0; i<modAttrs.size(); i++)
518                     {
519                         Attribute attr = ((RModuleAttribute)modAttrs.get(i)).getAttribute();
520                         String JavaDoc queryKey = "__" + attr.getAttributeId().toString();
521                         Group group = intake.get("AttributeValue", queryKey, false);
522                         String JavaDoc newValue = "";
523
524                         if (group != null)
525                         {
526                             if (attr.isOptionAttribute())
527                             {
528                                 newValue = group.get("OptionId").toString();
529                             }
530                             else
531                             {
532                                 newValue = group.get("Value").toString();
533                             }
534                             if (newValue.length() != 0)
535                             {
536                                 newValues.put(attr.getAttributeId(), newValue);
537                             }
538                         }
539                     }
540                     
541                     // Save the Reason
542
ActivitySet activitySet = null;
543                     Attachment reason = null;
544                     try
545                     {
546                         // grab the comment data
547
reason = new Attachment();
548                         reasonField.setProperty(reason);
549                         activitySet = issue
550                             .setInitialAttributeValues(activitySet, reason, newValues, user);
551                     }
552                     catch (Exception JavaDoc se)
553                     {
554                         scarabR.setAlertMessage(l10n.getMessage(se));
555                         return;
556                     }
557
558                     // Save any unsaved attachments as part of this ActivitySet as well
559
activitySet = issue.doSaveFileAttachments(activitySet, user);
560
561                     // set the template to the user selected value
562
int templateCode = data.getParameters()
563                         .getInt("template_code", 2);
564                 
565                     // if user preference for next template is unset,
566
// set it.
567
int userPref = user.getEnterIssueRedirect();
568                     if (userPref == 0 || userPref != templateCode)
569                     {
570                         user.setEnterIssueRedirect(templateCode);
571                     }
572                     doRedirect(data, context, templateCode, issue);
573                 
574                     // send email
575
try
576                     {
577                         activitySet.sendEmail(issue, "NewIssueNotification.vm");
578                     }
579                     catch(Exception JavaDoc e)
580                     {
581                         L10NMessage l10nMessage = new L10NMessage(L10NKeySet.IssueSavedButEmailError,e);
582                         scarabR.setInfoMessage(l10nMessage);
583                     }
584                     cleanup(data, context);
585                     data.getParameters().add("id", issue.getUniqueId().toString());
586                     L10NMessage l10nMessage =
587                         new L10NMessage(L10NKeySet.IssueAddedToModule,
588                             issue.getUniqueId(),
589                             getScarabRequestTool(context).getCurrentModule().getRealName());
590                     scarabR.setConfirmMessage(l10nMessage);
591                 }
592                 else
593                 {
594                     scarabR.setAlertMessage(ERROR_MESSAGE);
595                 }
596             }
597             else
598             {
599                 // this would be an application or hacking error
600
}
601         }
602     }
603     
604     /**
605      * Add attachment file
606      */

607     public void doAddfile(RunData data, TemplateContext context)
608         throws Exception JavaDoc
609     {
610         IntakeTool intake = getIntakeTool(context);
611         ScarabRequestTool scarabR = getScarabRequestTool(context);
612         Issue issue = scarabR.getReportingIssue();
613         Attachment attachment = AttachmentManager.getInstance();
614         Group group = intake.get("Attachment",
615                                  attachment.getQueryKey(), false);
616
617         ModifyIssue
618             .addFileAttachment(issue, group, attachment, scarabR, data, intake);
619         ScarabLocalizationTool l10n = getLocalizationTool(context);
620         if (scarabR.getAlertMessage() == null)
621         {
622             scarabR.setConfirmMessage(L10NKeySet.FileAdded);
623         }
624
625         LinkedMap avMap = issue.getModuleAttributeValuesMap();
626         // set any attribute values that were entered before adding the file.
627
setAttributeValues(issue, intake, context, avMap);
628         doGotowizard3(data, context);
629     }
630     
631     /**
632      * Remove an attachment file
633      */

634     public void doRemovefile(RunData data, TemplateContext context)
635         throws Exception JavaDoc
636     {
637         ScarabRequestTool scarabR = getScarabRequestTool(context);
638         ScarabLocalizationTool l10n = getLocalizationTool(context);
639         Issue issue = scarabR.getReportingIssue();
640         ParameterParser params = data.getParameters();
641         Object JavaDoc[] keys = params.getKeys();
642         String JavaDoc key;
643         String JavaDoc attachmentIndex;
644         boolean fileDeleted = false;
645         for (int i =0; i<keys.length; i++)
646         {
647             key = keys[i].toString();
648             if (key.startsWith("file_delete_"))
649             {
650                 attachmentIndex = key.substring(12);
651                 issue.removeFile(attachmentIndex);
652                 fileDeleted = true;
653             }
654         }
655         if (fileDeleted)
656         {
657             scarabR.setConfirmMessage(L10NKeySet.FileDeleted);
658         }
659         else
660         {
661             scarabR.setConfirmMessage(L10NKeySet.NoFilesChanged);
662         }
663         LinkedMap avMap = issue.getModuleAttributeValuesMap();
664         // set any attribute values that were entered before adding the file.
665
setAttributeValues(issue, getIntakeTool(context), context, avMap);
666         doGotowizard3(data, context);
667     }
668     
669     /**
670      * Handles adding a comment to one or more issues. This is an option
671      * which is available on Wizard2 during the dedupe process.
672      */

673     public void doAddcomment(RunData data, TemplateContext context)
674         throws Exception JavaDoc
675     {
676         ScarabLocalizationTool l10n = getLocalizationTool(context);
677         IntakeTool intake = getIntakeTool(context);
678         ScarabRequestTool scarabR = getScarabRequestTool(context);
679         Issue issue = scarabR.getReportingIssue();
680         if (intake.isAllValid())
681         {
682             Attachment attachment = new Attachment();
683             Group group = intake.get("Attachment",
684                           attachment.getQueryKey(), false);
685             if (group != null)
686             {
687                  List JavaDoc issues = scarabR.getIssues();
688                  if (issues == null || issues.size() == 0)
689                  {
690                      scarabR.setAlertMessage(L10NKeySet.NoIssuesSelectedToAddComment);
691                      searchAndSetTemplate(data, context, 0, MAX_RESULTS, issue, "entry,Wizard2.vm");
692                      return;
693                  }
694                  ActivitySet activitySet = null;
695                  for (Iterator JavaDoc itr = issues.iterator(); itr.hasNext();)
696                  {
697                      Issue prevIssue = (Issue)itr.next();
698                      // save the attachment
699
attachment = new Attachment();
700                      group.setProperties(attachment);
701                      if (attachment.getData() != null
702                          && attachment.getData().trim().length() > 0)
703                      {
704                           activitySet =
705                              prevIssue.addComment(activitySet, attachment,
706                             (ScarabUser)data.getUser());
707                           try
708                           {
709                               activitySet.sendEmail(prevIssue);
710                               scarabR.setConfirmMessage(L10NKeySet.CommentAdded);
711                           }
712                           catch(Exception JavaDoc e)
713                           {
714                               L10NMessage l10nMessage = new L10NMessage(L10NKeySet.CommentAddedButEmailError,e);
715                               scarabR.setInfoMessage(l10nMessage);
716                           }
717                      }
718                     else
719                     {
720                         scarabR.setAlertMessage(
721                            L10NKeySet.NoTextInCommentTextArea);
722                         searchAndSetTemplate(data, context, 0, MAX_RESULTS,
723                                              issue, "entry,Wizard2.vm");
724                         return;
725                     }
726                 }
727             }
728
729             // if there was only one duplicate issue and we just added
730
// a comment to it, assume user is done
731
String JavaDoc nextTemplate =
732                  ((ScarabUser)data.getUser()).getHomePage();
733             if (! searchAndSetTemplate(data, context, 1, MAX_RESULTS, issue, nextTemplate))
734             {
735                 cleanup(data, context);
736             }
737             else
738             {
739                 intake.remove(group);
740             }
741             return;
742         }
743         else
744         {
745             // Comment was probably too long. Repopulate the issue list, so
746
// the page can be shown again, and the user can fix the comment.
747
searchAndSetTemplate(data, context, 0, MAX_RESULTS, issue, "entry,Wizard2.vm");
748         }
749     }
750     
751     /**
752      * The button for this action is commented out on Wizard2, so it
753      * will not be called
754     public void doAddvote(RunData data, TemplateContext context)
755         throws Exception
756     {
757         IntakeTool intake = getIntakeTool(context);
758         ScarabLocalizationTool l10n = getLocalizationTool(context);
759         if (intake.isAllValid())
760         {
761             ScarabRequestTool scarabR = getScarabRequestTool(context);
762             Issue issue = scarabR.getReportingIssue();
763             
764             try
765             {
766                 issue.addVote((ScarabUser)data.getUser());
767                 scarabR.setConfirmMessage(
768                     l10n.format("VoteForIssueAccepted", issue.getUniqueId()));
769                 // if there was only one duplicate issue and the user just
770                 // voted for it, assume user is done
771                 String nextTemplate =
772                     ((ScarabUser)data.getUser()).getHomePage();
773                 if (! searchAndSetTemplate(data, context, 1, nextTemplate))
774                 {
775                     cleanup(data, context);
776                 }
777             }
778             catch (ScarabException e)
779             {
780                 scarabR.setAlertMessage(
781                     l10n.format("VoteFailedException", e.getMessage()));
782                 // User attempted to vote when they were not allowed. This
783                 // should probably not be allowed in the ui, but right now
784                 // it is and we should protect against url hacking anyway.
785                 // Repopulate the data so the dedupe page can be shown again.
786                 searchAndSetTemplate(data, context, 0, "entry,Wizard2.vm");
787             }
788         }
789         else
790         {
791             // Not sure this case needs to be covered, but just to be safe
792             // repopulate the data so the dedupe page can be shown again.
793             searchAndSetTemplate(data, context, 0, "entry,Wizard2.vm");
794         }
795     }
796      */

797     
798     public void doGotowizard3(RunData data, TemplateContext context)
799         throws Exception JavaDoc
800     {
801         setTarget(data, "entry,Wizard3.vm");
802     }
803     
804     public void doUsetemplates(RunData data, TemplateContext context)
805         throws Exception JavaDoc
806     {
807         getIntakeTool(context).removeAll();
808         String JavaDoc templateId = data.getParameters().getString("select_template_id");
809         if (templateId != null && templateId.length() > 0)
810         {
811             data.getParameters().add("templateId", templateId);
812         }
813     }
814     
815     private void cleanup(RunData data, TemplateContext context)
816     {
817         data.getParameters().remove(ScarabConstants.HISTORY_SCREEN);
818         String JavaDoc issueKey = data.getParameters()
819             .getString(ScarabConstants.REPORTING_ISSUE);
820         ((ScarabUser)data.getUser()).setReportingIssue(issueKey, null);
821         data.getParameters().remove(ScarabConstants.REPORTING_ISSUE);
822         getScarabRequestTool(context).setReportingIssue(null);
823         IntakeTool intake = getIntakeTool(context);
824         intake.removeAll();
825     }
826     
827     /**
828      * User selects page to redirect to after entering issue.
829      */

830     private void doRedirect(RunData data, TemplateContext context,
831                             int templateCode, Issue issue)
832         throws Exception JavaDoc
833     {
834         ScarabUser user = (ScarabUser)data.getUser();
835         ScarabRequestTool scarabR = getScarabRequestTool(context);
836         ScarabLocalizationTool l10n = getLocalizationTool(context);
837         String JavaDoc template = null;
838         switch (templateCode)
839         {
840             case 1:
841                 if (user.hasPermission(ScarabSecurity.ISSUE__ENTER,
842                                        user.getCurrentModule()))
843                 {
844                     IssueType issueType = issue.getIssueType();
845                     template = scarabR.getNextEntryTemplate(issueType);
846                     data.getParameters().setString(
847                         ScarabConstants.CURRENT_ISSUE_TYPE,
848                         issueType.getQueryKey());
849                 }
850                 else
851                 {
852                     template = user.getHomePage();
853                     scarabR.setAlertMessage(
854                         L10NKeySet.InsufficientPermissionsToEnterIssues);
855                 }
856                 break;
857             case 2:
858                 if (user.hasPermission(ScarabSecurity.ISSUE__ASSIGN,
859                                        user.getCurrentModule()))
860                 {
861                     template = "AssignIssue.vm";
862                     data.getParameters().setString(ScarabConstants.CANCEL_TEMPLATE,
863                                              "ViewIssue.vm");
864                     data.getParameters().add("issue_ids",
865                                              issue.getUniqueId());
866 // data.getParameters()
867
// .setString("id", issue.getUniqueId().toString());
868
getIntakeTool(context).removeAll();
869                     scarabR.resetAssociatedUsers();
870                 }
871                 else
872                 {
873                     template = user.getHomePage();
874                     scarabR.setAlertMessage(
875                         L10NKeySet.InsufficientPermissionsToAssignIssues);
876                 }
877                 break;
878             case 3:
879                 if (user.hasPermission(ScarabSecurity.ISSUE__VIEW,
880                                        user.getCurrentModule()))
881                 {
882                     template = "ViewIssue.vm";
883                     data.getParameters()
884                         .setString("id",issue.getUniqueId().toString());
885                 }
886                 else
887                 {
888                     template = user.getHomePage();
889                     scarabR.setAlertMessage(
890                         L10NKeySet.InsufficientPermissionsToViewIssues);
891                 }
892                 break;
893             case 4:
894                 template = user.getHomePage();
895                 break;
896             case 5:
897                 if (user.hasPermission(ScarabSecurity.ISSUE__VIEW,
898                                        user.getCurrentModule()))
899                 {
900                     template = "ViewIssue.vm";
901                     data.getParameters()
902                         .setString("id",issue.getUniqueId().toString());
903                     data.getParameters()
904                         .setString("tab","3"); // comment tab == 3
905
data.getUser().setTemp(ScarabConstants.TAB_KEY, "3");
906                 }
907                 else
908                 {
909                     template = user.getHomePage();
910                     scarabR.setAlertMessage(
911                         L10NKeySet.InsufficientPermissionsToViewIssues);
912                 }
913                 break;
914         }
915         setTarget(data, template);
916     }
917
918     public void doStart(RunData data, TemplateContext context)
919         throws Exception JavaDoc
920     {
921         cleanOutStaleIssue(data, context);
922     }
923
924     /**
925      * for easy access by TemplateList action
926      */

927     static void cleanOutStaleIssue(RunData data, TemplateContext context)
928         throws Exception JavaDoc
929     {
930         String JavaDoc key = data.getParameters()
931             .getString(ScarabConstants.REPORTING_ISSUE);
932         ScarabUser user = (ScarabUser)data.getUser();
933         if (key != null)
934         {
935             data.getParameters().remove(ScarabConstants.REPORTING_ISSUE);
936             user.setReportingIssue(key, null);
937         }
938         user.setHomePage("home,EnterNew.vm");
939     }
940 }
941
Popular Tags