001: package org.tigris.scarab.reports;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: */
041:
042: import java.util.List;
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.Collections;
046: import org.apache.torque.om.NumberKey;
047: import org.apache.fulcrum.intake.Retrievable;
048: import org.tigris.scarab.util.Log;
049: import org.tigris.scarab.om.AttributeOptionManager;
050:
051: /**
052: *
053: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
054: * @version $Id: ReportHeading.java 10132 2006-06-02 09:06:38Z dabbous $
055: */
056: public class ReportHeading implements java.io.Serializable, Retrievable {
057: private List reportGroups;
058:
059: private List reportOptionAttributes;
060:
061: private List reportDates;
062:
063: private List reportUserAttributes;
064:
065: private List reportDateRanges;
066:
067: private String queryKey;
068:
069: public int calculateType() {
070: int type = 0;
071: if (getReportGroups() != null && getReportGroups().size() > 0) {
072: ReportGroup firstGroup = (ReportGroup) getReportGroups()
073: .get(0);
074: if (firstGroup.getReportUserAttributes() != null) {
075: type = 1;
076: }
077: } else if (getReportUserAttributes() != null) {
078: type = 1;
079: } else if (getReportDates() != null
080: || getReportDateRanges() != null) {
081: type = 2;
082: }
083: return type;
084: }
085:
086: public List retrieveGroupedAttributes() {
087: List result = null;
088: List groups = getReportGroups();
089: if (groups == null || groups.isEmpty()) {
090: result = Collections.EMPTY_LIST;
091: } else if (calculateType() == 0) {
092: result = new ArrayList();
093: for (Iterator i = groups.iterator(); i.hasNext();) {
094: List options = ((ReportGroup) i.next())
095: .getReportOptionAttributes();
096: if (options != null) {
097: result.addAll(options);
098: }
099: }
100: } else {
101: result = new ArrayList();
102: for (Iterator i = groups.iterator(); i.hasNext();) {
103: List users = ((ReportGroup) i.next())
104: .getReportUserAttributes();
105: if (users != null) {
106: result.addAll(users);
107: }
108: }
109: }
110:
111: return result;
112: }
113:
114: public void reset() {
115: reportGroups = null;
116: reportOptionAttributes = null;
117: reportUserAttributes = null;
118: reportDates = null;
119: reportDateRanges = null;
120: }
121:
122: public Object get(int i) {
123: // FIXME: This method looks quite suspicious to me.
124: // i reengineered it in order to avoid OutOfBoundExceptions...
125: // Some questions raise:
126: // what is the relation between the object type and the index ?
127: // What happens if multiple lists contain an item at index i ?
128: // Maybe John can put some light here ?
129: Object obj = getItemFromReportGroups(i);
130: if (obj == null) {
131: obj = getItemFromReportOptionAttributes(i);
132: if (obj == null) {
133: obj = getItemFromReportUserAttributes(i);
134: if (obj == null) {
135: obj = getItemFromReportDates(i);
136: if (obj == null) {
137: obj = getItemFromReportDateRanges(i);
138: }
139: }
140: }
141: }
142: return obj;
143: }
144:
145: private Object getItemFromReportDates(int i) {
146: Object obj = null;
147: List reportDates = getReportDates();
148: if (reportDates != null) {
149: int size = reportDates.size();
150: if (size > i) {
151: obj = reportDates.get(i);
152: }
153: }
154: return obj;
155: }
156:
157: private Object getItemFromReportDateRanges(int i) {
158: Object obj = null;
159: List reportDateRanges = getReportDateRanges();
160: if (reportDateRanges != null) {
161: int size = reportDateRanges.size();
162: if (size > i) {
163: obj = reportDateRanges.get(i);
164: }
165: }
166: return obj;
167: }
168:
169: private Object getItemFromReportUserAttributes(int i) {
170: Object obj = null;
171: List reportUserAttributes = getReportUserAttributes();
172: if (reportUserAttributes != null) {
173: int size = reportUserAttributes.size();
174: if (size > i) {
175: obj = reportUserAttributes.get(i);
176: }
177: }
178: return obj;
179: }
180:
181: /**
182: * get item from reportOptionAttributes if available.
183: * @param i
184: * @return
185: */
186: private Object getItemFromReportOptionAttributes(int i) {
187: Object obj = null;
188: List reportOptionAttributes = getReportOptionAttributes();
189: if (reportOptionAttributes != null) {
190: int size = reportOptionAttributes.size();
191: if (size > i) {
192: obj = reportOptionAttributes.get(i);
193: }
194: }
195: return obj;
196: }
197:
198: /**
199: * get item from reportGroups if available.
200: * @param i
201: * @return
202: */
203: private Object getItemFromReportGroups(int i) {
204: Object obj = null;
205: List reportGroups = getReportGroups();
206: if (reportGroups != null) {
207: int size = reportGroups.size();
208: if (size > i)
209: obj = reportGroups.get(i);
210: }
211: return obj;
212: }
213:
214: public int size() {
215: int size = 0;
216: if (getReportGroups() != null) {
217: size = getReportGroups().size();
218: } else if (getReportOptionAttributes() != null) {
219: size = getReportOptionAttributes().size();
220: } else if (getReportUserAttributes() != null) {
221: size = getReportUserAttributes().size();
222: }
223: // need to sort out dates !FIXME!
224: else if (getReportDates() != null) {
225: size = getReportDates().size();
226: }
227: return size;
228: }
229:
230: public boolean singleAttribute() {
231: boolean result = false;
232: Integer firstId = null;
233: Iterator options = consolidateReportOptionAttributes()
234: .iterator();
235: if (options.hasNext()) {
236: result = true;
237: for (; options.hasNext() && result;) {
238: try {
239: Integer id = new Integer(
240: AttributeOptionManager
241: .getInstance(
242: new NumberKey(
243: ((ReportOptionAttribute) options
244: .next())
245: .getOptionId()
246: .toString()))
247: .getAttributeId().toString());
248: if (firstId == null) {
249: firstId = id;
250: } else {
251: result = firstId.equals(id);
252: }
253: } catch (Exception e) {
254: Log.get().warn("Error on attribute id", e);
255: result = false;
256: }
257: }
258: }
259:
260: if (firstId == null) {
261: Iterator users = consolidateReportUserAttributes()
262: .iterator();
263: if (users.hasNext()) {
264: result = true;
265: for (; users.hasNext() && result;) {
266: Integer id = ((ReportUserAttribute) users.next())
267: .getAttributeId();
268: if (firstId == null) {
269: firstId = id;
270: } else {
271: result = firstId.equals(id);
272: }
273: }
274: }
275: }
276: return result;
277: }
278:
279: /**
280: * returns all the the ReportOptionAttributes involved in the heading
281: * including those in groups
282: *
283: * @return a <code>List</code> value
284: */
285: public List consolidateReportOptionAttributes() {
286: List result = null;
287: List groups = getReportGroups();
288: if (groups != null && !groups.isEmpty()) {
289: for (Iterator i = groups.iterator(); i.hasNext();) {
290: ReportGroup group = (ReportGroup) i.next();
291: List options = group.getReportOptionAttributes();
292: if (options != null && !options.isEmpty()) {
293: for (Iterator j = options.iterator(); j.hasNext();) {
294: if (result == null) {
295: result = new ArrayList();
296: }
297: result.add(j.next());
298: }
299: }
300: }
301: }
302:
303: List options = getReportOptionAttributes();
304: if (options != null && !options.isEmpty()) {
305: for (Iterator j = options.iterator(); j.hasNext();) {
306: if (result == null) {
307: result = new ArrayList();
308: }
309: result.add(j.next());
310: }
311: }
312: if (result == null) {
313: result = Collections.EMPTY_LIST;
314: }
315:
316: return result;
317: }
318:
319: /**
320: * returns all the the ReportUserAttributes involved in the heading
321: * including those in groups
322: *
323: * @return a <code>List</code> value
324: */
325: public List consolidateReportUserAttributes() {
326: List result = null;
327: List groups = getReportGroups();
328: if (groups != null && !groups.isEmpty()) {
329: for (Iterator i = groups.iterator(); i.hasNext();) {
330: ReportGroup group = (ReportGroup) i.next();
331: List users = group.getReportUserAttributes();
332: if (users != null && !users.isEmpty()) {
333: for (Iterator j = users.iterator(); j.hasNext();) {
334: if (result == null) {
335: result = new ArrayList();
336: }
337: result.add(j.next());
338: }
339: }
340: }
341: }
342:
343: List users = getReportUserAttributes();
344: if (users != null && !users.isEmpty()) {
345: for (Iterator j = users.iterator(); j.hasNext();) {
346: if (result == null) {
347: result = new ArrayList();
348: }
349: result.add(j.next());
350: }
351: }
352: if (result == null) {
353: result = Collections.EMPTY_LIST;
354: }
355:
356: return result;
357: }
358:
359: /**
360: * Get the ReportGroups value.
361: * @return the ReportGroups value.
362: */
363: public List getReportGroups() {
364: return reportGroups;
365: }
366:
367: /**
368: * Set the ReportGroups value.
369: * @param newReportGroups The new ReportGroups value.
370: */
371: public void setReportGroups(List newReportGroups) {
372: this .reportGroups = newReportGroups;
373: }
374:
375: /**
376: * Add a ReportGroup value.
377: * @param newReportGroup The new ReportGroup value.
378: */
379: public void addReportGroup(ReportGroup newReportGroup) {
380: if (reportGroups == null) {
381: reportGroups = new ArrayList();
382: // this is the first group, so move any options/users that were
383: // part of the heading into the group
384: if (newReportGroup.getReportOptionAttributes() == null) {
385: newReportGroup
386: .setReportOptionAttributes(getReportOptionAttributes());
387: setReportOptionAttributes(null);
388: } else if (getReportOptionAttributes() != null) {
389: newReportGroup.getReportOptionAttributes().addAll(
390: getReportOptionAttributes());
391: setReportOptionAttributes(null);
392: }
393:
394: if (newReportGroup.getReportUserAttributes() == null) {
395: newReportGroup
396: .setReportUserAttributes(getReportUserAttributes());
397: setReportUserAttributes(null);
398: } else if (getReportUserAttributes() != null) {
399: newReportGroup.getReportUserAttributes().addAll(
400: getReportUserAttributes());
401: setReportUserAttributes(null);
402: }
403: }
404: if (!reportGroups.contains(newReportGroup)) {
405: reportGroups.add(newReportGroup);
406: }
407: }
408:
409: /**
410: * Get the ReportOptionAttributes value.
411: * @return the ReportOptionAttributes value.
412: */
413: public List getReportOptionAttributes() {
414: return reportOptionAttributes;
415: }
416:
417: /**
418: * Set the ReportOptionAttributes value.
419: * @param newReportOptionAttributes The new ReportOptionAttributes value.
420: */
421: public void setReportOptionAttributes(List newReportOptionAttributes) {
422: this .reportOptionAttributes = newReportOptionAttributes;
423: }
424:
425: /**
426: * Add a ReportOptionAttribute value.
427: * @param newReportOptionAttribute The new ReportOptionAttribute value.
428: */
429: public void addReportOptionAttribute(
430: ReportOptionAttribute newReportOptionAttribute) {
431: if (reportOptionAttributes == null) {
432: reportOptionAttributes = new ArrayList();
433: }
434: if (!reportOptionAttributes.contains(newReportOptionAttribute)) {
435: reportOptionAttributes.add(newReportOptionAttribute);
436: }
437: }
438:
439: /**
440: * Get the ReportUserAttributes value.
441: * @return the ReportUserAttributes value.
442: */
443: public List getReportUserAttributes() {
444: return reportUserAttributes;
445: }
446:
447: /**
448: * Set the ReportUserAttributes value.
449: * @param newReportUserAttributes The new ReportUserAttributes value.
450: */
451: public void setReportUserAttributes(List newReportUserAttributes) {
452: this .reportUserAttributes = newReportUserAttributes;
453: }
454:
455: /**
456: * Add a ReportUserAttribute value.
457: * @param newReportUserAttribute The new ReportUserAttribute value.
458: */
459: public void addReportUserAttribute(
460: ReportUserAttribute newReportUserAttribute) {
461: Log.get().debug("Potentially adding " + newReportUserAttribute);
462:
463: if (reportUserAttributes == null) {
464: reportUserAttributes = new ArrayList();
465: }
466: if (!reportUserAttributes.contains(newReportUserAttribute)) {
467: Log.get().debug("added " + newReportUserAttribute);
468: reportUserAttributes.add(newReportUserAttribute);
469: }
470: }
471:
472: /**
473: * Get the ReportDateRanges value.
474: * @return the ReportDateRanges value.
475: */
476: public List getReportDateRanges() {
477: return reportDateRanges;
478: }
479:
480: /**
481: * Set the ReportDateRanges value.
482: * @param newReportDateRanges The new ReportDateRanges value.
483: */
484: public void setReportDateRanges(List newReportDateRanges) {
485: this .reportDateRanges = newReportDateRanges;
486: }
487:
488: /**
489: * Add a ReportDateRange value.
490: * @param newReportDateRange The new ReportDateRange value.
491: */
492: public void addReportDateRange(ReportDateRange newReportDateRange) {
493: if (reportDateRanges == null) {
494: reportDateRanges = new ArrayList();
495: }
496: reportDateRanges.add(newReportDateRange);
497: }
498:
499: /**
500: * Get the ReportDates value.
501: * @return the ReportDates value.
502: */
503: public List getReportDates() {
504: return reportDates;
505: }
506:
507: /**
508: * Set the ReportDates value.
509: * @param newReportDates The new ReportDates value.
510: */
511: public void setReportDates(List newReportDates) {
512: this .reportDates = newReportDates;
513: }
514:
515: /**
516: * Add a ReportDate value.
517: * @param newReportDate The new ReportDate value.
518: */
519: public void addReportDate(ReportDate newReportDate) {
520: if (reportDates == null) {
521: reportDates = new ArrayList();
522: }
523: reportDates.add(newReportDate);
524: }
525:
526: /**
527: * Get the QueryKey value.
528: * @return the QueryKey value.
529: */
530: public String getQueryKey() {
531: return queryKey == null ? "" : queryKey;
532: }
533:
534: /**
535: * Set the QueryKey value.
536: * @param newQueryKey The new QueryKey value.
537: */
538: public void setQueryKey(String newQueryKey) {
539: this.queryKey = newQueryKey;
540: }
541: }
|