1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
77 Map row = (Map) templateResults.get(i);
78
79 List columnList = new ArrayList(row.keySet());
80 Collections.sort(columnList);
81
82
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);
88 }
89
90 return templateResults;
91 }
92
93 }