KickJava   Java API By Example, From Geeks To Geeks.

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


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 // JDK classes
50
import java.util.ArrayList JavaDoc;
51 import java.util.Date JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.HashMap JavaDoc;
56 import java.io.StringReader JavaDoc;
57
58 import org.apache.log4j.Logger;
59
60 // Turbine classes
61
import org.apache.torque.TorqueException;
62
63 import org.tigris.scarab.tools.localization.L10NKeySet;
64 import org.tigris.scarab.util.word.IssueSearch;
65 import org.tigris.scarab.om.ScarabUserManager;
66 import org.tigris.scarab.om.Module;
67 import org.tigris.scarab.om.IssueType;
68 import org.tigris.scarab.om.ModuleManager;
69 import org.tigris.scarab.om.AttributeValue;
70 import org.tigris.scarab.om.AttributeOptionManager;
71 import org.tigris.scarab.om.AttributeOption;
72 import org.tigris.scarab.om.AttributeManager;
73 import org.tigris.scarab.om.Attribute;
74 import org.tigris.scarab.om.ScarabUser;
75 import org.tigris.scarab.om.Scope;
76 import org.tigris.scarab.om.MITList;
77 import org.tigris.scarab.om.MITListItem;
78 import org.tigris.scarab.util.Log;
79 import org.tigris.scarab.util.ScarabConstants;
80 import org.tigris.scarab.services.security.ScarabSecurity;
81
82 import org.apache.commons.betwixt.io.BeanReader;
83
84 /**
85  * This class is a bridge between the xml related classes for defining a
86  * report and the business objects that are used within the screens
87  * to configure the report.
88  */

