KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > xmlEngine > DataValue


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.xmlEngine;
13
14 import org.openbravo.data.FieldProvider;
15
16 import java.sql.*;
17
18 import java.util.Vector JavaDoc;
19 import java.util.Enumeration JavaDoc;
20
21 import org.apache.log4j.Logger ;
22
23 /**
24   A piece of a XmlDocument with a defined data. This class manages the connection an the query
25   if there is not a FieldProvider[]
26   */

27 class DataValue implements XmlComponentValue {
28   DataTemplate dataTemplate;
29   XmlDocument xmlDocument;
30   SectionValue firstSectionValue;
31   // field if there are a connection
32
Connection connection;
33   PreparedStatement preparedStatement;
34   ResultSet result;
35
36   Vector JavaDoc<Object JavaDoc> vecSectionValue;
37   XmlVectorValue vecDetailValue; // vector of XmlComponentValues
38
Vector JavaDoc<Object JavaDoc> vecFieldValue; // vector of FieldValues
39
Vector JavaDoc<Object JavaDoc> vecParameterValue; // vector of ParameterValues for the query
40
Vector JavaDoc<Object JavaDoc> vecFunctionValueData; // vector of functions of structure
41
Vector JavaDoc<Object JavaDoc> vecFunctionValueOutSection; // vector of functions out of a Section, therefore out a Data
42

43   StringBuffer JavaDoc strDetailValue;
44   FieldProvider[] data = null;
45   int iArray;
46   FieldProvider[][] dataArray = null;
47
48   static Logger log4jDataValue = Logger.getLogger(DataValue.class);
49
50   public DataValue(DataTemplate dataTemplate, XmlDocument xmlDocument) {
51     this.dataTemplate = dataTemplate;
52     this.xmlDocument = xmlDocument;
53   }
54
55   public void initialize() {
56     //vector of Fields
57
vecFieldValue = new Vector JavaDoc<Object JavaDoc>();
58     log4jDataValue.debug("vector of Fields");
59     for (Enumeration JavaDoc e1 = dataTemplate.vecFieldTemplate.elements() ; e1.hasMoreElements();) {
60       FieldTemplate fieldTemplate = (FieldTemplate)e1.nextElement();
61       FieldValue fieldValue = fieldTemplate.createFieldValue(xmlDocument);
62       vecFieldValue.addElement(fieldValue);
63       log4jDataValue.debug("Field: " + fieldValue.fieldTemplate.name());
64     }
65
66     //vector of Functions
67
log4jDataValue.debug("vector of Functions");
68     vecFunctionValueData = new Vector JavaDoc<Object JavaDoc>();
69     vecFunctionValueOutSection = new Vector JavaDoc<Object JavaDoc>();
70     for (Enumeration JavaDoc e1 = dataTemplate.vecFunctionTemplateData.elements() ; e1.hasMoreElements();) {
71       FunctionTemplate functionTemplate = (FunctionTemplate)e1.nextElement();
72       FunctionValue functionValue = functionTemplate.createFunctionValue(xmlDocument);
73       vecFunctionValueData.addElement(functionValue);
74       if (functionValue.functionTemplate.fieldName == null) log4jDataValue.debug("Function");
75       else log4jDataValue.debug("Function" + functionValue.functionTemplate.fieldName);
76     }
77     //vector of Functions out of the section
78
log4jDataValue.debug("vector of Functions out of the section");
79     for (Enumeration JavaDoc e1 = dataTemplate.vecFunctionTemplateOutSection.elements() ; e1.hasMoreElements();) {
80       FunctionTemplate functionTemplate = (FunctionTemplate)e1.nextElement();
81       FunctionValue functionValue = functionTemplate.createFunctionValue(xmlDocument);
82       vecFunctionValueOutSection.addElement(functionValue);
83       log4jDataValue.debug("Function (OutSection): " + functionValue.functionTemplate.fieldName);
84     }
85
86     //vector of Sections
87
log4jDataValue.debug("vector of Sections");
88     vecSectionValue = new Vector JavaDoc<Object JavaDoc>();
89     for (Enumeration JavaDoc e1 = dataTemplate.vecSectionTemplate.elements() ; e1.hasMoreElements();) {
90       SectionTemplate sectionTemplate = (SectionTemplate)e1.nextElement();
91       SectionValue sectionValue = sectionTemplate.createSectionValue(xmlDocument, this);
92       vecSectionValue.addElement(sectionValue);
93       if (sectionTemplate.name.equals(dataTemplate.firstSectionTemplate.name)) {
94         firstSectionValue = sectionValue;
95         log4jDataValue.debug("First Section: " + sectionValue.sectionTemplate.name);
96       }
97       log4jDataValue.debug("Section: " + sectionValue.sectionTemplate.name);
98     }
99
100     log4jDataValue.debug("vector Detail");
101     vecDetailValue = new XmlVectorValue(dataTemplate.vecDetailTemplate, xmlDocument);
102
103     vecParameterValue = new Vector JavaDoc<Object JavaDoc>();
104     for (Enumeration JavaDoc e1 = dataTemplate.vecParameterTemplate.elements() ; e1.hasMoreElements();) {
105       ParameterTemplate parameterTemplate = (ParameterTemplate)e1.nextElement();
106       ParameterValue parameterValue = parameterTemplate.createParameterValue(xmlDocument);
107       vecParameterValue.addElement(parameterValue);
108       log4jDataValue.debug("Parameter: " + parameterValue.parameterTemplate.strName);
109     }
110
111   }
112
113   public void setData(FieldProvider[] data) {
114     this.data = data;
115   }
116
117   public void setDataArray(FieldProvider[][] dataArray) {
118     this.dataArray = dataArray;
119     iArray = 0;
120   }
121
122   public String JavaDoc printGenerated() {
123     if (firstSectionValue == null) {
124       return "";
125     } else {
126       log4jDataValue.debug("Init()");
127       init(); // delete the other init()s
128
if (data == null && dataArray == null) {
129         if (preparedStatement == null) {
130           ErrorManagement.error(101, xmlDocument.xmlTemplate.strName + "." + dataTemplate.strName);
131           return "";
132         }
133         query();
134         return execute();
135       } else {
136         if (dataArray != null) { // if there is array set the actual data
137
data = dataArray[iArray];
138           iArray++;
139         }
140         return executeArray();
141       }
142     }
143   }
144
145   public String JavaDoc printPrevious() {
146     return print();
147   }
148
149   public String JavaDoc print() {
150     if (firstSectionValue == null) {
151       return "";
152     } else {
153       return new String JavaDoc(firstSectionValue.strSection);
154     }
155   }
156
157   public String JavaDoc printSimple() {
158     return print();
159   }
160
161   public String JavaDoc printPreviousSimple() {
162     return printPrevious();
163   }
164
165   public String JavaDoc execute() {
166     int i = 0;
167     // init();
168
try {
169       while(result.next()) {
170         readFields();
171         if (i == 0) {
172           firstValues();
173         } else {
174           check();
175         }
176         acumulate(); //changing the order of these two columns when the AddFunction, SubtractFunction, EqualFunction functions are added
177
printDetail();
178         i++;
179       }
180       result.close();
181     } catch(SQLException e){
182       log4jDataValue.error("SQL error in execute: " + e);
183     }
184     if (i > 0) {
185       firstValues();
186       firstSectionValue.close();
187     }
188     return new String JavaDoc(firstSectionValue.strSection);
189   }
190
191   public String JavaDoc executeArray() {
192     int i = 0;
193     // init();
194
log4jDataValue.info("data.length:" + data.length);
195     for (i=0;i<data.length ;i++ ) {
196       readFieldsArray(data[i]);
197       log4jDataValue.info("data[" + i + "]");
198       if (i == 0) {
199         firstValues();
200       } else {
201         check();
202       }
203       acumulate(); //changing the order of these two columns when the AddFunction, SubtractFunction, EqualFunction functions are added
204
printDetail();
205     }
206     if (data.length >0) {
207       firstValues();
208       firstSectionValue.close();
209     }
210
211     log4jDataValue.info("StringBuffer length:" + firstSectionValue.strSection.length());
212     String JavaDoc strStringPrint = new String JavaDoc(firstSectionValue.strSection);
213     log4jDataValue.info("String length:" + strStringPrint.length());
214     return strStringPrint;
215
216     // return new String(firstSectionValue.strSection);
217
}
218
219   public String JavaDoc executeBlank(String JavaDoc strBlank) {
220     init();
221     setFieldsBlank(strBlank);
222     firstValues();
223     acumulate(); //cambiado el orden de estas dos columnas al aƱadir las funciones AddFunction, SubtractFunction, EqualFunction
224
printDetail();
225     firstSectionValue.close();
226     return new String JavaDoc(firstSectionValue.strSection);
227   }
228
229   public void connect() {
230     try {
231       // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
232
log4jDataValue.info("Loading driver" // of " + strName
233
+ " Driver: " + dataTemplate.strDriver);
234       Class.forName(dataTemplate.strDriver);
235       log4jDataValue.info("Driver loaded");
236     } catch(ClassNotFoundException JavaDoc e){
237       log4jDataValue.error("Class not found: " + e);
238     }
239     try {
240       // String url="jdbc:odbc:report";
241
connection=DriverManager.getConnection(dataTemplate.strURL);
242       log4jDataValue.info("connection created");
243       if (dataTemplate.strSQL != null) {
244         preparedStatement = connection.prepareStatement(dataTemplate.strSQL);
245         log4jDataValue.info("preparedStament created");
246       }
247     } catch(SQLException e){
248       log4jDataValue.error("SQL error in connect: " + e);
249       return;
250     }
251   }
252
253   public void closeConnection() {
254     try {
255       connection.close();
256     } catch(SQLException e){
257       log4jDataValue.error("SQL error in closeConnection: " + e);
258     }
259   }
260
261   public void query() {
262     try {
263       int i = 1;
264       //XmlEngineNP: htmlComponent's parameters treatment pendant, when it is a query (SQL)
265
for (Enumeration JavaDoc e = vecParameterValue.elements() ; e.hasMoreElements() ;) {
266         ParameterValue parameter = (ParameterValue)e.nextElement();
267         if (parameter.parameterTemplate.type == java.sql.Types.INTEGER) {
268           log4jDataValue.info("setInt: " + i + " valor: " + Integer.parseInt(parameter.strValue));
269           preparedStatement.setInt(i, Integer.parseInt(parameter.strValue));
270         } else if (parameter.parameterTemplate.type == java.sql.Types.VARCHAR) {
271           String JavaDoc strValue;
272           if (parameter.xmlComponentValue != null) {
273             log4jDataValue.info("value in xmlComponentValue");
274             if (parameter.parameterTemplate.section != null) {
275               strValue = parameter.xmlComponentValue.printPrevious(); // detailed in section print printPrevious
276
} else {
277               strValue = parameter.xmlComponentValue.print(); // detailed in section print printPrevious
278
}
279           } else {
280             log4jDataValue.info("value in strValue");
281             strValue = parameter.strValue;
282           }
283           log4jDataValue.info("setString: " + i + " name: " + parameter.parameterTemplate.strName + " value: " + strValue);
284           preparedStatement.setString(i, strValue);
285         }
286         i++;
287       }
288       log4jDataValue.info("query execution:");
289       result = preparedStatement.executeQuery();
290       log4jDataValue.info("query done");
291     } catch(SQLException e){
292       log4jDataValue.error("SQL error in query: " + dataTemplate.strSQL + "Exception:"+ e);
293     }
294   }
295
296   private void readFields() {
297     for (Enumeration JavaDoc e = vecFieldValue.elements() ; e.hasMoreElements() ;) {
298       FieldValue fieldValue = (FieldValue)e.nextElement();
299       fieldValue.read(result);
300     }
301   }
302
303   private void readFieldsArray(FieldProvider fieldProvider) {
304     for (Enumeration JavaDoc e = vecFieldValue.elements() ; e.hasMoreElements() ;) {
305       FieldValue fieldValue = (FieldValue)e.nextElement();
306       fieldValue.read(fieldProvider.getField(fieldValue.fieldTemplate.name()));
307     }
308   }
309
310   private void setFieldsBlank(String JavaDoc strBlank) {
311     for (Enumeration JavaDoc e = vecFieldValue.elements() ; e.hasMoreElements() ;) {
312       FieldValue fieldValue = (FieldValue)e.nextElement();
313       fieldValue.setBlank(strBlank);
314     }
315   }
316
317   private void init () {
318     log4jDataValue.info("DataValue: init");
319     for (Enumeration JavaDoc e = vecSectionValue.elements() ; e.hasMoreElements() ;) {
320       SectionValue section = (SectionValue)e.nextElement();
321       log4jDataValue.debug("DataValue: init, section:" + section.sectionTemplate.name);
322       section.init();
323     }
324     firstSectionValue.strSection = new StringBuffer JavaDoc();
325
326     //init for the functions out of StructureSQL
327
for (Enumeration JavaDoc e = vecFunctionValueOutSection.elements() ; e.hasMoreElements() ;) {
328       FunctionValue functionInstance = (FunctionValue)e.nextElement();
329       functionInstance.init();
330     }
331
332   }
333
334   private void firstValues() {
335     for (Enumeration JavaDoc e = vecSectionValue.elements() ; e.hasMoreElements();) {
336       SectionValue sectionValue = (SectionValue)e.nextElement();
337       if (sectionValue.breakFieldValue != null) {
338         sectionValue.breakFieldValue.savePrevious();
339       }
340     }
341
342     for (Enumeration JavaDoc e = vecFieldValue.elements() ; e.hasMoreElements() ;) {
343       FieldValue fieldValue = (FieldValue)e.nextElement();
344       fieldValue.savePrevious();
345     }
346
347   }
348
349   private void check () {
350     boolean checked = true;
351     for (Enumeration JavaDoc e = vecSectionValue.elements() ; e.hasMoreElements() && checked ;) {
352       SectionValue sectionValue = (SectionValue)e.nextElement();
353       if (!sectionValue.check()) {
354         checked = false;
355         sectionValue.close();
356       }
357     }
358   }
359
360   public void printDetail() {
361     for (Enumeration JavaDoc e = vecDetailValue.elements() ; e.hasMoreElements() ;) {
362       XmlComponentValue xmlComponentValue = (XmlComponentValue)e.nextElement();
363       strDetailValue.append(xmlComponentValue.print());
364     }
365
366     //strDetailValue.append("\n");
367
}
368
369   public void acumulate() {
370     for (Enumeration JavaDoc e = vecSectionValue.elements() ; e.hasMoreElements() ;) {
371       SectionValue sectionValue = (SectionValue)e.nextElement();
372       sectionValue.acumulate();
373     }
374
375     //Acumulate for the functions out of the DataTemplate
376
for (Enumeration JavaDoc e = vecFunctionValueOutSection.elements() ; e.hasMoreElements() ;) {
377       FunctionValue functionInstance = (FunctionValue)e.nextElement();
378       functionInstance.acumulate();
379     }
380   }
381 }
382
Popular Tags