📄 findservices.java
字号:
} else { cond = new EntityExpr(fieldName, (EntityComparisonOperator) fieldOp, fieldValue); } tmpList.add(cond); count++; // Repeat above operations if there is a "range" - second value subMap2 = (HashMap) subMap.get("fld1"); if (subMap2 == null) { continue; } opString = (String) subMap2.get("op"); if (opString != null) { if (opString.equals("contains")) { fieldOp = EntityOperator.LIKE; } else if (opString.equals("empty")) { fieldOp = EntityOperator.EQUALS; } else { fieldOp = (EntityOperator) entityOperators.get(opString); } } else { fieldOp = EntityOperator.EQUALS; } fieldValue = (String) subMap2.get("value"); if (fieldValue == null) { continue; } if (opString.equals("like")) { fieldValue += "%"; } else if (opString.equals("contains")) { fieldValue += "%" + fieldValue + "%"; } else if (opString.equals("empty")) { fieldOp = EntityOperator.EQUALS; fieldValue = null; } else if (opString.equals("upToDay")) { fieldValue = dayStart(fieldValue, 0); fieldOp = EntityOperator.LESS_THAN; } else if (opString.equals("upThruDay")) { fieldValue = dayStart(fieldValue, 1); fieldOp = EntityOperator.LESS_THAN; } // String rhs = fieldValue.toString(); cond = new EntityExpr(fieldName, (EntityComparisonOperator) fieldOp, fieldValue); tmpList.add(cond); // add to queryStringMap List origList = (List)origValueMap.get(fieldName); if (origList != null && origList.size() > 0) { Iterator origIter = origList.iterator(); while (origIter.hasNext()) { Object [] arr = (Object [])origIter.next(); queryStringMap.put(arr[0], arr[1]); } } } return tmpList; } /** * performFind * * This is a generic method that expects entity data affixed with special suffixes * to indicate their purpose in formulating an SQL query statement. */ public static Map performFind(DispatchContext dctx, Map context) { String entityName = (String) context.get("entityName"); String orderBy = (String) context.get("orderBy"); Map inputFields = (Map) context.get("inputFields"); // Input String noConditionFind = (String) context.get("noConditionFind"); if (UtilValidate.isEmpty(noConditionFind)) { // try finding in inputFields Map noConditionFind = (String) inputFields.get("noConditionFind"); } String filterByDate = (String) context.get("filterByDate"); if (UtilValidate.isEmpty(filterByDate)) { // try finding in inputFields Map filterByDate = (String) inputFields.get("filterByDate"); } LocalDispatcher dispatcher = dctx.getDispatcher(); Map prepareResult = null; try { prepareResult = dispatcher.runSync("prepareFind", UtilMisc.toMap("entityName", entityName, "orderBy", orderBy, "inputFields", inputFields, "filterByDate", filterByDate)); } catch (GenericServiceException gse) { return ServiceUtil.returnError("Error preparing conditions: " + gse.getMessage()); } EntityConditionList exprList = (EntityConditionList)prepareResult.get("entityConditionList"); List orderByList = (List)prepareResult.get("orderByList"); Map executeResult = null; try { executeResult = dispatcher.runSync("executeFind", UtilMisc.toMap("entityName", entityName, "orderByList", orderByList, "entityConditionList", exprList, "noConditionFind", noConditionFind)); } catch (GenericServiceException gse) { return ServiceUtil.returnError("Error finding iterator: " + gse.getMessage()); } if (executeResult.get("listIt") == null) { Debug.logInfo("No list iterator found for query string + [" + prepareResult.get("queryString") + "]", module); } Map results = ServiceUtil.returnSuccess(); results.put("listIt", executeResult.get("listIt")); results.put("queryString", prepareResult.get("queryString")); results.put("queryStringMap", prepareResult.get("queryStringMap")); return results; } /** * prepareFind * * This is a generic method that expects entity data affixed with special suffixes * to indicate their purpose in formulating an SQL query statement. */ public static Map prepareFind(DispatchContext dctx, Map context) { String entityName = (String) context.get("entityName"); String orderBy = (String) context.get("orderBy"); Map inputFields = (Map) context.get("inputFields"); // Input String noConditionFind = (String) context.get("noConditionFind"); if (UtilValidate.isEmpty(noConditionFind)) { // try finding in inputFields Map noConditionFind = (String) inputFields.get("noConditionFind"); } String filterByDate = (String) context.get("filterByDate"); if (UtilValidate.isEmpty(filterByDate)) { // try finding in inputFields Map filterByDate = (String) inputFields.get("filterByDate"); } // parameters run thru UtilHttp.getParameterMap Map queryStringMap = new HashMap(); Map origValueMap = new HashMap(); HashMap normalizedFields = prepareField(inputFields, queryStringMap, origValueMap); // Now use only the values that correspond to entity fields to build // an EntityConditionList GenericDelegator delegator = dctx.getDelegator(); GenericValue entityValue = delegator.makeValue(entityName, new HashMap()); ModelEntity modelEntity = entityValue.getModelEntity(); List keys = modelEntity.getAllFieldNames(); ArrayList tmpList = createCondition(keys, normalizedFields, queryStringMap, origValueMap); /* the filter by date condition should only be added when there are other conditions or when * the user has specified a noConditionFind. Otherwise, specifying filterByDate will become * its own condition. */ if ((tmpList.size() > 0) || ((noConditionFind != null) && (noConditionFind.equals("Y")))) { if (!UtilValidate.isEmpty(filterByDate) && "Y".equals(filterByDate)) { EntityCondition filterByDateCondition = EntityUtil.getFilterByDateExpr(); tmpList.add(filterByDateCondition); } } EntityConditionList exprList = null; if (tmpList.size() > 0) { exprList = new EntityConditionList(tmpList, (EntityJoinOperator) EntityOperator.AND); } List orderByList = null; if (UtilValidate.isNotEmpty(orderBy)) { orderByList = StringUtil.split(orderBy,"|"); } Map results = ServiceUtil.returnSuccess(); Map reducedQueryStringMap = buildReducedQueryString(inputFields, entityName, delegator); reducedQueryStringMap.put("noConditionFind", noConditionFind); reducedQueryStringMap.put("filterByDate", filterByDate); String queryString = UtilHttp.urlEncodeArgs(reducedQueryStringMap); results.put("queryString", queryString); results.put("queryStringMap", reducedQueryStringMap); results.put("orderByList", orderByList); results.put("entityConditionList", exprList); return results; } /** * executeFind * * This is a generic method that returns an EntityListIterator. */ public static Map executeFind(DispatchContext dctx, Map context) { String entityName = (String) context.get("entityName"); EntityConditionList entityConditionList = (EntityConditionList) context.get("entityConditionList"); List orderByList = (List) context.get("orderByList"); boolean noConditionFind = "Y".equals((String) context.get("noConditionFind")); GenericDelegator delegator = dctx.getDelegator(); // Retrieve entities - an iterator over all the values EntityListIterator listIt = null; try { if (noConditionFind || (entityConditionList != null && entityConditionList.getConditionListSize() > 0)) { listIt = delegator.findListIteratorByCondition(entityName, entityConditionList, null, null, orderByList, new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, false)); } } catch (GenericEntityException e) { return ServiceUtil.returnError("Error running Find on the [" + entityName + "] entity: " + e.getMessage()); } Map results = ServiceUtil.returnSuccess(); results.put("listIt", listIt); return results; } private static String dayStart(String timeStampString, int daysLater) { String retValue = null; Timestamp ts = null; Timestamp startTs = null; try { ts = Timestamp.valueOf(timeStampString); } catch (IllegalArgumentException e) { timeStampString += " 00:00:00.000"; try { ts = Timestamp.valueOf(timeStampString); } catch (IllegalArgumentException e2) { return retValue; } } startTs = UtilDateTime.getDayStart(ts, daysLater); retValue = startTs.toString(); return retValue; } public static HashMap buildReducedQueryString(Map inputFields, String entityName, GenericDelegator delegator) { // Strip the "_suffix" off of the parameter name and // build a three-level map of values keyed by fieldRoot name, // fld0 or fld1, and, then, "op" or "value" // ie. id // - fld0 // - op:like // - value:abc // - fld1 (if there is a range) // - op:lessThan // - value:55 (note: these two "flds" wouldn't really go together) // Also note that op/fld can be in any order. (eg. id_fld1_equals or id_equals_fld1) // Note that "normalizedFields" will contain values other than those // Contained in the associated entity. // Those extra fields will be ignored in the second half of this method. ModelEntity modelEntity = delegator.getModelEntity(entityName); HashMap normalizedFields = new HashMap(); Iterator ifIter = inputFields.keySet().iterator(); //StringBuffer queryStringBuf = new StringBuffer(); while (ifIter.hasNext()) { String fieldNameRaw = null; // The name as it appeas in the HTML form String fieldNameRoot = null; // The entity field name. Everything to the left of the first "_" if // it exists, or the whole word, if not. String fieldPair = null; // "fld0" or "fld1" - begin/end of range or just fld0 if no range. Object fieldValue = null; // If it is a "value" field, it will be the value to be used in the query. // If it is an "op" field, it will be "equals", "greaterThan", etc. int iPos = -1; int iPos2 = -1; HashMap subMap = null; HashMap subMap2 = null; String fieldMode = null; fieldNameRaw = (String) ifIter.next(); fieldValue = inputFields.get(fieldNameRaw); if (ObjectType.isEmpty(fieldValue)) { continue; } //queryStringBuffer.append(fieldNameRaw + "=" + fieldValue); iPos = fieldNameRaw.indexOf("_"); // Look for suffix // This is a hack to skip fields from "multi" forms // These would have the form "fieldName_o_1" if (iPos >= 0) { String suffix = fieldNameRaw.substring(iPos + 1); iPos2 = suffix.indexOf("_"); if (iPos2 == 1) { continue; } } // If no suffix, assume no range (default to fld0) and operations of equals // If no field op is present, it will assume "equals". if (iPos < 0) { fieldNameRoot = fieldNameRaw; fieldPair = "fld0"; fieldMode = "value"; } else { // Must have at least "fld0/1" or "equals, greaterThan, etc." // Some bogus fields will slip in, like "ENTITY_NAME", but they will be ignored fieldNameRoot = fieldNameRaw.substring(0, iPos); } if (modelEntity.isField(fieldNameRoot)) { normalizedFields.put(fieldNameRaw, fieldValue); } } return normalizedFields; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -