1 12 package org.openbravo.xmlEngine; 13 14 import org.openbravo.data.FieldProvider; 15 16 import java.sql.*; 17 18 import java.util.Vector ; 19 import java.util.Enumeration ; 20 21 import org.apache.log4j.Logger ; 22 23 27 class DataValue implements XmlComponentValue { 28 DataTemplate dataTemplate; 29 XmlDocument xmlDocument; 30 SectionValue firstSectionValue; 31 Connection connection; 33 PreparedStatement preparedStatement; 34 ResultSet result; 35 36 Vector <Object > vecSectionValue; 37 XmlVectorValue vecDetailValue; Vector <Object > vecFieldValue; Vector <Object > vecParameterValue; Vector <Object > vecFunctionValueData; Vector <Object > vecFunctionValueOutSection; 43 StringBuffer 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 vecFieldValue = new Vector <Object >(); 58 log4jDataValue.debug("vector of Fields"); 59 for (Enumeration 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 log4jDataValue.debug("vector of Functions"); 68 vecFunctionValueData = new Vector <Object >(); 69 vecFunctionValueOutSection = new Vector <Object >(); 70 for (Enumeration 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 log4jDataValue.debug("vector of Functions out of the section"); 79 for (Enumeration 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 log4jDataValue.debug("vector of Sections"); 88 vecSectionValue = new Vector <Object >(); 89 for (Enumeration 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 <Object >(); 104 for (Enumeration 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 printGenerated() { 123 if (firstSectionValue == null) { 124 return ""; 125 } else { 126 log4jDataValue.debug("Init()"); 127 init(); 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) { data = dataArray[iArray]; 138 iArray++; 139 } 140 return executeArray(); 141 } 142 } 143 } 144 145 public String printPrevious() { 146 return print(); 147 } 148 149 public String print() { 150 if (firstSectionValue == null) { 151 return ""; 152 } else { 153 return new String (firstSectionValue.strSection); 154 } 155 } 156 157 public String printSimple() { 158 return print(); 159 } 160 161 public String printPreviousSimple() { 162 return printPrevious(); 163 } 164 165 public String execute() { 166 int i = 0; 167 try { 169 while(result.next()) { 170 readFields(); 171 if (i == 0) { 172 firstValues(); 173 } else { 174 check(); 175 } 176 acumulate(); 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 (firstSectionValue.strSection); 189 } 190 191 public String executeArray() { 192 int i = 0; 193 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(); printDetail(); 205 } 206 if (data.length >0) { 207 firstValues(); 208 firstSectionValue.close(); 209 } 210 211 log4jDataValue.info("StringBuffer length:" + firstSectionValue.strSection.length()); 212 String strStringPrint = new String (firstSectionValue.strSection); 213 log4jDataValue.info("String length:" + strStringPrint.length()); 214 return strStringPrint; 215 216 } 218 219 public String executeBlank(String strBlank) { 220 init(); 221 setFieldsBlank(strBlank); 222 firstValues(); 223 acumulate(); printDetail(); 225 firstSectionValue.close(); 226 return new String (firstSectionValue.strSection); 227 } 228 229 public void connect() { 230 try { 231 log4jDataValue.info("Loading driver" + " Driver: " + dataTemplate.strDriver); 234 Class.forName(dataTemplate.strDriver); 235 log4jDataValue.info("Driver loaded"); 236 } catch(ClassNotFoundException e){ 237 log4jDataValue.error("Class not found: " + e); 238 } 239 try { 240 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 for (Enumeration 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 strValue; 272 if (parameter.xmlComponentValue != null) { 273 log4jDataValue.info("value in xmlComponentValue"); 274 if (parameter.parameterTemplate.section != null) { 275 strValue = parameter.xmlComponentValue.printPrevious(); } else { 277 strValue = parameter.xmlComponentValue.print(); } 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 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 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 strBlank) { 311 for (Enumeration 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 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 (); 325 326 for (Enumeration 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 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 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 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 e = vecDetailValue.elements() ; e.hasMoreElements() ;) { 362 XmlComponentValue xmlComponentValue = (XmlComponentValue)e.nextElement(); 363 strDetailValue.append(xmlComponentValue.print()); 364 } 365 366 } 368 369 public void acumulate() { 370 for (Enumeration e = vecSectionValue.elements() ; e.hasMoreElements() ;) { 371 SectionValue sectionValue = (SectionValue)e.nextElement(); 372 sectionValue.acumulate(); 373 } 374 375 for (Enumeration e = vecFunctionValueOutSection.elements() ; e.hasMoreElements() ;) { 377 FunctionValue functionInstance = (FunctionValue)e.nextElement(); 378 functionInstance.acumulate(); 379 } 380 } 381 } 382 | Popular Tags |