1 32 33 package com.knowgate.hipergate; 34 35 import java.io.OutputStream ; 36 import java.io.UnsupportedEncodingException ; 37 38 import java.sql.Connection ; 39 import java.sql.SQLException ; 40 import java.sql.Types ; 41 42 import java.util.Vector ; 43 import java.lang.StringBuffer ; 44 45 import org.w3c.dom.Node ; 46 import org.w3c.dom.Element ; 47 import dom.DOMDocument; 48 49 import javax.servlet.http.HttpServletRequest ; 50 51 import com.knowgate.jdc.JDCConnection; 52 import com.knowgate.dataobjs.DBSubset; 53 import com.knowgate.debug.DebugFile; 54 55 import com.knowgate.dataobjs.DB; 56 import com.knowgate.dataobjs.DBTable; 57 import com.knowgate.dataobjs.DBColumn; 58 import com.knowgate.dataobjs.DBPersist; 59 60 import com.knowgate.http.Cookies; 61 62 import com.knowgate.crm.DistributionList; 63 64 69 70 public class QueryByForm extends DBPersist { 71 72 private DBTable oBaseTable; 73 String sAlias; 74 DOMDocument oXMLDoc; 75 76 77 79 86 87 public QueryByForm(String sQBFURI) 88 throws ClassNotFoundException , IllegalAccessException , Exception { 89 super(DB.k_queries, "QueryByForm"); 90 parseURI(sQBFURI); 91 } 92 93 95 104 105 public QueryByForm(String sQBFURI, String sEncoding) 106 throws ClassNotFoundException , IllegalAccessException , 107 UnsupportedEncodingException , Exception { 108 super(DB.k_queries, "QueryByForm"); 109 parseURI(sQBFURI, sEncoding); 110 } 111 112 120 public QueryByForm(JDCConnection oConn, String sBaseTable, String sTableAlias, String sQueryGUID) throws SQLException { 121 super(DB.k_queries, "QueryByForm"); 122 123 oBaseTable = new DBTable(oConn.getCatalog(), "dbo", sBaseTable, 1); 124 oBaseTable.readColumns(oConn, oConn.getMetaData()); 125 sAlias = sTableAlias; 126 127 Object aQry[] = { sQueryGUID }; 128 129 load(oConn, aQry); 130 } 131 132 134 143 public void parseURI(String sURI, String sEncoding) 144 throws ClassNotFoundException , IllegalAccessException , Exception { 145 146 if (DebugFile.trace) { 147 DebugFile.writeln("Begin QueryByForm.parseURI(" + sURI + "," + sEncoding + ")"); 148 DebugFile.incIdent(); 149 } 150 151 oXMLDoc = new DOMDocument(); 152 try { 153 oXMLDoc.parseURI(sURI, sEncoding); 154 } 155 catch (Exception xcpt) { 156 oXMLDoc = null; 157 throw new Exception (xcpt.getMessage(), xcpt.getCause()); 158 } 159 160 if (DebugFile.trace) { 161 DebugFile.decIdent(); 162 DebugFile.writeln("End QueryByForm.parseURI()"); 163 } 164 } 166 168 174 public void parseURI(String sURI) 175 throws ClassNotFoundException , IllegalAccessException , Exception { 176 parseURI(sURI, null); 177 } 178 179 181 184 public DOMDocument getDocument() { 185 return oXMLDoc; 186 } 187 188 190 194 public String getAction() { 195 if (DebugFile.trace) { 196 DebugFile.writeln("Begin QueryByForm.getAction()"); 197 DebugFile.incIdent(); 198 } 199 200 String sAction; 201 202 Node oTopNode = oXMLDoc.getRootNode().getFirstChild(); 204 if (oTopNode.getNodeName().equals("xml-stylesheet")) oTopNode = oTopNode.getNextSibling(); 205 206 Element oAction = (Element ) oXMLDoc.seekChildByName(oTopNode, "action"); 207 208 if (null==oAction) 209 sAction = null; 210 else 211 sAction = oXMLDoc.getTextValue(oAction); 212 213 if (DebugFile.trace) { 214 DebugFile.decIdent(); 215 DebugFile.writeln("End QueryByForm.getAction() : " + (sAction!=null ? sAction : "null")); 216 } 217 218 return sAction; 219 } 221 223 227 public String getBaseObject() { 228 if (DebugFile.trace) { 229 DebugFile.writeln("Begin QueryByForm.getBaseObject()"); 230 DebugFile.incIdent(); 231 } 232 233 String sBaseObj; 234 235 Node oTopNode = oXMLDoc.getRootNode().getFirstChild(); 237 if (oTopNode.getNodeName().equals("xml-stylesheet")) oTopNode = oTopNode.getNextSibling(); 238 239 Element oBaseObject = (Element ) oXMLDoc.seekChildByName(oTopNode, "baseobject"); 240 241 if (null==oBaseObject) 242 sBaseObj = null; 243 else 244 sBaseObj = oXMLDoc.getTextValue(oBaseObject); 245 246 if (DebugFile.trace) { 247 DebugFile.decIdent(); 248 DebugFile.writeln("End QueryByForm.getBaseObject() : " + (sBaseObj!=null ? sBaseObj : "null")); 249 } 250 251 return sBaseObj; 252 } 254 256 269 public String getBaseFilter(HttpServletRequest oReq) { 270 if (DebugFile.trace) { 271 DebugFile.writeln("Begin QueryByForm.getBaseFilter([HttpServletRequest])"); 272 DebugFile.incIdent(); 273 } 274 275 Node oTopNode = oXMLDoc.getRootNode().getFirstChild(); 277 if (oTopNode.getNodeName().equals("xml-stylesheet")) oTopNode = oTopNode.getNextSibling(); 278 279 Element oBaseObject = (Element ) oXMLDoc.seekChildByName(oTopNode, "basefilter"); 280 String sFilter = oXMLDoc.getTextValue(oBaseObject); 281 282 int iLength = sFilter.length(); 283 StringBuffer oFilter = new StringBuffer (iLength); 284 int iClose; 285 int iDot; 286 String sItem; 287 288 for (int c=0; c<iLength; c++) { 289 if (sFilter.charAt(c)=='$' && c<iLength-1) { 290 if (sFilter.charAt(c+1)=='{') { 291 iDot = sFilter.indexOf('.', c); 292 iClose = sFilter.indexOf('}', iDot); 293 sItem = sFilter.substring(iDot+1, iClose); 294 295 if (sFilter.substring(c+2,iDot).equals("cookie")) 296 oFilter.append(Cookies.getCookie(oReq, sItem, "")); 297 else if (sFilter.substring(c+2,iDot).equals("param")) 298 oFilter.append(oReq.getParameter(sItem)); 299 300 c = iClose; 301 } 302 } else 304 oFilter.append(sFilter.charAt(c)); 305 } 307 if (DebugFile.trace) { 308 DebugFile.decIdent(); 309 DebugFile.writeln("End QueryByForm.getBaseFilter() : " + oFilter.toString()); 310 } 311 312 return oFilter.toString(); 313 } 315 317 322 public String getMethod() { 323 if (DebugFile.trace) { 324 DebugFile.writeln("Begin QueryByForm.getMethod()"); 325 DebugFile.incIdent(); 326 } 327 328 if (null==oXMLDoc) { 329 if (DebugFile.trace) DebugFile.decIdent(); 330 throw new NullPointerException ("QueryByForm.getMethod() - XML document not set"); 331 } 332 333 String sMethod; 334 335 Node oRootNode= oXMLDoc.getRootNode(); 337 Node oTopNode = oRootNode.getFirstChild(); 338 if (oTopNode.getNodeName().equals("xml-stylesheet")) oTopNode = oTopNode.getNextSibling(); 339 340 Element oMethod = (Element ) oXMLDoc.seekChildByName(oTopNode, "method"); 341 342 if (null==oMethod) 343 sMethod = null; 344 else 345 sMethod = oXMLDoc.getTextValue(oMethod); 346 347 if (DebugFile.trace) { 348 DebugFile.decIdent(); 349 DebugFile.writeln("End QueryByForm.getMethod() : " + (sMethod!=null ? sMethod : "null")); 350 } 351 352 return sMethod; 353 } 355 357 362 public String getTitle(String sLanguage) 363 throws NullPointerException { 364 if (DebugFile.trace) { 365 DebugFile.writeln("Begin QueryByForm.getTitle(" + sLanguage + ")"); 366 DebugFile.incIdent(); 367 } 368 369 if (null==oXMLDoc) { 370 if (DebugFile.trace) DebugFile.decIdent(); 371 throw new NullPointerException ("QueryByForm.getTitle() - XML document not set"); 372 } 373 374 String sTitle; 375 376 Node oTopNode = oXMLDoc.getRootNode().getFirstChild(); 378 if (oTopNode.getNodeName().equals("xml-stylesheet")) oTopNode = oTopNode.getNextSibling(); 379 380 Element oTitleNode = (Element ) oXMLDoc.seekChildByName(oTopNode, "title_" + sLanguage); 381 382 if (null!=oTitleNode) 383 sTitle = oXMLDoc.getTextValue(oTitleNode); 384 else 385 sTitle = "Query"; 386 387 if (DebugFile.trace) { 388 DebugFile.decIdent(); 389 DebugFile.writeln("End QueryByForm.getTitle() : " + sTitle); 390 } 391 392 return sTitle; 393 } 395 397 400 public Vector getFields() { 401 Node oTopNode = oXMLDoc.getRootNode().getFirstChild(); 403 if (oTopNode.getNodeName().equals("xml-stylesheet")) oTopNode = oTopNode.getNextSibling(); 404 405 Element oFieldsNode = (Element ) oXMLDoc.seekChildByName(oTopNode, "fields"); 406 407 if (null==oFieldsNode) 408 return new Vector (); 409 else 410 return oXMLDoc.filterChildsByName(oFieldsNode, "field"); 411 } 413 415 420 public Vector getSortable() { 421 Node oTopNode = oXMLDoc.getRootNode().getFirstChild(); 423 if (oTopNode.getNodeName().equals("xml-stylesheet")) oTopNode = oTopNode.getNextSibling(); 424 425 Element oFieldsNode = (Element ) oXMLDoc.seekChildByName(oTopNode, "sortable"); 426 427 if (null==oFieldsNode) 428 return new Vector (); 429 else 430 return oXMLDoc.filterChildsByName(oFieldsNode, "by"); 431 } 433 435 439 public Vector getColumns() throws NullPointerException { 440 Node oTopNode = oXMLDoc.getRootNode().getFirstChild(); 442 if (oTopNode.getNodeName().equals("xml-stylesheet")) oTopNode = oTopNode.getNextSibling(); 443 444 Element oFieldsNode = (Element ) oXMLDoc.seekChildByName(oTopNode, "columns"); 445 446 if (null==oFieldsNode) 447 throw new NullPointerException ("Cannot find <columns> tag"); 448 449 return oXMLDoc.filterChildsByName(oFieldsNode, "column"); 450 } 452 454 471 private String getClause(String fld, String opr, String vle) 472 throws NullPointerException { 473 474 String ret; 475 short type; 476 DBColumn col; 477 478 col = oBaseTable.getColumnByName(fld.toLowerCase()); 479 480 if (null==col) 481 throw new NullPointerException ("Cannot find column " + fld + " on " + oBaseTable.getName()); 482 483 type = col.getSqlType(); 484 485 if (type==Types.VARCHAR || type==Types.CHAR || type==Types.LONGVARCHAR || type==Types.CLOB) { 486 if (opr.equals("S")) 487 ret = sAlias + "." + fld + " LIKE '" + vle + "%' "; 488 else if (opr.equals("C")) 489 ret = sAlias + "." + fld + " LIKE '%" + vle + "%' "; 490 else if (opr.equals("N")) 491 ret = sAlias + "." + fld + " IS NULL "; 492 else if (opr.equals("M")) 493 ret = sAlias + "." + fld + " IS NOT NULL "; 494 else 495 ret = sAlias + "." + fld + " " + opr + " '" + vle + "' "; 496 } 497 else if (type==Types.DATE || type==Types.TIMESTAMP) { 498 ret = sAlias + "." + fld + opr + "{ d '" + vle + "'}" + " "; 499 } 500 else { 501 ret = sAlias + "." + fld + opr + vle + " "; 502 } 503 return ret; 504 } 506 508 514 515 public String composeSQL() throws NullPointerException { 516 517 if (DebugFile.trace) { 518 DebugFile.writeln("Begin QueryByForm.composeSQL()"); 519 DebugFile.incIdent(); 520 } 521 522 String fld, opr, val, cod, qry = ""; 523 524 fld = getStringNull("nm_field1",""); 525 opr = getStringNull("nm_operator1",""); 526 val = getStringNull("tx_value1",""); 527 cod = getStringNull("vl_code1",""); 528 529 if (val.length()>0 || opr.equals("N") || opr.equals("M")) { 530 qry = getClause(fld, opr, cod.length() > 0 ? cod : val); 531 532 if (!isNull("tx_condition1")) { 533 qry += " " + getString("tx_condition1") + " "; 534 535 fld = getStringNull("nm_field2",""); 536 opr = getStringNull("nm_operator2",""); 537 val = getStringNull("tx_value2",""); 538 cod = getStringNull("vl_code2",""); 539 540 if (val.length()>0 || opr.equals("N") || opr.equals("M")) { 541 qry += getClause(fld, opr, cod.length() > 0 ? cod : val); 542 543 if (!isNull("tx_condition2")) { 544 qry += " " + getString("tx_condition2") + " "; 545 546 fld = getStringNull("nm_field3", ""); 547 opr = getStringNull("nm_operator3", ""); 548 val = getStringNull("tx_value3", ""); 549 cod = getStringNull("vl_code3", ""); 550 551 qry += getClause(fld, opr, cod.length() > 0 ? cod : val); 552 } } } } 557 if (DebugFile.trace) { 558 DebugFile.decIdent(); 559 DebugFile.writeln("End QueryByForm.composeSQL() : " + qry); 560 } 561 562 return qry; 563 } 565 567 582 public void queryToStream(Connection oConn, String sColumnList, String sFilter, OutputStream oOutStrm, String sShowAs) throws SQLException { 583 if (DebugFile.trace) { 584 DebugFile.writeln("Begin QueryByForm.queryToStream([Connection]," + sColumnList + "," + sFilter + ", [OutputStream], " + sShowAs + ")"); 585 DebugFile.incIdent(); 586 } 587 588 DBSubset oDBSS = new DBSubset(getBaseObject(), sColumnList, sFilter, 100); 589 590 if (sShowAs.equalsIgnoreCase("TSV")) { 591 oDBSS.setColumnDelimiter("\t"); 592 oDBSS.setRowDelimiter("\n"); 593 oDBSS.setTextQualifier(""); 594 } 595 else if (sShowAs.equalsIgnoreCase("XLS")) { 596 oDBSS.setColumnDelimiter(";"); 597 oDBSS.setRowDelimiter("\n"); 598 oDBSS.setTextQualifier(""); 599 } 600 else { 601 oDBSS.setColumnDelimiter(","); 602 oDBSS.setRowDelimiter("\n"); 603 oDBSS.setTextQualifier("\""); 604 } 605 606 oDBSS.print(oConn, oOutStrm); 607 608 if (DebugFile.trace) { 609 DebugFile.decIdent(); 610 DebugFile.writeln("End QueryByForm.queryToStream()"); 611 } 612 } 614 616 623 public static boolean delete(JDCConnection oConn, String sQBFGUID) throws SQLException { 624 if (DebugFile.trace) { 625 DebugFile.writeln("Begin QueryByForm.delete([Connection]," + sQBFGUID + ")"); 626 DebugFile.incIdent(); 627 } 628 629 DBSubset oLists = new DBSubset(DB.k_lists, DB.gu_list, DB.gu_query+"=?", 10); 630 int iLists = oLists.load(oConn, new Object []{sQBFGUID}); 631 632 for (int l=0; l<iLists; l++) { 633 DistributionList.delete(oConn, oLists.getString(0,l)); 634 } 636 DBPersist oDBP = new DBPersist(DB.k_queries, "QueryByForm"); 637 638 oDBP.put(DB.gu_query, sQBFGUID); 639 640 boolean bRetVal = oDBP.delete(oConn); 641 642 if (DebugFile.trace) { 643 DebugFile.decIdent(); 644 DebugFile.writeln("End QueryByForm.delete() : " + String.valueOf(bRetVal)); 645 } 646 647 return bRetVal; 648 } 649 650 652 } 653
| Popular Tags
|