89 public class ReportBridge
90     implements java.io.Serializable JavaDoc,
91                org.apache.fulcrum.intake.Retrievable // do we want this?
92
{
93
94     private ScarabUser generatedBy;
95     private Date JavaDoc generatedDate;
96
97     private org.tigris.scarab.om.Report torqueReport;
98     private ReportDefinition reportDefn;
99     private ReportHeading newHeading;
100
101     public ReportBridge()
102     {
103         torqueReport = new org.tigris.scarab.om.Report();
104         reportDefn = new ReportDefinition();
105     }
106
107     public ReportBridge(org.tigris.scarab.om.Report report)
108         throws Exception JavaDoc
109     {
110         torqueReport = report;
111         populate(report.getQueryString());
112     }
113
114     public ReportDefinition getReportDefinition()
115     {
116         return reportDefn;
117     }
118
119     public ReportHeading getNewHeading()
120     {
121         if (newHeading == null)
122         {
123             newHeading = new ReportHeading();
124         }
125         return newHeading;
126     }
127
128     // I'm not sure I want this but for now we will implement the Retrievable
129
// interface
130
public String JavaDoc getQueryKey()
131     {
132         return torqueReport.getQueryKey();
133     }
134     public void setQueryKey(String JavaDoc key)
135         throws TorqueException
136     {
137         torqueReport.setQueryKey(key);
138     }
139
140     public String JavaDoc getName()
141     {
142         return torqueReport.getName();
143     }
144     public void setName(String JavaDoc name)
145     {
146         torqueReport.setName(name);
147         reportDefn.setName(name);
148     }
149     public String JavaDoc getDescription()
150     {
151         return torqueReport.getDescription();
152     }
153     public void setDescription(String JavaDoc name)
154     {
155         torqueReport.setDescription(name);
156         reportDefn.setDescription(name);
157     }
158
159     public String JavaDoc getFormat()
160     {
161         return reportDefn.getFormat();
162     }
163
164     public void setFormat(String JavaDoc format)
165     {
166         reportDefn.setFormat(format);
167     }
168
169     public boolean getDeleted()
170     {
171         return torqueReport.getDeleted();
172     }
173     public void setDeleted(boolean b)
174     {
175         torqueReport.setDeleted(b);
176     }
177     public Integer JavaDoc getScopeId()
178     {
179         return torqueReport.getScopeId();
180     }
181     public void setScopeId(Integer JavaDoc id)
182         throws TorqueException
183     {
184         torqueReport.setScopeId(id);
185     }
186
187     public Integer JavaDoc getUserId()
188     {
189         return torqueReport.getUserId();
190     }
191     public void setUserId(Integer JavaDoc id)
192         throws TorqueException
193     {
194         torqueReport.setUserId(id);
195     }
196
197     public Integer JavaDoc getReportId()
198     {
199         return torqueReport.getReportId();
200     }
201     public void setReportId(Integer JavaDoc id)
202     {
203         torqueReport.setReportId(id);
204     }
205
206     public Scope getScope()
207         throws TorqueException
208     {
209         return torqueReport.getScope();
210     }
211
212     /**
213      * Get the value of module.
214      * @return value of module.
215      */

216     public Module getModule()
217         throws TorqueException
218     {
219         Module module = null;
220         if (torqueReport.getModuleId() != null)
221         {
222             module = ModuleManager.getInstance(torqueReport.getModuleId());
223         }
224         
225         return module;
226     }
227     
228     /**
229      * Set the value of module.
230      * @param v Value to assign to module.
231      */

232     public void setModule(Module v)
233         throws TorqueException
234     {
235         reportDefn.setModuleIssueTypes(null);
236         if (v == null)
237         {
238             torqueReport.setModuleId((Integer JavaDoc)null);
239         }
240         else
241         {
242             torqueReport.setModuleId(v.getModuleId());
243             if (torqueReport.getIssueTypeId() != null)
244             {
245                 ModuleIssueType mit = new ModuleIssueType();
246                 mit.setModuleId(v.getModuleId());
247                 mit.setIssueTypeId(torqueReport.getIssueTypeId());
248                 reportDefn.addModuleIssueType(mit);
249             }
250         }
251     }
252
253     /**
254      * Set the value of module.
255      * @param v Value to assign to module.
256      */

257     public void setIssueType(IssueType v)
258         throws TorqueException
259     {
260         reportDefn.setModuleIssueTypes(null);
261         if (v == null)
262         {
263             // issue type id cannot be null
264
torqueReport.setIssueTypeId(ScarabConstants.INTEGER_0);
265         }
266         else
267         {
268             torqueReport.setIssueTypeId(v.getIssueTypeId());
269             if (torqueReport.getModuleId() != null)
270             {
271                 ModuleIssueType mit = new ModuleIssueType();
272                 mit.setModuleId(torqueReport.getModuleId());
273                 mit.setIssueTypeId(v.getIssueTypeId());
274                 reportDefn.addModuleIssueType(mit);
275             }
276         }
277     }
278
279     /**
280      * Checks permission in all modules involved in report
281      */

282     private boolean hasPermission(String JavaDoc permission, ScarabUser user)
283     {
284         boolean result = false;
285         try
286         {
287             MITList mitlist = getMITList();
288             result = mitlist.isSingleModule() ?
289                 user.hasPermission(permission, mitlist.getModule()) :
290                 user.hasPermission(permission, mitlist.getModules());
291         }
292         catch (Exception JavaDoc e)
293         {
294             result = false;
295             Log.get().error(e);
296         }
297         return result;
298     }
299
300     public boolean isEditable(ScarabUser user)
301     {
302         return torqueReport.isNew()
303             ||
304             (Scope.PERSONAL__PK.equals(torqueReport.getScopeId())
305              && user.getUserId().equals(torqueReport.getUserId()))
306             ||
307             (Scope.MODULE__PK.equals(torqueReport.getScopeId()) &&
308              hasPermission(ScarabSecurity.MODULE__EDIT, user));
309     }
310
311     public boolean isDeletable(ScarabUser user)
312     {
313         return (Scope.PERSONAL__PK.equals(torqueReport.getScopeId())
314                 && user.getUserId().equals(torqueReport.getUserId()))
315             ||
316             (Scope.MODULE__PK.equals(torqueReport.getScopeId()) &&
317              hasPermission(ScarabSecurity.ITEM__DELETE, user));
318     }
319
320     public boolean isSavable(ScarabUser user)
321     {
322         return isEditable(user) &&
323             hasPermission( ScarabSecurity.USER__EDIT_PREFERENCES, user);
324     }
325
326     /**
327      * Get the value of generatedBy.
328      * @return value of generatedBy.
329      */

330     public ScarabUser getGeneratedBy()
331         throws Exception JavaDoc
332     {
333         if (generatedBy == null)
334         {
335             if (torqueReport.getUserId() != null)
336             {
337                 generatedBy =
338                     ScarabUserManager.getInstance(torqueReport.getUserId());
339             }
340         }
341         
342         return generatedBy;
343     }
344     
345     /**
346      * Set the value of generatedBy.
347      * @param v Value to assign to generatedBy.
348      */

349     public void setGeneratedBy(ScarabUser v)
350         throws Exception JavaDoc
351     {
352         this.generatedBy = v;
353         torqueReport.setUserId(v.getUserId());
354     }
355     
356     
357     /**
358      * This is the date that was used in the queries for reports on a
359      * single date. It is not necessarily the same as the date on which the
360      * queries were run.
361      * @return value of generatedDate.
362      */

363     public Date JavaDoc getGeneratedDate()
364     {
365         if (generatedDate == null)
366         {
367             // if no date was set just set this
368
// date to the current time
369
generatedDate = getDefaultDate();
370             if (generatedDate == null)
371             {
372                 generatedDate = new Date JavaDoc();
373             }
374         }
375         
376         return generatedDate;
377     }
378
379     /**
380      * Date used for a single date report.
381      */

382     public Date JavaDoc getDefaultDate()
383     {
384         ReportDate rdate = reportDefn.getDefaultDate();
385         return (rdate != null ? new Date JavaDoc(rdate.getTime()) : null);
386     }
387
388     /**
389      * Date used for a single date report.
390      */

391     public void setDefaultDate(Date JavaDoc date)
392     {
393         if (date == null)
394         {
395             reportDefn.setDefaultDate(null);
396         }
397         else
398         {
399             ReportDate rdate = reportDefn.getDefaultDate();
400             if (rdate == null)
401             {
402                 rdate = new ReportDate();
403                 reportDefn.setDefaultDate(rdate);
404             }
405             rdate.setTime(date.getTime());
406             Log.get().debug("Default date set to " + date);
407         }
408     }
409
410
411     public MITList getMITList()
412         throws TorqueException
413     {
414         MITList mitList = null;
415         List JavaDoc mits = reportDefn.getModuleIssueTypes();
416         if (mits != null)
417         {
418             Log.get().debug("mits were not null");
419             mitList = new MITList();
420             for (Iterator JavaDoc i = mits.iterator(); i.hasNext();)
421             {
422                 ModuleIssueType mit = (ModuleIssueType)i.next();
423                 MITListItem item = new MITListItem();
424                 item.setModuleId(mit.getModuleId());
425                 item.setIssueTypeId(mit.getIssueTypeId());
426                 mitList.addMITListItem(item);
427             }
428         }
429         
430         return mitList;
431     }
432
433     public void setMITList(MITList mitList)
434         throws Exception JavaDoc
435     {
436         if (mitList == null)
437         {
438             reportDefn.setModuleIssueTypes(null);
439             setModule(null);
440             setIssueType(null);
441         }
442         else
443         {
444             boolean isOk = true;
445             // need to check that the changes are compatible with the currently
446
// selected criteria
447
for (Iterator JavaDoc roai = reportDefn
448                 .retrieveAllReportOptionAttributes().iterator();
449                  roai.hasNext() && isOk;)
450             {
451                 isOk = mitList.isCommon( AttributeOptionManager.getInstance(
452                     ((ReportOptionAttribute)roai.next())
453                     .getOptionId()));
454             }
455             for (Iterator JavaDoc ruai = reportDefn
456                 .retrieveAllReportUserAttributes().iterator();
457                  ruai.hasNext() && isOk;)
458             {
459                 isOk = mitList.isCommon( AttributeManager.getInstance(
460                     ((ReportUserAttribute)ruai.next())
461                     .getAttributeId()));
462             }
463             
464             if (!isOk)
465             {
466                 throw new IncompatibleMITListException(L10NKeySet.ExceptionIncompatibleMITListChanges); //EXCEPTION
467
}
468             
469             reportDefn.setModuleIssueTypes(null);
470             setModule(null);
471             setIssueType(null);
472
473             for (Iterator JavaDoc i = mitList.getExpandedMITListItems().iterator(); i.hasNext();)
474             {
475                 MITListItem item = (MITListItem)i.next();
476                 ModuleIssueType mit = new ModuleIssueType();
477                 mit.setModuleId(item.getModuleId());
478                 mit.setIssueTypeId(item.getIssueTypeId());
479                 reportDefn.addModuleIssueType(mit);
480             }
481             if (mitList.isSingleModule())
482             {
483                 torqueReport.setModule(mitList.getModule());
484             }
485             if (mitList.isSingleIssueType())
486             {
487                 torqueReport.setIssueType(mitList.getIssueType());
488             }
489         }
490     }
491
492     public boolean isReadyForCalculation()
493     {
494         List JavaDoc mits = reportDefn.getModuleIssueTypes();
495         boolean result = mits != null && !mits.isEmpty();
496         List JavaDoc axes = reportDefn.getReportAxisList();
497         result &= axes != null && axes.size() > 1;
498         if (result)
499         {
500             for (Iterator JavaDoc i = axes.iterator(); i.hasNext() && result;)
501             {
502                 ReportAxis axis = (ReportAxis)i.next();
503                 List JavaDoc headings = axis.getReportHeadings();
504                 result &= headings != null && !headings.isEmpty();
505                 if (result)
506                 {
507                     for (Iterator JavaDoc j = headings.iterator(); j.hasNext() && result;)
508                     {
509                         ReportHeading heading = (ReportHeading)j.next();
510                         result &= heading.size() > 0;
511                     }
512                 }
513             }
514         }
515         
516         return result;
517     }
518
519     public boolean removeStaleDefinitions()
520         throws Exception JavaDoc
521     {
522         boolean reportModified = false;
523         MITList mitList = getMITList();
524         List JavaDoc axes = reportDefn.getReportAxisList();
525         if (axes != null)
526         {
527             for (Iterator JavaDoc i = axes.iterator(); i.hasNext();)
528             {
529                 ReportAxis axis = (ReportAxis)i.next();
530                 List JavaDoc headings = axis.getReportHeadings();
531                 if (headings != null)
532                 {
533                     for (Iterator JavaDoc j = headings.iterator(); j.hasNext();)
534                     {
535                         ReportHeading heading = (ReportHeading)j.next();
536                         reportModified |= removeStaleOptions(
537                             heading.getReportOptionAttributes(), mitList);
538                         reportModified |= removeStaleUserAttributes(heading
539                             .getReportUserAttributes(), mitList);
540                         List JavaDoc groups = heading.getReportGroups();
541                         if (groups != null && !groups.isEmpty())
542                         {
543                             for (Iterator JavaDoc n = groups.iterator(); n.hasNext();)
544                             {
545                                 ReportGroup group = (ReportGroup)n.next();
546                                 reportModified |= removeStaleOptions(
547                                     group.getReportOptionAttributes(), mitList );
548                                 reportModified |= removeStaleUserAttributes(
549                                     group.getReportUserAttributes(), mitList );
550                             }
551                         }
552                     }
553                 }
554             }
555         }
556         return reportModified;
557     }
558
559     private boolean removeStaleOptions(List JavaDoc options, MITList mitList)
560         throws Exception JavaDoc
561     {
562         boolean anyRemoved = false;
563         if (options != null && !options.isEmpty())
564         {
565             for (Iterator JavaDoc n = options.iterator(); n.hasNext();)
566             {
567                 ReportOptionAttribute rao =
568                     (ReportOptionAttribute)n.next();
569                 AttributeOption ao = AttributeOptionManager
570                     .getInstance(rao.getOptionId());
571                 if (!mitList.isCommon(ao, false))
572                 {
573                     n.remove();
574                     anyRemoved = true;
575                 }
576                 else if (!mitList.isCommon(ao.getAttribute(), false))
577                 {
578                     n.remove();
579                     anyRemoved = true;
580                 }
581             }
582         }
583         return anyRemoved;
584     }
585
586     private boolean removeStaleUserAttributes(List JavaDoc attributes, MITList mitList)
587         throws Exception JavaDoc
588     {
589         boolean anyRemoved = false;
590         if (attributes != null && !attributes.isEmpty())
591         {
592             for (Iterator JavaDoc n = attributes.iterator(); n.hasNext();)
593             {
594                 ReportUserAttribute rua =
595                     (ReportUserAttribute)n.next();
596                 Attribute attr = AttributeManager
597                     .getInstance(rua.getAttributeId());
598                 if (!mitList.isCommon(attr, false))
599                 {
600                     n.remove();
601                     anyRemoved = true;
602                 }
603             }
604         }
605         return anyRemoved;
606     }
607
608     public ReportTableModel getModel(ScarabUser searcher)
609         throws Exception JavaDoc
610     {
611         return new ReportTableModel(this, getGeneratedDate(), searcher);
612     }
613         
614     public void save()
615         throws Exception JavaDoc
616     {
617         torqueReport.setQueryString(getQueryString());
618         torqueReport.save();
619     }
620     
621     /**
622      * State of persistence of the report.
623      *
624      * @return true if the report has NOT been saved.
625      */

626     public boolean isNew()
627     {
628         return torqueReport.isNew();
629     }
630
631     public void populate(String JavaDoc v)
632         throws Exception JavaDoc
633     {
634         if (v == null)
635         {
636             reportDefn = new ReportDefinition();
637         }
638         else
639         {
640             BeanReader reader = new BeanReader();
641             reader.registerBeanClass(ReportDefinition.class);
642             reportDefn = (ReportDefinition)
643                 reader.parse(new StringReader JavaDoc(v));
644
645             Logger log = Log.get();
646             if (log.isDebugEnabled())
647             {
648                 log.debug("Created a new report using:\n " + v +
649                           "; and it resulted in:\n " +
650                           reportDefn.toXmlString());
651             }
652         }
653     }
654
655     String JavaDoc getQueryString()
656         throws Exception JavaDoc
657     {
658         return reportDefn.toXmlString();
659     }
660
661
662     public void populateSearch(IssueSearch search, ReportHeading heading)
663         throws Exception JavaDoc
664     {
665         List JavaDoc reportOptions = heading.getReportOptionAttributes();
666         if (reportOptions == null || reportOptions.isEmpty())
667         {
668             List JavaDoc groups = heading.getReportGroups();
669             if (groups != null && !groups.isEmpty())
670             {
671                 reportOptions = new ArrayList JavaDoc();
672                 for (Iterator JavaDoc i = groups.iterator(); i.hasNext();)
673                 {
674                     ReportGroup group = (ReportGroup)i.next();
675                     List JavaDoc tmpOptions = group.getReportOptionAttributes();
676                     if (tmpOptions != null && !tmpOptions.isEmpty())
677                     {
678                         for (Iterator JavaDoc j = tmpOptions.iterator(); j.hasNext();)
679                         {
680                             reportOptions.add(j.next());
681                         }
682                     }
683                 }
684             }
685         }
686
687         if (reportOptions != null && !reportOptions.isEmpty())
688         {
689             Map JavaDoc commonAttributeMap = new HashMap JavaDoc(reportOptions.size());
690             for (Iterator JavaDoc i = reportOptions.iterator(); i.hasNext();)
691             {
692                 ReportOptionAttribute roa = (ReportOptionAttribute)i.next();
693                 Integer JavaDoc optionId = roa.getOptionId();
694                 Integer JavaDoc attId = AttributeOptionManager.getInstance(optionId)
695                     .getAttributeId();
696                 AttributeValue av = AttributeValue
697                     .getNewInstance(attId, search);
698                 av.setOptionId(optionId);
699                 if (commonAttributeMap.containsKey(attId))
700                 {
701                     AttributeValue prevAV =
702                         (AttributeValue)commonAttributeMap.get(attId);
703                     prevAV.setChainedValue(av);
704                 }
705                 else
706                 {
707                     search.addAttributeValue(av);
708                     commonAttributeMap.put(attId, av);
709                 }
710             }
711         }
712     }
713 }
714
Popular Tags