1 25 package org.ofbiz.common; 26 27 import java.sql.Timestamp ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import org.ofbiz.base.util.Debug; 35 import org.ofbiz.base.util.ObjectType; 36 import org.ofbiz.base.util.StringUtil; 37 import org.ofbiz.base.util.UtilDateTime; 38 import org.ofbiz.base.util.UtilHttp; 39 import org.ofbiz.base.util.UtilValidate; 40 import org.ofbiz.base.util.UtilMisc; 41 import org.ofbiz.entity.GenericDelegator; 42 import org.ofbiz.entity.GenericEntityException; 43 import org.ofbiz.entity.GenericValue; 44 import org.ofbiz.entity.condition.EntityComparisonOperator; 45 import org.ofbiz.entity.condition.EntityConditionList; 46 import org.ofbiz.entity.condition.EntityCondition; 47 import org.ofbiz.entity.condition.EntityExpr; 48 import org.ofbiz.entity.condition.EntityJoinOperator; 49 import org.ofbiz.entity.condition.EntityOperator; 50 import org.ofbiz.entity.condition.EntityFunction; 51 import org.ofbiz.entity.condition.EntityFieldValue; 52 import org.ofbiz.entity.model.ModelEntity; 53 import org.ofbiz.entity.util.EntityFindOptions; 54 import org.ofbiz.entity.util.EntityListIterator; 55 import org.ofbiz.entity.util.EntityUtil; 56 import org.ofbiz.service.DispatchContext; 57 import org.ofbiz.service.ServiceUtil; 58 import org.ofbiz.service.GenericServiceException; 59 import org.ofbiz.service.LocalDispatcher; 60 61 68 public class FindServices { 69 70 public static final String module = FindServices.class.getName(); 71 72 public static HashMap entityOperators; 73 74 static { 75 entityOperators = new HashMap (); 76 entityOperators.put("and", EntityOperator.AND); 77 entityOperators.put("between", EntityOperator.BETWEEN); 78 entityOperators.put("equals", EntityOperator.EQUALS); 79 entityOperators.put("greaterThan", EntityOperator.GREATER_THAN); 80 entityOperators.put("greaterThanEqualTo", EntityOperator.GREATER_THAN_EQUAL_TO); 81 entityOperators.put("in", EntityOperator.IN); 82 entityOperators.put("lessThan", EntityOperator.LESS_THAN); 83 entityOperators.put("lessThanEqualTo", EntityOperator.LESS_THAN_EQUAL_TO); 84 entityOperators.put("like", EntityOperator.LIKE); 85 entityOperators.put("not", EntityOperator.NOT); 86 entityOperators.put("notEqual", EntityOperator.NOT_EQUAL); 87 entityOperators.put("or", EntityOperator.OR); 88 } 89 90 public FindServices() {} 91 92 100 public static HashMap prepareField(Map inputFields, Map queryStringMap, Map origValueMap) { 101 HashMap normalizedFields = new HashMap (); 116 Iterator ifIter = inputFields.keySet().iterator(); 117 while (ifIter.hasNext()) { 119 String fieldNameRaw = null; String fieldNameRoot = null; String fieldPair = null; Object fieldValue = null; int iPos = -1; 126 int iPos2 = -1; 127 HashMap subMap = null; 128 HashMap subMap2 = null; 129 String fieldMode = null; 130 131 fieldNameRaw = (String ) ifIter.next(); 132 fieldValue = inputFields.get(fieldNameRaw); 133 if (ObjectType.isEmpty(fieldValue)) { 134 continue; 135 } 136 137 queryStringMap.put(fieldNameRaw, fieldValue); 139 iPos = fieldNameRaw.indexOf("_"); 141 if (iPos >= 0) { 144 String suffix = fieldNameRaw.substring(iPos + 1); 145 iPos2 = suffix.indexOf("_"); 146 if (iPos2 == 1) { 147 continue; 148 } 149 } 150 151 if (iPos < 0) { 154 fieldNameRoot = fieldNameRaw; 155 fieldPair = "fld0"; 156 fieldMode = "value"; 157 } else { 160 fieldNameRoot = fieldNameRaw.substring(0, iPos); 161 String suffix = fieldNameRaw.substring(iPos + 1); 162 iPos2 = suffix.indexOf("_"); 163 if (iPos2 < 0) { 164 if (suffix.startsWith("fld")) { 165 fieldPair = suffix; 168 fieldMode = "value"; 169 } else { 170 fieldPair = "fld0"; 172 fieldMode = suffix; 173 } 174 } else { 175 String tkn0 = suffix.substring(0, iPos2); 176 String tkn1 = suffix.substring(iPos2 + 1); 177 if (tkn0.startsWith("fld")) { 180 fieldPair = tkn0; 181 fieldMode = tkn1; 182 } else { 183 fieldPair = tkn1; 184 fieldMode = tkn0; 185 } 186 } 187 } 188 subMap = (HashMap ) normalizedFields.get(fieldNameRoot); 189 if (subMap == null) { 190 subMap = new HashMap (); 191 normalizedFields.put(fieldNameRoot, subMap); 192 } 193 subMap2 = (HashMap ) subMap.get(fieldPair); 194 if (subMap2 == null) { 195 subMap2 = new HashMap (); 196 subMap.put(fieldPair, subMap2); 197 } 198 subMap2.put(fieldMode, fieldValue); 199 200 List origList = (List ) origValueMap.get(fieldNameRoot); 201 if (origList == null) { 202 origList = new ArrayList (); 203 origValueMap.put(fieldNameRoot, origList); 204 } 205 Object [] origValues = {fieldNameRaw, fieldValue}; 206 origList.add(origValues); 207 } 208 return normalizedFields; 209 } 210 211 220 public static ArrayList createCondition(List keys, HashMap normalizedFields, Map queryStringMap, Map origValueMap) { 221 String fieldName = null; 222 HashMap subMap = null; 223 HashMap subMap2 = null; 224 EntityOperator fieldOp = null; 225 String fieldValue = null; Iterator iter = keys.iterator(); 228 EntityExpr cond = null; 229 ArrayList tmpList = new ArrayList (); 230 String opString = null; 231 String ignoreCase = null; 232 int count = 0; 233 while (iter.hasNext()) { 234 fieldName = (String ) iter.next(); 235 subMap = (HashMap ) normalizedFields.get(fieldName); 236 if (subMap == null) { 237 continue; 238 } 239 240 subMap2 = (HashMap ) subMap.get("fld0"); 241 opString = (String ) subMap2.get("op"); 242 ignoreCase = (String ) subMap2.get("ic"); 243 244 if (opString != null) { 245 if (opString.equals("contains")) { 246 fieldOp = EntityOperator.LIKE; 247 } else if (opString.equals("empty")) { 248 fieldOp = EntityOperator.EQUALS; 249 } else { 250 fieldOp = (EntityOperator) entityOperators.get(opString); 251 } 252 } else { 253 fieldOp = EntityOperator.EQUALS; 254 } 255 256 fieldValue = (String ) subMap2.get("value"); 257 if (fieldValue == null) { 258 continue; 259 } 260 261 if (opString != null) { 262 if (opString.equals("contains")) { 263 fieldOp = EntityOperator.LIKE; 264 fieldValue = "%" + fieldValue + "%"; 265 } else if (opString.equals("empty")) { 266 fieldOp = EntityOperator.EQUALS; 267 fieldValue = null; 268 ignoreCase = null; 269 } else if (opString.equals("like")) { 270 fieldOp = EntityOperator.LIKE; 271 fieldValue += "%"; 272 } else if (opString.equals("greaterThanFromDayStart")) { 273 fieldValue = dayStart(fieldValue, 0); 274 fieldOp = EntityOperator.GREATER_THAN; 275 ignoreCase = null; 276 } else if (opString.equals("sameDay")) { 277 String timeStampString = fieldValue; 278 fieldValue = dayStart(timeStampString, 0); 279 fieldOp = EntityOperator.GREATER_THAN_EQUAL_TO; 280 ignoreCase = null; 281 subMap2 = (HashMap ) subMap.get("fld1"); 283 if (subMap2 == null) { 284 subMap2 = new HashMap (); 285 subMap.put("fld1", subMap2); 286 } 287 String endOfDay = dayStart(timeStampString, 1); 288 subMap2.put("value", endOfDay); 289 subMap2.put("op", "lessThan"); 290 } else { 291 fieldOp = (EntityOperator) entityOperators.get(opString); 292 } 293 } else { 294 fieldOp = EntityOperator.EQUALS; 295 } 296 297 if (ignoreCase != null && ignoreCase.equals("Y")) { 298 cond = new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue(fieldName)), (EntityComparisonOperator) fieldOp, new EntityFunction.UPPER(fieldValue.toUpperCase())); 299 } else { 300 cond = new EntityExpr(fieldName, (EntityComparisonOperator) fieldOp, fieldValue); 301 } 302 tmpList.add(cond); 303 count++; 304 305 subMap2 = (HashMap ) subMap.get("fld1"); 307 if (subMap2 == null) { 308 continue; 309 } 310 opString = (String ) subMap2.get("op"); 311 312 if (opString != null) { 313 if (opString.equals("contains")) { 314 fieldOp = EntityOperator.LIKE; 315 } else if (opString.equals("empty")) { 316 fieldOp = EntityOperator.EQUALS; 317 } else { 318 fieldOp = (EntityOperator) entityOperators.get(opString); 319 } 320 } else { 321 fieldOp = EntityOperator.EQUALS; 322 } 323 324 fieldValue = (String ) subMap2.get("value"); 325 if (fieldValue == null) { 326 continue; 327 } 328 if (opString.equals("like")) { 329 fieldValue += "%"; 330 } else if (opString.equals("contains")) { 331 fieldValue += "%" + fieldValue + "%"; 332 } else if (opString.equals("empty")) { 333 fieldOp = EntityOperator.EQUALS; 334 fieldValue = null; 335 } else if (opString.equals("upToDay")) { 336 fieldValue = dayStart(fieldValue, 0); 337 fieldOp = EntityOperator.LESS_THAN; 338 } else if (opString.equals("upThruDay")) { 339 fieldValue = dayStart(fieldValue, 1); 340 fieldOp = EntityOperator.LESS_THAN; 341 } 342 cond = new EntityExpr(fieldName, (EntityComparisonOperator) fieldOp, fieldValue); 344 tmpList.add(cond); 345 346 List origList = (List )origValueMap.get(fieldName); 348 if (origList != null && origList.size() > 0) { 349 Iterator origIter = origList.iterator(); 350 while (origIter.hasNext()) { 351 Object [] arr = (Object [])origIter.next(); 352 queryStringMap.put(arr[0], arr[1]); 353 } 354 } 355 } 356 return tmpList; 357 } 358 359 365 public static Map performFind(DispatchContext dctx, Map context) { 366 String entityName = (String ) context.get("entityName"); 367 String orderBy = (String ) context.get("orderBy"); 368 Map inputFields = (Map ) context.get("inputFields"); String noConditionFind = (String ) context.get("noConditionFind"); 370 if (UtilValidate.isEmpty(noConditionFind)) { 371 noConditionFind = (String ) inputFields.get("noConditionFind"); 373 } 374 String filterByDate = (String ) context.get("filterByDate"); 375 if (UtilValidate.isEmpty(filterByDate)) { 376 filterByDate = (String ) inputFields.get("filterByDate"); 378 } 379 380 LocalDispatcher dispatcher = dctx.getDispatcher(); 381 382 Map prepareResult = null; 383 try { 384 prepareResult = dispatcher.runSync("prepareFind", UtilMisc.toMap("entityName", entityName, "orderBy", orderBy, "inputFields", inputFields, "filterByDate", filterByDate)); 385 } catch (GenericServiceException gse) { 386 return ServiceUtil.returnError("Error preparing conditions: " + gse.getMessage()); 387 } 388 EntityConditionList exprList = (EntityConditionList)prepareResult.get("entityConditionList"); 389 List orderByList = (List )prepareResult.get("orderByList"); 390 391 Map executeResult = null; 392 try { 393 executeResult = dispatcher.runSync("executeFind", UtilMisc.toMap("entityName", entityName, "orderByList", orderByList, "entityConditionList", exprList, "noConditionFind", noConditionFind)); 394 } catch (GenericServiceException gse) { 395 return ServiceUtil.returnError("Error finding iterator: " + gse.getMessage()); 396 } 397 398 if (executeResult.get("listIt") == null) { 399 Debug.logInfo("No list iterator found for query string + [" + prepareResult.get("queryString") + "]", module); 400 } 401 402 Map results = ServiceUtil.returnSuccess(); 403 results.put("listIt", executeResult.get("listIt")); 404 results.put("queryString", prepareResult.get("queryString")); 405 results.put("queryStringMap", prepareResult.get("queryStringMap")); 406 return results; 407 } 408 409 415 public static Map prepareFind(DispatchContext dctx, Map context) { 416 String entityName = (String ) context.get("entityName"); 417 String orderBy = (String ) context.get("orderBy"); 418 Map inputFields = (Map ) context.get("inputFields"); String noConditionFind = (String ) context.get("noConditionFind"); 420 if (UtilValidate.isEmpty(noConditionFind)) { 421 noConditionFind = (String ) inputFields.get("noConditionFind"); 423 } 424 String filterByDate = (String ) context.get("filterByDate"); 425 if (UtilValidate.isEmpty(filterByDate)) { 426 filterByDate = (String ) inputFields.get("filterByDate"); 428 } 429 430 Map queryStringMap = new HashMap (); 432 Map origValueMap = new HashMap (); 433 HashMap normalizedFields = prepareField(inputFields, queryStringMap, origValueMap); 434 435 GenericDelegator delegator = dctx.getDelegator(); 438 439 GenericValue entityValue = delegator.makeValue(entityName, new HashMap ()); 440 441 ModelEntity modelEntity = entityValue.getModelEntity(); 442 List keys = modelEntity.getAllFieldNames(); 443 ArrayList tmpList = createCondition(keys, normalizedFields, queryStringMap, origValueMap); 444 445 449 if ((tmpList.size() > 0) || ((noConditionFind != null) && (noConditionFind.equals("Y")))) { 450 if (!UtilValidate.isEmpty(filterByDate) && "Y".equals(filterByDate)) { 451 EntityCondition filterByDateCondition = EntityUtil.getFilterByDateExpr(); 452 tmpList.add(filterByDateCondition); 453 } 454 } 455 456 EntityConditionList exprList = null; 457 if (tmpList.size() > 0) { 458 exprList = new EntityConditionList(tmpList, (EntityJoinOperator) EntityOperator.AND); 459 } 460 461 List orderByList = null; 462 if (UtilValidate.isNotEmpty(orderBy)) { 463 orderByList = StringUtil.split(orderBy,"|"); 464 } 465 466 Map results = ServiceUtil.returnSuccess(); 467 Map reducedQueryStringMap = buildReducedQueryString(inputFields, entityName, delegator); 468 reducedQueryStringMap.put("noConditionFind", noConditionFind); 469 reducedQueryStringMap.put("filterByDate", filterByDate); 470 String queryString = UtilHttp.urlEncodeArgs(reducedQueryStringMap); 471 results.put("queryString", queryString); 472 results.put("queryStringMap", reducedQueryStringMap); 473 474 results.put("orderByList", orderByList); 475 results.put("entityConditionList", exprList); 476 return results; 477 } 478 479 484 public static Map executeFind(DispatchContext dctx, Map context) { 485 String entityName = (String ) context.get("entityName"); 486 EntityConditionList entityConditionList = (EntityConditionList) context.get("entityConditionList"); 487 List orderByList = (List ) context.get("orderByList"); 488 boolean noConditionFind = "Y".equals((String ) context.get("noConditionFind")); 489 490 GenericDelegator delegator = dctx.getDelegator(); 491 492 EntityListIterator listIt = null; 494 try { 495 if (noConditionFind || (entityConditionList != null && entityConditionList.getConditionListSize() > 0)) { 496 listIt = delegator.findListIteratorByCondition(entityName, entityConditionList, 497 null, null, orderByList, new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, false)); 498 } 499 } catch (GenericEntityException e) { 500 return ServiceUtil.returnError("Error running Find on the [" + entityName + "] entity: " + e.getMessage()); 501 } 502 503 Map results = ServiceUtil.returnSuccess(); 504 results.put("listIt", listIt); 505 return results; 506 } 507 508 private static String dayStart(String timeStampString, int daysLater) { 509 String retValue = null; 510 Timestamp ts = null; 511 Timestamp startTs = null; 512 try { 513 ts = Timestamp.valueOf(timeStampString); 514 } catch (IllegalArgumentException e) { 515 timeStampString += " 00:00:00.000"; 516 try { 517 ts = Timestamp.valueOf(timeStampString); 518 } catch (IllegalArgumentException e2) { 519 return retValue; 520 } 521 } 522 startTs = UtilDateTime.getDayStart(ts, daysLater); 523 retValue = startTs.toString(); 524 return retValue; 525 } 526 527 public static HashMap buildReducedQueryString(Map inputFields, String entityName, GenericDelegator delegator) { 528 ModelEntity modelEntity = delegator.getModelEntity(entityName); 543 HashMap normalizedFields = new HashMap (); 544 Iterator ifIter = inputFields.keySet().iterator(); 545 while (ifIter.hasNext()) { 547 String fieldNameRaw = null; String fieldNameRoot = null; String fieldPair = null; Object fieldValue = null; int iPos = -1; 554 int iPos2 = -1; 555 HashMap subMap = null; 556 HashMap subMap2 = null; 557 String fieldMode = null; 558 559 fieldNameRaw = (String ) ifIter.next(); 560 fieldValue = inputFields.get(fieldNameRaw); 561 if (ObjectType.isEmpty(fieldValue)) { 562 continue; 563 } 564 565 iPos = fieldNameRaw.indexOf("_"); 568 if (iPos >= 0) { 571 String suffix = fieldNameRaw.substring(iPos + 1); 572 iPos2 = suffix.indexOf("_"); 573 if (iPos2 == 1) { 574 continue; 575 } 576 } 577 578 if (iPos < 0) { 581 fieldNameRoot = fieldNameRaw; 582 fieldPair = "fld0"; 583 fieldMode = "value"; 584 } else { 587 fieldNameRoot = fieldNameRaw.substring(0, iPos); 588 } 589 if (modelEntity.isField(fieldNameRoot)) { 590 normalizedFields.put(fieldNameRaw, fieldValue); 591 } 592 } 593 return normalizedFields; 594 } 595 } 596 | Popular Tags |