1 23 package org.dbforms.taglib; 24 import org.apache.commons.lang.StringUtils; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.commons.validator.ValidatorResources; 28 29 import org.dbforms.config.Constants; 30 import org.dbforms.config.DbEventInterceptor; 31 import org.dbforms.config.DbEventInterceptorData; 32 import org.dbforms.config.DbFormsErrors; 33 import org.dbforms.config.Field; 34 import org.dbforms.config.FieldTypes; 35 import org.dbforms.config.FieldValue; 36 import org.dbforms.config.FieldValues; 37 import org.dbforms.config.GrantedPrivileges; 38 import org.dbforms.config.ResultSetVector; 39 import org.dbforms.config.Table; 40 41 import org.dbforms.event.NavEventFactory; 42 import org.dbforms.event.NavEventFactoryImpl; 43 import org.dbforms.event.NavigationEvent; 44 import org.dbforms.event.WebEvent; 45 import org.dbforms.event.eventtype.EventType; 46 47 import org.dbforms.util.MessageResources; 48 import org.dbforms.util.MessageResourcesInternal; 49 import org.dbforms.util.ParseUtil; 50 import org.dbforms.util.SqlUtil; 51 import org.dbforms.util.TimeUtil; 52 import org.dbforms.util.Util; 53 54 import org.dbforms.validation.DbFormsValidatorUtil; 55 import org.dbforms.validation.ValidatorConstants; 56 57 import java.io.IOException ; 58 import java.io.PrintWriter ; 59 import java.io.StringWriter ; 60 61 import java.sql.Connection ; 62 import java.sql.SQLException ; 63 64 import java.util.ArrayList ; 65 import java.util.Date ; 66 import java.util.Enumeration ; 67 import java.util.Hashtable ; 68 import java.util.Locale ; 69 import java.util.Map ; 70 import java.util.StringTokenizer ; 71 import java.util.Vector ; 72 73 import javax.servlet.http.HttpServletRequest ; 74 import javax.servlet.http.HttpServletResponse ; 75 import javax.servlet.jsp.JspException ; 76 import javax.servlet.jsp.JspWriter ; 77 import javax.servlet.jsp.tagext.Tag ; 78 import javax.servlet.jsp.tagext.TryCatchFinally ; 79 80 81 82 87 public class DbFormTag extends TagSupportWithScriptHandler 88 implements TryCatchFinally { 89 90 private static Log logCat = LogFactory.getLog(DbFormTag.class.getName()); 91 92 93 private static NavEventFactory navEventFactory = NavEventFactoryImpl 94 .instance(); 95 96 97 private transient DbFormTag parentForm; 98 private transient FieldValues orderFields = null; 99 100 101 102 103 private transient Hashtable childFieldNames = new Hashtable (); 104 105 106 private transient Hashtable fieldNames; 107 private transient Hashtable gotoHt = null; 108 109 110 private transient Hashtable javascriptDistinctFunctions = new Hashtable (); 111 private transient Hashtable validationFields; 112 113 116 private Locale locale = null; 117 118 119 private transient ResultSetVector resultSetVector; 120 121 122 private String action; 123 124 132 private String autoUpdate = "false"; 133 134 135 private String autocomplete = null; 136 137 138 private String captionResource = "false"; 139 140 144 private String childField; 145 private String dbConnectionName = null; 146 147 148 private String filter; 149 150 154 private String followUp; 155 156 161 private String followUpOnError; 162 163 164 private String formValidatorName; 165 private String gotoPrefix; 166 167 168 private String javascriptFieldsArray = "false"; 169 170 171 private String javascriptValidation = "false"; 172 173 178 private String javascriptValidationSrcFile; 179 private String localWebEvent; 180 181 182 private String maxRows; 183 184 185 private String multipart; 186 187 188 private String name = null; 189 190 191 private String onSubmit; 192 193 194 private String orderBy; 195 196 197 private String overrideFieldCheck = null; 198 199 203 private String parentField; 204 205 209 private String positionPath; 210 211 215 private String positionPathCore; 216 217 218 private String readOnly = "false"; 219 220 221 private String redisplayFieldsOnError = "false"; 222 223 224 private String sqlFilter = null; 225 226 227 private String tableList; 228 229 230 private String tableName; 231 232 236 private String target; 237 238 239 private String whereClause; 240 241 242 private StringBuffer childElementOutput; 243 244 245 private transient Table table; 246 private Vector overrulingOrderFields; 247 248 249 private Vector validationForms; 250 251 252 private transient WebEvent webEvent = null; 253 254 258 private transient FieldValue[] childFieldValues; 259 260 264 private boolean footerReached = false; 265 266 267 private boolean isSubForm = false; 268 269 273 private int count; 274 275 279 private int currentCount = 0; 280 281 282 private int tableId = -1; 283 284 289 public void setAction(java.lang.String newAction) { 290 action = newAction; 291 } 292 293 294 299 public java.lang.String getAction() { 300 return action; 301 } 302 303 304 309 public void setAutoUpdate(String autoUpdate) { 310 this.autoUpdate = autoUpdate; 311 } 312 313 314 319 public String getAutoUpdate() { 320 return autoUpdate; 321 } 322 323 324 329 public void setAutocomplete(String string) { 330 autocomplete = string; 331 } 332 333 334 339 public String getAutocomplete() { 340 return autocomplete; 341 } 342 343 344 349 public void setCaptionResource(String res) { 350 this.captionResource = res; 351 } 352 353 354 359 public void setChildField(String childField) { 360 this.childField = childField; 361 } 362 363 364 369 public String getChildField() { 370 return childField; 371 } 372 373 374 379 public int getCount() { 380 return count; 381 } 382 383 384 389 public int getCurrentCount() { 390 return currentCount; 391 } 392 393 394 399 public void setDbConnectionName(String dbConnectionName) { 400 this.dbConnectionName = dbConnectionName; 401 } 402 403 404 409 public String getDbConnectionName() { 410 return dbConnectionName; 411 } 412 413 414 419 public void setFilter(String filter) { 420 this.filter = filter; 421 } 422 423 424 429 public String getFilter() { 430 return filter; 431 } 432 433 434 439 public void setFollowUp(String followUp) { 440 this.followUp = followUp; 441 } 442 443 444 449 public String getFollowUp() { 450 return followUp; 451 } 452 453 454 459 public void setFollowUpOnError(String followUpOnError) { 460 this.followUpOnError = followUpOnError; 461 } 462 463 464 469 public String getFollowUpOnError() { 470 return followUpOnError; 471 } 472 473 474 479 public void setFooterReached(boolean footerReached) { 480 this.footerReached = footerReached; 481 } 482 483 484 489 public boolean isFooterReached() { 490 return footerReached; 491 } 492 493 494 499 public void setFormValidatorName(String fv) { 500 this.formValidatorName = fv; 501 } 502 503 504 509 public String getFormValidatorName() { 510 return formValidatorName; 511 } 512 513 514 519 public void setGotoHt(Hashtable gotoHt) { 520 this.gotoHt = gotoHt; 521 } 522 523 524 529 public Hashtable getGotoHt() { 530 return gotoHt; 531 } 532 533 534 539 public void setGotoPrefix(String gotoPrefix) { 540 this.gotoPrefix = gotoPrefix; 541 } 542 543 544 549 public String getGotoPrefix() { 550 return gotoPrefix; 551 } 552 553 554 559 public void setJavascriptFieldsArray(String jfa) { 560 this.javascriptFieldsArray = jfa; 561 } 562 563 564 569 public void setJavascriptValidation(String jsv) { 570 this.javascriptValidation = jsv; 571 } 572 573 574 579 public void setJavascriptValidationSrcFile(String jsvs) { 580 this.javascriptValidationSrcFile = jsvs; 581 } 582 583 584 589 public String getJavascriptValidationSrcFile() { 590 return javascriptValidationSrcFile; 591 } 592 593 594 599 public void setLocalWebEvent(String localWebEvent) { 600 this.localWebEvent = localWebEvent; 601 } 602 603 604 609 public String getLocalWebEvent() { 610 return localWebEvent; 611 } 612 613 614 620 public Locale getLocale() { 621 return locale; 622 } 623 624 625 630 public void setMaxRows(String maxRows) { 631 this.maxRows = maxRows; 632 633 if (maxRows.trim().equals("*")) { 634 this.count = 0; 635 } else { 636 this.count = Integer.parseInt(maxRows); 637 } 638 } 639 640 641 646 public String getMaxRows() { 647 return maxRows; 648 } 649 650 651 656 public void setMultipart(String value) { 657 this.multipart = value; 658 } 659 660 661 666 public void setName(String string) { 667 name = string; 668 } 669 670 671 676 public String getName() { 677 return (name != null) ? name : tableName; 678 } 679 680 681 686 public void setOnSubmit(String newonSubmit) { 687 onSubmit = newonSubmit; 688 } 689 690 691 696 public java.lang.String getOnSubmit() { 697 return onSubmit; 698 } 699 700 701 706 public void setOrderBy(String orderBy) { 707 this.orderBy = orderBy; 708 } 709 710 711 716 public String getOrderBy() { 717 return orderBy; 718 } 719 720 721 726 public FieldValues getOrderFields() { 727 return orderFields; 728 } 729 730 731 736 public void setOverrideFieldCheck(String string) { 737 overrideFieldCheck = string; 738 } 739 740 741 746 public String getOverrideFieldCheck() { 747 return overrideFieldCheck; 748 } 749 750 751 756 public void setParent(Tag p) { 757 super.setParent(p); 758 this.parentForm = (DbFormTag) findAncestorWithClass(this, DbFormTag.class); 759 } 760 761 762 767 public void setParentField(String parentField) { 768 this.parentField = parentField; 769 } 770 771 772 777 public String getParentField() { 778 return parentField; 779 } 780 781 782 787 public String getPositionPath() { 788 return positionPath; 789 } 790 791 792 797 798 799 public String getPositionPathCore() { 800 return positionPathCore; 801 } 802 803 804 809 public void setReadOnly(String readOnly) { 810 this.readOnly = readOnly; 811 } 812 813 814 819 public void setRedisplayFieldsOnError( 820 java.lang.String newRedisplayFieldsOnError) { 821 redisplayFieldsOnError = newRedisplayFieldsOnError; 822 } 823 824 825 830 public ResultSetVector getResultSetVector() { 831 return resultSetVector; 832 } 833 834 835 840 public void setSqlFilter(String string) { 841 sqlFilter = string; 842 } 843 844 845 850 public String getSqlFilter() { 851 return sqlFilter; 852 } 853 854 855 860 public boolean isSubForm() { 861 return isSubForm; 862 } 863 864 865 870 public Table getTable() { 871 return table; 872 } 873 874 875 880 public void setTableList(String tableList) { 881 this.tableList = tableList; 882 } 883 884 885 890 public String getTableList() { 891 return tableList; 892 } 893 894 895 902 public void setTableName(String tableName) throws Exception { 903 this.tableName = tableName; 904 this.table = getConfig().getTableByName(tableName); 905 906 if (getTable() == null) { 907 throw new Exception ("Table " + tableName 908 + " not found in configuration"); 909 } 910 911 this.tableId = getTable().getId(); 912 } 913 914 915 920 public String getTableName() { 921 return tableName; 922 } 923 924 925 930 public void setTarget(String target) { 931 this.target = target; 932 } 933 934 935 940 public String getTarget() { 941 return target; 942 } 943 944 945 950 public WebEvent getWebEvent() { 951 return webEvent; 952 } 953 954 955 960 public void setWhereClause(String wc) { 961 this.whereClause = wc; 962 } 963 964 965 970 public String getWhereClause() { 971 return whereClause; 972 } 973 974 975 982 public void addChildName(String tableFieldName, String dbFormGeneratedName) { 983 childFieldNames.put(dbFormGeneratedName, tableFieldName); 984 } 985 986 987 995 public void addJavascriptFunction(String jsFctName, StringBuffer jsFct) { 996 if (!existJavascriptFunction(jsFctName)) { 997 javascriptDistinctFunctions.put(jsFctName, jsFct); 998 } 999 } 1000 1001 1002 1008 public void addValidationForm(String formName, Hashtable childFields) { 1009 if (validationForms == null) { 1010 validationForms = new Vector(); 1011 } 1012 1013 validationForms.add(formName); 1014 1015 if (validationFields == null) { 1016 validationFields = new Hashtable (); 1017 } 1018 1019 validationFields.putAll(childFields); 1020 } 1021 1022 1023 1028 public void appendToChildElementOutput(String str) { 1029 if (!Util.isNull(str)) { 1030 this.childElementOutput.append(str); 1031 } 1032 } 1033 1034 1035 1042 public int doAfterBody() throws JspException { 1043 if (ResultSetVector.isNull(resultSetVector)) { 1044 setFooterReached(true); 1045 1046 return SKIP_BODY; 1047 } 1048 1049 boolean renderPage = !isFooterReached(); 1050 1051 currentCount++; 1052 1053 int pCount = getCount(); 1055 logCat.info("we are talking about=" + getTableName() + " pcount=" 1056 + pCount + " pcurrent=" + currentCount); 1057 1058 if (isSubForm() && !isFooterReached()) { 1061 appendToChildElementOutput(produceLinkedTags()); } 1063 1064 if (((pCount != -1) && (currentCount == pCount)) || (ResultSetVector.isNull(getResultSetVector())) 1066 || (getResultSetVector().isLast())) { 1067 logCat.info("setting footerreached to true"); 1068 setFooterReached(true); } 1070 1071 getResultSetVector().moveNext(); 1072 1073 if (renderPage) { 1076 return EVAL_BODY_BUFFERED; 1077 } else { 1078 return SKIP_BODY; 1079 } 1080 } 1081 1082 1083 1090 public void doCatch(Throwable t) throws Throwable { 1091 StringWriter sw = new StringWriter (); 1092 PrintWriter pw = new PrintWriter (sw); 1093 t.printStackTrace(pw); 1094 logCat.error("DbFormTag.doCatch called - " + t.toString() + "\n" 1095 + sw.toString()); 1096 throw t; 1097 } 1098 1099 1100 1107 public int doEndTag() throws JspException { 1108 JspWriter jspOut = pageContext.getOut(); 1109 1110 try { 1112 if (bodyContent != null) { 1113 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 1114 bodyContent.clearBody(); 1115 1116 } 1118 1119 logCat.debug("pageContext.getOut()=" + pageContext.getOut()); 1120 logCat.debug("childElementOutput=" + childElementOutput); 1121 1122 if (childElementOutput != null) { 1125 jspOut.println(childElementOutput.toString()); 1126 } 1127 1128 if (parentForm == null) { 1129 jspOut.println("</form>"); 1130 } 1131 1132 1133 if (!Util.isNull(getFormValidatorName()) 1134 && hasJavascriptValidationSet()) { 1135 jspOut.println(generateJavascriptValidation()); 1136 } 1137 1138 1144 if (hasJavascriptFieldsArraySet()) { 1145 jspOut.println(generateJavascriptFieldsArray()); 1146 } 1147 1148 1149 if (javascriptDistinctFunctions.size() > 0) { 1150 jspOut.println("\n<SCRIPT language=\"javascript\">\n"); 1151 1152 Enumeration e = javascriptDistinctFunctions.keys(); 1153 1154 while (e.hasMoreElements()) { 1155 String aKey = (String ) e.nextElement(); 1156 StringBuffer sbFonction = (StringBuffer ) javascriptDistinctFunctions 1157 .get(aKey); 1158 1159 jspOut.println(sbFonction); 1160 } 1161 1162 jspOut.println("\n</SCRIPT>\n"); 1163 } 1164 } catch (IOException ioe) { 1165 logCat.error("::doEndTag - IOException", ioe); 1166 } 1167 1168 logCat.info("end reached of " + tableName); 1169 1170 return EVAL_PAGE; 1171 } 1172 1173 1174 1177 public void doFinally() { 1178 logCat.info("doFinally called"); 1179 1180 if (validationForms != null) { 1181 validationForms.clear(); 1182 } 1183 1184 if (validationFields != null) { 1185 validationFields.clear(); 1186 } 1187 1188 if (fieldNames != null) { 1189 fieldNames.clear(); 1190 } 1191 1192 if (childFieldNames != null) { 1193 childFieldNames.clear(); 1194 } 1195 1196 sqlFilter = null; 1197 orderFields = null; 1198 overrideFieldCheck = null; 1199 } 1200 1201 1202 1220 public int doStartTag() { 1221 try { 1222 Connection con = getConfig().getConnection(dbConnectionName); 1223 1224 HttpServletRequest request = (HttpServletRequest ) pageContext 1228 .getRequest(); 1229 HttpServletResponse response = (HttpServletResponse ) pageContext 1230 .getResponse(); 1231 1232 locale = MessageResources.getLocale(request); 1233 1234 logCat.info("servlet path = " + request.getServletPath()); 1235 logCat.info("servlet getPathInfo = " + request.getPathInfo()); 1236 1237 logCat.info("servlet getContextPath = " + request.getContextPath()); 1238 logCat.info("servlet getRequestURI = " + request.getRequestURI()); 1239 1240 String strFollowUp = getFollowUp(); 1241 1242 if (Util.isNull(strFollowUp)) { 1243 strFollowUp = request.getRequestURI(); 1244 1245 String contextPath = request.getContextPath(); 1246 1247 if (!Util.isNull(contextPath)) { 1248 strFollowUp = strFollowUp.substring(contextPath.length(), 1249 strFollowUp.length()); 1250 } 1251 1252 if (!Util.isNull(request.getQueryString())) { 1253 strFollowUp += ("?" + request.getQueryString()); 1254 } 1255 } 1256 1257 logCat.debug("pos1"); 1258 1259 JspWriter out = pageContext.getOut(); 1261 1262 logCat.debug("pos2"); 1263 1264 if ((getTable() != null) 1266 && !getTable().hasUserPrivileg(request, 1267 GrantedPrivileges.PRIVILEG_SELECT)) { 1268 logCat.debug("pos3"); 1269 1270 String str = MessageResourcesInternal.getMessage("dbforms.events.view.nogrant", 1271 getLocale(), new String [] { 1272 getTable().getName() 1273 }); 1274 logCat.warn(str); 1275 out.println(str); 1276 1277 return SKIP_BODY; 1278 } 1279 1280 logCat.debug("pos4"); 1281 1282 DbEventInterceptorData interceptorData = new DbEventInterceptorData(request, 1283 getConfig(), con, getTable()); 1284 interceptorData.setAttribute(DbEventInterceptorData.CONNECTIONNAME, 1285 dbConnectionName); 1286 interceptorData.setAttribute(DbEventInterceptorData.PAGECONTEXT, 1287 pageContext); 1288 1289 if ((getTable() != null) && getTable().hasInterceptors()) { 1291 try { 1292 logCat.debug("pos5"); 1293 getTable().processInterceptors(DbEventInterceptor.PRE_SELECT, 1294 interceptorData); 1295 } catch (Exception sqle) { 1296 logCat.error("pos6"); 1297 logCat.error(sqle.getMessage(), sqle); 1298 out.println(sqle.getMessage()); 1299 1300 return SKIP_BODY; 1301 } 1302 } 1303 1304 logCat.debug("pos7"); 1305 1306 StringBuffer tagBuf = new StringBuffer (); 1312 1313 logCat.info("resetting values of tag"); 1318 currentCount = 0; 1319 footerReached = false; 1320 resultSetVector = null; 1321 childElementOutput = new StringBuffer (); 1322 logCat.debug("first steps finished"); 1323 1324 if (parentForm == null) { 1327 tagBuf.append("<form "); 1328 1329 if (!Util.isNull(getId())) { 1330 tagBuf.append("id=\""); 1331 tagBuf.append(getId()); 1332 tagBuf.append("\" "); 1333 } 1334 1335 tagBuf.append("name=\"dbform\" action=\""); 1336 1337 if ((this.getAction() != null) 1339 && (this.getAction().trim().length() > 0)) { 1340 tagBuf.append(this.getAction()); 1341 } else { 1342 tagBuf.append(response.encodeURL(request.getContextPath() 1343 + "/servlet/control")); 1344 } 1345 1346 tagBuf.append("\""); 1347 1348 if (getAutocomplete() != null) { 1350 tagBuf.append(" autocomplete=\"" + getAutocomplete() + "\""); 1351 } 1352 1353 if (target != null) { 1355 tagBuf.append(" target=\""); 1356 tagBuf.append(target); 1357 tagBuf.append("\""); 1358 } 1359 1360 tagBuf.append(" method=\"post\""); 1361 1362 if (hasMultipartSet()) { 1363 tagBuf.append(" enctype=\"multipart/form-data\""); 1364 } 1365 1366 String validationFct = null; 1367 1368 if (hasJavascriptValidationSet()) { 1369 validationFct = getFormValidatorName(); 1370 1371 if (!Util.isNull(validationFct)) { 1372 validationFct = Character.toUpperCase(validationFct.charAt( 1373 0)) 1374 + validationFct.substring(1, validationFct.length()); 1375 validationFct = "validate" + validationFct + "(this)"; 1376 } 1377 } 1378 1379 if (!Util.isNull(validationFct) && !Util.isNull(getOnSubmit())) { 1380 boolean found = false; 1381 String s = getOnSubmit(); 1382 String [] cmds = StringUtils.split(s, ';'); 1383 1384 for (int i = 0; i < cmds.length; i++) { 1385 cmds[i] = cmds[i].trim(); 1386 1387 if (cmds[i].startsWith("return")) { 1388 cmds[i] = cmds[i].substring("return".length()); 1389 cmds[i] = "return " + validationFct + " && " + cmds[i]; 1390 found = true; 1391 1392 break; 1393 } 1394 } 1395 1396 s = ""; 1397 1398 for (int i = 0; i < cmds.length; i++) { 1399 s = s + cmds[i] + ";"; 1400 } 1401 1402 if (!found) { 1403 s = s + "return " + validationFct + ";"; 1404 } 1405 1406 tagBuf.append(" onSubmit=\""); 1407 tagBuf.append(s); 1408 tagBuf.append("\" "); 1409 } else if (!Util.isNull(validationFct)) { 1410 tagBuf.append(" onSubmit=\""); 1411 tagBuf.append("return " + validationFct); 1412 tagBuf.append("\" "); 1413 } else if (!Util.isNull(getOnSubmit())) { 1414 tagBuf.append(" onSubmit=\""); 1415 tagBuf.append(getOnSubmit()); 1416 tagBuf.append("\" "); 1417 } 1418 1419 tagBuf.append(">"); 1420 1421 if (tableName == null) { 1424 appendSource(request, tagBuf); 1425 tagBuf.append("<input type=\"hidden\" name=\"fu_" + tableId 1426 + "\" value=\"" + strFollowUp + "\"/>"); 1427 1428 out.println(tagBuf.toString()); 1431 1432 return EVAL_BODY_BUFFERED; 1433 } 1434 1435 positionPathCore = "root"; 1436 } else { 1437 this.isSubForm = true; 1440 positionPathCore = parentForm.getPositionPath(); 1441 1442 if (Util.isNull(getWhereClause())) { 1445 initChildFieldValues(); 1446 1447 if (childFieldValues == null) { 1448 return SKIP_BODY; 1449 } 1450 } 1451 } 1452 1453 tagBuf.append("<input type=\"hidden\" name=\"invtable\" value=\"" 1455 + tableId + "\"/>"); 1456 1457 tagBuf.append("<input type='hidden' name='invname_" + tableId 1459 + "' value='" 1460 + (Util.isNull(dbConnectionName) ? "" : dbConnectionName) + "'/>"); 1461 1462 tagBuf.append("<input type=\"hidden\" name=\"autoupdate_" + tableId 1464 + "\" value=\"" + autoUpdate + "\"/>"); 1465 1466 tagBuf.append("<input type=\"hidden\" name=\"fu_" + tableId 1468 + "\" value=\"" + strFollowUp + "\"/>"); 1469 1470 tagBuf.append("<input type=\"hidden\" name=\"lang" + "\" value=\"" 1472 + locale.getLanguage() + "\"/>"); 1473 tagBuf.append("<input type=\"hidden\" name=\"country" + "\" value=\"" 1474 + locale.getCountry() + "\"/>"); 1475 1476 if (!Util.isNull(getFollowUpOnError())) { 1478 tagBuf.append("<input type=\"hidden\" name=\"fue_" + tableId 1479 + "\" value=\"" + getFollowUpOnError() + "\"/>"); 1480 } 1481 1482 if (!Util.isNull(getOverrideFieldCheck())) { 1483 tagBuf.append("<input type=\"hidden\" name=\"" 1484 + Constants.FIELDNAME_OVERRIDEFIELDTEST + tableId 1485 + "\" value=\"" + getOverrideFieldCheck() + "\"/>"); 1486 } 1487 1488 if (!Util.isNull(getFormValidatorName())) { 1490 tagBuf.append("<input type=\"hidden\" name=\"" 1491 + ValidatorConstants.FORM_VALIDATOR_NAME + "_" + tableId 1492 + "\" value=\"" + getFormValidatorName() + "\"/>"); 1493 } 1494 1495 appendSource(request, tagBuf); 1496 1497 tagBuf.append("<input type=\"hidden\" name=\"customEvent\"/>"); 1499 1500 String sqlFilterString = ""; 1508 String requestSqlFilterString = DbFilterTag.getSqlFilter(request, 1509 this.getTable().getId()); 1510 FieldValue[] sqlFilterParams = null; 1511 1512 if (!Util.isNull(getSqlFilter()) 1513 && !Util.isNull(requestSqlFilterString)) { 1514 sqlFilterString = " ( " + requestSqlFilterString + " ) AND ( " 1515 + getSqlFilter() + " ) "; 1516 } else if (!Util.isNull(getSqlFilter())) { 1517 sqlFilterString = getSqlFilter(); 1518 } else if (!Util.isNull(requestSqlFilterString)) { 1519 sqlFilterString = requestSqlFilterString; 1520 } 1521 1522 logCat.debug("filter to apply : " + sqlFilterString); 1523 1524 if (!Util.isNull(requestSqlFilterString)) { 1525 sqlFilterParams = DbFilterTag.getSqlFilterParams(request, 1526 this.getTable().getId()); 1527 } 1528 1529 FieldValue[] overrulingOrder = initOverrulingOrder(request); 1532 1533 FieldValue[] orderConstraint; 1540 1541 if ((overrulingOrder != null) && (overrulingOrder.length > 0)) { 1543 orderConstraint = overrulingOrder; 1544 logCat.info("using OverrulingOrder (dbform tag attribute)"); 1545 } 1546 else { 1549 FieldValue[] tmpOrderConstraint = getTable().getDefaultOrder(); 1550 orderConstraint = new FieldValue[tmpOrderConstraint.length]; 1551 1552 for (int i = 0; i < tmpOrderConstraint.length; i++) { 1555 orderConstraint[i] = (FieldValue) tmpOrderConstraint[i].clone(); 1556 } 1557 1558 logCat.info("using DefaultOrder (from config file)"); 1559 } 1560 1561 if (orderConstraint == null) { 1563 throw new IllegalArgumentException ( 1564 "OrderBy-Clause must be specified either in table-element in config.xml or in dbform-tag on jsp view"); 1565 } 1566 1567 FieldValue[] filterFieldValues = null; 1568 1569 if (!Util.isNull(filter)) { 1570 filterFieldValues = getTable().getFilterFieldArray(filter, locale); 1571 } 1572 1573 FieldValue[] mergedFieldValues = null; 1574 1575 if (childFieldValues == null) { 1576 mergedFieldValues = filterFieldValues; 1577 } else if (filterFieldValues == null) { 1578 mergedFieldValues = childFieldValues; 1579 } else { 1580 mergedFieldValues = new FieldValue[childFieldValues.length 1581 + filterFieldValues.length]; 1582 System.arraycopy(childFieldValues, 0, mergedFieldValues, 0, 1583 childFieldValues.length); 1584 System.arraycopy(filterFieldValues, 0, mergedFieldValues, 1585 childFieldValues.length, filterFieldValues.length); 1586 } 1587 1588 FieldValue[] searchFieldValues = initSearchFieldValues(); 1591 1592 if (searchFieldValues != null) { 1593 mergedFieldValues = searchFieldValues; 1594 } 1595 1596 String firstPosition = Util.decode(ParseUtil.getParameter(request, 1602 "firstpos_" + tableId), 1603 pageContext.getRequest().getCharacterEncoding()); 1604 String lastPosition = Util.decode(ParseUtil.getParameter(request, 1605 "lastpos_" + tableId), 1606 pageContext.getRequest().getCharacterEncoding()); 1607 1608 if (firstPosition == null) { 1609 firstPosition = lastPosition; 1610 } 1611 1612 if (lastPosition == null) { 1613 lastPosition = firstPosition; 1614 } 1615 1616 if ((childFieldValues != null) && (firstPosition != null)) { 1623 if (!checkLinkage(childFieldValues, firstPosition)) { 1624 firstPosition = null; 1627 lastPosition = null; 1628 } 1629 } 1630 1631 logCat.info("firstposition " + firstPosition); 1632 logCat.info("lastPosition " + lastPosition); 1633 1634 1685 webEvent = (WebEvent) request.getAttribute("webEvent"); 1694 1695 if (webEvent != null) { 1698 webEvent.setRequest(request); 1699 } 1700 1701 if (((webEvent == null) 1708 || webEvent instanceof org.dbforms.event.NoopEvent) 1709 && (getLocalWebEvent() != null)) { 1710 webEvent = navEventFactory.createEvent(localWebEvent, request, 1711 getConfig(), getTable()); 1712 1713 if (webEvent != null) { 1715 request.setAttribute("webEvent", webEvent); 1716 } 1717 } 1718 1719 NavigationEvent navEvent = null; 1721 1722 if ((webEvent != null) && webEvent instanceof NavigationEvent) { 1726 navEvent = (NavigationEvent) webEvent; 1727 1728 if ((navEvent.getTable() == null) 1729 || (navEvent.getTable().getId() != tableId)) { 1730 navEvent = null; 1737 } 1738 } 1739 1740 if (navEvent == null) { 1744 if (!Util.isNull(gotoPrefix)) { 1746 logCat.info("§§§§ NAV GOTO §§§§"); 1747 1748 Vector v = ParseUtil.getParametersStartingWith(request, 1749 gotoPrefix); 1750 gotoHt = new Hashtable (); 1751 1752 for (int i = 0; i < v.size(); i++) { 1753 String paramName = (String ) v.elementAt(i); 1754 String fieldName = paramName.substring(gotoPrefix.length()); 1755 logCat.debug("fieldName=" + fieldName); 1756 1757 String fieldValue = ParseUtil.getParameter(request, paramName); 1758 logCat.debug("fieldValue=" + fieldValue); 1759 1760 if ((fieldName != null) && (fieldValue != null)) { 1761 gotoHt.put(fieldName, fieldValue); 1762 } 1763 } 1764 } 1765 1766 if ((gotoHt != null) && (gotoHt.size() > 0)) { 1768 String positionString = getTable().getPositionString(gotoHt); 1769 navEvent = navEventFactory.createGotoEvent(getTable(), request, 1770 getConfig(), positionString); 1771 } 1772 } 1773 1774 if (navEvent == null) { 1780 Vector errors = (Vector) request.getAttribute("errors"); 1794 1795 if ((webEvent != null) 1796 && EventType.EVENT_DATABASE_INSERT.equals( 1797 webEvent.getType()) && (errors != null) 1798 && (errors.size() > 0) 1799 && (webEvent.getTable().getId() == getTable().getId())) { 1800 navEvent = null; 1802 resultSetVector = null; 1803 setFooterReached(true); 1804 } else if (!Util.isNull(getWhereClause())) { 1805 navEvent = navEventFactory.createGotoEvent(getTable(), request, 1807 getConfig(), whereClause, getTableList()); 1808 } else { 1809 String myPosition = (count == 0) ? null : firstPosition; 1810 1811 if ((webEvent != null) 1812 && (webEvent instanceof org.dbforms.event.NoopEvent)) { 1815 myPosition = null; 1816 } 1817 1818 navEvent = navEventFactory.createGotoEvent(getTable(), request, 1819 getConfig(), myPosition); 1820 } 1821 } 1822 1823 logCat.info("§§§ NAV/I §§§"); 1825 1826 if (navEvent != null) { 1827 logCat.info("about to process nav event:" 1828 + navEvent.getClass().getName()); 1829 resultSetVector = navEvent.processEvent(mergedFieldValues, 1830 orderConstraint, sqlFilterString, sqlFilterParams, count, 1831 firstPosition, lastPosition, interceptorData); 1832 1833 if (ResultSetVector.isNull(resultSetVector)) { 1834 setFooterReached(true); 1835 } 1836 1837 orderFields = new FieldValues(orderConstraint); 1838 } 1839 1840 if ((getTable() != null) && getTable().hasInterceptors()) { 1854 try { 1856 interceptorData.setAttribute(DbEventInterceptorData.RESULTSET, 1857 resultSetVector); 1858 getTable().processInterceptors(DbEventInterceptor.POST_SELECT, 1859 interceptorData); 1860 } catch (SQLException sqle) { 1861 SqlUtil.logSqlException(sqle); 1866 throw sqle; 1867 } 1868 } 1869 1870 if (!ResultSetVector.isNull(resultSetVector)) { 1874 resultSetVector.moveFirst(); 1875 firstPosition = getTable().getPositionString(resultSetVector); 1876 resultSetVector.moveLast(); 1877 lastPosition = getTable().getPositionString(resultSetVector); 1878 resultSetVector.moveFirst(); 1879 } 1880 1881 if (!footerReached) { 1882 if (firstPosition != null) { 1884 tagBuf.append("<input type=\"hidden\" name=\"firstpos_" 1885 + tableId + "\" value=\"" 1886 + Util.encode(firstPosition, 1887 pageContext.getRequest().getCharacterEncoding()) + "\"/>"); 1888 } 1889 1890 if (lastPosition != null) { 1891 tagBuf.append("<input type=\"hidden\" name=\"lastpos_" + tableId 1892 + "\" value=\"" 1893 + Util.encode(lastPosition, 1894 pageContext.getRequest().getCharacterEncoding()) + "\"/>"); 1895 } 1896 } 1897 1898 if (!isSubForm) { 1899 pageContext.setAttribute("dbforms", new Hashtable ()); 1900 } 1901 1902 Map dbforms = (Map ) pageContext.getAttribute("dbforms"); 1903 DbFormContext dbContext = new DbFormContext(getTable() 1904 .getNamesHashtable(Constants.FIELDNAME_SEARCH), 1905 getTable().getNamesHashtable(Constants.FIELDNAME_SEARCHMODE), 1906 getTable().getNamesHashtable(Constants.FIELDNAME_SEARCHALGO), 1907 resultSetVector); 1908 1909 if (!ResultSetVector.isNull(resultSetVector)) { 1910 dbContext.setCurrentRow(resultSetVector.getCurrentRowAsMap()); 1911 dbContext.setPosition(Util.encode(getTable().getPositionString(resultSetVector), 1912 pageContext.getRequest().getCharacterEncoding())); 1913 } 1914 1915 dbforms.put(getName(), dbContext); 1916 1917 pageContext.setAttribute("searchFieldNames_" 1921 + tableName.replace('.', '_'), 1922 getTable().getNamesHashtable(Constants.FIELDNAME_SEARCH)); 1923 pageContext.setAttribute("searchFieldModeNames_" 1924 + tableName.replace('.', '_'), 1925 getTable().getNamesHashtable(Constants.FIELDNAME_SEARCHMODE)); 1926 pageContext.setAttribute("searchFieldAlgorithmNames_" 1927 + tableName.replace('.', '_'), 1928 getTable().getNamesHashtable(Constants.FIELDNAME_SEARCHALGO)); 1929 1930 if (!ResultSetVector.isNull(resultSetVector)) { 1937 pageContext.setAttribute("rsv_" + tableName.replace('.', '_'), 1938 resultSetVector); 1939 pageContext.setAttribute("currentRow_" 1940 + tableName.replace('.', '_'), 1941 resultSetVector.getCurrentRowAsMap()); 1942 pageContext.setAttribute("position_" + tableName.replace('.', '_'), 1943 Util.encode(getTable().getPositionString(resultSetVector), 1944 pageContext.getRequest().getCharacterEncoding())); 1945 } 1946 1947 out.println(tagBuf.toString()); 1948 SqlUtil.closeConnection(con); 1949 } catch (IOException e) { 1950 logCat.error("::doStartTag - IOException", e); 1951 1952 return SKIP_BODY; 1953 } catch (SQLException ne) { 1954 SqlUtil.logSqlException(ne); 1955 1956 return SKIP_BODY; 1957 } 1958 1959 return EVAL_BODY_BUFFERED; 1960 } 1961 1962 1963 1970 public boolean existJavascriptFunction(String jsFctName) { 1971 return javascriptDistinctFunctions.containsKey(jsFctName); 1972 } 1973 1974 1975 1980 public boolean hasCaptionResourceSet() { 1981 return Util.getTrue(captionResource); 1982 } 1983 1984 1985 1990 public boolean hasJavascriptFieldsArraySet() { 1991 return Util.getTrue(javascriptFieldsArray); 1992 } 1993 1994 1995 2000 public boolean hasJavascriptValidationSet() { 2001 return Util.getTrue(javascriptValidation); 2002 } 2003 2004 2005 2010 public boolean hasMultipartSet() { 2011 return Util.getTrue(multipart); 2012 } 2013 2014 2015 2020 public boolean hasReadOnlySet() { 2021 return Util.getTrue(readOnly); 2022 } 2023 2024 2025 2032 public boolean hasRedisplayFieldsOnErrorSet() { 2033 return Util.getTrue(redisplayFieldsOnError); 2034 } 2035 2036 2037 2042 public String produceLinkedTags() { 2043 StringBuffer buf = new StringBuffer (); 2044 2045 if (childFieldValues != null) { 2048 for (int i = 0; i < childFieldValues.length; i++) { 2049 if (childFieldValues[i].getRenderHiddenHtmlTag()) { 2050 TextFormatterUtil formatter = new TextFormatterUtil(childFieldValues[i] 2051 .getField(), getLocale(), null, 2052 childFieldValues[i].getFieldValueAsObject()); 2053 buf.append("<input type=\"hidden\" name=\""); 2054 buf.append(formatter.getFormFieldName(this)); 2055 buf.append("\" value=\""); 2056 buf.append(formatter.getFormattedFieldValue()); 2057 buf.append("\" />"); 2058 buf.append(formatter.renderPatternHtmlInputField()); 2059 } 2060 } 2061 } 2062 2063 return buf.toString(); 2064 } 2065 2066 2067 2077 public void strikeOut(Field f) { 2078 if (childFieldValues != null) { 2081 for (int i = 0; i < childFieldValues.length; i++) { 2082 if ((f == childFieldValues[i].getField()) 2083 && childFieldValues[i].getRenderHiddenHtmlTag()) { 2084 childFieldValues[i].setRenderHiddenHtmlTag(false); 2085 logCat.info("stroke out field:" + f.getName()); 2086 2087 return; 2088 } 2089 } 2090 } 2091 } 2092 2093 2094 2097 2098 2099 public void updatePositionPath() { 2100 StringBuffer positionPathBuf = new StringBuffer (); 2101 positionPathBuf.append(this.currentCount); 2102 positionPathBuf.append("@"); 2103 positionPathBuf.append(this.positionPathCore); 2104 this.positionPath = positionPathBuf.toString(); 2105 } 2106 2107 2108 2113 private void addFieldNames(Hashtable fields) { 2114 if (fieldNames == null) { 2115 fieldNames = new Hashtable (); 2116 } 2117 2118 fieldNames.putAll(fields); 2119 } 2120 2121 2122 private void appendSource(HttpServletRequest request, StringBuffer tagBuf) { 2123 tagBuf.append("<input type=\"hidden\" name=\"source\" value=\""); 2124 2125 String reqSource = (String ) request.getAttribute("dbforms.source"); 2131 2132 if (reqSource == null) { 2134 tagBuf.append(request.getRequestURI()); 2135 } 2136 else { 2138 tagBuf.append(reqSource); 2139 } 2140 2141 if (request.getQueryString() != null) { 2142 tagBuf.append("?").append(request.getQueryString()); 2143 } 2144 2145 tagBuf.append("\"/>"); 2146 } 2147 2148 2149 2162 private boolean checkLinkage(FieldValue[] achildFieldValues, String aPosition) { 2163 FieldValues ht = getTable().getFieldValues(aPosition); 2166 2167 for (int i = 0; i < achildFieldValues.length; i++) { 2168 String actualValue = achildFieldValues[i].getFieldValue(); 2169 logCat.debug("actualValue=" + actualValue); 2170 2171 Field f = achildFieldValues[i].getField(); 2172 logCat.debug("f.getName=" + f.getName()); 2173 logCat.debug("f.getId=" + f.getId()); 2174 2175 FieldValue aFieldValue = ht.get(f.getName()); 2176 2177 if (aFieldValue == null) { 2178 throw new IllegalArgumentException ("ERROR: Make sure that field " 2179 + f.getName() + " is a KEY of the table " + getTable().getName() 2180 + "! Otherwise you can not use it as PARENT/CHILD LINK argument!"); 2181 } 2182 2183 String valueInPos = aFieldValue.getFieldValue(); 2184 2185 logCat.info("comparing " + actualValue + " TO " + valueInPos); 2186 2187 if (!actualValue.trim().equals(valueInPos.trim())) { 2188 return false; 2189 } 2190 } 2191 2192 return true; 2193 } 2194 2195 2196 2202 private StringBuffer generateJavascriptFieldsArray() { 2203 StringBuffer result = new StringBuffer (); 2208 String key = null; 2209 String val = null; 2210 String values = ""; 2211 2212 Hashtable fields = new Hashtable (); 2213 Enumeration e = childFieldNames.keys(); 2214 2215 while (e.hasMoreElements()) { 2220 key = (String ) e.nextElement(); 2221 val = (String ) childFieldNames.get(key); 2222 values = ""; 2223 2224 if (fields.containsKey(val)) { 2225 values = (String ) fields.get(val); 2226 } 2227 2228 fields.put(val, values + ";" + key); 2229 } 2230 2231 if (isSubForm()) { 2232 parentForm.addFieldNames(fields); 2233 } else { 2234 if (fieldNames != null) { 2235 fields.putAll(fieldNames); 2236 } 2237 2238 result.append("<SCRIPT language=\"javascript\">\n"); 2239 result.append("<!-- \n\n"); 2240 result.append(" var dbFormFields = new Array();\n"); 2241 e = fields.keys(); 2242 2243 while (e.hasMoreElements()) { 2250 key = (String ) e.nextElement(); 2251 val = (String ) fields.get(key); 2252 result.append(" dbFormFields[\"").append(key).append("\"] = new Array("); 2253 2254 ArrayList arrValues = sortFields(val); 2256 2257 if (arrValues.size() == 1) { 2258 result.append("\"").append((String ) arrValues.get(0)).append("\""); 2259 } else { 2260 for (int i = 0; i <= (arrValues.size() - 1); i++) { 2261 result.append("\"").append((String ) arrValues.get(i)).append("\""); 2262 2263 if (i != (arrValues.size() - 1)) { 2264 result.append(", "); 2265 } 2266 } 2267 } 2268 2269 result.append(");\n"); 2270 } 2271 2272 result.append("\n function getDbFormFieldName(name){ \n"); 2273 result.append(" return getDbFormFieldName(name,null); \n"); 2274 result.append(" }\n\n"); 2275 result.append("\n function getDbFormFieldName(name,pos){ \n"); 2276 result.append(" var result = dbFormFields[name]; \n"); 2277 result.append(" if(pos==null) return result[result.length-1]; \n"); 2278 result.append(" return result[pos]; \n"); 2279 result.append(" }\n"); 2280 result.append("--></SCRIPT> \n"); 2281 } 2282 2283 return result; 2284 } 2285 2286 2287 2292 private StringBuffer generateJavascriptValidation() { 2293 if (isSubForm()) { 2294 parentForm.addValidationForm(getFormValidatorName(), childFieldNames); 2295 2296 return new StringBuffer (); 2297 } else { 2298 ValidatorResources vr = (ValidatorResources) pageContext.getServletContext() 2299 .getAttribute(ValidatorConstants.VALIDATOR); 2300 DbFormsErrors errors = (DbFormsErrors) pageContext.getServletContext() 2301 .getAttribute(DbFormsErrors.ERRORS); 2302 addValidationForm(getFormValidatorName(), childFieldNames); 2303 2304 return DbFormsValidatorUtil.getJavascript(validationForms, 2305 MessageResources.getLocale( 2306 (HttpServletRequest ) pageContext.getRequest()), 2307 validationFields, vr, getJavascriptValidationSrcFile(), errors); 2308 } 2309 } 2310 2311 2312 2315 private void initChildFieldValues() { 2316 if (ResultSetVector.isNull(parentForm.getResultSetVector())) { 2318 childFieldValues = null; 2319 2320 return; 2322 } 2323 2324 String aPosition = parentForm.getTable().getPositionString(parentForm 2325 .getResultSetVector()); 2326 2327 if (Util.isNull(aPosition)) { 2328 childFieldValues = null; 2329 2330 return; 2332 } 2333 2334 childFieldValues = getTable() 2335 .mapChildFieldValues(parentForm.getTable(), 2336 parentField, childField, aPosition).toArray(); 2337 } 2338 2339 2340 2343 2353 private FieldValue[] initOverrulingOrder(HttpServletRequest request) { 2354 if (request != null) { 2358 String refSource = request.getRequestURI(); 2359 2360 if (request.getQueryString() != null) { 2361 refSource += ("?" + request.getQueryString()); 2362 } 2363 2364 String sourceTag = ParseUtil.getParameter(request, "source"); 2365 logCat.info("!comparing page " + refSource + " TO " + sourceTag); 2366 2367 } 2371 2372 logCat.debug("orderBy=" + orderBy); 2373 2374 if ((orderBy == null) && (request == null)) { 2377 return null; 2378 } 2379 2380 FieldValue[] overrulingOrder = getTable().createOrderFieldValues(orderBy, 2382 request, false); 2383 overrulingOrderFields = new Vector(); 2384 2385 if (overrulingOrder != null) { 2386 for (int i = 0; i < overrulingOrder.length; i++) 2387 overrulingOrderFields.addElement(overrulingOrder[i].getField()); 2388 } 2389 2390 return overrulingOrder; 2391 } 2392 2393 2394 2402 private FieldValue[] initSearchFieldValues() { 2403 FieldValue[] fieldValues; 2404 HttpServletRequest request = (HttpServletRequest ) pageContext 2405 .getRequest(); 2406 Vector searchFieldNames = ParseUtil.getParametersStartingWith(request, 2407 Constants.FIELDNAME_SEARCH); 2408 2409 if ((searchFieldNames == null) || (searchFieldNames.size() == 0)) { 2410 return null; 2411 } 2412 2413 Vector mode_and = new Vector(); 2414 Vector mode_or = new Vector(); 2415 2416 for (int i = 0; i < searchFieldNames.size(); i++) { 2417 String searchFieldName = (String ) searchFieldNames.elementAt(i); 2418 String aSearchFieldPattern = ParseUtil.getParameter(request, 2419 Constants.FIELDNAME_PATTERNTAG + searchFieldName); 2420 String aSearchFieldValue = ParseUtil.getParameter(request, 2421 searchFieldName); 2422 aSearchFieldValue = aSearchFieldValue.trim(); 2423 2424 if (!Util.isNull(aSearchFieldValue)) { 2426 int firstUnderscore = searchFieldName.indexOf('_'); 2427 int secondUnderscore = searchFieldName.indexOf('_', 2428 firstUnderscore + 1); 2429 int ptableId = Integer.parseInt(searchFieldName.substring(firstUnderscore 2430 + 1, secondUnderscore)); 2431 int fieldId = Integer.parseInt(searchFieldName.substring(secondUnderscore 2432 + 1)); 2433 2434 Table ptable = getConfig().getTable(ptableId); 2435 Field f = ptable.getField(fieldId); 2436 2437 if (f != null) { 2438 String aSearchMode = ParseUtil.getParameter(request, 2439 f.getSearchModeName()); 2440 int mode = ("or".equals(aSearchMode)) 2441 ? Constants.SEARCHMODE_OR : Constants.SEARCHMODE_AND; 2442 String aSearchAlgorithm = ParseUtil.getParameter(request, 2443 f.getSearchAlgoName()); 2444 2445 if (ptable.getId() != getTable().getId()) { 2446 Field fTest = f; 2449 f = getTable().getFieldByName(fTest.getName()); 2450 2451 if (f != null) { 2452 if (!fTest.getFieldType().equals(f.getFieldType())) { 2454 f = null; 2455 } else { 2456 request.setAttribute(f.getSearchFieldName(), aSearchFieldValue); 2459 } 2460 } 2461 } 2462 2463 if (f != null) { 2464 int algorithm = Constants.SEARCH_ALGO_SHARP; 2466 int operator = Constants.FILTER_EQUAL; 2467 2468 if (!Util.isNull(aSearchAlgorithm)) { 2469 if (aSearchAlgorithm.startsWith("sharpLT")) { 2470 operator = Constants.FILTER_SMALLER_THEN; 2471 } else if (aSearchAlgorithm.startsWith("sharpLE")) { 2472 operator = Constants.FILTER_SMALLER_THEN_EQUAL; 2473 } else if (aSearchAlgorithm.startsWith("sharpGT")) { 2474 operator = Constants.FILTER_GREATER_THEN; 2475 } else if (aSearchAlgorithm.startsWith("sharpGE")) { 2476 operator = Constants.FILTER_GREATER_THEN_EQUAL; 2477 } else if (aSearchAlgorithm.startsWith("sharpNE")) { 2478 operator = Constants.FILTER_NOT_EQUAL; 2479 } else if (aSearchAlgorithm.startsWith("sharpNULL")) { 2480 operator = Constants.FILTER_NULL; 2481 } else if (aSearchAlgorithm.startsWith("sharpNOTNULL")) { 2482 operator = Constants.FILTER_NOT_NULL; 2483 } else if (aSearchAlgorithm.startsWith("weakStartEnd")) { 2484 algorithm = Constants.SEARCH_ALGO_WEAK_START_END; 2485 operator = Constants.FILTER_LIKE; 2486 } else if (aSearchAlgorithm.startsWith("weakStart")) { 2487 algorithm = Constants.SEARCH_ALGO_WEAK_START; 2488 operator = Constants.FILTER_LIKE; 2489 } else if (aSearchAlgorithm.startsWith("weakEnd")) { 2490 algorithm = Constants.SEARCH_ALGO_WEAK_END; 2491 operator = Constants.FILTER_LIKE; 2492 } else if (aSearchAlgorithm.startsWith("weak")) { 2493 algorithm = Constants.SEARCH_ALGO_WEAK; 2494 operator = Constants.FILTER_LIKE; 2495 } 2496 } 2497 2498 if ((aSearchAlgorithm == null) 2499 || (aSearchAlgorithm.toLowerCase().indexOf("extended") == -1)) { 2500 FieldValue fv = FieldValue.createFieldValueForSearching(f, 2502 aSearchFieldValue, getLocale(), operator, mode, 2503 algorithm, false); 2504 2505 if (!Util.isNull(aSearchFieldPattern)) { 2506 fv.setPattern(aSearchFieldPattern); 2507 } 2508 2509 if (mode == Constants.SEARCHMODE_AND) { 2510 mode_and.addElement(fv); 2511 } else { 2512 mode_or.addElement(fv); 2513 } 2514 } else if (aSearchFieldValue.indexOf("-") != -1) { 2515 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2519 2520 StringTokenizer st = new StringTokenizer (" " 2521 + aSearchFieldValue + " ", "-"); 2522 int tokenCounter = 0; 2523 2524 StringBuffer merkeDate = new StringBuffer (); 2525 StringBuffer merkeTime = new StringBuffer (); 2526 2527 while (st.hasMoreTokens()) { 2528 aSearchFieldValue = st.nextToken().trim(); 2529 tokenCounter++; 2530 2531 if (aSearchFieldValue.length() > 0) { 2532 switch (tokenCounter) { 2533 case 1: 2534 operator = Constants.FILTER_GREATER_THEN_EQUAL; 2535 TimeUtil.splitDate(aSearchFieldValue, merkeDate, merkeTime); 2536 break; 2537 2538 case 2: 2539 operator = Constants.FILTER_SMALLER_THEN_EQUAL; 2540 StringBuffer mDate = new StringBuffer (); 2541 StringBuffer mTime = new StringBuffer (); 2542 TimeUtil.splitDate(aSearchFieldValue, mDate, mTime); 2543 if (mDate.length() == 0) { 2544 mDate.append(merkeDate); 2545 } 2546 mDate.append(" "); 2547 mDate.append(mTime); 2548 aSearchFieldValue = mDate.toString(); 2549 2550 break; 2551 2552 default: 2553 operator = -1; 2554 2555 break; 2556 } 2557 2558 if (operator != -1) { 2559 FieldValue fv = FieldValue 2560 .createFieldValueForSearching(f, 2561 aSearchFieldValue, getLocale(), operator, 2562 mode, algorithm, false); 2563 2564 if (!Util.isNull(aSearchFieldPattern)) { 2565 fv.setPattern(aSearchFieldPattern); 2566 } 2567 2568 if (mode == Constants.SEARCHMODE_AND) { 2569 mode_and.addElement(fv); 2570 } else { 2571 mode_or.addElement(fv); 2572 } 2573 } 2574 } 2575 } 2576 } else { 2577 int jump = 0; 2579 2580 if (aSearchFieldValue.startsWith("<>")) { 2582 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2583 operator = Constants.FILTER_NOT_EQUAL; 2584 jump = 2; 2585 2586 } else if (aSearchFieldValue.startsWith("!=")) { 2588 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2591 operator = Constants.FILTER_NOT_EQUAL; 2592 jump = 2; 2593 2594 } else if (aSearchFieldValue.startsWith(">=")) { 2596 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2599 operator = Constants.FILTER_GREATER_THEN_EQUAL; 2600 jump = 2; 2601 2602 } else if (aSearchFieldValue.startsWith(">")) { 2604 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2607 operator = Constants.FILTER_GREATER_THEN; 2608 2609 } else if (aSearchFieldValue.startsWith("<=")) { 2611 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2614 operator = Constants.FILTER_SMALLER_THEN_EQUAL; 2615 jump = 2; 2616 2617 } else if (aSearchFieldValue.startsWith("<")) { 2619 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2622 operator = Constants.FILTER_SMALLER_THEN; 2623 jump = 1; 2624 2625 } else if (aSearchFieldValue.startsWith("=")) { 2627 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2630 operator = Constants.FILTER_EQUAL; 2631 jump = 1; 2632 } else if (aSearchFieldValue.startsWith("[NULL]")) { 2633 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2634 operator = Constants.FILTER_NULL; 2635 jump = 0; 2636 } else if (aSearchFieldValue.startsWith("[!NULL]")) { 2637 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2638 operator = Constants.FILTER_NOT_NULL; 2639 jump = 0; 2640 } else if (aSearchFieldValue.startsWith("[EMPTY]")) { 2641 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2642 operator = Constants.FILTER_EMPTY; 2643 jump = 0; 2644 } else if (aSearchFieldValue.startsWith("[!EMPTY]")) { 2645 algorithm = Constants.SEARCH_ALGO_EXTENDED; 2646 operator = Constants.FILTER_NOT_EMPTY; 2647 jump = 0; 2648 } 2649 2650 if (jump > 0) { 2651 aSearchFieldValue = aSearchFieldValue.substring(jump) 2652 .trim(); 2653 } 2654 2655 Vector errors = (Vector) request.getAttribute("errors"); 2656 2657 if ((operator == Constants.FILTER_EQUAL) && (jump == 0) 2658 && (f.getType() == FieldTypes.TIMESTAMP)) { 2659 operator = Constants.FILTER_GREATER_THEN_EQUAL; 2662 2663 FieldValue fv = FieldValue.createFieldValueForSearching(f, 2664 aSearchFieldValue, getLocale(), operator, mode, 2665 algorithm, false); 2666 2667 if (!Util.isNull(aSearchFieldPattern)) { 2668 fv.setPattern(aSearchFieldPattern); 2669 } 2670 2671 Date d = (Date ) fv.getFieldValueAsObject(); 2672 2673 if (d == null) { 2674 errors.add(new Exception ( 2675 MessageResourcesInternal.getMessage( 2676 "dbforms.error.filter.invalid.date", 2677 getLocale()))); 2678 } else { 2679 if (mode == Constants.SEARCHMODE_AND) { 2680 mode_and.addElement(fv); 2681 } else { 2682 mode_or.addElement(fv); 2683 } 2684 2685 operator = Constants.FILTER_SMALLER_THEN_EQUAL; 2686 d = TimeUtil.findEndOfDay(d); 2687 aSearchFieldValue = d.toString(); 2688 2689 fv = FieldValue.createFieldValueForSearching(f, 2690 aSearchFieldValue, getLocale(), operator, 2691 mode, algorithm, false); 2692 2693 if (!Util.isNull(aSearchFieldPattern)) { 2694 fv.setPattern(aSearchFieldPattern); 2695 } 2696 2697 if (mode == Constants.SEARCHMODE_AND) { 2698 mode_and.addElement(fv); 2699 } else { 2700 mode_or.addElement(fv); 2701 } 2702 } 2703 } else { 2704 FieldValue fv = FieldValue.createFieldValueForSearching(f, 2705 aSearchFieldValue, getLocale(), operator, mode, 2706 algorithm, false); 2707 2708 if (!Util.isNull(aSearchFieldPattern)) { 2709 fv.setPattern(aSearchFieldPattern); 2710 } 2711 2712 Object obj = fv.getFieldValueAsObject(); 2713 2714 if (obj == null) { 2715 errors.add(new Exception ( 2716 MessageResourcesInternal.getMessage( 2717 "dbforms.error.filter.invalid", getLocale()))); 2718 } else { 2719 if (mode == Constants.SEARCHMODE_AND) { 2720 mode_and.addElement(fv); 2721 } else { 2722 mode_or.addElement(fv); 2723 } 2724 } 2725 } 2726 } 2727 } 2728 } 2729 } 2730 } 2731 2732 int andBagSize = mode_and.size(); 2733 int orBagSize = mode_or.size(); 2734 int criteriaFieldCount = andBagSize + orBagSize; 2735 logCat.info("criteriaFieldCount=" + criteriaFieldCount); 2736 2737 if (criteriaFieldCount == 0) { 2738 return null; 2739 } 2740 2741 fieldValues = new FieldValue[criteriaFieldCount]; 2744 2745 int i = 0; 2746 2747 for (i = 0; i < andBagSize; i++) { 2748 fieldValues[i] = (FieldValue) mode_and.elementAt(i); 2749 } 2750 2751 for (int j = 0; j < orBagSize; j++) { 2752 fieldValues[j + i] = (FieldValue) mode_or.elementAt(j); 2753 } 2754 2755 return fieldValues; 2756 } 2757 2758 2759 2766 private ArrayList sortFields(String str) { 2767 2775 ArrayList arr = new ArrayList (); 2776 String tmp = ""; 2777 String tmp1 = null; 2778 String tmp2 = null; 2779 String insroot = null; 2780 int ident1 = 0; 2781 int ident2 = 0; 2782 StringTokenizer token = new StringTokenizer (str, ";"); 2783 2784 while (token.hasMoreTokens()) { 2785 tmp = token.nextToken(); 2786 2787 if (tmp.indexOf("@root") != -1) { 2788 arr.add(tmp); 2789 } else { 2790 insroot = tmp; 2791 } 2792 } 2793 2794 if (insroot != null) { 2795 arr.add(insroot); 2796 } 2797 2798 if (arr.size() == 1) { 2800 return arr; 2801 } 2802 2803 for (int i = 0; i <= (arr.size() - 2); i++) { 2804 tmp1 = (String ) arr.get(i); 2805 2806 for (int j = i + 1; j <= (arr.size() - 1); j++) { 2807 tmp2 = (String ) arr.get(j); 2808 2809 if ((tmp1.indexOf("@root") != -1) && (tmp2.indexOf("@root") != -1)) { 2810 try { 2811 ident1 = Integer.parseInt(tmp1.substring(tmp1.indexOf( 2812 "_", 2) + 1, tmp1.indexOf("@"))); 2813 ident2 = Integer.parseInt(tmp2.substring(tmp2.indexOf("_", 2) 2814 + 1, tmp2.indexOf("@"))); 2815 } catch (Exception e) { 2816 ident1 = -1; 2817 ident2 = -1; 2818 } 2819 2820 if (ident2 < ident1) { 2821 arr.set(i, tmp2); 2822 arr.set(j, tmp1); 2823 tmp1 = tmp2; 2824 } 2825 } 2826 } 2827 } 2828 2829 return arr; 2830 } 2831} 2832 | Popular Tags |