1 23 24 package org.dbforms.config; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.dbforms.util.IEscaper; 30 import org.dbforms.util.MessageResourcesInternal; 31 import org.dbforms.util.ParseUtil; 32 import org.dbforms.util.ReflectionUtil; 33 import org.dbforms.util.SqlUtil; 34 import org.dbforms.util.StringUtil; 35 import org.dbforms.util.Util; 36 37 import java.sql.PreparedStatement ; 38 import java.sql.ResultSet ; 39 import java.sql.SQLException ; 40 import java.sql.Statement ; 41 42 import java.util.Collection ; 43 import java.util.Hashtable ; 44 import java.util.Iterator ; 45 import java.util.Locale ; 46 import java.util.StringTokenizer ; 47 import java.util.Vector ; 48 49 import java.io.Serializable ; 50 51 52 import javax.servlet.http.HttpServletRequest ; 53 54 55 56 64 public class Table implements Serializable { 65 66 public static final int BLOB_INTERCEPTOR = 0; 67 68 69 public static final int BLOB_CLASSIC = 1; 70 private static final int MAXFIELDS = 1000; 71 72 73 protected static final int DB_FIELD = 0; 74 75 76 protected static final int SEARCH_FIELD = 1; 77 78 79 protected static final int CALC_FIELD = 2; 80 81 85 private GrantedPrivileges grantedPrivileges = null; 86 private Hashtable calcFieldsNameHash = new Hashtable (); 87 88 89 private Hashtable fieldNameHash = new Hashtable (); 90 91 92 private Hashtable foreignKeyNameHash = new Hashtable (); 93 private IEscaper escaper = null; 94 95 96 private static Log logCat = LogFactory.getLog(Table.class.getName()); 97 98 99 private String alias = null; 100 private String blobHandling = null; 101 102 103 private String dataAccessClass = null; 104 105 106 private String defaultVisibleFields; 107 108 109 private String defaultVisibleFieldsFormat; 110 private String escaperClass = null; 111 112 113 private String name; 114 115 119 private String orderBy; 120 121 122 private TableEvents tableEvents = null; 123 private Vector calcFields = new Vector (); 124 125 129 private Vector diskblobs = new Vector (); 130 131 132 private Vector fields = new Vector (); 133 private Vector foreignKeys = new Vector (); 134 135 136 private Vector interceptors = new Vector (); 137 138 139 private Vector key = new Vector (); 140 141 142 private FieldValue[] defaultOrder; 143 private int blobHandlingStrategy = BLOB_INTERCEPTOR; 144 145 149 private int id; 150 151 156 public void setAlias(String alias) { 157 this.alias = alias; 158 } 159 160 161 166 public String getAlias() { 167 return alias; 168 } 169 170 171 176 public void setBlobHandling(String blobHandling) { 177 this.blobHandling = blobHandling; 178 179 if ("classic".equals(blobHandling)) { 180 this.blobHandlingStrategy = BLOB_CLASSIC; 181 } else { 182 this.blobHandlingStrategy = BLOB_INTERCEPTOR; 183 } 184 } 185 186 187 192 public String getBlobHandling() { 193 return blobHandling; 194 } 195 196 197 202 public int getBlobHandlingStrategy() { 203 return this.blobHandlingStrategy; 204 } 205 206 207 212 public Vector getCalcFields() { 213 return calcFields; 214 } 215 216 217 222 public void setDataAccessClass(String dataAccessClass) { 223 this.dataAccessClass = dataAccessClass; 224 } 225 226 227 232 public String getDataAccessClass() { 233 return dataAccessClass; 234 } 235 236 237 244 public FieldValue[] getDefaultOrder() { 245 return defaultOrder; 246 } 247 248 249 254 public void setDefaultVisibleFields(String defaultVisibleFields) { 255 this.defaultVisibleFields = defaultVisibleFields; 256 } 257 258 259 264 public String getDefaultVisibleFields() { 265 return this.defaultVisibleFields; 266 } 267 268 269 275 public void setDefaultVisibleFieldsFormat(String defaultVisibleFieldsFormat) { 276 this.defaultVisibleFieldsFormat = defaultVisibleFieldsFormat; 277 } 278 279 280 285 public String getDefaultVisibleFieldsFormat() { 286 return this.defaultVisibleFieldsFormat; 287 } 288 289 290 295 public String getDeleteStatement() { 296 StringBuffer queryBuf = new StringBuffer (); 298 queryBuf.append("DELETE FROM "); 299 queryBuf.append(getQueryToChange()); 300 queryBuf.append(" WHERE "); 301 queryBuf.append(getWhereClauseForKeyFields()); 302 logCat.info(queryBuf.toString()); 303 304 return queryBuf.toString(); 305 } 306 307 308 316 public String getDisblobSelectStatement() { 317 StringBuffer buf = new StringBuffer (); 318 buf.append("SELECT "); 319 320 int cnt = diskblobs.size(); 321 322 for (int i = 0; i < cnt; i++) { 323 Field diskblobField = (Field) diskblobs.elementAt(i); 324 325 buf.append(diskblobField.getName()); 327 328 if (i < (cnt - 1)) { 329 buf.append(", "); 330 } 331 } 332 333 buf.append(" FROM "); 334 buf.append(getQueryFrom()); 335 336 return buf.toString(); 337 } 338 339 340 345 public Vector getDiskblobs() { 346 return diskblobs; 347 } 348 349 350 369 public ResultSet getDoSelectResultSet(FieldValue[] fvEqual, 370 FieldValue[] fvOrder, 371 FieldValue[] sqlFilterParams, 372 int compareMode, 373 PreparedStatement ps) 374 throws SQLException { 375 int curCol = 1; 377 378 logCat.debug("###getDoSelectResultSet pos1"); 379 380 if (!FieldValue.isNull(sqlFilterParams)) { 381 curCol = populateWhereEqualsClause(sqlFilterParams, ps, curCol); 382 } 383 384 logCat.debug("###getDoSelectResultSet pos2"); 385 386 if (!FieldValue.isNull(fvEqual)) { 387 curCol = populateWhereEqualsClause(fvEqual, ps, curCol); 388 } 389 390 logCat.debug("###getDoSelectResultSet pos3"); 391 392 if ((compareMode != Constants.COMPARE_NONE) 393 && (fvOrder != null) 394 && (fvOrder.length > 0)) { 395 populateWhereAfterClause(fvOrder, ps, curCol); 396 } 397 398 logCat.debug("###getDoSelectResultSet pos3"); 399 400 ResultSet result = null; 401 402 try { 403 result = ps.executeQuery(); 404 } catch (SQLException sqle) { 405 SqlUtil.logSqlException(sqle); 406 throw sqle; 407 } 408 409 return result; 410 } 411 412 413 418 public IEscaper getEscaper() { 419 if (escaper == null) { 420 String s = getEscaperClass(); 421 422 if (!Util.isNull(s)) { 423 try { 424 escaper = (IEscaper) ReflectionUtil.newInstance(s); 425 } catch (Exception e) { 426 logCat.error("cannot create the new escaper [" + s + "]", e); 427 } 428 } 429 430 if ((escaper == null)) { 431 try { 432 escaper = DbFormsConfigRegistry.instance() 433 .lookup() 434 .getEscaper(); 435 } catch (Exception e) { 436 logCat.error("cannot create the new default escaper", e); 437 } 438 } 439 } 440 441 return escaper; 442 } 443 444 445 450 public void setEscaperClass(String string) { 451 escaperClass = string; 452 } 453 454 455 460 public String getEscaperClass() { 461 return escaperClass; 462 } 463 464 465 472 public Field getField(int fieldId) { 473 Field f = null; 474 475 if (checkFieldId(CALC_FIELD, fieldId)) { 476 f = (Field) calcFields.elementAt(decodeFieldId(CALC_FIELD, fieldId)); 477 } else { 478 f = (Field) fields.elementAt(fieldId); 479 } 480 481 return f; 482 } 483 484 485 493 public Field getFieldByName(String aname) { 494 Field f = (Field) fieldNameHash.get(aname); 495 if (f == null) { 496 f = (Field) calcFieldsNameHash.get(aname); 497 } 498 return f; 499 } 500 501 502 509 public String getFieldName(int fieldID) { 510 Field f = (Field) getFields() 511 .elementAt(fieldID); 512 513 return (f.getName()); 514 } 515 516 517 527 public FieldValues getFieldValues(String position) { 528 if (Util.isNull(position)) { 530 return null; 531 } 532 533 FieldValues result = new FieldValues(); 538 539 try { 540 int startIndex = 0; 541 boolean endOfString = false; 542 543 while (!endOfString) { 545 int firstColon = position.indexOf(':', startIndex); 546 int secondColon = position.indexOf(':', firstColon + 1); 547 548 if ((firstColon == -1) && (secondColon == -1)) { 549 return null; 550 } 551 552 String fieldIdStr = position.substring(startIndex, firstColon); 553 int fieldId = Integer.parseInt(fieldIdStr); 554 555 String valueLengthStr = position.substring(firstColon + 1, 556 secondColon); 557 int valueLength = Integer.parseInt(valueLengthStr); 558 559 int controlIndex = secondColon + 1 + valueLength; 560 561 String valueStr = (controlIndex < position.length()) 563 ? position.substring(secondColon + 1, controlIndex) 564 : position.substring(secondColon + 1); 565 566 Field f = getField(fieldId); 567 FieldValue fv = new FieldValue(f, valueStr); 568 result.put(fv); 569 570 if (controlIndex == position.length()) { 571 endOfString = true; 572 } else if (controlIndex > position.length()) { 573 logCat.warn("Controlbyte wrong but continuing execution"); 574 endOfString = true; 575 } else { 576 char controlByte = position.charAt(controlIndex); 577 578 if (controlByte != '-') { 579 logCat.error("Controlbyte wrong, abandon execution"); 580 throw new IllegalArgumentException (); 581 } 582 583 startIndex = controlIndex + 1; 584 585 if (position.length() == startIndex) { 586 endOfString = true; 587 } 588 } 589 } 590 } catch (Exception e) { 591 logCat.error("::getFieldValuesFromPositionAsHt - exception:", e); 592 result = null; 593 } 594 595 return result; 596 } 597 598 599 604 public Vector getFields() { 605 return fields; 606 } 607 608 609 619 public FieldValue[] getFilterFieldArray(String filter, 620 Locale locale) { 621 Vector keyValPairs = StringUtil.splitString(filter, ",;"); 623 624 int len = keyValPairs.size(); 626 627 FieldValue[] result = new FieldValue[len]; 628 629 for (int i = 0; i < len; i++) { 630 int operator = 0; 631 boolean isLogicalOR = false; 632 int jump = 1; 633 String aKeyValPair = (String ) keyValPairs.elementAt(i); 634 635 logCat.debug("initFilterFieldValues: aKeyValPair = " + aKeyValPair); 637 638 int n; 641 642 if ((n = aKeyValPair.indexOf("<>")) != -1) { 644 operator = Constants.FILTER_NOT_EQUAL; 646 jump = 2; 647 } else if ((n = aKeyValPair.indexOf(">=")) != -1) { 648 operator = Constants.FILTER_GREATER_THEN_EQUAL; 652 jump = 2; 653 } else if ((n = aKeyValPair.indexOf('>')) != -1) { 654 operator = Constants.FILTER_GREATER_THEN; 657 } else if ((n = aKeyValPair.indexOf("<=")) != -1) { 658 operator = Constants.FILTER_SMALLER_THEN_EQUAL; 662 jump = 2; 663 } else if ((n = aKeyValPair.indexOf('<')) != -1) { 664 operator = Constants.FILTER_SMALLER_THEN; 667 } else if ((n = aKeyValPair.indexOf('=')) != -1) { 668 operator = Constants.FILTER_EQUAL; 671 } else if ((n = aKeyValPair.indexOf('~')) != -1) { 672 operator = Constants.FILTER_LIKE; 675 } else if ((n = aKeyValPair.toUpperCase() 676 .indexOf("NOTISNULL")) != -1) { 677 jump = 9; 680 operator = Constants.FILTER_NOT_NULL; 681 } else if ((n = aKeyValPair.toUpperCase() 682 .indexOf("ISNULL")) != -1) { 683 jump = 6; 686 operator = Constants.FILTER_NULL; 687 } 688 689 String fieldName = aKeyValPair.substring(0, n) 695 .trim(); 696 697 logCat.debug("Filter field=" + fieldName); 699 700 if (fieldName.charAt(0) == '|') { 701 fieldName = fieldName.substring(1); 704 isLogicalOR = true; 705 } 706 707 Field filterField = getFieldByName(fieldName); 708 709 String value = aKeyValPair.substring(n + jump) 711 .trim(); 712 713 logCat.debug("Filter value=" + value); 715 716 result[i] = FieldValue.createFieldValueForSearching(filterField, 718 value, locale, 719 operator, 720 Constants.SEARCHMODE_NONE, 721 Constants.SEARCH_ALGO_SHARP, 722 isLogicalOR); 723 logCat.debug("and fv is =" + result[i].toString()); 724 } 725 726 return result; 727 } 728 729 730 736 public Collection getForeignKeys() { 737 return foreignKeys; 738 } 739 740 741 750 public String getFreeFormSelectQuery(Vector fieldsToSelect, 751 String whereClause, 752 String tableList) { 753 StringBuffer buf = new StringBuffer (); 754 buf.append("SELECT "); 755 buf.append(getQuerySelect(fieldsToSelect)); 756 buf.append(" FROM "); 757 758 if (Util.isNull(tableList)) { 759 buf.append(getQueryFrom()); 760 } else { 761 buf.append(tableList); 762 } 763 764 buf.append(" "); 765 buf.append(whereClause); 766 logCat.info("::getFreeFormSelectQuery -- [" + buf.toString() + "]"); 767 768 return buf.toString(); 769 } 770 771 772 778 public void setGrantedPrivileges(GrantedPrivileges grantedPrivileges) { 779 this.grantedPrivileges = grantedPrivileges; 780 } 781 782 783 789 public GrantedPrivileges getGrantedPrivileges() { 790 return grantedPrivileges; 791 } 792 793 794 799 public void setId(int id) { 800 this.id = id; 801 } 802 803 804 809 public int getId() { 810 return id; 811 } 812 813 814 821 public String getInsertStatement(FieldValues fieldValues) { 822 StringBuffer queryBuf = new StringBuffer (); 823 queryBuf.append("INSERT INTO "); 824 queryBuf.append(getQueryToChange()); 825 queryBuf.append(" ("); 826 827 Iterator e = fieldValues.keys(); 829 830 while (e.hasNext()) { 831 String fieldName = (String ) e.next(); 832 queryBuf.append(fieldName); 833 834 if (e.hasNext()) { 835 queryBuf.append(","); 836 } 837 } 838 839 queryBuf.append(") VALUES ("); 841 842 for (int i = 0; i < fieldValues.size(); i++) { 843 if (i != 0) { 844 queryBuf.append(","); 845 } 846 847 queryBuf.append("?"); 848 } 849 850 queryBuf.append(")"); 851 logCat.info(queryBuf.toString()); 852 853 return queryBuf.toString(); 854 } 855 856 857 863 public Vector getInterceptors() { 864 Vector res = interceptors; 865 if ((getConfig() != null) && getConfig().hasInterceptors()) { 866 res = new Vector (interceptors); 867 res.addAll(getConfig().getInterceptors()); 868 res.addAll(interceptors); 869 } 870 return res; 871 } 872 873 874 881 public Vector getKey() { 882 return key; 883 } 884 885 886 895 public String getKeyPositionString(ResultSetVector rsv) { 896 if (ResultSetVector.isNull(rsv)) { 897 return null; 898 } 899 900 String [] currentRow = rsv.getCurrentRow(); 901 902 if (currentRow == null) { 903 return null; 904 } 905 906 return getKeyPositionString(currentRow); 907 } 908 909 910 917 public String getKeyPositionString(String [] currentRow) { 918 StringBuffer buf = new StringBuffer (); 919 920 if (currentRow != null) { 921 for (int i = 0; i < getKey() 922 .size(); i++) { 923 Field f = (Field) getKey() 924 .elementAt(i); 925 926 if (i > 0) { 927 buf.append("-"); } 929 930 buf.append(createToken(f, currentRow[f.getId()])); 931 } 932 } 933 934 return buf.toString(); 935 } 936 937 938 947 public String getKeyPositionString(FieldValues fvHT) { 948 if (fvHT == null) { 949 return null; 950 } 951 952 StringBuffer buf = new StringBuffer (); 953 int cnt = 0; 954 955 for (int i = 0; i < getKey() 956 .size(); i++) { 957 Field f = (Field) getKey() 958 .elementAt(i); 959 FieldValue fv = fvHT.get(f.getName()); 960 961 if (fv != null) { 962 String value = fv.getFieldValue(); 963 964 if (value == null) { 965 throw new IllegalArgumentException ("wrong fields provided"); 966 } 967 968 if (cnt > 0) { 969 buf.append("-"); } 971 972 buf.append(createToken(f, value)); 973 cnt++; 974 } 975 } 976 977 return buf.toString(); 978 } 979 980 981 986 public void setName(String name) { 987 this.name = name; 988 } 989 990 991 996 public String getName() { 997 return name; 998 } 999 1000 1001 1010 public Hashtable getNamesHashtable(String core) { 1011 Hashtable result = new Hashtable (); 1012 Iterator e = getFields() 1013 .iterator(); 1014 1015 while (e.hasNext()) { 1016 Field f = (Field) e.next(); 1017 result.put(f.getName(), f.getFieldName(core)); 1018 } 1019 1020 return result; 1021 } 1022 1023 1024 1030 public void setOrderBy(String orderBy) { 1031 this.orderBy = orderBy; 1032 } 1033 1034 1035 1041 public String getOrderBy() { 1042 return orderBy; 1043 } 1044 1045 1046 1060 public String getPositionString(ResultSetVector rsv) { 1061 if (ResultSetVector.isNull(rsv)) { 1062 return null; 1063 } 1064 1065 String [] currentRow = rsv.getCurrentRow(); 1066 1067 if (currentRow == null) { 1068 return null; 1069 } 1070 1071 return getPositionString(currentRow); 1072 } 1073 1074 1075 1087 public String getPositionString(String [] currentRow) { 1088 StringBuffer buf = new StringBuffer (); 1089 int cnt = 0; 1090 1091 for (int i = 0; i < getFields() 1092 .size(); i++) { 1093 Field f = (Field) getFields() 1094 .elementAt(i); 1095 1096 if (f.hasIsKeySet() || f.hasSortableSet()) { 1097 if (cnt > 0) { 1098 buf.append("-"); } 1100 1101 buf.append(createToken(f, currentRow[f.getId()])); 1102 cnt++; 1103 } 1104 } 1105 1106 return buf.toString(); 1107 } 1108 1109 1110 1119 public String getPositionString(Hashtable ht) { 1120 StringBuffer buf = new StringBuffer (); 1121 int cnt = 0; 1122 Iterator e = ht.keySet() 1123 .iterator(); 1124 1125 while (e.hasNext()) { 1126 String fieldName = (String ) e.next(); 1127 Field aField = getFieldByName(fieldName); 1128 1129 if (aField != null) { 1130 if (aField.hasIsKeySet() || aField.hasSortableSet()) { 1131 String fieldValue = (String ) ht.get(fieldName); 1132 1133 if (cnt > 0) { 1134 buf.append('-'); } 1136 1137 buf.append(createToken(aField, fieldValue)); 1138 cnt++; 1139 } else { 1140 logCat.debug("provided goto field " + fieldName 1141 + " is not key/search field!"); 1142 } 1143 } else { 1144 logCat.error("provided goto field " + fieldName + " not found!"); 1145 } 1146 } 1147 1148 return buf.toString(); 1149 } 1150 1151 1152 1161 public String getPositionString(FieldValues fvHT) { 1162 String res = null; 1163 if (fvHT != null) { 1164 StringBuffer buf = new StringBuffer (); 1165 int cnt = 0; 1166 Iterator e = fvHT.keys(); 1167 1168 while (e.hasNext()) { 1169 String fieldName = (String ) e.next(); 1170 FieldValue fv = fvHT.get(fieldName); 1171 Field f = fv.getField(); 1172 1173 if (f.hasIsKeySet() || f.hasSortableSet()) { 1174 String value = fv.getFieldValue(); 1175 1176 if (value == null) { 1177 throw new IllegalArgumentException ("wrong fields provided"); 1178 } 1179 1180 if (cnt > 0) { 1181 buf.append("-"); } 1183 1184 buf.append(createToken(f, value)); 1185 cnt++; 1186 } 1187 } 1188 1189 res = buf.toString(); 1190 } 1191 return res; 1192 } 1193 1194 1195 1200 public String getQueryFrom() { 1201 return name; 1202 } 1203 1204 1205 1213 public String getQuerySelect(Vector fieldsToSelect) { 1214 if (fieldsToSelect != null) { 1215 StringBuffer buf = new StringBuffer (); 1216 int fieldsToSelectSize = fieldsToSelect.size(); 1217 1218 for (int i = 0; i < fieldsToSelectSize; i++) { 1222 Field f = (Field) fieldsToSelect.elementAt(i); 1223 buf.append(f.getName()); 1224 buf.append(", "); 1225 } 1226 1227 buf.deleteCharAt(buf.length() - 2); 1228 1229 return buf.toString(); 1230 } 1231 1232 return "*"; 1233 } 1234 1235 1236 1251 public String getSelectQuery(Vector fieldsToSelect, 1252 FieldValue[] fvEqual, 1253 FieldValue[] fvOrder, 1254 String sqlFilter, 1255 int compareMode) { 1256 StringBuffer buf = new StringBuffer (); 1257 1258 buf.append("SELECT "); 1259 buf.append(getQuerySelect(fieldsToSelect)); 1260 buf.append(" FROM "); 1261 buf.append(getQueryFrom()); 1262 1263 String s = getQueryWhere(fvEqual, fvOrder, compareMode); 1264 1265 if (!Util.isNull(s) || !Util.isNull(sqlFilter)) { 1266 buf.append(" WHERE "); 1267 } 1268 1269 if (!Util.isNull(sqlFilter)) { 1271 buf.append(" ( "); 1272 buf.append(sqlFilter); 1273 buf.append(" ) "); 1274 } 1275 1276 if (!Util.isNull(s)) { 1278 if (!Util.isNull(sqlFilter)) { 1279 buf.append(" AND "); 1280 } 1281 1282 buf.append(" ( "); 1283 buf.append(s); 1284 buf.append(" ) "); 1285 } 1286 1287 s = getQueryOrderBy(fvOrder); 1288 1289 if (s.length() > 0) { 1290 buf.append(" ORDER BY "); 1291 buf.append(s); 1292 } 1293 1294 logCat.info("::getSelectQuery - [" + buf.toString() + "]"); 1295 1296 return buf.toString(); 1297 } 1298 1299 1300 1303 1308 public String getSelectStatement() { 1309 StringBuffer queryBuf = new StringBuffer (); 1310 queryBuf.append("SELECT "); 1311 queryBuf.append(getQuerySelect(fields)); 1312 queryBuf.append(" FROM "); 1313 queryBuf.append(getQueryFrom()); 1314 logCat.info(queryBuf.toString()); 1315 1316 return queryBuf.toString(); 1317 } 1318 1319 1320 1325 public void setTableEvents(TableEvents tableEvents) { 1326 this.tableEvents = tableEvents; 1327 tableEvents.setTable(this); 1328 } 1329 1330 1331 1338 public TableEvents getTableEvents() { 1339 if (tableEvents == null) { 1340 tableEvents = new TableEvents(); 1341 } 1342 1343 return tableEvents; 1344 } 1345 1346 1347 1354 public String getUpdateStatement(FieldValues fieldValues) { 1355 StringBuffer queryBuf = new StringBuffer (); 1356 queryBuf.append("UPDATE "); 1357 queryBuf.append(getQueryToChange()); 1358 queryBuf.append(" SET "); 1359 1360 boolean kommaNeeded = false; 1366 Iterator e = fieldValues.keys(); 1367 1368 while (e.hasNext()) { 1369 String fieldName = (String ) e.next(); 1370 1371 if (kommaNeeded) { 1372 queryBuf.append(", "); 1373 } else { 1374 kommaNeeded = true; 1375 } 1376 1377 queryBuf.append(fieldName); 1378 queryBuf.append("= ?"); 1379 } 1380 1381 queryBuf.append(" WHERE "); 1382 queryBuf.append(getWhereClauseForKeyFields()); 1383 logCat.info(queryBuf.toString()); 1384 1385 return queryBuf.toString(); 1386 } 1387 1388 1389 1396 public String getWhereClause(FieldValue[] fv) { 1397 StringBuffer buf = new StringBuffer (); 1398 1399 if ((fv != null) && (fv.length > 0)) { 1400 buf.append(" ( "); 1402 1403 for (int i = 0; i < fv.length; i++) { 1404 if (i != 0) { 1408 if (fv[i].getLogicalOR()) { 1409 buf.append(" OR "); 1410 } else { 1411 buf.append(" ) AND ( "); 1412 } 1413 } 1414 1415 buf.append(getSQLExpression(fv[i])); 1416 } 1417 1418 buf.append(" ) "); 1419 } 1420 1421 return buf.toString(); 1422 } 1423 1424 1425 1428 1435 public String getWhereClauseForKeyFields() { 1436 StringBuffer buf = new StringBuffer (); 1437 int cnt = this.getKey() 1438 .size(); 1439 1440 for (int i = 0; i < cnt; i++) { 1441 Field keyField = (Field) this.getKey() 1442 .elementAt(i); 1443 1444 buf.append(keyField.getName()); 1446 buf.append(" = ?"); 1447 1448 if (i < (cnt - 1)) { 1449 buf.append(" AND "); 1450 } 1451 } 1452 1453 return buf.toString(); 1454 } 1455 1456 1457 1465 public void addCalcField(Field field) throws Exception { 1466 if (field.getType() == 0) { 1467 throw new Exception ("no type!"); 1468 } 1469 1470 field.setId(encodeFieldId(CALC_FIELD, calcFields.size())); 1471 field.setTable(this); 1472 calcFields.addElement(field); 1473 1474 calcFieldsNameHash.put(field.getName(), field); 1476 } 1477 1478 1479 1487 public void addField(Field field) throws Exception { 1488 if (field.getType() == 0) { 1489 throw new Exception ("no type!"); 1490 } 1491 1492 field.setId(encodeFieldId(DB_FIELD, fields.size())); 1493 field.setTable(this); 1494 fields.addElement(field); 1495 1496 if (field.hasIsKeySet()) { 1498 logCat.info("wow - field " + getName() + "." + field.getName() 1499 + " is a key"); 1500 key.addElement(field); 1503 } else { 1504 logCat.info("field " + getName() + "." + field.getName() 1505 + " is NO key"); 1506 } 1507 1508 fieldNameHash.put(field.getName(), field); 1510 1511 if (field.getType() == FieldTypes.DISKBLOB) { 1513 diskblobs.addElement(field); 1514 } 1515 } 1516 1517 1518 1525 public void addForeignKey(ForeignKey fk) { 1526 fk.setId(foreignKeys.size()); 1527 1528 foreignKeys.addElement(fk); 1530 1531 foreignKeyNameHash.put(fk.getName(), fk); 1533 } 1534 1535 1536 1541 public void addInterceptor(Interceptor interceptor) { 1542 interceptors.addElement(interceptor); 1543 } 1544 1545 1546 1553 public boolean containsDiskblob() { 1554 return diskblobs.size() > 0; 1555 } 1556 1557 1558 1574 public FieldValue[] createOrderFieldValues(String order, 1575 HttpServletRequest request, 1576 boolean includeKeys) { 1577 Vector result = null; 1578 1579 if (request != null) { 1580 String paramStub = Constants.FIELDNAME_SORT + this.getId(); 1581 Vector sortFields = ParseUtil.getParametersStartingWith(request, 1582 paramStub); 1583 1584 if (sortFields.size() > 0) { 1585 result = createOrderFVFromRequest(request, paramStub, sortFields); 1586 } 1587 } 1588 1589 if (((result == null) || result.isEmpty())) { 1593 if (order == null) { 1595 order = getOrderBy(); 1596 } 1597 1598 result = createOrderFVFromAttribute(order); 1599 logCat.debug("@@@ 1"); 1600 1601 for (int i = 0; i < result.size(); i++) { 1602 FieldValue fieldVal = (FieldValue) result.elementAt(i); 1603 logCat.debug("fieldValue " + fieldVal.toString()); 1604 } 1605 } 1606 1607 if (includeKeys) { 1608 for (int i = 0; i < this.getKey() 1611 .size(); i++) { 1612 Field keyField = (Field) getKey() 1613 .elementAt(i); 1614 boolean found = false; 1615 int j = 0; 1616 1617 while (!found && (j < result.size())) { 1618 FieldValue fv = (FieldValue) result.elementAt(j); 1619 1620 if (fv.getField() == keyField) { 1621 found = true; 1622 } 1623 1624 j++; 1625 } 1626 1627 if (!found) { 1628 result.addElement(FieldValue.createFieldValueForSorting(keyField, 1629 Constants.ORDER_ASCENDING)); 1630 } 1631 } 1632 } 1633 1634 FieldValue[] resultArray = new FieldValue[result.size()]; 1635 result.copyInto(resultArray); 1636 logCat.debug("@@@ 2"); 1637 1638 for (int i = 0; i < resultArray.length; i++) { 1639 logCat.debug("fieldValue " + resultArray[i].toString()); 1640 } 1641 1642 return resultArray; 1643 } 1644 1645 1646 1649 1667 public ResultSetVector doConstrainedSelect(FieldValue[] fvEqual, 1668 FieldValue[] fvOrder, 1669 String sqlFilter, 1670 FieldValue[] sqlFilterParams, 1671 int compareMode, 1672 int maxRows, 1673 DbEventInterceptorData interceptorData) 1674 throws SQLException { 1675 String query = getSelectQuery(getFields(), fvEqual, fvOrder, 1676 sqlFilter, compareMode); 1677 PreparedStatement ps = interceptorData.getConnection() 1678 .prepareStatement(query); 1679 ps.setMaxRows(maxRows); 1681 ResultSet rs = getDoSelectResultSet(fvEqual, fvOrder, 1682 sqlFilterParams, compareMode, ps); 1683 ResultSetVector result = new ResultSetVector(this); 1684 result.addResultSet(interceptorData, rs); 1685 ps.close(); 1686 logCat.info("::doConstrainedSelect - rsv size = " + result.size()); 1687 1688 return result; 1689 } 1690 1691 1692 1705 public ResultSetVector doFreeFormSelect(String whereClause, 1706 String tableList, 1707 int maxRows, 1708 DbEventInterceptorData interceptorData) 1709 throws SQLException { 1710 Statement stmt = interceptorData.getConnection() 1711 .createStatement(); 1712 String query = getFreeFormSelectQuery(getFields(), whereClause, 1713 tableList); 1714 stmt.setMaxRows(maxRows); 1716 ResultSet rs; 1717 1718 try { 1719 rs = stmt.executeQuery(query); 1720 } catch (SQLException sqle) { 1721 SqlUtil.logSqlException(sqle); 1722 throw new SQLException (sqle.getMessage()); 1723 } 1724 1725 ResultSetVector result = new ResultSetVector(this); 1726 result.addResultSet(interceptorData, rs); 1727 1728 stmt.close(); 1731 logCat.info("rsv size=" + result.size()); 1732 1733 return result; 1734 } 1735 1736 1737 1745 public void fillWithValues(FieldValue[] orderConstraint, 1746 String aPosition) { 1747 FieldValues ht = getFieldValues(aPosition); 1749 1750 if (ht != null) { 1752 logCat.info("*** parsing through: " + aPosition); 1753 1754 for (int i = 0; i < orderConstraint.length; i++) { 1756 Field f = orderConstraint[i].getField(); 1757 1758 if (f != null) { 1759 FieldValue aFieldValue = ht.get(f.getName()); 1760 1761 if (aFieldValue != null) { 1762 orderConstraint[i].setFieldValue(aFieldValue.getFieldValue()); 1763 } else { 1764 logCat.warn("position entry has null value:" + f.getName()); 1765 } 1766 } 1767 } 1768 } 1769 } 1770 1771 1772 1777 public boolean hasInterceptors() { 1778 return (getConfig() != null && getConfig().hasInterceptors()) 1779 || ((interceptors != null) && (interceptors.size() > 0)); 1780 } 1781 1782 1783 1793 public boolean hasUserPrivileg(HttpServletRequest request, 1794 int privileg) { 1795 return (grantedPrivileges == null) ? true 1796 : grantedPrivileges.hasUserPrivileg(request, 1797 privileg); 1798 } 1799 1800 1801 1805 public void initDefaultOrder() { 1806 if (orderBy == null) { 1809 initDefaultOrderFromKeys(); 1810 } else { 1811 defaultOrder = createOrderFieldValues(orderBy, null, true); 1813 } 1814 1815 logCat.info("Table.initDefaultOrder done."); 1816 } 1817 1818 1819 1831 public FieldValues mapChildFieldValues(Table parentTable, 1832 String parentFieldString, 1833 String childFieldString, 1834 String aPosition) { 1835 Vector childFieldNames = StringUtil.splitString(childFieldString, ",;~"); 1837 Vector parentFieldNames = StringUtil.splitString(parentFieldString, ",;~"); 1838 1839 int len = childFieldNames.size(); 1842 1843 if ((len == 0) || (len != parentFieldNames.size())) { 1844 return null; 1845 } 1846 1847 FieldValues ht = parentTable.getFieldValues(aPosition); 1849 1850 if (ht == null) { 1851 return null; 1852 } 1853 1854 FieldValues childFieldValues = new FieldValues(); 1855 1856 for (int i = 0; i < len; i++) { 1857 String parentFieldName = (String ) parentFieldNames.elementAt(i); 1858 Field parentField = parentTable.getFieldByName(parentFieldName); 1859 String childFieldName = (String ) childFieldNames.elementAt(i); 1860 Field childField = this.getFieldByName(childFieldName); 1861 FieldValue aFieldValue = ht.get(parentField.getName()); 1862 1863 if (aFieldValue == null) { 1864 throw new IllegalArgumentException ("ERROR: Make sure that field " 1865 + parentField.getName() 1866 + " is a KEY of the table " 1867 + parentTable.getName() 1868 + "! Otherwise you can not use it as PARENT/CHILD LINK argument!"); 1869 } 1870 1871 String currentParentFieldValue = aFieldValue.getFieldValue(); 1872 childFieldValues.put(new FieldValue(childField, currentParentFieldValue)); 1873 } 1874 1875 return childFieldValues; 1876 } 1877 1878 1879 1890 public void populateWhereClauseWithKeyFields(String keyValuesStr, 1891 PreparedStatement ps, 1892 int startColumn) 1893 throws SQLException { 1894 int col = startColumn; 1895 1896 FieldValues keyValuesHt = getFieldValues(keyValuesStr); 1900 int keyLength = this.getKey() 1901 .size(); 1902 1903 for (int i = 0; i < keyLength; i++) { 1904 Field curField = (Field) this.getKey() 1905 .elementAt(i); 1906 FieldValue aFieldValue = keyValuesHt.get(curField.getName()); 1907 Object value = aFieldValue.getFieldValueAsObject(); 1908 JDBCDataHelper.fillWithData(ps, curField.getEscaper(), col, value, 1909 curField.getType(), 1910 this.getBlobHandlingStrategy()); 1911 col++; 1912 } 1913 } 1914 1915 1916 1930 public int populateWhereEqualsClause(FieldValue[] fv, 1931 PreparedStatement ps, 1932 int curCol) 1933 throws SQLException { 1934 if ((fv != null) && (fv.length > 0)) { 1935 for (int i = 0; i < fv.length; i++) { 1936 curCol = fillPreparedStatement(fv[i], ps, curCol); 1937 } 1938 } 1939 1940 return curCol; 1941 } 1942 1943 1944 1954 public int processInterceptors(int action, 1955 DbEventInterceptorData data) 1956 throws SQLException { 1957 String s; 1958 1959 try { 1960 Vector allInterceptors = getInterceptors(); 1961 int interceptorsCnt = allInterceptors.size(); 1962 1963 for (int i = 0; i < interceptorsCnt; i++) { 1964 Interceptor interceptor = (Interceptor) allInterceptors 1965 .elementAt(i); 1966 Class interceptorClass = Class.forName(interceptor 1967 .getClassName()); 1968 DbEventInterceptor dbi = (DbEventInterceptor) interceptorClass 1969 .newInstance(); 1970 1971 dbi.setParameterMap(interceptor.getParameterMap()); 1974 1975 int operation = DbEventInterceptor.GRANT_OPERATION; 1978 String denyMessage = null; 1979 1980 if (action == DbEventInterceptor.PRE_INSERT) { 1981 operation = dbi.preInsert(data); 1982 denyMessage = "dbforms.events.insert.nogrant"; 1983 } else if (action == DbEventInterceptor.POST_INSERT) { 1984 dbi.postInsert(data); 1985 } else if (action == DbEventInterceptor.PRE_UPDATE) { 1986 operation = dbi.preUpdate(data); 1987 denyMessage = "dbforms.events.update.nogrant"; 1988 } else if (action == DbEventInterceptor.POST_UPDATE) { 1989 dbi.postUpdate(data); 1990 } else if (action == DbEventInterceptor.PRE_DELETE) { 1991 operation = dbi.preDelete(data); 1992 denyMessage = "dbforms.events.delete.nogrant"; 1993 } else if (action == DbEventInterceptor.POST_DELETE) { 1994 dbi.postDelete(data); 1995 } else if (action == DbEventInterceptor.PRE_SELECT) { 1996 operation = dbi.preSelect(data); 1997 denyMessage = "dbforms.events.view.nogrant"; 1998 } else if (action == DbEventInterceptor.POST_SELECT) { 1999 dbi.postSelect(data); 2000 } else if (action == DbEventInterceptor.PRE_ADDROW) { 2001 operation = dbi.preAddRow(data); 2002 denyMessage = "dbforms.events.addrow.nogrant"; 2003 } else if (action == DbEventInterceptor.POST_ADDROW) { 2004 dbi.postAddRow(data); 2005 } 2006 2007 switch (operation) { 2008 case DbEventInterceptor.DENY_OPERATION: 2009 s = MessageResourcesInternal.getMessage(denyMessage, 2010 data.getRequest().getLocale(), 2011 new String [] { 2012 getName() 2013 }); 2014 throw new SQLException (s); 2015 2016 case DbEventInterceptor.IGNORE_OPERATION: 2017 return operation; 2018 2019 default: 2020 break; 2021 } 2022 } 2023 } catch (ClassNotFoundException cnfe) { 2024 logCat.warn(" ClassNotFoundException : " + cnfe.getMessage()); 2025 throw new SQLException (cnfe.getMessage()); 2026 } catch (InstantiationException ie) { 2027 logCat.warn(" InstantiationException : " + ie.getMessage()); 2028 throw new SQLException (ie.getMessage()); 2029 } catch (IllegalAccessException iae) { 2030 logCat.warn(" IllegalAccessException : " + iae.getMessage()); 2031 throw new SQLException (iae.getMessage()); 2032 } catch (SQLException sqle) { 2033 throw sqle; 2034 } catch (MultipleValidationException ve) { 2035 throw new SQLException (ve.getMessage()); 2036 } catch (ValidationException ve) { 2037 throw new SQLException (ve.getMessage()); 2038 } 2039 2040 return DbEventInterceptor.GRANT_OPERATION; 2041 } 2042 2043 2044 2047 2052 public String toString() { 2053 StringBuffer buf = new StringBuffer (); 2054 buf.append("\nname="); 2055 buf.append(name); 2056 buf.append(" "); 2057 buf.append("\nid="); 2058 buf.append(String.valueOf(getId())); 2059 buf.append(" "); 2060 2061 if (getFields() != null) { 2062 for (int i = 0; i < getFields() 2063 .size(); i++) { 2064 Field f = (Field) getFields() 2065 .elementAt(i); 2066 buf.append("\nfield: "); 2067 buf.append(f.toString()); 2068 } 2069 } 2070 2071 return buf.toString(); 2072 } 2073 2074 2075 2088 protected String getQueryOrderBy(FieldValue[] fvOrder) { 2089 StringBuffer buf = new StringBuffer (); 2090 2091 if (fvOrder != null) { 2092 for (int i = 0; i < fvOrder.length; i++) { 2093 buf.append(fvOrder[i].getField().getName()); 2094 2095 if (fvOrder[i].getSortDirection() == Constants.ORDER_DESCENDING) { 2096 buf.append(" DESC"); 2097 } 2098 2099 if (i < (fvOrder.length - 1)) { 2100 buf.append(","); 2101 } 2102 } 2103 } 2104 2105 return buf.toString(); 2106 } 2107 2108 2109 2114 protected String getQueryToChange() { 2115 return getQueryFrom(); 2116 } 2117 2118 2119 2131 protected String getQueryWhere(FieldValue[] fvEqual, 2132 FieldValue[] fvOrder, 2133 int compareMode) { 2134 boolean firstTermExists = false; 2135 StringBuffer buf = new StringBuffer (); 2136 2137 if (!FieldValue.isNull(fvEqual)) { 2139 buf.append(" ( "); 2141 2142 if (fvEqual[0].getSearchMode() == Constants.SEARCHMODE_NONE) { 2143 buf.append(getWhereClause(fvEqual)); 2144 } else { 2145 buf.append(getWhereEqualsSearchClause(fvEqual)); 2146 } 2147 2148 buf.append(" ) "); 2149 firstTermExists = true; 2150 } 2151 2152 if (!FieldValue.isNull(fvOrder) 2156 && (compareMode != Constants.COMPARE_NONE)) { 2157 buf.append(firstTermExists ? " AND ( " 2158 : ""); 2159 buf.append(getWhereAfterClause(fvOrder, compareMode)); 2160 buf.append(firstTermExists ? " ) " 2161 : ""); 2162 } 2163 2164 return buf.toString(); 2165 } 2166 2167 2168 2176 protected boolean checkFieldId(int type, 2177 int aid) { 2178 int i = aid / MAXFIELDS; 2179 return i == type; 2180 } 2181 2182 2183 2191 protected int decodeFieldId(int type, 2192 int aid) { 2193 return aid - (type * MAXFIELDS); 2194 } 2195 2196 2197 2205 protected int encodeFieldId(int type, 2206 int aid) { 2207 return (type * MAXFIELDS) + aid; 2208 } 2209 2210 2211 2218 private String getSQLExpression(FieldValue fv) { 2219 StringBuffer buf = new StringBuffer (); 2220 2221 Field f = fv.getField(); 2223 String fieldName = Util.isNull(f.getExpression()) ? f.getName() 2224 : f.getExpression(); 2225 buf.append(fieldName); 2226 2227 switch (fv.getOperator()) { 2229 case Constants.FILTER_EQUAL: 2230 buf.append(" = "); 2231 buf.append(" ? "); 2232 2233 break; 2234 2235 case Constants.FILTER_NOT_EQUAL: 2236 buf.append(" <> "); 2237 buf.append(" ? "); 2238 2239 break; 2240 2241 case Constants.FILTER_GREATER_THEN: 2242 buf.append(" > "); 2243 buf.append(" ? "); 2244 2245 break; 2246 2247 case Constants.FILTER_SMALLER_THEN: 2248 buf.append(" < "); 2249 buf.append(" ? "); 2250 2251 break; 2252 2253 case Constants.FILTER_GREATER_THEN_EQUAL: 2254 buf.append(" >= "); 2255 buf.append(" ? "); 2256 2257 break; 2258 2259 case Constants.FILTER_SMALLER_THEN_EQUAL: 2260 buf.append(" <= "); 2261 buf.append(" ? "); 2262 2263 break; 2264 2265 case Constants.FILTER_LIKE: 2266 2267 if (f.getType() == FieldTypes.CHAR) { 2268 buf.append(" LIKE "); 2269 } else { 2270 buf.append(" = "); 2271 } 2272 2273 buf.append(" ? "); 2274 2275 break; 2276 2277 case Constants.FILTER_NULL: 2278 buf.append(" IS NULL "); 2279 2280 break; 2281 2282 case Constants.FILTER_NOT_NULL: 2283 buf.append(" IS NOT NULL "); 2284 2285 break; 2286 2287 case Constants.FILTER_EMPTY: 2288 2289 if (f.getType() == FieldTypes.CHAR) { 2290 buf.append(" = '' "); 2291 buf.append(" OR "); 2292 } 2293 2294 buf.append(fieldName); 2295 buf.append(" IS NULL "); 2296 2297 break; 2298 2299 case Constants.FILTER_NOT_EMPTY: 2300 2301 if (f.getType() == FieldTypes.CHAR) { 2302 buf.append(" <> '' "); 2303 buf.append(" OR "); 2304 } 2305 2306 buf.append(fieldName); 2307 buf.append(" IS NOT NULL "); 2308 2309 break; 2310 } 2311 2312 return buf.toString(); 2313 } 2314 2315 2316 2358 private String getWhereAfterClause(FieldValue[] fv, 2359 int compareMode) { 2360 String conj; 2361 String disj; 2362 String opA1; 2363 String opA2; 2364 String opB1; 2365 String opB2; 2366 2367 if (compareMode == Constants.COMPARE_INCLUSIVE) { 2369 opA1 = ">="; 2370 opA2 = "<="; 2371 opB1 = ">"; 2372 opB2 = "<"; 2373 conj = " AND "; 2374 disj = " OR "; 2375 } else { 2376 opA1 = ">"; 2377 opA2 = "<"; 2378 opB1 = ">="; 2379 opB2 = "<="; 2380 conj = " OR "; 2381 disj = " AND "; 2382 } 2383 2384 StringBuffer buf = new StringBuffer (); 2385 2386 if ((fv != null) && (fv.length > 0)) { 2387 for (int i = 0; i < fv.length; i++) { 2389 buf.append("("); 2391 buf.append(fv[i].getField().getName()); 2392 buf.append((fv[i].getSortDirection() == Constants.ORDER_ASCENDING) 2393 ? opA1 2394 : opA2); 2395 2396 buf.append(" ? "); 2398 2399 if (i > 0) { 2402 for (int j = i - 1; j >= 0; j--) { 2403 buf.append(disj); 2404 buf.append(fv[j].getField().getName()); 2405 buf.append((fv[j].getSortDirection() == Constants.ORDER_ASCENDING) 2406 ? opB1 2407 : opB2); 2408 2409 buf.append(" ? "); 2411 } 2412 } 2413 2414 buf.append(" ) "); 2415 2416 if (i < (fv.length - 1)) { 2417 buf.append(conj); 2418 2419 } 2421 } 2422 } 2423 2424 return buf.toString(); 2425 } 2426 2427 2428 2471 private String getWhereEqualsSearchClause(FieldValue[] fv) { 2472 StringBuffer buf = new StringBuffer (); 2473 2474 if ((fv != null) && (fv.length > 0)) { 2475 int mode; 2476 int oldMode = -1; 2477 2478 for (int i = 0; i < fv.length; i++) { 2479 mode = fv[i].getSearchMode(); 2480 2481 if (oldMode != mode) { 2482 oldMode = mode; 2483 buf.append("("); 2484 2485 } 2487 2488 buf.append(getSQLExpression(fv[i])); 2490 2491 if ((i < (fv.length - 1)) && (fv[i + 1].getSearchMode() == mode)) { 2492 buf.append((mode == Constants.SEARCHMODE_AND) ? "AND " 2493 : "OR "); 2494 2495 } else { 2497 buf.append(")"); 2499 2500 if (i != (fv.length - 1)) { 2502 buf.append(" OR "); 2503 2504 } 2506 } 2507 } 2508 } 2509 2510 return buf.toString(); 2511 } 2512 2513 2514 2528 private Vector createOrderFVFromAttribute(String order) { 2529 Vector result = new Vector (); 2530 2531 if (order != null) { 2532 StringTokenizer st = new StringTokenizer (order, ","); 2533 2534 while (st.hasMoreTokens()) { 2535 String token = st.nextToken() 2537 .trim(); 2538 logCat.info("token = " + token); 2539 2540 int sortDirection = Constants.ORDER_ASCENDING; 2541 2542 int index = token.indexOf(" "); 2545 2546 if (index != -1) { 2549 String command = token.substring(index) 2550 .toUpperCase(); 2551 int pos = command.indexOf("ASC"); 2552 2553 if (pos == -1) { 2555 pos = command.indexOf("DESC"); 2556 2557 if (index != -1) { 2558 sortDirection = Constants.ORDER_DESCENDING; 2559 2560 } 2562 } 2563 } 2564 2565 String fieldName; 2566 2567 if (index == -1) { 2568 fieldName = token.trim(); 2569 } else { 2570 fieldName = token.substring(0, index) 2571 .trim(); 2572 } 2573 2574 Field f = this.getFieldByName(fieldName); 2575 2576 if (f != null) { 2577 FieldValue fv = FieldValue.createFieldValueForSorting(f, 2578 sortDirection); 2579 logCat.info("Field '" + fieldName + "' is ordered in mode:" 2580 + sortDirection); 2581 result.addElement(fv); 2582 } 2583 } 2584 } 2585 2586 return result; 2587 } 2588 2589 2590 2599 private Vector createOrderFVFromRequest(HttpServletRequest request, 2600 String paramStub, 2601 Vector sortFields) { 2602 Vector result = new Vector (); 2603 int fieldIndex = paramStub.length() + 1; 2604 2605 for (int i = 0; i < sortFields.size(); i++) { 2607 String dataParam = (String ) sortFields.elementAt(i); 2608 int fieldId = Integer.parseInt(dataParam.substring(fieldIndex)); 2609 String sortState = ParseUtil.getParameter(request, dataParam); 2610 logCat.info("### dataparam=" + dataParam); 2611 logCat.info("### fieldId=" + fieldId); 2612 logCat.info("### sortState=" + sortState); 2613 2614 if (sortState.equalsIgnoreCase("asc") 2615 || sortState.equalsIgnoreCase("desc")) { 2616 int sortDirection = sortState.equalsIgnoreCase("asc") 2617 ? Constants.ORDER_ASCENDING 2618 : Constants.ORDER_DESCENDING; 2619 FieldValue fv = FieldValue.createFieldValueForSorting(getField(fieldId), 2620 sortDirection); 2621 result.addElement(fv); 2622 } 2623 } 2624 2625 return result; 2626 } 2627 2628 2629 2632 2661 private String createToken(Field field, 2662 String fieldValue) { 2663 StringBuffer buf = new StringBuffer (); 2664 2665 buf.append(field.getId()); 2666 buf.append(":"); 2667 2668 if (!Util.isNull(fieldValue)) { 2669 buf.append(fieldValue.length()); 2670 buf.append(":"); 2671 buf.append(fieldValue); 2672 } else { 2673 buf.append(0); 2674 buf.append(":"); 2675 } 2676 2677 return buf.toString(); 2678 } 2679 2680 2681 2693 private int fillPreparedStatement(FieldValue cur, 2694 PreparedStatement ps, 2695 int curCol) 2696 throws SQLException { 2697 logCat.info("setting col " + curCol + " with name " 2698 + cur.getField().getName() + " to value " 2699 + cur.getFieldValue() + " of type " 2700 + cur.getField().getType() + " operator " + cur.getOperator()); 2701 2702 Field curField = cur.getField(); 2703 Object curValue = cur.getFieldValueAsObject(); 2704 2705 if (curField.getType() == FieldTypes.CHAR) { 2709 String valueStr = cur.getFieldValue(); 2710 2711 switch (cur.getSearchAlgorithm()) { 2712 case Constants.SEARCH_ALGO_WEAK_START: 2713 valueStr = '%' + valueStr; 2714 2715 break; 2716 2717 case Constants.SEARCH_ALGO_WEAK_END: 2718 valueStr = valueStr + '%'; 2719 2720 break; 2721 2722 case Constants.SEARCH_ALGO_WEAK_START_END: 2723 valueStr = '%' + valueStr + '%'; 2724 2725 break; 2726 } 2727 2728 curValue = valueStr; 2729 } 2730 2731 switch (cur.getOperator()) { 2732 case Constants.FILTER_NULL: 2733 break; 2734 2735 case Constants.FILTER_NOT_NULL: 2736 break; 2737 2738 case Constants.FILTER_EMPTY: 2739 break; 2740 2741 case Constants.FILTER_NOT_EMPTY: 2742 break; 2743 2744 default: 2745 JDBCDataHelper.fillWithData(ps, curField.getEscaper(), curCol, 2746 curValue, curField.getType(), 2747 getBlobHandlingStrategy()); 2748 curCol++; 2749 } 2750 2751 return curCol; 2752 } 2753 2754 2755 2760 private void initDefaultOrderFromKeys() { 2761 defaultOrder = new FieldValue[getKey() 2762 .size()]; 2763 2764 for (int i = 0; i < this.getKey() 2765 .size(); i++) { 2766 Field keyField = (Field) getKey() 2767 .elementAt(i); 2768 defaultOrder[i] = FieldValue.createFieldValueForSorting(keyField, 2769 Constants.ORDER_ASCENDING); 2770 } 2771 2772 logCat.info("Table.initDefaultOrderfromKey done."); 2773 } 2774 2775 2776 2790 private int populateWhereAfterClause(FieldValue[] fv, 2791 PreparedStatement ps, 2792 int curCol) 2793 throws SQLException { 2794 if ((fv != null) && (fv.length > 0)) { 2795 for (int i = 0; i < fv.length; i++) { 2797 curCol = fillPreparedStatement(fv[i], ps, curCol); 2799 2800 if (i > 0) { 2803 for (int j = i - 1; j >= 0; j--) { 2804 curCol = fillPreparedStatement(fv[j], ps, curCol); 2805 } 2806 } 2807 } 2808 } 2809 2810 return curCol; 2811 } 2812 2813 protected static DbFormsConfig getConfig() { 2814 DbFormsConfig config = null; 2815 try { 2816 config = DbFormsConfigRegistry.instance().lookup(); 2817 } catch (Exception e) { 2818 logCat.error("no config object", e); 2819 } 2820 return config; 2821 } 2822 2823} 2824 | Popular Tags |