KickJava   Java API By Example, From Geeks To Geeks.

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


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.Date JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.List JavaDoc;
53 import com.workingdogs.village.Record;
54
55 // Turbine classes
56
import org.apache.torque.util.Criteria;
57
58 import org.tigris.scarab.om.ModuleManager;
59 import org.tigris.scarab.om.MITList;
60 import org.tigris.scarab.om.MITListItem;
61 import org.tigris.scarab.om.ScarabUser;
62 import org.tigris.scarab.om.ActivityPeer;
63 import org.tigris.scarab.om.ActivitySetPeer;
64 import org.tigris.scarab.om.ActivitySetTypePeer;
65 import org.tigris.scarab.om.IssuePeer;
66 import org.tigris.scarab.util.TableModel;
67 import org.tigris.scarab.services.security.ScarabSecurity;
68
69 /**
70  * This class represents a ReportTableModel.
71  *
72  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
73  * @version $Id: ReportTableModel.java 9104 2004-05-10 21:04:51Z dabbous $
74  */

75 public class ReportTableModel
76     extends TableModel
77 {
78     private static final String JavaDoc ACT_ATTRIBUTE_ID =
79         ActivityPeer.ATTRIBUTE_ID.substring(
80             ActivityPeer.ATTRIBUTE_ID.indexOf('.')+1);
81     private static final String JavaDoc ACT_NEW_USER_ID =
82         ActivityPeer.NEW_USER_ID.substring(
83             ActivityPeer.NEW_USER_ID.indexOf('.')+1);
84     private static final String JavaDoc ACT_NEW_OPTION_ID =
85         ActivityPeer.NEW_OPTION_ID.substring(
86             ActivityPeer.NEW_OPTION_ID.indexOf('.')+1);
87     private static final String JavaDoc ACT_ISSUE_ID =
88         ActivityPeer.ISSUE_ID.substring(
89             ActivityPeer.ISSUE_ID.indexOf('.')+1);
90     private static final String JavaDoc ACT_TRANSACTION_ID =
91         ActivityPeer.TRANSACTION_ID.substring(
92             ActivityPeer.TRANSACTION_ID.indexOf('.')+1);
93     private static final String JavaDoc ACT_END_DATE =
94         ActivityPeer.END_DATE.substring(
95             ActivityPeer.END_DATE.indexOf('.')+1);
96     private static final String JavaDoc TRAN_TRANSACTION_ID =
97         ActivitySetPeer.TRANSACTION_ID.substring(
98             ActivitySetPeer.TRANSACTION_ID.indexOf('.')+1);
99     private static final String JavaDoc TRAN_CREATED_DATE =
100         ActivitySetPeer.CREATED_DATE.substring(
101             ActivitySetPeer.CREATED_DATE.indexOf('.')+1);
102     private static final String JavaDoc TRAN_CREATED_BY =
103         ActivitySetPeer.CREATED_BY.substring(
104             ActivitySetPeer.CREATED_BY.indexOf('.')+1);
105     private static final String JavaDoc TRAN_TYPE_ID =
106         ActivitySetPeer.TYPE_ID.substring(
107             ActivitySetPeer.TYPE_ID.indexOf('.')+1);
108
109     private ReportDefinition reportDefn;
110     private List JavaDoc rowHeadings;
111     private List JavaDoc columnHeadings;
112     private Date JavaDoc date;
113     private Integer JavaDoc moduleId;
114     private Integer JavaDoc issueTypeId;
115     private MITList mitList;
116
117     private int[] colspan;
118     private int[] rowspan;
119     private boolean isSearchAllowed;
120
121     ReportTableModel(ReportBridge report, Date JavaDoc date, ScarabUser searcher)
122         throws Exception JavaDoc
123     {
124         this.reportDefn = report.getReportDefinition();
125         ReportAxis axis = null;
126         List JavaDoc axes = reportDefn.getReportAxisList();
127         if (axes != null && axes.size() >= 2)
128         {
129             axis = (ReportAxis)axes.get(1);
130         }
131         if (axis != null)
132         {
133             columnHeadings = axis.getReportHeadings();
134         }
135         
136         if (axes != null && axes.size() >= 1)
137         {
138             axis = (ReportAxis)axes.get(0);
139         }
140         if (axis != null)
141         {
142             rowHeadings = axis.getReportHeadings();
143         }
144
145         this.date = date;
146
147         List JavaDoc xmits = reportDefn.getModuleIssueTypes();
148         if (xmits.size() == 1)
149         {
150             ModuleIssueType mit = (ModuleIssueType)xmits.get(0);
151             this.moduleId = mit.getModuleId();
152             this.issueTypeId = mit.getIssueTypeId();
153             isSearchAllowed = searcher.hasPermission(
154                 ScarabSecurity.ISSUE__SEARCH, ModuleManager.getInstance(moduleId));
155         }
156         else
157         {
158             String JavaDoc[] perms = {ScarabSecurity.ISSUE__SEARCH};
159             MITList searchableList = report.getMITList()
160                 .getPermittedSublist(perms, searcher);
161             isSearchAllowed = searchableList.size() > 0;
162             if (searchableList.size() == 1)
163             {
164                 MITListItem item = searchableList.getFirstItem();
165                 this.moduleId = item.getModuleId();
166                 this.issueTypeId = item.getIssueTypeId();
167             }
168             else
169             {
170                 this.mitList = searchableList;
171             }
172         }
173     }
174
175     /**
176      * Get the RowHeadings value.
177      * @return the RowHeadings value.
178      */

179     public List JavaDoc getRowHeadings()
180     {
181         return rowHeadings;
182     }
183
184     /**
185      * Get the ColumnHeadings value.
186      * @return the ColumnHeadings value.
187      */

188     public List JavaDoc getColumnHeadings()
189     {
190         return columnHeadings;
191     }
192
193     public int getColspan(int index)
194     {
195         int result = 1;
196         if (columnHeadings != null)
197         {
198             if (colspan == null)
199             {
200                 int numLevels = columnHeadings.size();
201                 colspan = new int[numLevels - 1];
202                 for (int j=0; j<numLevels-1; j++)
203                 {
204                     colspan[j] = 1;
205                     for (int i=numLevels-1; i>j; i--)
206                     {
207                         colspan[j] *=
208                             ((ReportHeading)columnHeadings.get(i)).size();
209                     }
210                 }
211             }
212
213             if (index < colspan.length)
214             {
215                 result = colspan[index];
216             }
217         }
218         
219         return result;
220     }
221
222     public int getRowspan(int index)
223     {
224         int result = 1;
225         if (rowHeadings != null)
226         {
227             if (rowspan == null)
228             {
229                 int numLevels = rowHeadings.size();
230                 rowspan = new int[numLevels - 1];
231                 for (int j=0; j<numLevels-1; j++)
232                 {
233                     rowspan[j] = 1;
234                     for (int i=numLevels-1; i>j; i--)
235                     {
236                         rowspan[j] *=
237                             ((ReportHeading)rowHeadings.get(i)).size();
238                     }
239                 }
240             }
241
242             if (index < rowspan.length)
243             {
244                 result = rowspan[index];
245             }
246         }
247         
248         return result;
249     }
250
251     public int getColumnCount()
252     {
253         return ((ReportHeading)columnHeadings.get(0)).size() * getColspan(0);
254     }
255     
256     public int getRowCount()
257     {
258         return ((ReportHeading)rowHeadings.get(0)).size() * getRowspan(0);
259     }
260
261
262     private Object JavaDoc[] getColumnDataArray(int column)
263     {
264         Object JavaDoc[] dataArray;
265         if (columnHeadings == null)
266         {
267             dataArray = new Object JavaDoc[0];
268         }
269         else
270         {
271             int numLevels = columnHeadings.size();
272             dataArray = new Object JavaDoc[numLevels];
273             int index = 0;
274             for (Iterator JavaDoc i=columnHeadings.iterator(); i.hasNext(); index++)
275             {
276                 ReportHeading heading = (ReportHeading)i.next();
277                 dataArray[index] = heading.get((column/getColspan(index)) % heading.size());
278             }
279         }
280         
281         return dataArray;
282     }
283
284     private Object JavaDoc[] getRowDataArray(int row)
285     {
286         Object JavaDoc[] dataArray;
287         if (rowHeadings == null)
288         {
289             dataArray = new Object JavaDoc[0];
290         }
291         else
292         {
293             int numLevels = rowHeadings.size();
294             dataArray = new Object JavaDoc[numLevels];
295             int index = 0;
296             for (Iterator JavaDoc i=rowHeadings.iterator(); i.hasNext(); index++)
297             {
298                 ReportHeading heading = (ReportHeading)i.next();
299                 dataArray[index] = heading.get((row/getRowspan(index)) % heading.size());
300             }
301         }
302         return dataArray;
303     }
304
305     public Object JavaDoc getValueAt(int row, int column)
306         throws Exception JavaDoc
307     {
308         if (row < 0 || row >= getRowCount())
309         {
310             throw new IndexOutOfBoundsException JavaDoc("Row index was " + row); //EXCEPTION
311
}
312         
313         if (column < 0 || column >= getColumnCount())
314         {
315             throw new IndexOutOfBoundsException JavaDoc("Column index was " + column); //EXCEPTION
316
}
317
318         Object JavaDoc contents = null;
319         // could use a categories list to make this simpler
320
if (columnHeadings != null && columnHeadings.size() == 1 &&
321             ((ReportHeading)columnHeadings.get(0)).get(0) instanceof ReportDate)
322         {
323             Date JavaDoc date = ((ReportDate) ((ReportHeading) columnHeadings.get(0))
324                          .get(column)).dateValue();
325             if (date.getTime() <= System.currentTimeMillis())
326             {
327                 contents = new Integer JavaDoc(getIssueCount(getRowDataArray(row),
328                                                      date));
329             }
330             else
331             {
332                 // Dates in the future are not applicable to reporting.
333
contents = "";
334             }
335         }
336         else if (rowHeadings != null && rowHeadings.size() == 1 &&
337                  ((ReportHeading)rowHeadings.get(0)).get(0) instanceof ReportDate)
338         {
339             Date JavaDoc date = ((ReportDate)((ReportHeading)rowHeadings.get(0))
340                          .get(row)).dateValue();
341             if (date.getTime() <= System.currentTimeMillis())
342             {
343                contents = new Integer JavaDoc(getIssueCount(
344                     getColumnDataArray(column), date));
345             }
346             else
347             {
348                // Dates in the future are not applicable to reporting.
349
contents = "";
350             }
351         }
352         else
353         {
354             contents = new Integer JavaDoc(getIssueCount(
355                 getRowDataArray(row), getColumnDataArray(column), date));
356         }
357
358         return contents;
359     }
360
361     private int getIssueCount(Object JavaDoc[] rowData, Object JavaDoc[] columnData, Date JavaDoc date)
362         throws Exception JavaDoc
363     {
364         Criteria crit = new Criteria();
365         // select count(issue_id) from activity a1 a2 a3, activitySet t1 t2 t3
366
crit.addSelectColumn("count(DISTINCT a0." + ACT_ISSUE_ID + ')');
367         int rowLength = rowData.length;
368         for (int i=0; i<rowLength; i++)
369         {
370             addOptionOrGroup(i, rowData[i], date, crit);
371             
372         }
373         for (int i=0; i<columnData.length; i++)
374         {
375             addOptionOrGroup(i+rowLength, columnData[i], date, crit);
376         }
377         return getCountAndCleanUp(crit);
378     }
379
380     public int getIssueCount(Object JavaDoc[] dataArray, Date JavaDoc date)
381         throws Exception JavaDoc
382     {
383         Criteria crit = new Criteria();
384         crit.addSelectColumn("count(DISTINCT a0." + ACT_ISSUE_ID + ')');
385         for (int i=0; i<dataArray.length; i++)
386         {
387             addOptionOrGroup(i, dataArray[i], date, crit);
388         }
389         return getCountAndCleanUp(crit);
390     }
391
392     private void addOptionOrGroup(int alias, Object JavaDoc optionOrGroup,
393                                   Date JavaDoc date, Criteria crit)
394     {
395         if (optionOrGroup == null)
396         {
397             throw new NullPointerException JavaDoc("cell definition cannot contain nulls"); //EXCEPTION
398
}
399
400         String JavaDoc a = "a"+alias;
401         String JavaDoc t = "t"+alias;
402         // there is some redundancy here, but over specifying the joins usually
403
// allows the query optimizer to obtain a better optimization
404
crit.addJoin(a + '.' + ACT_ISSUE_ID, IssuePeer.ISSUE_ID);
405         for (int i=alias-1; i>=0; i--)
406         {
407             crit.addJoin("a"+i+'.'+ACT_ISSUE_ID, a+'.'+ACT_ISSUE_ID);
408         }
409         crit.addAlias("a"+alias, ActivityPeer.TABLE_NAME);
410         crit.addAlias("t"+alias, ActivitySetPeer.TABLE_NAME);
411         
412         crit.addJoin(a+"."+ACT_TRANSACTION_ID, t+'.'+TRAN_TRANSACTION_ID);
413         crit.add(t, TRAN_CREATED_DATE, date, Criteria.LESS_THAN);
414         // end date criteria
415
Criteria.Criterion c1 = crit
416             .getNewCriterion(a, ACT_END_DATE, date, Criteria.GREATER_THAN);
417         c1.or(crit.getNewCriterion(a, ACT_END_DATE, null, Criteria.EQUAL));
418         crit.add(c1);
419
420         if (optionOrGroup instanceof ReportGroup)
421         {
422             List JavaDoc options = ((ReportGroup)optionOrGroup)
423                 .getReportOptionAttributes();
424             if (options != null && options.size() > 0)
425             {
426                 Integer JavaDoc[] nks = new Integer JavaDoc[options.size()];
427                 for (int i=0; i<nks.length; i++)
428                 {
429                     nks[i] = ((ReportOptionAttribute)options.get(i))
430                         .getOptionId();
431                 }
432                 
433                 crit.addIn(a+'.'+ACT_NEW_OPTION_ID, nks);
434             }
435             else
436             {
437                 List JavaDoc users = ((ReportGroup)optionOrGroup)
438                     .getReportUserAttributes();
439                 if (users != null && users.size() > 0)
440                 {
441                     Integer JavaDoc[] nks = new Integer JavaDoc[users.size()];
442                     for (int i=0; i<nks.length; i++)
443                     {
444                         nks[i] = ((ReportUserAttribute)users.get(i))
445                             .getUserId();
446                     }
447                     
448                     crit.addIn(a+'.'+ACT_NEW_USER_ID, nks);
449                 }
450                 else
451                 {
452                     // group is empty make sure there are no results
453
crit.add(a+'.'+ACT_NEW_OPTION_ID, -1);
454                 }
455             }
456         }
457         else if (optionOrGroup instanceof ReportOptionAttribute)
458         {
459             crit.add(a, ACT_NEW_OPTION_ID,
460                      ((ReportOptionAttribute)optionOrGroup).getOptionId());
461         }
462         else if (optionOrGroup instanceof ReportUserAttribute)
463         {
464             ReportUserAttribute rua = (ReportUserAttribute)optionOrGroup;
465             Integer JavaDoc attributeId = rua.getAttributeId();
466             if (attributeId.intValue() == 0)
467             {
468                 // committed by
469
crit.add(t, TRAN_TYPE_ID,
470                          ActivitySetTypePeer.CREATE_ISSUE__PK);
471                 crit.add(t, TRAN_CREATED_BY, rua.getUserId());
472             }
473             else
474             {
475                 crit.add(a, ACT_ATTRIBUTE_ID, rua.getAttributeId());
476                 crit.add(a, ACT_NEW_USER_ID, rua.getUserId());
477             }
478         }
479     }
480
481     private boolean isXMITSearch()
482     {
483         return mitList != null && !mitList.isSingleModuleIssueType();
484     }
485
486     private int getCountAndCleanUp(Criteria crit)
487         throws Exception JavaDoc
488     {
489         int result = 0;
490         if (isSearchAllowed)
491         {
492             if (isXMITSearch())
493             {
494                 mitList.addToCriteria(crit);
495             }
496             else
497             {
498                 crit.add(IssuePeer.MODULE_ID, moduleId);
499                 crit.add(IssuePeer.TYPE_ID, issueTypeId);
500             }
501             crit.add(IssuePeer.DELETED, false);
502             result = ((Record)ActivityPeer.doSelectVillageRecords(crit).get(0))
503             .getValue(1).asInt();
504         }
505         
506         return result;
507     }
508
509     public boolean isOption(Object JavaDoc obj)
510     {
511         return obj instanceof ReportOptionAttribute;
512     }
513     public boolean isOptionGroup(Object JavaDoc obj)
514     {
515         return obj instanceof ReportGroup;
516     }
517     public boolean isAttributeAndUser(Object JavaDoc obj)
518     {
519         return obj instanceof ReportUserAttribute;
520     }
521     public boolean isUser(Object JavaDoc obj)
522     {
523         return obj instanceof ScarabUser;
524     }
525     public boolean isReportDate(Object JavaDoc obj)
526     {
527         return obj instanceof ReportDate;
528     }
529
530     public String JavaDoc displayAttribute(Object JavaDoc cell)
531     {
532         return reportDefn.displayAttribute(cell);
533     }
534
535     public String JavaDoc displayOption(ReportOptionAttribute cell)
536     {
537         return reportDefn.displayOption(cell);
538     }
539
540     public String JavaDoc displayUser(ReportUserAttribute cell)
541     {
542         return reportDefn.displayUser(cell);
543     }
544 }
545
Popular Tags