KickJava   Java API By Example, From Geeks To Geeks.

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


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

48
49 import java.util.ArrayList JavaDoc;
50 import java.util.Calendar JavaDoc;
51 import java.util.HashMap JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55
56 import org.apache.commons.collections.MapIterator;
57 import org.apache.commons.collections.map.LinkedMap;
58 import org.apache.commons.lang.StringUtils;
59 import org.apache.fulcrum.intake.Intake;
60 import org.apache.fulcrum.intake.model.Group;
61 import org.apache.fulcrum.parser.ParameterParser;
62 import org.apache.torque.om.NumberKey;
63 import org.apache.turbine.RunData;
64 import org.apache.turbine.TemplateContext;
65 import org.tigris.scarab.actions.base.RequireLoginFirstAction;
66 import org.tigris.scarab.om.AttributeValue;
67 import org.tigris.scarab.om.Report;
68 import org.tigris.scarab.om.ReportManager;
69 import org.tigris.scarab.om.ReportPeer;
70 import org.tigris.scarab.om.ScarabUser;
71 import org.tigris.scarab.reports.ReportAxis;
72 import org.tigris.scarab.reports.ReportBridge;
73 import org.tigris.scarab.reports.ReportDate;
74 import org.tigris.scarab.reports.ReportDefinition;
75 import org.tigris.scarab.reports.ReportGroup;
76 import org.tigris.scarab.reports.ReportHeading;
77 import org.tigris.scarab.reports.ReportOptionAttribute;
78 import org.tigris.scarab.reports.ReportUserAttribute;
79 import org.tigris.scarab.tools.ScarabLocalizationTool;
80 import org.tigris.scarab.tools.ScarabRequestTool;
81 import org.tigris.scarab.tools.localization.L10NKeySet;
82 import org.tigris.scarab.tools.localization.L10NMessage;
83 import org.tigris.scarab.tools.localization.LocalizationKey;
84 import org.tigris.scarab.util.ScarabConstants;
85 import org.tigris.scarab.util.ScarabUtil;
86 import org.tigris.scarab.util.export.ExportFormat;
87 import org.tigris.scarab.util.word.IssueSearch;
88
89 /**
90  * This class is responsible for report generation forms
91  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
92  * @version $Id: ConfigureReport.java 9290 2004-12-03 15:57:52Z dep4b $
93  */

