KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > reports > ReportDefinition


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

48
49 import java.util.List JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.Collections JavaDoc;
53
54 import java.io.StringWriter JavaDoc;
55 import org.apache.commons.betwixt.io.BeanWriter;
56 import org.tigris.scarab.util.Log;
57 import org.apache.torque.om.NumberKey;
58
59 import org.tigris.scarab.om.ScarabUserManager;
60 import org.tigris.scarab.om.RModuleAttribute;
61 import org.tigris.scarab.om.RModuleAttributeManager;
62 import org.tigris.scarab.om.RModuleOption;
63 import org.tigris.scarab.om.RModuleOptionManager;
64 import org.tigris.scarab.om.AttributeOptionManager;
65 import org.tigris.scarab.om.AttributeManager;
66 import org.tigris.scarab.util.ScarabConstants;
67
68 /**
69  * This class is the container for the information used to generate a
70  * report. It is the outermost tag of the XML representation defined
71  * by <a
72  * HREF="http://scarab.tigris.org/source/browse/scarab/src/dtd/report.dtd?rev=1&content-type=text/x-cvsweb-markup">report.dtd</a>
73  * (please see this file for an example).
74  *
75  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
76  * @version $Id: ReportDefinition.java 8567 2003-09-08 17:24:07Z jmcnally $
77  * @see <a HREF="http://scarab.tigris.org/source/browse/scarab/src/dtd/report.dtd?rev=1&content-type=text/x-cvsweb-markup">report.dtd</a>
78  */

