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.Collections;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.log4j.Logger;
29  
30  import com.ebay.carad.os.vitalsigns.dao.ISqlDAO;
31  
32  /***
33   * Takes a query and a DAO, and generates a list of rows as <code>Object[]</code> arrays,
34   * sorted by column name.
35   * 
36   * @author Jeremy Kraybill
37   * @author Jeremy Thomerson
38   * @version $Id$
39   */
40  public class QueryProvider implements IMultiTemplatizableProvider {
41  
42  	private static final Logger LOGGER = Logger.getLogger(QueryProvider.class);
43  	
44  	private String mQuery = "";
45  	
46  	private ISqlDAO mSqlDAO = null;
47  
48  	/***
49  	 * IoC method to set the template query.
50  	 * 
51  	 * @param query query to use to generate child reports
52  	 */
53  	public void setQuery(String query) {
54  		mQuery = query;
55  	}
56  	
57  	/***
58  	 * IoC method to specify which DAO is to be used for running the template query.
59  	 * This allows the DAO to be different from the wrapped report's DAO, which
60  	 * may be useful.
61  	 * 
62  	 * @param dao the DAO to use for the template query.
63  	 */
64  	public void setDao(ISqlDAO dao) {
65  		mSqlDAO = dao;
66  	}
67  
68  	public List getParameterList() {
69  		List templateResults = mSqlDAO.findBySqlQuery(mQuery);
70  		if (templateResults.size() == 0) {
71  			LOGGER.warn("Template generation query returned zero results: " + mQuery);
72  			return Collections.EMPTY_LIST;
73  		}
74  
75  		for (int i = 0; i < templateResults.size(); i++) {
76  			// iterate through this list of maps, and convert each map to a list ordered by column name.
77  			Map row = (Map) templateResults.get(i);
78  			
79  			List columnList = new ArrayList(row.keySet());
80  			Collections.sort(columnList); // sort the keys (column names) alphabetically
81  
82  			// now replace each column name with column data
83  			for (int j = 0; j < columnList.size(); j++) {
84  				columnList.set(j, row.get(columnList.get(j)));
85  			}
86  
87  			templateResults.set(i, columnList); // replace the map with a list in place.
88  		}
89  		
90  		return templateResults;
91  	}
92  
93  }