94 public class ConfigureReport
95     extends RequireLoginFirstAction
96 {
97     static final String JavaDoc NO_PERMISSION_MESSAGE =
98         "NoPermissionToEditReport";
99
100     private static final String JavaDoc ADD_USER = "add_user";
101     private static final String JavaDoc SELECTED_USER = "select_user";
102     
103     ScarabLocalizationTool l10n;
104     ScarabRequestTool scarabR;
105     ReportBridge report;
106     Intake intake;
107     ParameterParser params;
108     ScarabUser user;
109
110     private void setup(RunData data, TemplateContext context) throws Exception JavaDoc{
111         l10n = getLocalizationTool(context);
112         scarabR = getScarabRequestTool(context);
113         report = scarabR.getReport();
114         intake = getIntakeTool(context);
115         params = data.getParameters();
116         user = (ScarabUser)data.getUser();
117     }
118     
119     public void doSaveinfo(RunData data, TemplateContext context)
120         throws Exception JavaDoc
121     {
122         setup(data,context);
123         
124         if (!report.isEditable((ScarabUser)data.getUser()))
125         {
126             setNoPermissionMessage();
127             setTarget(data, "reports,ReportList.vm");
128         }
129         else if (intake.isAllValid())
130         {
131             Group intakeReport = getIntakeReportGroup(intake, report);
132             if (intakeReport != null)
133             {
134                 intakeReport.setValidProperties(report);
135
136                 if (report.isReadyForCalculation())
137                 {
138                     String JavaDoc msg = report.isNew() ?
139                            l10n.get(L10NKeySet.ReportUpdated) :
140                            l10n.get(L10NKeySet.ReportUpdatedNotSaved);
141                     scarabR.setConfirmMessage(msg);
142                     setTarget(data, "reports,Info.vm");
143                 }
144                 else
145                 {
146                     String JavaDoc msg = report.isNew() ?
147                            l10n.get("ReportUpdatedPleaseAddRowAndColumnCriteria") :
148                            l10n.get("ReportUpdatedNotSavedPleaseAddRowAndColumnCriteria");
149                     scarabR.setConfirmMessage(msg);
150                     setTarget(data, "reports,AxisConfiguration.vm");
151                 }
152             }
153             else
154             {
155                 // FIXME! i don't know that the intakeReport should ever be
156
// null, but since the conditional was here, don't fail silently
157
scarabR.setAlertMessage(
158                         L10NKeySet.ThisShouldNotHappenPleaseContactAdmin);
159                 setTarget(data, "reports,Info.vm");
160             }
161         }
162         else
163         {
164             scarabR.setAlertMessage(
165                     L10NKeySet.InvalidData);
166             setTarget(data, "reports,Info.vm");
167         }
168     }
169
170     public void doSelectheading(RunData data, TemplateContext context)
171         throws Exception JavaDoc
172     {
173         setup(data,context);
174         // the form will carry over the selected heading. just make sure
175
// to remove old intake data
176

177         intake.removeAll();
178         // give the user a message if they are already on the selected
179
// heading, in the event they are confused.
180

181         int level = params.getInt("heading", -1);
182         int prevLevel = params.getInt("prevheading", -2);
183         if (level == prevLevel)
184         {
185             getScarabRequestTool(context).setInfoMessage(
186                 getLocalizationTool(context)
187                 .format("AlreadyEditingSelectedHeading", new Integer JavaDoc(level+1)));
188         }
189     }
190
191     public void doSettype(RunData data, TemplateContext context)
192         throws Exception JavaDoc
193     {
194         setup(data,context);
195         int axis = params.getInt("axis", 0); // 0=row; 1=column
196
int level = params.getInt("heading", -1);
197         int type = params.getInt("headingtype", 0);
198
199         // remove any old data
200
if (level != -1)
201         {
202             
203             ReportDefinition reportDefn = report.getReportDefinition();
204             ReportAxis reportAxis = reportDefn.getAxis(axis);
205             ReportHeading heading = reportAxis.getHeading(level);
206             int currentType = heading.calculateType();
207             if (type != currentType)
208             {
209                 if (currentType == 2 &&
210                     !reportDefn.allowMoreHeadings(reportAxis))
211                 {
212                     scarabR.setAlertMessage(L10NKeySet.ThisAxisMustBeDatesUnlessHeadingsAreRemoved);
213                     params.setString("headingtype", "2");
214                 }
215                 else
216                 {
217                     LocalizationKey msg;
218                     if (heading.size() > 0)
219                     {
220                         heading.reset();
221                         msg = L10NKeySet.HeadingTypeChangedOldDataDiscarded;
222                     }
223                     else
224                     {
225                         msg = L10NKeySet.HeadingTypeChanged;
226                     }
227                     scarabR.setConfirmMessage(msg);
228                 }
229             }
230         }
231     }
232
233     public void doAddoptions(RunData data, TemplateContext context)
234         throws Exception JavaDoc
235     {
236         setup(data,context);
237         if (!report.isEditable((ScarabUser)data.getUser()))
238         {
239             setNoPermissionMessage();
240             setTarget(data, "reports,ReportList.vm");
241         }
242         else if (intake.isAllValid())
243         {
244
245
246             int axis = params.getInt("axis", 0); // 0=row; 1=column
247
int level = params.getInt("heading", -1);
248             int type = params.getInt("headingtype", 0);
249
250             // we are using an IssueSearch object to gather the data to
251
// create the ReportOptionAttribute objects.
252

253             IssueSearch search = scarabR.getNewSearch();
254             // Set intake properties
255
//Group searchGroup = intake.get("SearchIssue", search.getQueryKey());
256
//searchGroup.setProperties(search);
257

258             // Set attribute values to search on
259
LinkedMap avMap = search.getCommonAttributeValuesMap();
260             for (MapIterator i = avMap.mapIterator(); i.hasNext();)
261             {
262                 i.next();
263                 AttributeValue aval = (AttributeValue)i.getValue();
264                 Group group = intake.get("AttributeValue", aval.getQueryKey());
265                 if (group != null)
266                 {
267                     group.setProperties(aval);
268                 }
269             }
270             
271             // remove unset AttributeValues
272
List JavaDoc setAttValues = removeUnsetValues(search.getAttributeValues());
273             
274             ReportHeading heading = report.getReportDefinition()
275                 .getAxis(axis).getHeading(level);
276             if (type != heading.calculateType())
277             {
278                 scarabR.setAlertMessage(L10NKeySet.ChangeOfTypeMessage);
279             }
280
281             // we are going to delete the old heading data and reconstruct it
282
// so if there is any group info, we need to get it first
283
List JavaDoc groups = heading.getReportGroups();
284             Map JavaDoc optionGroupMap = null;
285             if (groups != null && !groups.isEmpty())
286             {
287                 optionGroupMap = new HashMap JavaDoc();
288                 for (Iterator JavaDoc i = groups.iterator(); i.hasNext();)
289                 {
290                     ReportGroup group = (ReportGroup)i.next();
291                     List JavaDoc options = group.getReportOptionAttributes();
292                     if (options != null && !options.isEmpty())
293                     {
294                         for (Iterator JavaDoc j = options.iterator(); j.hasNext();)
295                         {
296                             optionGroupMap.put(j.next(), group);
297                         }
298                     }
299                 }
300             }
301             heading.reset();
302             
303             //convert to ReportOptionAttributes
304
for (Iterator JavaDoc i = setAttValues.iterator(); i.hasNext();)
305             {
306                 AttributeValue av = (AttributeValue)i.next();
307                 //pull any chained values out to create a flat list
308
List JavaDoc chainedValues = av.getValueList();
309                 for (Iterator JavaDoc j = chainedValues.iterator(); j.hasNext();)
310                 {
311                     ReportOptionAttribute roa = new ReportOptionAttribute();
312                     Integer JavaDoc id = Integer.valueOf(((AttributeValue)j.next())
313                                              .getOptionId().toString());
314                     roa.setOptionId(id);
315
316                     if (optionGroupMap == null)
317                     {
318                         heading.addReportOptionAttribute(roa);
319                     }
320                     else
321                     {
322                         ReportGroup group =
323                             (ReportGroup)optionGroupMap.get(roa);
324                         if (group == null)
325                         {
326                             // add it to the first group
327
((ReportGroup)groups.get(0))
328                                 .addReportOptionAttribute(roa);
329                         }
330                         else
331                         {
332                             group.addReportOptionAttribute(roa);
333                         }
334                     }
335                 }
336             }
337             if (level == -1)
338             {
339                 params.setString("heading", "0");
340             }
341
342             String JavaDoc msg = getLocalizedHeadingConfirmMessage(report, l10n);
343             scarabR.setConfirmMessage(msg);
344
345 /*
346             //testing
347             java.io.FileWriter fw = new java.io.FileWriter("/tmp/Report.xml");
348             BeanWriter bw = new BeanWriter(fw);
349             bw.writeXmlDeclaration("<?xml version='1.0' encoding='UTF-8' ?>");
350             bw.write(report.getReportDefinition());
351             bw.flush();
352             bw.close();
353 */

354         }
355     }
356
357
358     private static String JavaDoc getLocalizedHeadingConfirmMessage(ReportBridge report, ScarabLocalizationTool l10n)
359     {
360         LocalizationKey key;
361         if (report.isReadyForCalculation())
362         {
363             key = report.isNew() ?
364                 L10NKeySet.ReportUpdatedDoMoreOrCalculate:
365                 L10NKeySet.ReportUpdatedNotSavedDoMoreOrCalculate;
366         }
367         else
368         {
369             key = report.isNew() ?
370                 L10NKeySet.ReportUpdatedDoMore:
371                 L10NKeySet.ReportUpdatedNotSavedDoMore;
372         }
373         L10NMessage msg = new L10NMessage(key);
374         return msg.getMessage(l10n);
375     }
376
377     /**
378      * remove unset AttributeValues. this method is c/p from IssueSearch
379      *
380      * @param attValues a <code>List</code> value
381      */

382     private List JavaDoc removeUnsetValues(List JavaDoc attValues)
383     {
384         int size = attValues.size();
385         List JavaDoc setAVs = new ArrayList JavaDoc(size);
386         for (int i=0; i<size; i++)
387         {
388             AttributeValue attVal = (AttributeValue) attValues.get(i);
389             if (attVal.getOptionId() != null || attVal.getValue() != null
390                  || attVal.getUserId() != null)
391             {
392                 setAVs.add(attVal);
393             }
394         }
395         return setAVs;
396     }
397         
398     /**
399      * Adds users to the current header.
400      */

401     public void doAddusers(RunData data, TemplateContext context)
402         throws Exception JavaDoc
403     {
404         setup(data,context);
405         if (!report.isEditable(user))
406         {
407             setNoPermissionMessage();
408             setTarget(data, "reports,ReportList.vm");
409             return;
410         }
411
412
413         int axis = params.getInt("axis", 0); // 0=row; 1=column
414
int level = params.getInt("heading", -1);
415         int type = params.getInt("headingtype", 0);
416         
417         // not too elegant for editing, but if groups have been defined
418
// we remove them for now until the rest of the reports is
419
// more code complete !FIXME!
420
ReportHeading heading = report.getReportDefinition()
421             .getAxis(axis).getHeading(level);
422         if (type != heading.calculateType()
423             || heading.getReportGroups() != null)
424         {
425             if (heading.getReportGroups() != null)
426             {
427                scarabR.setAlertMessage(L10NKeySet.CouldNotMakeRequestedChange);
428             }
429             heading.reset();
430         }
431
432         String JavaDoc[] userIds = params.getStrings(ADD_USER);
433         if (userIds != null && userIds.length > 0)
434         {
435             for (int i =0; i<userIds.length; i++)
436             {
437                 String JavaDoc userId = userIds[i];
438                 ReportUserAttribute rua = new ReportUserAttribute();
439                 rua.setUserId(new Integer JavaDoc(userId));
440                 rua.setAttributeId(new Integer JavaDoc(
441                     params.getString("user_attr_" + userId)));
442                 heading.addReportUserAttribute(rua);
443             }
444             if (level == -1)
445             {
446                 params.setString("heading", "0");
447             }
448
449             String JavaDoc msg = getLocalizedHeadingConfirmMessage(report, l10n);
450             scarabR.setConfirmMessage(msg);
451         }
452         else
453         {
454             scarabR.setAlertMessage(L10NKeySet.NoUsersSelected);
455         }
456     }
457         
458     /**
459      * Removes users from temporary working list.
460      */

461     public void doRemoveusers(RunData data, TemplateContext context)
462         throws Exception JavaDoc
463     {
464         setup(data,context);
465         if (!report.isEditable(user))
466         {
467             setNoPermissionMessage();
468             setTarget(data, "reports,ReportList.vm");
469             return;
470         }
471
472         int axis = params.getInt("axis", 0); // 0=row; 1=column
473
int level = params.getInt("heading", -1);
474         int type = params.getInt("headingtype", 0);
475         
476         // not too elegant for editing, but if groups have been defined
477
// we remove them for now until the rest of the reports is
478
// more code complete !FIXME!
479
ReportHeading heading = report.getReportDefinition()
480             .getAxis(axis).getHeading(level);
481         if (type != heading.calculateType()
482             || heading.getReportGroups() != null)
483         {
484             heading.reset();
485             scarabR.setAlertMessage(L10NKeySet.CouldNotMakeRequestedChange);
486         }
487         else
488         {
489             String JavaDoc[] userIds = params.getStrings(SELECTED_USER);
490             if (userIds != null && userIds.length > 0)
491             {
492                 for (int i =0; i<userIds.length; i++)
493                 {
494                     String JavaDoc userId = userIds[i];
495                     ReportUserAttribute rua = new ReportUserAttribute();
496                     rua.setUserId(new Integer JavaDoc(userId));
497                     rua.setAttributeId(new Integer JavaDoc(
498                         params.getString("old_attr_" + userId)));
499                     
500                     List JavaDoc ruas = heading.getReportUserAttributes();
501                     if (ruas != null)
502                     {
503                         ruas.remove(rua);
504                     }
505                 }
506                 scarabR.setConfirmMessage(L10NKeySet.SelectedUsersWereRemoved);
507             }
508             else
509             {
510                 scarabR.setAlertMessage(L10NKeySet.NoUsersSelected);
511             }
512         }
513     }
514
515     /**
516      * Changes the user attribute a user is associated with.
517      */

518     public void doUpdateusers(RunData data, TemplateContext context)
519         throws Exception JavaDoc
520     {
521         
522         setup(data,context);
523         if (!report.isEditable(user))
524         {
525             setNoPermissionMessage();
526             setTarget(data, "reports,ReportList.vm");
527             return;
528         }
529
530         int axis = params.getInt("axis", 0); // 0=row; 1=column
531
int level = params.getInt("heading", -1);
532         int type = params.getInt("headingtype", 0);
533         
534         // not too elegant for editing, but if groups have been defined
535
// we remove them for now until the rest of the reports is
536
// more code complete !FIXME!
537
ReportHeading heading = report.getReportDefinition()
538             .getAxis(axis).getHeading(level);
539         if (type != heading.calculateType()
540             || heading.getReportGroups() != null)
541         {
542             heading.reset();
543             scarabR.setAlertMessage(L10NKeySet.CouldNotMakeRequestedChange);
544         }
545         else
546         {
547             String JavaDoc[] userIds = params.getStrings(SELECTED_USER);
548             if (userIds != null && userIds.length > 0)
549             {
550                 for (int i =0; i<userIds.length; i++)
551                 {
552                     String JavaDoc userId = userIds[i];
553                     ReportUserAttribute rua = new ReportUserAttribute();
554                     rua.setUserId(new Integer JavaDoc(userId));
555                     rua.setAttributeId(new Integer JavaDoc(
556                         params.getString("old_attr_" + userId)));
557                     
558                     List JavaDoc ruas = heading.getReportUserAttributes();
559                     if (ruas != null)
560                     {
561                         /* this will make it hard to find dupes
562                         for (Iterator i=ruas.iterator(); i.hasNext();)
563                         {
564                             Object obj = i.next();
565                             if (obj.equals(rua))
566                             {
567                                 ((ReportUserAttribute)obj).setAttributeId();
568                             }
569                         }
570                         */

571                         ruas.remove(rua);
572                     }
573                     
574                     rua = new ReportUserAttribute();
575                     rua.setUserId(new Integer JavaDoc(userId));
576                     rua.setAttributeId(new Integer JavaDoc(
577                         params.getString("asso_user_{" + userId + "}")));
578                     heading.addReportUserAttribute(rua);
579                 }
580                 scarabR.setConfirmMessage(L10NKeySet.SelectedUsersWereModified);
581             }
582             else
583             {
584                 scarabR.setAlertMessage(L10NKeySet.NoUsersSelected);
585             }
586         }
587     }
588
589     /**
590      * Changes the user attribute a user is associated with.
591      */

592     public void doRemoveheading(RunData data, TemplateContext context)
593         throws Exception JavaDoc
594     {
595         setup(data,context);
596         if (!report.isEditable(user))
597         {
598             setNoPermissionMessage();
599             setTarget(data, "reports,ReportList.vm");
600             return;
601         }
602
603         int axis = params.getInt("axis", 0); // 0=row; 1=column
604
int level = params.getInt("heading", -1);
605         //int type = params.getInt("headingtype", 0);
606

607         if (level >= 0)
608         {
609             List JavaDoc headings = report.getReportDefinition()
610                 .getAxis(axis).getReportHeadings();
611             headings.remove(level);
612             scarabR.setConfirmMessage(L10NKeySet.HeadingRemoved);
613         }
614         else
615         {
616             scarabR.setAlertMessage(L10NKeySet.NoHeadingSelected);
617         }
618     }
619
620     /**
621      * Redirects to screen to group the options/users in the selected
622      * heading.
623      */

624     public void doGotoeditgroups(RunData data, TemplateContext context)
625         throws Exception JavaDoc
626     {
627         setup(data,context);
628         
629         int level = params.getInt("heading", -1);
630
631         // user groups is not implemented
632
if (level >= 0)
633         {
634             int axis = params.getInt("axis", 0); // 0=row; 1=column
635
ReportHeading heading = (ReportHeading)report.getReportDefinition()
636                 .getAxis(axis).getReportHeadings().get(level);
637             if (heading.calculateType() == 0)
638             {
639                 setTarget(data, "reports,EditGroups.vm");
640             }
641             else
642             {
643                 scarabR.setAlertMessage(getLocalizationTool(context)
644                     .get("GroupsAreForOptionsOnly"));
645             }
646         }
647         else
648         {
649             scarabR.setAlertMessage(L10NKeySet.NoHeadingSelected);
650         }
651     }
652
653     /**
654      *
655      */

656     public void doAddheading(RunData data, TemplateContext context)
657         throws Exception JavaDoc
658     {
659         setup(data,context);
660         if (!report.isEditable(user))
661         {
662             setNoPermissionMessage();
663             setTarget(data, "reports,ReportList.vm");
664             return;
665         }
666
667
668         int axisIndex = params.getInt("axis", 0); // 0=row; 1=column
669
ReportAxis axis = report.getReportDefinition().getAxis(axisIndex);
670         axis.addReportHeading(new ReportHeading());
671         params.setString("heading", String.valueOf(axis.getReportHeadings().size()-1));
672         // remove old intake data
673
Intake intake = getIntakeTool(context);
674         intake.removeAll();
675         scarabR.setConfirmMessage(L10NKeySet.HeadingAddedNowAddContent);
676     }
677
678     public void doAddgroup(RunData data, TemplateContext context)
679         throws Exception JavaDoc
680     {
681         setup(data,context);
682         if (!report.isEditable(user))
683         {
684             setNoPermissionMessage();
685             setTarget(data, "reports,ReportList.vm");
686             return;
687         }
688
689         int axis = params.getInt("axis", 0); // 0=row; 1=column
690
int level = params.getInt("heading", -1);
691                     
692         String JavaDoc name = params.getString("groupname_new");
693         if (name != null)
694         {
695             name = name.trim();
696         }
697
698         if (name == null || name.length() == 0)
699         {
700             scarabR.setAlertMessage(L10NKeySet.InvalidGroupName);
701         }
702         else
703         {
704             ReportHeading heading = report.getReportDefinition()
705                 .getAxis(axis).getHeading(level);
706             ReportGroup group = new ReportGroup();
707             group.setName(name);
708             // make sure we are not adding a new group with a non-unique name
709
List JavaDoc groups = heading.getReportGroups();
710             if (groups != null && groups.contains(group))
711             {
712                 scarabR.setAlertMessage(L10NKeySet.DuplicateGroupName);
713             }
714             else
715             {
716                 heading.addReportGroup(group);
717                 
718                 params.remove("groupname_new");
719                 //params.setString("groupname_" + index, group.getName());
720
scarabR.setConfirmMessage(L10NKeySet.GroupAdded);
721             }
722
723             /* intake way
724             UIReportGroup group = new UIReportGroup();
725             Group intakeGroup = intake.get("UIReportGroup",
726                                            group.getQueryKey(), false);
727             if (intakeGroup != null)
728             {
729                 intakeGroup.setProperties(group);
730                 if (group.getDisplayValue() != null
731                      && group.getDisplayValue().length() > 0)
732                 {
733                     ReportHeading heading = report.getReportDefinition()
734                         .getAxis(axis).getHeading(level);
735                     ReportGroup rgroup = new ReportGroup();
736                     rgroup.setName(group.getDisplayValue());
737                     heading.addReportGroup(rgroup);
738                 }
739             }
740             */

741         }
742     }
743
744     public void doDeletegroup(RunData data, TemplateContext context)
745         throws Exception JavaDoc
746     {
747         setup(data,context);
748         if (!report.isEditable(user))
749         {
750             setNoPermissionMessage();
751             setTarget(data, "reports,ReportList.vm");
752             return;
753         }
754         
755                 String JavaDoc[] groupIndices = params.getStrings("selectgroup");
756         if (groupIndices == null || groupIndices.length == 0)
757         {
758             scarabR.setAlertMessage(L10NKeySet.NoGroupSelected);
759         }
760         else
761         {
762             int axis = params.getInt("axis", 0); // 0=row; 1=column
763
int level = params.getInt("heading", -1);
764             List JavaDoc reportGroups = report.getReportDefinition()
765                 .getAxis(axis).getHeading(level).getReportGroups();
766
767             for (int j = groupIndices.length-1; j>=0; j--)
768             {
769                 reportGroups.remove(Integer.parseInt(groupIndices[j]));
770             }
771             scarabR.setConfirmMessage(L10NKeySet.SelectedGroupDeleted);
772                         
773             /* intake way
774             for (int i=groups.size()-1; i>=0; i--)
775             {
776                 ReportGroup rgroup = (ReportGroup)groups.get(i);
777                 UIReportGroup uirg = new UIReportGroup(rgroup.getName());
778                 Group intakeGroup = intake.get("UIReportGroup",
779                                                uirg.getQueryKey(), false);
780                 if (intakeGroup != null)
781                 {
782                     intakeGroup.setProperties(uirg);
783                     if (uirg.isSelected())
784                     {
785                         groups.remove(i);
786                     }
787                 }
788             }
789             */

790         }
791     }
792
793     public void doEditgroupname(RunData data, TemplateContext context)
794         throws Exception JavaDoc
795     {
796         setup(data,context);
797         
798         if (!report.isEditable(user))
799         {
800             setNoPermissionMessage();
801             setTarget(data, "reports,ReportList.vm");
802             return;
803         }
804         
805         Object JavaDoc[] keys = params.getKeys();
806         int axis = params.getInt("axis", 0); // 0=row; 1=column
807
int level = params.getInt("heading", -1);
808         List JavaDoc reportGroups = report.getReportDefinition()
809             .getAxis(axis).getHeading(level).getReportGroups();
810
811         for (int i =0; i < keys.length; i++)
812         {
813             String JavaDoc key = keys[i].toString();
814             if (key.startsWith("groupname_") && key.indexOf("new") == -1)
815             {
816                 int index = Integer.parseInt(key.substring(key.indexOf("_")+1,
817                                              key.length()));
818                 ReportGroup group = (ReportGroup)reportGroups.get(index);
819                 String JavaDoc name = params.getString(key, "").trim();
820                 if (name.length() == 0)
821                 {
822                     scarabR.setAlertMessage(L10NKeySet.InvalidGroupName);
823                 }
824                 else
825                 {
826                     group.setName(name);
827                 }
828             }
829         }
830         scarabR.setConfirmMessage(L10NKeySet.GroupsChanged);
831     }
832
833
834     public void doSavegroups(RunData data, TemplateContext context)
835         throws Exception JavaDoc
836     {
837         setup(data,context);
838         
839         if (!report.isEditable(user))
840         {
841             setNoPermissionMessage();
842             setTarget(data, "reports,ReportList.vm");
843             return;
844         }
845
846         int axis = params.getInt("axis", 0); // 0=row; 1=column
847
int level = params.getInt("heading", -1);
848         ReportHeading heading = report.getReportDefinition()
849             .getAxis(axis).getHeading(level);
850         List JavaDoc reportGroups = heading.getReportGroups();
851         // the form contains data to restore the groups and it is
852
// easier to start from scratch but grab a copy of the option/users
853
// before resetting them to null.
854
List JavaDoc groupedAttributes = heading.retrieveGroupedAttributes();
855         for (Iterator JavaDoc i = reportGroups.iterator(); i.hasNext();)
856         {
857             ReportGroup group = (ReportGroup)i.next();
858             group.reset();
859         }
860
861         boolean success = true;
862         if (heading.calculateType() == 0)
863         {
864             for (Iterator JavaDoc j = groupedAttributes.iterator(); j.hasNext();)
865             {
866                 ReportOptionAttribute reportOption =
867                     (ReportOptionAttribute)j.next();
868                 String JavaDoc name = params.getString("option_" +
869                                                reportOption.getOptionId());
870                 if (name == null || name.trim().length() == 0)
871                 {
872                     scarabR.setAlertMessage(L10NKeySet.InvalidGroupName);
873                     success = false;
874                     break;
875                 }
876                 else
877                 {
878                     for (Iterator JavaDoc i = reportGroups.iterator(); i.hasNext();)
879                     {
880                         ReportGroup group = (ReportGroup)i.next();
881                         if (name.equals(group.getName()))
882                         {
883                             group.addReportOptionAttribute(reportOption);
884                         }
885                     }
886                 }
887             }
888         }
889         else
890         {
891             for (Iterator JavaDoc j = groupedAttributes.iterator(); j.hasNext();)
892             {
893                 ReportUserAttribute reportUser =
894                     (ReportUserAttribute)j.next();
895                 String JavaDoc name = params.getString(new StringBuffer JavaDoc(20)
896                     .append("att_").append(reportUser.getAttributeId())
897                     .append("user_").append(reportUser.getUserId()).toString());
898                 if (name == null || name.trim().length() == 0)
899                 {
900                     scarabR.setAlertMessage(L10NKeySet.InvalidGroupName);
901                     success = false;
902                     break;
903                 }
904                 else
905                 {
906                     for (Iterator JavaDoc i = reportGroups.iterator(); i.hasNext();)
907                     {
908                         ReportGroup group = (ReportGroup)i.next();
909                         if (name.equals(group.getName()))
910                         {
911                             group.addReportUserAttribute(reportUser);
912                         }
913                     }
914                 }
915             }
916         }
917
918         if (success)
919         {
920             setTarget(data, "reports,AxisConfiguration.vm");
921         }
922     }
923
924
925     public void doAdddate(RunData data, TemplateContext context)
926         throws Exception JavaDoc
927     {
928         setup(data,context);
929         if (!report.isEditable(user))
930         {
931             setNoPermissionMessage();
932             setTarget(data, "reports,ReportList.vm");
933             return;
934         }
935
936
937         int axis = params.getInt("axis", 0); // 0=row; 1=column
938
int level = params.getInt("heading", -1);
939
940         ReportHeading heading = report.getReportDefinition()
941             .getAxis(axis).getHeading(level);
942         // if level was -1, we have created a new level. So mark the new
943
// level as the current one.
944
params.setString("heading", "0");
945         
946         List JavaDoc dates = heading.getReportDates();
947         int index = 1;
948         if (dates == null)
949         {
950             // make sure the heading does not contain old option or user data
951
heading.reset();
952         }
953         else
954         {
955             index = dates.size() + 1;
956         }
957         Calendar JavaDoc cal = scarabR.getCalendar();
958         cal.set(Calendar.YEAR, params.getInt("y_" + index));
959         cal.set(Calendar.MONTH, params.getInt("m_" + index) - 1);
960         cal.set(Calendar.DAY_OF_MONTH, params.getInt("d_" + index));
961         cal.set(Calendar.HOUR_OF_DAY, params.getInt("h_" + index));
962         cal.set(Calendar.MINUTE, 0);
963         cal.set(Calendar.SECOND, 0);
964         ReportDate rdate = new ReportDate();
965         rdate.setTime(cal.getTime().getTime());
966         heading.addReportDate(rdate);
967         scarabR.setConfirmMessage(L10NKeySet.DateAdded);
968     }
969
970     public void doDeletedate(RunData data, TemplateContext context)
971         throws Exception JavaDoc
972     {
973         setup(data,context);
974         
975         if (!report.isEditable(user))
976         {
977             setNoPermissionMessage();
978             setTarget(data, "reports,ReportList.vm");
979             return;
980         }
981         
982         String JavaDoc[] dateIndices = params.getStrings("selectdate");
983         if (dateIndices == null || dateIndices.length == 0)
984         {
985             scarabR.setAlertMessage(L10NKeySet.NoDateSelected);
986         }
987         else
988         {
989             int axis = params.getInt("axis", 0); // 0=row; 1=column
990
int level = params.getInt("heading", -1);
991             List JavaDoc reportDates = report.getReportDefinition()
992                 .getAxis(axis).getHeading(level).getReportDates();
993
994             for (int j = dateIndices.length-1; j>=0; j--)
995             {
996                 reportDates.remove(Integer.parseInt(dateIndices[j])-1);
997             }
998             scarabR.setConfirmMessage(L10NKeySet.SelectedDateDeleted);
999         }
1000    }
1001
1002
1003    public void doRedirecttocrossmodulelist(RunData data, TemplateContext context)
1004         throws Exception JavaDoc
1005    {
1006        setup(data,context);
1007        // x-module/issuetype works off of user's list, so make the report
1008
// list the current user's list.
1009
user.setCurrentMITList(
1010            report.getMITList());
1011        setTarget(data, "reports,XModuleList.vm");
1012    }
1013
1014    public void doConfinedataset(RunData data, TemplateContext context)
1015        throws Exception JavaDoc
1016    {
1017        setup(data,context);
1018        
1019        if (!report.isEditable(user))
1020        {
1021            setNoPermissionMessage();
1022            setTarget(data, "reports,ReportList.vm");
1023            return;
1024        }
1025
1026        
1027        if ("fixed".equals(params.getString("def_date")))
1028        {
1029            Calendar JavaDoc cal = scarabR.getCalendar();
1030            cal.set(Calendar.YEAR, params.getInt("def_yr"));
1031            cal.set(Calendar.MONTH, params.getInt("def_month") - 1);
1032            cal.set(Calendar.DAY_OF_MONTH, params.getInt("def_day"));
1033            cal.set(Calendar.HOUR_OF_DAY, params.getInt("def_hr"));
1034            cal.set(Calendar.MINUTE, 0);
1035            cal.set(Calendar.SECOND, 0);
1036            report.setDefaultDate(cal.getTime());
1037        }
1038        else
1039        {
1040            report.setDefaultDate(null);
1041        }
1042        ScarabLocalizationTool l10n = getLocalizationTool(context);
1043        scarabR.setConfirmMessage(L10NKeySet.ChangesSaved);
1044        setTarget(data, "reports,ConfineDataset.vm");
1045    }
1046
1047
1048    public void doSwaprowcol(RunData data, TemplateContext context)
1049        throws Exception JavaDoc
1050    {
1051        setup(data,context);
1052        
1053        if (!report.isEditable(user))
1054        {
1055            setNoPermissionMessage();
1056            setTarget(data, "reports,ReportList.vm");
1057            return;
1058        }
1059
1060              
1061
1062        ReportDefinition reportDefn = report.getReportDefinition();
1063        List JavaDoc axes = reportDefn.getReportAxisList();
1064        if (axes != null && !axes.isEmpty())
1065        {
1066            if (axes.size() == 1)
1067            {
1068                reportDefn.addReportAxis(new ReportAxis());
1069            }
1070            ReportAxis row = (ReportAxis)axes.remove(0);
1071            // add back as column
1072
reportDefn.addReportAxis(row);
1073        }
1074        
1075        // FIXME: do we need a confirmation message? -jon
1076
}
1077
1078    public void doVerifyreport(RunData data, TemplateContext context)
1079         throws Exception JavaDoc
1080    {
1081        setup(data,context);
1082        if (report.removeStaleDefinitions())
1083        {
1084            scarabR.setInfoMessage(l10n.get("ReportDefinitionModified"));
1085        }
1086    }
1087
1088    public void doGeneratereport(RunData data, TemplateContext context)
1089         throws Exception JavaDoc
1090    {
1091        setup(data,context);
1092
1093        if (report.removeStaleDefinitions())
1094        {
1095            scarabR.setInfoMessage(l10n.get("ReportDefinitionModified"));
1096        }
1097        else if (report.getReportDefinition().reportQueryIsExpensive() )
1098        {
1099            scarabR.setAlertMessage(l10n.format("ReportIsTooExpensive", String.valueOf(
1100                             report.getReportDefinition().maximumHeadings())));
1101        }
1102        else
1103        {
1104        // Determine the report display format, looking first at the
1105
// request parameters.
1106
String JavaDoc format = ScarabUtil.findValue(data, ExportFormat.KEY_NAME);
1107        if (StringUtils.isEmpty(format))
1108        {
1109            // Next, examine the request for the Intake parameter
1110
// (which we'll allow to override a persisted pref).
1111
Group intakeReport = getIntakeReportGroup(getIntakeTool(context),
1112                                                      report);
1113            // Exports from the "Report output" screen have a null
1114
// Intake Group.
1115
if (intakeReport != null)
1116            {
1117                format = (String JavaDoc) intakeReport.get("Format").getValue();
1118            }
1119
1120            if (format == null)
1121            {
1122                // Lastly, look at the persisted report format.
1123
format = report.getFormat();
1124            }
1125        }
1126
1127        if (ExportFormat.EXCEL_FORMAT.equalsIgnoreCase(format)
1128            || ExportFormat.TSV_FORMAT.equalsIgnoreCase(format))
1129        {
1130            // The ReportExport screen has no corresponding template.
1131
data.getParameters().setString(ExportFormat.KEY_NAME, format);
1132            setTarget(data, "ReportExport.vm");
1133        }
1134        else
1135        {
1136            setTarget(data, "reports,Report_1.vm");
1137        }
1138            
1139        }
1140    }
1141    
1142    public void doCreatenew(RunData data, TemplateContext context)
1143        throws Exception JavaDoc
1144    {
1145        String JavaDoc key = data.getParameters()
1146            .getString(ScarabConstants.CURRENT_REPORT);
1147        data.getParameters().remove(ScarabConstants.CURRENT_REPORT);
1148        ScarabUser user = (ScarabUser)data.getUser();
1149        if (key != null && key.length() > 0)
1150        {
1151            user.setCurrentReport(key, null);
1152        }
1153        if (user.getCurrentMITList() == null)
1154        {
1155            //context.add("report", Boolean.TRUE);
1156
setTarget(data, "reports,XModuleList.vm");
1157        }
1158        else
1159        {
1160            setTarget(data, "reports,Info.vm");
1161        }
1162    }
1163    
1164
1165    public void doSavereport(RunData data, TemplateContext context)
1166        throws Exception JavaDoc
1167    {
1168        setup(data,context);
1169        
1170        if (!report.isSavable((ScarabUser)data.getUser()))
1171        {
1172            setNoPermissionMessage();
1173            setTarget(data, "reports,ReportList.vm");
1174        }
1175        else if (intake.isAllValid())
1176        {
1177            // make sure report has a name
1178
if (report.getName() == null || report.getName().trim().length() == 0)
1179            {
1180                Group intakeReport = getIntakeReportGroup(intake, report);
1181                if (intakeReport != null)
1182                {
1183                    intakeReport.setValidProperties(report);
1184                }
1185            }
1186
1187            if (report.getName() == null || report.getName().trim().length() == 0)
1188            {
1189                scarabR.setAlertMessage(L10NKeySet.SavedReportsMustHaveName);
1190                setTarget(data, "reports,Info.vm");
1191            }
1192            else
1193            {
1194                //don't save extra whitespace as part of name.
1195
String JavaDoc name = report.getName().trim();
1196                report.setName(name);
1197                // make sure name is unique, mysql text queries are
1198
// case-insensitive, otherwise we may need to do this
1199
// differently to avoid similar but not exact matches.
1200
org.tigris.scarab.om.Report savedReport = ReportPeer
1201                    .retrieveByName(name);
1202                if (savedReport == null
1203                    || savedReport.getQueryKey().equals(report.getQueryKey()))
1204                {
1205                    report.save();
1206                    scarabR.setConfirmMessage(L10NKeySet.ReportSaved);
1207                }
1208                else
1209                {
1210                    scarabR.setAlertMessage(L10NKeySet.ReportNameNotUnique);
1211                    setTarget(data, "reports,Info.vm");
1212                }
1213            }
1214        }
1215        else
1216        {
1217            scarabR.setAlertMessage(L10NKeySet.ErrorPreventedSavingReport);
1218        }
1219    }
1220
1221    public void doDeletestoredreport(RunData data, TemplateContext context)
1222        throws Exception JavaDoc
1223    {
1224        setup(data,context);
1225        ScarabUser user = (ScarabUser)data.getUser();
1226        String JavaDoc[] reportIds = data.getParameters().getStrings("report_id");
1227        if (reportIds == null || reportIds.length == 0)
1228        {
1229            scarabR.setAlertMessage(l10n.get("MustSelectReport"));
1230        }
1231        else
1232        {
1233            for (int i=0;i<reportIds.length; i++)
1234            {
1235                String JavaDoc reportId = reportIds[i];
1236                if (reportId != null && reportId.length() > 0)
1237                {
1238                    Report torqueReport = ReportManager
1239                        .getInstance(new NumberKey(reportId), false);
1240                    if (new ReportBridge(torqueReport).isDeletable(user))
1241                    {
1242                        torqueReport.setDeleted(true);
1243                        torqueReport.save();
1244                    }
1245                    else
1246                    {
1247                        setNoPermissionMessage();
1248                    }
1249                }
1250            }
1251        }
1252    }
1253
1254    private void setNoPermissionMessage()
1255    {
1256        if(scarabR==null | l10n==null){
1257            throw new RuntimeException JavaDoc("setup() method not called for action event");
1258        }
1259        scarabR.setAlertMessage(
1260            l10n.get(NO_PERMISSION_MESSAGE));
1261    }
1262
1263    /**
1264     * @param intake The instance of Intake used for lookup.
1265     * @param report A specific report.
1266     * @return The <code>Report</code> group.
1267     */

1268    private Group getIntakeReportGroup(Intake intake, ReportBridge report)
1269        throws Exception JavaDoc
1270    {
1271        Group intakeReport = intake.get("Report", report.getQueryKey(), false);
1272        if (intakeReport == null)
1273        {
1274            intakeReport = intake.get("Report", "", false);
1275        }
1276        return intakeReport;
1277    }
1278}
1279
Popular Tags