KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Vector JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.text.DecimalFormat JavaDoc;
17
18 import org.apache.log4j.Logger ;
19
20 /**
21   A piece of a XmlTemplate with a defined data. This class manages the connection an the query
22   if there is not a FieldProvider[]
23
24  **/

25 class DataTemplate implements XmlComponentTemplate {
26   String JavaDoc strName; // the name should be initialized
27

28   Vector JavaDoc<Object JavaDoc> vecSectionTemplate;
29   XmlVectorTemplate vecDetailTemplate; // vector of XmlComponents
30
Vector JavaDoc<Object JavaDoc> vecFieldTemplate; // vector of FieldComponent's
31
Vector JavaDoc<Object JavaDoc> vecParameterTemplate; // vector of ParameterTemplates for the query
32
Vector JavaDoc<Object JavaDoc> vecFunctionTemplateData; // vector of functions of structure
33
Vector JavaDoc<Object JavaDoc> vecFunctionTemplateOutSection; // vector of functions out of a Section, therefore out of a Data, but need to initialize and accumulte in the data
34
SectionTemplate firstSectionTemplate;
35   int intTotalLevels;
36   // field if there are a connection
37
String JavaDoc strDriver;
38   String JavaDoc strURL;
39   String JavaDoc strSQL;
40
41   static Logger log4jDataTemplate = Logger.getLogger(DataTemplate.class);
42
43   public DataTemplate() {
44     intTotalLevels = -1;
45     log4jDataTemplate.debug("Initial value: " + intTotalLevels);
46     vecSectionTemplate = new Vector JavaDoc<Object JavaDoc>();
47     vecDetailTemplate = new XmlVectorTemplate();
48     vecFieldTemplate = new Vector JavaDoc<Object JavaDoc>();
49     vecParameterTemplate = new Vector JavaDoc<Object JavaDoc>();
50     firstSectionTemplate = null;
51     vecFunctionTemplateData = new Vector JavaDoc<Object JavaDoc>();
52     vecFunctionTemplateOutSection = new Vector JavaDoc<Object JavaDoc>();
53   }
54
55
56   public FieldTemplate addField(String JavaDoc name) {
57     return addField(name, null, null, null);
58   }
59
60   public FieldTemplate addField(String JavaDoc name, DecimalFormat JavaDoc formatOutput, DecimalFormat JavaDoc formatSimple,
61       Vector JavaDoc vecReplace) {
62     for (Enumeration JavaDoc e = vecFieldTemplate.elements() ; e.hasMoreElements() ;) {
63       FieldTemplate field = (FieldTemplate)e.nextElement();
64       // log4jDataTemplate.debug("Comparing: " + field.name() + " & " + name.trim().toUpperCase());
65
// if (field.name().equals(name.trim().toUpperCase())) {
66
if (field.name().equals(name)) { // XmlEngineNP
67
log4jDataTemplate.debug("VECTOR addField, already exists:" + name);
68         // set the atributes that nor are null
69
if (formatOutput != null) field.formatOutput = formatOutput;
70         if (formatSimple != null) field.formatSimple = formatSimple;
71         if (vecReplace != null) field.vecReplace = vecReplace;
72         return field;
73       }
74       }
75     log4jDataTemplate.debug("(addField) Crear FieldTemplate:" + name);
76     // FieldComponent fieldComponent = new FieldComponent(name.trim().toUpperCase(), this);
77
// FieldTemplate field = new FieldTemplate(name.trim().toUpperCase(), format, vecReplace); //, this); // XmlEngineNP: .trim().toUpperCase() has been used again
78
FieldTemplate field = new FieldTemplate(name, formatOutput, formatSimple, vecReplace); //, this); // XmlEngineNP: .trim().toUpperCase() has been used again
79
log4jDataTemplate.debug("(addField) call a addElement:" + name);
80     vecFieldTemplate.addElement(field);
81     log4jDataTemplate.debug("VECTOR addField, created:" + name);
82     return field;
83     }
84
85     public FunctionTemplate addFunction(String JavaDoc strFunction, String JavaDoc name, DecimalFormat JavaDoc formatOutput, DecimalFormat JavaDoc formatSimple) {
86       return addFunction(strFunction, name, formatOutput, formatSimple, null, null, null);
87     }
88
89     public FunctionTemplate addFunction(String JavaDoc strFunction, String JavaDoc name, DecimalFormat JavaDoc formatOutput, DecimalFormat JavaDoc formatSimple,
90         XmlComponentTemplate arg1, XmlComponentTemplate arg2, String JavaDoc id) {
91       FieldTemplate field = null;
92       if (!name.equals("*") && arg1 == null) { // if it's * or an evaluation function then the field is not used
93
log4jDataTemplate.debug("Call to addField: " + name + " " + strFunction);
94         field = addField(name);
95         log4jDataTemplate.debug("addField returns: " + name + " " + strFunction);
96       }
97       FunctionTemplate functionInstance = null;
98       if (strFunction.equals("COUNT")) {
99         functionInstance = new FunctionCountTemplate(name, field, null, this);
100       } else if (strFunction.equals("ORDER")) {
101         functionInstance = new FunctionOrderTemplate(name, field, null, this);
102       } else if (strFunction.equals("SUM")) {
103         functionInstance = new FunctionSumTemplate(name, field, formatOutput, formatSimple, this);
104       } else if (strFunction.equals("MED")) {
105         functionInstance = new FunctionMedTemplate(name, field, formatOutput, formatSimple, this);
106       } else if (strFunction.equals("EQUAL")) {
107         functionInstance = new FunctionEqualTemplate(id, formatOutput, formatSimple, this, arg1);
108       } else if (strFunction.equals("ADD")) {
109         functionInstance = new FunctionAddTemplate(id, formatOutput, formatSimple, this, arg1, arg2);
110       } else if (strFunction.equals("SUBTRACT")) {
111         functionInstance = new FunctionSubtractTemplate(id, formatOutput, formatSimple, this, arg1, arg2);
112       } else if (strFunction.equals("MULTIPLY")) {
113         functionInstance = new FunctionMultiplyTemplate(id, formatOutput, formatSimple, this, arg1, arg2);
114       } else if (strFunction.equals("DIVIDE")) {
115         functionInstance = new FunctionDivideTemplate(id, formatOutput, formatSimple, this, arg1, arg2);
116       } else if (strFunction.equals("MODULE")) {
117         functionInstance = new FunctionModuleTemplate(id, formatOutput, formatSimple, this, arg1, arg2);
118       } else if (strFunction.equals("GT")) {
119         functionInstance = new FunctionGtTemplate(id, formatOutput, formatSimple, this, arg1, arg2);
120       } else if (strFunction.equals("LT")) {
121         functionInstance = new FunctionLtTemplate(id, formatOutput, formatSimple, this, arg1, arg2);
122       } else if (strFunction.equals("MAX")) {
123         functionInstance = new FunctionMaxTemplate(id, formatOutput, formatSimple, this, arg1, arg2);
124       } else if (strFunction.equals("MIN")) {
125         functionInstance = new FunctionMinTemplate(id, formatOutput, formatSimple, this, arg1, arg2);
126       } else {
127         log4jDataTemplate.warn("Function " + strFunction + " not exists");
128       }
129       vecFunctionTemplateData.addElement(functionInstance);
130       return functionInstance;
131     }
132
133     public DataValue createDataValue(XmlDocument xmlDocument) {
134       DataValue dataValue = new DataValue(this, xmlDocument);
135       return dataValue;
136     }
137
138     public XmlComponentValue createXmlComponentValue(XmlDocument xmlDocument) {
139       DataValue dataValue = xmlDocument.hasDataValue.get(strName);
140       if (dataValue == null) {
141         dataValue = new DataValue(this, xmlDocument);
142         dataValue.initialize();
143       }
144       return dataValue;
145     }
146
147   }
148
149
Popular Tags