View Javadoc

1   /*
2    * The contents of this file are subject to the terms 
3    * of the Common Development and Distribution License 
4    * (the "License").  You may not use this file except 
5    * in compliance with the License.
6    * 
7    * You can obtain a copy of the license at 
8    * http://www.sun.com/cddl/cddl.html. 
9    * See the License for the specific language governing 
10   * permissions and limitations under the License.
11   * 
12   * When distributing Covered Code, include this CDDL 
13   * HEADER in each file and include the License file at 
14   * license.txt.  If applicable, add the following below 
15   * this CDDL HEADER, with the fields enclosed by brackets 
16   * "[]" replaced with your own identifying information: 
17   * Portions Copyright [yyyy] [name of copyright owner]
18   * 
19   * Portions Copyright 2004 eBay, Inc.
20   */
21  package com.ebay.carad.os.vitalsigns.templates;
22  
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.apache.log4j.Logger;
28  
29  import com.ebay.carad.os.vitalsigns.DashboardReport;
30  import com.ebay.carad.os.vitalsigns.IDashboardReportContainer;
31  
32  /***
33   * <p>
34   * Provides a way to generate templatized sets of reports based on the output of
35   * a query. This is useful if you want numerous dashboard reports that are
36   * reporting against a possibly changing set of values in the database.
37   * </p>
38   * 
39   * <p>
40   * Reports are created at load time, and only re-loaded upon dashboard restart.
41   * </p>
42   * 
43   * @author Jeremy Kraybill
44   * @author Jeremy Thomerson
45   * @version $Id$
46   */
47  public class DashboardReportTemplate implements IDashboardReportContainer, Comparable {
48  
49      private static final Logger LOGGER = Logger.getLogger(DashboardReportTemplate.class);
50  
51      private IDashboardReportContainer mContainer = null;
52      private String mGroupName;
53      private IMultiTemplatizableProvider mDataProvider = null;
54  
55      /*** List of generated reports. */
56      private List mReports = null;
57  
58  	/***
59  	 * IoC method to allow a report container to be passed in. Note that all
60  	 * children DashboardReports must implement <code>ITemplatizable</code>
61  	 * 
62  	 * @param inContainer the container to use as a template
63  	 */
64  	public void setTemplateContainer(IDashboardReportContainer inContainer) {
65  		mContainer = inContainer;
66  	}
67  
68  	/***
69  	 * Generates a set of report objects based on the data provided by the
70  	 * IMultiTemplatizableProvider and the contained template reports. 
71  	 */
72  	public List getReports() {
73  		createReports();
74  		return mReports;
75  	}
76  
77  	/***
78  	 * IoC method for initialization; defines where our list of lists comes
79  	 * from.
80  	 * 
81  	 * @param provider
82  	 *            the provider of our template data
83  	 */
84  	public void setDataProvider(IMultiTemplatizableProvider provider) {
85  		mDataProvider = provider;
86  	}
87  
88  	/***
89  	 * Generates the list of templatized reports, if they have not been generated yet.
90  	 */
91  	private void createReports() {
92  		if (mReports == null) {
93  			mReports = new ArrayList(10);
94  			// get my template data
95  			List data = mDataProvider.getParameterList();
96  
97  			if (data.size() == 0) {
98  				LOGGER.warn("Template generation provider returned zero results.");
99  				return;
100 			}
101 
102 			for (Iterator it = mContainer.getReports().iterator(); it.hasNext();) { 
103 				// go through each one of our templates
104 				// TODO : this should probably be IDashboardReport, but that may require it to extend Clonable
105 				DashboardReport template = (DashboardReport) it.next();
106 				for (Iterator it2 = data.iterator(); it2.hasNext();) { 
107 					// go through each row of our provided data
108 					DashboardReport newReport = null;
109 					newReport = new DashboardReport(template);
110 					newReport.templatize(((List) it2.next()).toArray());
111 					mReports.add(newReport);
112 				}
113 			}
114 		}
115 	}
116 
117 	/***
118 	 * Returns the sort order of the container.
119 	 */
120 	public int getSortOrder() {
121 		if (mContainer.getReports().size() > 0) {
122 			return mContainer.getSortOrder();
123 		}
124 
125 		// we're empty, so our order is moot anyway
126 		return 0;
127 	}
128 
129 	public int compareTo(Object obj) {
130 		IDashboardReportContainer rc = (IDashboardReportContainer) obj;
131 		return getSortOrder() - rc.getSortOrder();
132 	}
133 
134     /***
135      * @return Returns the groupName.
136      */
137     public String getGroupName() {
138         return mGroupName;
139     }
140 
141     /***
142      * @param groupName The groupName to set.
143      */
144     public void setGroupName(String groupName) {
145         mGroupName = groupName;
146     }
147 }