KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > reportmanager > XmlReportDataSource


1 package com.calipso.reportgenerator.reportmanager;
2
3
4 import com.calipso.reportgenerator.common.*;
5 import com.calipso.reportgenerator.reportcalculator.*;
6 import com.calipso.reportgenerator.reportdefinitions.types.ReportDataType;
7 import com.calipso.common.DateEx;
8
9 import java.io.File JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.ArrayList JavaDoc;
14
15 import org.xml.sax.InputSource JavaDoc;
16 import org.w3c.dom.*;
17 import org.apache.poi.hssf.usermodel.HSSFCell;
18 import com.calipso.reportgenerator.reportcalculator.IDataSource;
19 import com.calipso.reportgenerator.common.InfoException;
20
21 /**
22  * Resuelve la obtención de los datos desde un Xml y los devuelve en un objeto <code>IDataSource</code>
23  * Se encarga de obtener el Xml según el Url del DataSourceDefinition, parsear el texto Xml utilizando la información
24  * contenida en el ReportSourceDefinition, ejecutarle los pre-filtros y agregar los registros resultantes en el
25  * objeto <code>DataSource</code>.
26  */

27 public class XmlReportDataSource extends ReportDataSource {
28
29   private String JavaDoc url;
30   private IDataSource dataSource;
31
32   public XmlReportDataSource(ReportSpec reportSpec, ReportDataSourceSpec dataSourceSpec, ReportGeneratorConfiguration managerConfiguration) {
33     super(reportSpec, dataSourceSpec);
34     super.setGeneratorConfiguration(managerConfiguration);
35   }
36
37   /**
38    * Método que inicializa el objeto. Asigna la dirección URL
39    */

40   protected void initialize() {
41     url = getReportDataSourceSpec().getExpression();
42   }
43
44   /**
45    * Parsea el XML. Crea el Parser y lo instancia desde la dirección URL
46    */

47   private void loadFromXml(Matrix matrix) throws InfoException {
48     org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser();
49     try {
50       File JavaDoc file = new File JavaDoc(url);
51       InputStream JavaDoc in = new FileInputStream JavaDoc(file);
52       InputSource JavaDoc source = new InputSource JavaDoc(in);
53       parser.parse(source);
54     } catch (Exception JavaDoc e) {
55       throw new InfoException(LanguageTraslator.traslate("100"), e);
56     }
57     Document document = parser.getDocument();
58     loadXmlDocument(document, matrix);
59   }
60
61   /**
62    * Busca el elemento raiz del XML y comienza a recorrer las filas
63    * @param document
64    */

65   private void loadXmlDocument(Document document, Matrix matrix) throws InfoException {
66     Node rootNode = FindElement(document, "DataSource");
67     loadRowList(rootNode, matrix);
68   }
69
70   /**
71    * Busca el nodo correspondiente a las filas y las recorre.
72    * @param node
73    */

74   private void loadRowList(Node node, Matrix matrix) throws InfoException {
75     Node rowsNode = FindElement(node, "Rows");
76     if (rowsNode != null) {
77       loadRows(rowsNode, matrix);
78     }
79   }
80
81   /**
82    * Recorre cada fila y llena una row y la agrega al datasource.
83    * @param node
84    */

85   private void loadRows(Node node, Matrix matrix) throws InfoException {
86     try {
87       if (node != null) {
88         Node childNode;
89         NodeList children = node.getChildNodes();
90         if (children != null) {
91           for (int i = 0; i < children.getLength(); i++) {
92             childNode = children.item(i);
93             if (childNode.getNodeType() == Node.ELEMENT_NODE) {
94               NamedNodeMap attributes = childNode.getAttributes();
95               Object JavaDoc[] row = getRow(attributes);
96               //int size = getReportSpec().getDataSourceIndexes().size();
97
try {
98                 if ((getFilter()== null)||((getFilter()!= null) && (getFilter().matches(row)))) {
99                   matrix.add(row);
100                 }
101               } catch (InfoException e) {
102                 throw new InfoException(LanguageTraslator.traslate("101"), e);
103               }
104             }
105           }
106         }
107       }
108     } catch(OutOfMemoryError JavaDoc e) {
109       throw new InfoException(LanguageTraslator.traslate("326"), e);
110     }
111   }
112
113   private Object JavaDoc[] getRow(NamedNodeMap attributes) throws InfoException{
114     int dimCount = getReportSpec().getDimensionsByIndex().size();
115     int colNum = dimCount + getReportSpec().getMetricsByIndex().size();
116     Object JavaDoc[] collection = new Object JavaDoc[colNum];
117     //for(int i = row.getFirstCellNum() ; i < row.getLastCellNum() ; i++) {
118
for (int i = 0, j = 0; i < (colNum); i++) {
119       //HSSFCell cell = row.getCell((short)j);
120
if(i < dimCount){
121         //Es dimension
122
ReportDimensionSpec dimension = getReportSpec().getDimensionFromIndex(i);
123         if(dimension.getCalculated()){
124           collection[i] = dimension.getValue(collection, getReportDataSourceSpec());
125         }else{
126           collection[i] = getValueForDimension(attributes.item(j).getNodeValue(), dimension, collection, i);
127           j++;
128         }
129       }else{
130         //Es metrica
131
ReportMetricSpec metric = getReportSpec().getMetricFromIndex(i - dimCount);
132         if(metric.getCalculated()){
133           collection[i] = metric.getValue(collection);
134         }else{
135           collection[i] = getValueForMetric(attributes.item(j).getNodeValue(), metric, collection, i);
136           j++;
137         }
138       }
139     }
140     return collection;
141   }
142
143   /*private void addNonCalculatedRow(List row, String nodeValue, int index, int dataType) throws InfoException{
144     switch(dataType){
145       case ReportDataType.DATETIME_TYPE:
146         row.add(index, SharedDate.newFrom(new DateEx(nodeValue, getReportDataSourceSpec().getDateTimePattern())));
147         break;
148       case ReportDataType.DATE_TYPE:
149         row.add(index, SharedDate.newFrom(new DateEx(nodeValue, getReportDataSourceSpec().getDatePattern())));
150         break;
151       case ReportDataType.STRING_TYPE:
152         row.add(index, SharedString.newFrom(nodeValue));
153         break;
154       case ReportDataType.FLOAT_TYPE:
155         row.add(index, SharedFloat.newFrom(new Float(nodeValue)));
156         break;
157       case ReportDataType.INTEGER_TYPE:
158         row.add(index, SharedInteger.newFrom(new Integer(nodeValue)));
159         break;
160       case ReportDataType.BOOLEAN_TYPE:
161         row.add(index, Boolean.valueOf(nodeValue));
162     }
163   }*/

164
165   /**
166    * Busca un elemento en el xml por nombre a partir de un nodo
167    * @param node Nodo origen
168    * @param rootName nodo a buscar
169    * @return
170    */

171   private static Node FindElement(Node node, String JavaDoc rootName) {
172     Node resNode = null;
173
174     if (node.getNodeName().compareTo(rootName) == 0) {
175       resNode = node;
176     }
177
178     if (resNode == null) {
179       NodeList children = node.getChildNodes();
180       if (children != null) {
181         for (int i = 0; i < children.getLength(); i++) {
182           if (resNode == null) {
183             resNode = FindElement(children.item(i), rootName);
184           }
185         }
186       }
187     }
188     return resNode;
189   }
190
191
192   /**
193    * Crea el datasource y lo llena
194    * @return
195    * @throws InfoException
196    */

197   public IDataSource getDataSource(Matrix matrix) throws InfoException {
198     //if (dataSource == null) {
199
//dataSource = newDataSource();
200
loadFromXml(matrix);
201     //}
202
return dataSource;
203   }
204
205   /**
206    * Asigna el modo de uso de las variables
207    * @return
208    */

209   public int getFilterVarMode() {
210     return ReportFilterBuilder.VARMODE_DATAINDEX;
211   }
212
213 }
214
Popular Tags