79 public class ReportDefinition
80     implements java.io.Serializable JavaDoc
81                //Retrievable
82
{
83     /**
84      * A report can be expensive, so limit the criteria (which translates
85      * to headings) to a number that mysql can handle safely
86      */

87     private static final int MAX_CRITERIA = ScarabConstants.REPORT_MAX_CRITERIA;
88
89     private String JavaDoc name;
90
91     private String JavaDoc description;
92
93     private String JavaDoc format;
94
95     private List JavaDoc moduleIssueTypes;
96
97     private List JavaDoc reportAxisList;
98
99     private ReportDate defaultDate;
100
101     /**
102      * Get the Name value.
103      * @return the Name value.
104      */

105     public String JavaDoc getName()
106     {
107         return name;
108     }
109
110     /**
111      * Set the Name value.
112      * @param newName The new Name value.
113      */

114     public void setName(String JavaDoc newName)
115     {
116         this.name = newName;
117     }
118
119     /**
120      * Get the Description value.
121      * @return the Description value.
122      */

123     public String JavaDoc getDescription()
124     {
125         return description;
126     }
127
128     /**
129      * Set the Description value.
130      * @param newDescription The new Description value.
131      */

132     public void setDescription(String JavaDoc newDescription)
133     {
134         this.description = newDescription;
135     }
136
137     /**
138      * Get the format value.
139      * @return The format value.
140      */

141     public String JavaDoc getFormat()
142     {
143         return format;
144     }
145
146     /**
147      * Set the format value.
148      * @param format The new format value.
149      */

150     public void setFormat(String JavaDoc format)
151     {
152         this.format = format;
153     }
154
155     /**
156      * Get the ModuleIssueTypes value.
157      * @return the ModuleIssueTypes value.
158      */

159     public List JavaDoc getModuleIssueTypes()
160     {
161         return moduleIssueTypes;
162     }
163
164     /**
165      * Set the ModuleIssueTypes value.
166      * @param newModuleIssueTypes The new ModuleIssueTypes value.
167      */

168     public void setModuleIssueTypes(List JavaDoc newModuleIssueTypes)
169     {
170         this.moduleIssueTypes = newModuleIssueTypes;
171     }
172
173     /**
174      * Add a ModuleIssueTypes value.
175      * @param newModuleIssueType The new ModuleIssueTypes value.
176      */

177     public void addModuleIssueType(ModuleIssueType newModuleIssueType)
178     {
179         Log.get().debug("Added a mit, " + newModuleIssueType +
180                            ", to reportDefn");
181         if (moduleIssueTypes == null)
182         {
183             moduleIssueTypes = new ArrayList JavaDoc();
184         }
185         if (!moduleIssueTypes.contains(newModuleIssueType))
186         {
187             moduleIssueTypes.add(newModuleIssueType);
188         }
189     }
190
191     /**
192      * Get the ReportAxisList value.
193      * @return the ReportAxisList value.
194      */

195     public List JavaDoc getReportAxisList()
196     {
197         return reportAxisList;
198     }
199
200     /**
201      * Set the ReportAxisList value.
202      * @param newReportAxisList The new ReportAxisList value.
203      */

204     public void setReportAxisList(List JavaDoc newReportAxisList)
205     {
206         this.reportAxisList = newReportAxisList;
207     }
208
209     /**
210      * Add a ReportAxis value.
211      * @param newReportAxis The new ReportAxis value.
212      */

213     public void addReportAxis(ReportAxis newReportAxis)
214     {
215         if (reportAxisList == null)
216         {
217             reportAxisList = new ArrayList JavaDoc();
218         }
219         reportAxisList.add(newReportAxis);
220     }
221
222     /**
223      * Get the ReportDate value used if no axis is time.
224      * @return the ReportDate value.
225      */

226     public ReportDate getDefaultDate()
227     {
228         return defaultDate;
229     }
230
231     /**
232      * Set the ReportDate value used if no axis is time.
233      * @param newDefaultDate The new ReportDate value.
234      */

235     public void setDefaultDate(ReportDate newDefaultDate)
236     {
237         this.defaultDate = newDefaultDate;
238     }
239
240     
241
242     // private String queryKey;
243

244     /* *
245      * Get the QueryKey value.
246      * @return the QueryKey value.
247      * /
248     public String getQueryKey()
249     {
250         if (queryKey == null)
251         {
252             return "";
253         }
254         return queryKey;
255     }
256     
257     /* *
258      * Set the QueryKey value.
259      * @param newQueryKey The new QueryKey value.
260      * /
261     public void setQueryKey(String newQueryKey)
262     {
263         this.queryKey = newQueryKey;
264     }
265     */

266
267     /**
268      * Gets the specified axis, if it was null prior to this method request
269      * a new ReportAxis is returned for the given index
270      *
271      * @param axisIndex an <code>int</code> value
272      * @return a <code>ReportHeading</code> value
273      */

274     public ReportAxis getAxis(int axisIndex)
275     {
276         List JavaDoc axisList = getReportAxisList();
277         while (axisList == null || axisList.size() < axisIndex + 1)
278         {
279             addReportAxis(new ReportAxis());
280             axisList = getReportAxisList();
281         }
282
283         return (ReportAxis)axisList.get(axisIndex);
284     }
285
286     public List JavaDoc retrieveAllReportOptionAttributes()
287     {
288         List JavaDoc result = null;
289         List JavaDoc axes = getReportAxisList();
290         if (axes != null && !axes.isEmpty())
291         {
292             for (Iterator JavaDoc axi = axes.iterator(); axi.hasNext();)
293             {
294                 List JavaDoc headings = ((ReportAxis)axi.next()).getReportHeadings();
295                 if (headings != null && !headings.isEmpty())
296                 {
297                     for (Iterator JavaDoc hi = headings.iterator(); hi.hasNext();)
298                     {
299                         List JavaDoc options = ((ReportHeading)hi.next())
300                             .consolidateReportOptionAttributes();
301                         if (options != null && !options.isEmpty())
302                         {
303                             for (Iterator JavaDoc i = options.iterator(); i.hasNext();)
304                             {
305                                 if (result == null)
306                                 {
307                                     result = new ArrayList JavaDoc();
308                                 }
309                                 result.add(i.next());
310                             }
311                         }
312                     }
313                 }
314             }
315         }
316         return result == null ? Collections.EMPTY_LIST : result;
317     }
318
319     public List JavaDoc retrieveAllReportUserAttributes()
320     {
321         List JavaDoc result = null;
322         List JavaDoc axes = getReportAxisList();
323         if (axes != null && !axes.isEmpty())
324         {
325             for (Iterator JavaDoc axi = axes.iterator(); axi.hasNext();)
326             {
327                 List JavaDoc headings = ((ReportAxis)axi.next()).getReportHeadings();
328                 if (headings != null && !headings.isEmpty())
329                 {
330                     for (Iterator JavaDoc hi = headings.iterator(); hi.hasNext();)
331                     {
332                         List JavaDoc users = ((ReportHeading)hi.next())
333                             .consolidateReportUserAttributes();
334                         if (users != null && !users.isEmpty())
335                         {
336                             for (Iterator JavaDoc i = users.iterator(); i.hasNext();)
337                             {
338                                 if (result == null)
339                                 {
340                                     result = new ArrayList JavaDoc();
341                                 }
342                                 result.add(i.next());
343                             }
344                         }
345                     }
346                 }
347             }
348         }
349         return result == null ? Collections.EMPTY_LIST : result;
350     }
351
352     public String JavaDoc toXmlString()
353     {
354         String JavaDoc s;
355         try
356         {
357             StringWriter JavaDoc sw = new StringWriter JavaDoc(1024);
358             BeanWriter bw = new BeanWriter(sw);
359                 /*
360                 {
361                     {
362                         writeIDs = false;
363                     }
364                 };
365                 */

366             bw.enablePrettyPrint();
367             bw.writeXmlDeclaration("<?xml version='1.0' encoding='UTF-8' ?>");
368             bw.write(this);
369             bw.flush();
370             s = sw.toString();
371             bw.close();
372         }
373         catch (Exception JavaDoc e)
374         {
375             s = "ERROR! on " + super.toString();
376             Log.get().error("", e);
377         }
378
379         return s;
380     }
381
382     public String JavaDoc displayAttribute(Object JavaDoc obj)
383     // throws TorqueException
384
{
385         Integer JavaDoc attId = null;
386         if (obj instanceof ReportOptionAttribute)
387         {
388             try
389             {
390                 attId = new Integer JavaDoc(AttributeOptionManager.getInstance(
391                     new NumberKey(((ReportOptionAttribute)obj).getOptionId()
392                     .toString())).getAttributeId().toString());
393             }
394             catch (Exception JavaDoc e)
395             {
396                 Log.get().error("Error on Attribute Id=" + attId, e);
397                 return "Error on Attribute Id=" + attId;
398             }
399         }
400         else if (obj instanceof ReportUserAttribute)
401         {
402             attId = ((ReportUserAttribute)obj).getAttributeId();
403         }
404         else
405         {
406             return "";
407         }
408         
409     
410         String JavaDoc result = null;
411         List JavaDoc mits = getModuleIssueTypes();
412         if (mits != null && mits.size() == 1)
413         {
414             ModuleIssueType mit = (ModuleIssueType)mits.get(0);
415             try
416             {
417                 RModuleAttribute rma = RModuleAttributeManager.getInstance(
418                     mit.getModuleId(), attId, mit.getIssueTypeId());
419                 result = rma.getDisplayValue();
420             }
421             catch (Exception JavaDoc e)
422             {
423                 result = "Error on Attribute Id=" + attId;
424                 Log.get().error(result, e);
425             }
426         }
427         else
428         {
429             try
430             {
431                 result = AttributeManager.getInstance(
432                     new NumberKey(attId.toString())).getName();
433             }
434             catch (Exception JavaDoc e)
435             {
436                 result = "Error on Attribute Id=" + attId;
437                 Log.get().error(result, e);
438             }
439         }
440         return result;
441     }
442
443     public String JavaDoc displayOption(ReportOptionAttribute roa)
444     // throws TorqueException
445
{
446         Integer JavaDoc optionId = roa.getOptionId();
447         String JavaDoc result = null;
448         List JavaDoc mits = getModuleIssueTypes();
449         if (mits != null && mits.size() == 1)
450         {
451             ModuleIssueType mit = (ModuleIssueType)mits.get(0);
452             try
453             {
454                 RModuleOption rmo = RModuleOptionManager.getInstance(
455                     mit.getModuleId(), mit.getIssueTypeId(), optionId);
456                 result = rmo.getDisplayValue();
457             }
458             catch (Exception JavaDoc e)
459             {
460                 result = "Error on Option Id=" + optionId;
461                 Log.get().error(result, e);
462             }
463         }
464         else
465         {
466             try
467             {
468                 result = AttributeOptionManager.getInstance(
469                     new NumberKey(optionId.toString())).getName();
470             }
471             catch (Exception JavaDoc e)
472             {
473                 result = "Error on Option Id=" + optionId;
474                 Log.get().error(result, e);
475             }
476         }
477         return result;
478     }
479
480     public String JavaDoc displayUser(ReportUserAttribute rua)
481     // throws TorqueException
482
{
483         String JavaDoc result = null;
484         try
485         {
486             result = ScarabUserManager.getInstance(
487                 new NumberKey(rua.getUserId().toString())).getName();
488         }
489         catch (Exception JavaDoc e)
490         {
491             result = "Error on Option Id=" + rua.getUserId();
492             Log.get().error(result, e);
493         }
494         return result;
495     }
496
497     public String JavaDoc displayHeading(ReportHeading heading)
498     {
499         String JavaDoc summary = null;
500         List JavaDoc options = heading.getReportOptionAttributes();
501         List JavaDoc groups = heading.getReportGroups();
502         List JavaDoc users = heading.getReportUserAttributes();
503         if (options != null && !options.isEmpty())
504         {
505             String JavaDoc attribute = null;
506             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(20*options.size());
507             for (Iterator JavaDoc i = options.iterator(); i.hasNext();)
508             {
509                 ReportOptionAttribute roa = (ReportOptionAttribute)i.next();
510                 String JavaDoc newAttribute = displayAttribute(roa);
511                 if (!newAttribute.equals(attribute))
512                 {
513                     if (attribute != null)
514                     {
515                         sb.append("; ");
516                     }
517                     attribute = newAttribute;
518                     sb.append(attribute).append(": ");
519                 }
520                 sb.append(displayOption(roa)).append(", ");
521             }
522             sb.setLength(sb.length() - 2);
523             summary = sb.toString();
524         }
525         else if (groups != null && !groups.isEmpty())
526         {
527             StringBuffer JavaDoc sb =
528                 new StringBuffer JavaDoc(10*groups.size());
529             for (Iterator JavaDoc i = groups.iterator(); i.hasNext();)
530             {
531                 sb.append(((ReportGroup)i.next()).getName())
532                     .append('/');
533             }
534             sb.setLength(sb.length() - 1);
535                 summary = sb.toString();
536         }
537         else if (users != null && !users.isEmpty())
538         {
539             String JavaDoc attribute = null;
540             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(20*users.size());
541             for (Iterator JavaDoc i = users.iterator(); i.hasNext();)
542             {
543                 ReportUserAttribute rua = (ReportUserAttribute)i.next();
544                 String JavaDoc newAttribute = displayAttribute(rua);
545                 if (!newAttribute.equals(attribute))
546                 {
547                     if (attribute != null)
548                     {
549                         sb.append("; ");
550                     }
551                     attribute = newAttribute;
552                     sb.append(attribute).append(": ");
553                 }
554                 sb.append(displayUser(rua)).append(", ");
555                 }
556             sb.setLength(sb.length() - 2);
557             summary = sb.toString();
558         }
559         // FIXME: Date ranges are not implemented yet.
560
else if (heading.getReportDates() != null
561                  && !heading.getReportDates().isEmpty())
562             //|| heading.getReportDateRanges() != null)
563
{
564             summary = "Dates";
565         }
566         return summary;
567     }
568
569     /**
570      * The configured maximum from Scarab.properties:
571      * scarab.report.max.criteria
572      */

573     public int maximumHeadings()
574     {
575         return MAX_CRITERIA;
576     }
577
578     /**
579      * Determines whether another heading level is allowed for the given axis.
580      *
581      * @param axis a non-null <code>ReportAxis</code> value
582      */

583     public boolean allowMoreHeadings(ReportAxis axis)
584     {
585         return availableNumberOfHeadings(axis) > 0;
586     }
587
588     /**
589      * Determines whether the report is going to be expensive. This is based
590      * on a comparison of the number of heading levels on both axes with
591      * the configured maximum in Scarab.properties.
592      */

593     public boolean reportQueryIsExpensive()
594     {
595         return totalNumberOfNonDateHeadings() > MAX_CRITERIA;
596     }
597
598     /**
599      * The number of headings that could still be added to the report without
600      * exceeding the configured maximum.
601      */

602     public int totalAvailableNumberOfHeadings()
603     {
604         return maximumHeadings() - totalNumberOfNonDateHeadings();
605     }
606
607     /**
608      * The number of headings that could still be added to the given axis
609      * without exceeding the configured maximum.
610      *
611      * @param axis a non-null <code>ReportAxis</code> value
612      */

613     public int availableNumberOfHeadings(ReportAxis axis)
614     {
615         // the following assumes two axes
616
int result = maximumHeadings() - totalNumberOfNonDateHeadings() - 1;
617         List JavaDoc axes = getReportAxisList();
618         if (axes != null)
619         {
620             ReportAxis tmpAxis;
621             for (Iterator JavaDoc i = axes.iterator(); i.hasNext();)
622             {
623                 tmpAxis = (ReportAxis)i.next();
624                 if (tmpAxis != null && !tmpAxis.equals(axis))
625                 {
626                     List JavaDoc headings = tmpAxis.getReportHeadings();
627                     if (headings != null && headings.size() > 0 &&
628                         (((ReportHeading)headings.get(0)).size() > 0))
629                     {
630                         result++;
631                     }
632                     break;
633                 }
634             }
635         }
636         return result;
637     }
638
639     private int totalNumberOfNonDateHeadings()
640     {
641         int count = 0;
642         List JavaDoc axes = getReportAxisList();
643         if (axes != null)
644         {
645             ReportAxis axis;
646             for (Iterator JavaDoc i = axes.iterator(); i.hasNext();)
647             {
648                 axis = (ReportAxis)i.next();
649                 if (axis != null)
650                 {
651                     count += numberOfNonDateHeadings(axis);
652                 }
653             }
654         }
655         return count;
656     }
657
658     private int numberOfNonDateHeadings(ReportAxis axis)
659     {
660         int count = 0;
661         List JavaDoc headings = axis.getReportHeadings();
662         if (headings != null)
663         {
664             int size = headings.size();
665             if (size > 0)
666             {
667                 if ( size != 1 ||
668                      !(((ReportHeading)headings.get(0))
669                        .get(0) instanceof ReportDate))
670                 {
671                     count += size;
672                 }
673             }
674         }
675         return count;
676     }
677 }
678
679
Popular Tags