KickJava   Java API By Example, From Geeks To Geeks.

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


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  */

41
42 import java.util.List JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.Collections JavaDoc;
46 import org.apache.torque.om.NumberKey;
47 import org.apache.fulcrum.intake.Retrievable;
48 import org.tigris.scarab.util.Log;
49 import org.tigris.scarab.om.AttributeOptionManager;
50
51 /**
52  *
53  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
54  * @version $Id: ReportHeading.java 7823 2003-05-01 18:37:06Z elicia $
55  */

56 public class ReportHeading
57     implements java.io.Serializable JavaDoc,
58                Retrievable
59 {
60     private List JavaDoc reportGroups;
61
62     private List JavaDoc reportOptionAttributes;
63
64     private List JavaDoc reportDates;
65
66     private List JavaDoc reportUserAttributes;
67
68     private List JavaDoc reportDateRanges;
69
70     private String JavaDoc queryKey;
71
72     public int calculateType()
73     {
74         int type = 0;
75         if (getReportGroups() != null && getReportGroups().size() > 0)
76         {
77             ReportGroup firstGroup = (ReportGroup)getReportGroups().get(0);
78             if (firstGroup.getReportUserAttributes() != null)
79             {
80                 type = 1;
81             }
82         }
83         else if (getReportUserAttributes() != null)
84         {
85             type = 1;
86         }
87         else if (getReportDates() != null || getReportDateRanges() != null)
88         {
89             type = 2;
90         }
91         return type;
92     }
93
94     public List JavaDoc retrieveGroupedAttributes()
95     {
96         List JavaDoc result = null;
97         List JavaDoc groups = getReportGroups();
98         if (groups == null || groups.isEmpty())
99         {
100             result = Collections.EMPTY_LIST;
101         }
102         else if (calculateType() == 0)
103         {
104             result = new ArrayList JavaDoc();
105             for (Iterator JavaDoc i = groups.iterator(); i.hasNext();)
106             {
107                 List JavaDoc options = ((ReportGroup)i.next()).
108                     getReportOptionAttributes();
109                 if (options != null)
110                 {
111                     result.addAll(options);
112                 }
113             }
114         }
115         else
116         {
117             result = new ArrayList JavaDoc();
118             for (Iterator JavaDoc i = groups.iterator(); i.hasNext();)
119             {
120                 List JavaDoc users = ((ReportGroup)i.next()).
121                     getReportUserAttributes();
122                 if (users != null)
123                 {
124                     result.addAll(users);
125                 }
126             }
127         }
128         
129         return result;
130     }
131
132     public void reset()
133     {
134         reportGroups = null;
135         reportOptionAttributes = null;
136         reportUserAttributes = null;
137         reportDates = null;
138         reportDateRanges = null;
139     }
140
141     public Object JavaDoc get(int i)
142     {
143         Object JavaDoc obj = null;
144         if (getReportGroups() != null)
145         {
146             obj = getReportGroups().get(i);
147         }
148         else if (getReportOptionAttributes() != null)
149         {
150             obj = getReportOptionAttributes().get(i);
151         }
152         else if (getReportUserAttributes() != null)
153         {
154             obj = getReportUserAttributes().get(i);
155         }
156         // need to sort out dates !FIXME!
157
else if (getReportDates() != null || getReportDateRanges() != null)
158         {
159             obj = getReportDates().get(i);
160         }
161         return obj;
162     }
163
164     public int size()
165     {
166         int size = 0;
167         if (getReportGroups() != null)
168         {
169             size = getReportGroups().size();
170         }
171         else if (getReportOptionAttributes() != null)
172         {
173             size = getReportOptionAttributes().size();
174         }
175         else if (getReportUserAttributes() != null)
176         {
177             size = getReportUserAttributes().size();
178         }
179         // need to sort out dates !FIXME!
180
else if (getReportDates() != null)
181         {
182             size = getReportDates().size();
183         }
184         return size;
185     }
186
187     public boolean singleAttribute()
188     {
189         boolean result = false;
190         Integer JavaDoc firstId = null;
191         Iterator JavaDoc options = consolidateReportOptionAttributes().iterator();
192         if (options.hasNext())
193         {
194             result = true;
195             for (; options.hasNext() && result;)
196             {
197                 try
198                 {
199                     Integer JavaDoc id = new Integer JavaDoc(
200                         AttributeOptionManager.getInstance(
201                         new NumberKey(((ReportOptionAttribute)options.next())
202                                       .getOptionId().toString()))
203                                              .getAttributeId().toString());
204                     if (firstId == null)
205                     {
206                         firstId = id;
207                     }
208                     else
209                     {
210                         result = firstId.equals(id);
211                     }
212                 }
213                 catch (Exception JavaDoc e)
214                 {
215                     Log.get().warn("Error on attribute id", e);
216                     result = false;
217                 }
218             }
219         }
220
221         if (firstId == null)
222         {
223             Iterator JavaDoc users = consolidateReportUserAttributes().iterator();
224             if (users.hasNext())
225             {
226                 result = true;
227                 for (; users.hasNext() && result;)
228                 {
229                     Integer JavaDoc id = ((ReportUserAttribute)users.next())
230                         .getAttributeId();
231                     if (firstId == null)
232                     {
233                         firstId = id;
234                     }
235                     else
236                     {
237                         result = firstId.equals(id);
238                     }
239                 }
240             }
241         }
242         return result;
243     }
244
245
246     /**
247      * returns all the the ReportOptionAttributes involved in the heading
248      * including those in groups
249      *
250      * @return a <code>List</code> value
251      */

252     public List JavaDoc consolidateReportOptionAttributes()
253     {
254         List JavaDoc result = null;
255         List JavaDoc groups = getReportGroups();
256         if (groups != null && !groups.isEmpty())
257         {
258             for (Iterator JavaDoc i = groups.iterator(); i.hasNext();)
259             {
260                 ReportGroup group = (ReportGroup)i.next();
261                 List JavaDoc options = group.getReportOptionAttributes();
262                 if (options != null && !options.isEmpty())
263                 {
264                     for (Iterator JavaDoc j = options.iterator(); j.hasNext();)
265                     {
266                         if (result == null)
267                         {
268                             result = new ArrayList JavaDoc();
269                         }
270                         result.add(j.next());
271                     }
272                 }
273             }
274         }
275         
276         List JavaDoc options = getReportOptionAttributes();
277         if (options != null && !options.isEmpty())
278         {
279             for (Iterator JavaDoc j = options.iterator(); j.hasNext();)
280             {
281                 if (result == null)
282                 {
283                     result = new ArrayList JavaDoc();
284                 }
285                 result.add(j.next());
286             }
287         }
288         if (result == null)
289         {
290             result = Collections.EMPTY_LIST;
291         }
292
293         return result;
294     }
295
296     /**
297      * returns all the the ReportUserAttributes involved in the heading
298      * including those in groups
299      *
300      * @return a <code>List</code> value
301      */

302     public List JavaDoc consolidateReportUserAttributes()
303     {
304         List JavaDoc result = null;
305         List JavaDoc groups = getReportGroups();
306         if (groups != null && !groups.isEmpty())
307         {
308             for (Iterator JavaDoc i = groups.iterator(); i.hasNext();)
309             {
310                 ReportGroup group = (ReportGroup)i.next();
311                 List JavaDoc users = group.getReportUserAttributes();
312                 if (users != null && !users.isEmpty())
313                 {
314                     for (Iterator JavaDoc j = users.iterator(); j.hasNext();)
315                     {
316                         if (result == null)
317                         {
318                             result = new ArrayList JavaDoc();
319                         }
320                         result.add(j.next());
321                     }
322                 }
323             }
324         }
325         
326         List JavaDoc users = getReportUserAttributes();
327         if (users != null && !users.isEmpty())
328         {
329             for (Iterator JavaDoc j = users.iterator(); j.hasNext();)
330             {
331                 if (result == null)
332                 {
333                     result = new ArrayList JavaDoc();
334                 }
335                 result.add(j.next());
336             }
337         }
338         if (result == null)
339         {
340             result = Collections.EMPTY_LIST;
341         }
342
343         return result;
344     }
345     
346     /**
347      * Get the ReportGroups value.
348      * @return the ReportGroups value.
349      */

350     public List JavaDoc getReportGroups()
351     {
352         return reportGroups;
353     }
354
355     /**
356      * Set the ReportGroups value.
357      * @param newReportGroups The new ReportGroups value.
358      */

359     public void setReportGroups(List JavaDoc newReportGroups)
360     {
361         this.reportGroups = newReportGroups;
362     }
363
364     /**
365      * Add a ReportGroup value.
366      * @param newReportGroup The new ReportGroup value.
367      */

368     public void addReportGroup(ReportGroup newReportGroup)
369     {
370         if (reportGroups == null)
371         {
372             reportGroups = new ArrayList JavaDoc();
373             // this is the first group, so move any options/users that were
374
// part of the heading into the group
375
if (newReportGroup.getReportOptionAttributes() == null)
376             {
377                 newReportGroup
378                     .setReportOptionAttributes(getReportOptionAttributes());
379                 setReportOptionAttributes(null);
380             }
381             else if (getReportOptionAttributes() != null)
382             {
383                 newReportGroup.getReportOptionAttributes()
384                     .addAll(getReportOptionAttributes());
385                 setReportOptionAttributes(null);
386             }
387
388             if (newReportGroup.getReportUserAttributes() == null)
389             {
390                 newReportGroup
391                     .setReportUserAttributes(getReportUserAttributes());
392                 setReportUserAttributes(null);
393             }
394             else if (getReportUserAttributes() != null)
395             {
396                 newReportGroup.getReportUserAttributes()
397                     .addAll(getReportUserAttributes());
398                 setReportUserAttributes(null);
399             }
400         }
401         if (!reportGroups.contains(newReportGroup))
402         {
403             reportGroups.add(newReportGroup);
404         }
405     }
406
407     /**
408      * Get the ReportOptionAttributes value.
409      * @return the ReportOptionAttributes value.
410      */

411     public List JavaDoc getReportOptionAttributes()
412     {
413         return reportOptionAttributes;
414     }
415
416     /**
417      * Set the ReportOptionAttributes value.
418      * @param newReportOptionAttributes The new ReportOptionAttributes value.
419      */

420     public void setReportOptionAttributes(List JavaDoc newReportOptionAttributes)
421     {
422         this.reportOptionAttributes = newReportOptionAttributes;
423     }
424
425     /**
426      * Add a ReportOptionAttribute value.
427      * @param newReportOptionAttribute The new ReportOptionAttribute value.
428      */

429     public void addReportOptionAttribute(ReportOptionAttribute newReportOptionAttribute)
430     {
431         if (reportOptionAttributes == null)
432         {
433             reportOptionAttributes = new ArrayList JavaDoc();
434         }
435         if (!reportOptionAttributes.contains(newReportOptionAttribute))
436         {
437             reportOptionAttributes.add(newReportOptionAttribute);
438         }
439     }
440
441     /**
442      * Get the ReportUserAttributes value.
443      * @return the ReportUserAttributes value.
444      */

445     public List JavaDoc getReportUserAttributes()
446     {
447         return reportUserAttributes;
448     }
449
450     /**
451      * Set the ReportUserAttributes value.
452      * @param newReportUserAttributes The new ReportUserAttributes value.
453      */

454     public void setReportUserAttributes(List JavaDoc newReportUserAttributes)
455     {
456         this.reportUserAttributes = newReportUserAttributes;
457     }
458
459     /**
460      * Add a ReportUserAttribute value.
461      * @param newReportUserAttribute The new ReportUserAttribute value.
462      */

463     public void addReportUserAttribute(ReportUserAttribute newReportUserAttribute)
464     {
465         Log.get().debug("Potentially adding " + newReportUserAttribute);
466         
467         if (reportUserAttributes == null)
468         {
469             reportUserAttributes = new ArrayList JavaDoc();
470         }
471         if (!reportUserAttributes.contains(newReportUserAttribute))
472         {
473             Log.get().debug("added " + newReportUserAttribute);
474             reportUserAttributes.add(newReportUserAttribute);
475         }
476     }
477
478     /**
479      * Get the ReportDateRanges value.
480      * @return the ReportDateRanges value.
481      */

482     public List JavaDoc getReportDateRanges()
483     {
484         return reportDateRanges;
485     }
486
487     /**
488      * Set the ReportDateRanges value.
489      * @param newReportDateRanges The new ReportDateRanges value.
490      */

491     public void setReportDateRanges(List JavaDoc newReportDateRanges)
492     {
493         this.reportDateRanges = newReportDateRanges;
494     }
495
496     /**
497      * Add a ReportDateRange value.
498      * @param newReportDateRange The new ReportDateRange value.
499      */

500     public void addReportDateRange(ReportDateRange newReportDateRange)
501     {
502         if (reportDateRanges == null)
503         {
504             reportDateRanges = new ArrayList JavaDoc();
505         }
506         reportDateRanges.add(newReportDateRange);
507     }
508
509     /**
510      * Get the ReportDates value.
511      * @return the ReportDates value.
512      */

513     public List JavaDoc getReportDates()
514     {
515         return reportDates;
516     }
517
518     /**
519      * Set the ReportDates value.
520      * @param newReportDates The new ReportDates value.
521      */

522     public void setReportDates(List JavaDoc newReportDates)
523     {
524         this.reportDates = newReportDates;
525     }
526
527     /**
528      * Add a ReportDate value.
529      * @param newReportDate The new ReportDate value.
530      */

531     public void addReportDate(ReportDate newReportDate)
532     {
533         if (reportDates == null)
534         {
535             reportDates = new ArrayList JavaDoc();
536         }
537         reportDates.add(newReportDate);
538     }
539
540     /**
541      * Get the QueryKey value.
542      * @return the QueryKey value.
543      */

544     public String JavaDoc getQueryKey()
545     {
546         return queryKey == null ? "" : queryKey;
547     }
548     
549     /**
550      * Set the QueryKey value.
551      * @param newQueryKey The new QueryKey value.
552      */

553     public void setQueryKey(String JavaDoc newQueryKey)
554     {
555         this.queryKey = newQueryKey;
556     }
557 }
558
Popular Tags