📄 entityfinderutil.java
字号:
if (this.ignoreIfEmpty && ObjectType.isEmpty(value)) { return null; } if (operator == EntityOperator.NOT_EQUAL && value != null) { // since some databases don't consider nulls in != comparisons, explicitly include them // this makes more sense logically, but if anyone ever needs it to not behave this way we should add an "or-null" attribute that is true by default return new EntityExpr( new EntityExpr(fieldName, (EntityComparisonOperator) operator, value), EntityOperator.OR, new EntityExpr(fieldName, EntityOperator.EQUALS, null)); } else { return new EntityExpr(fieldName, (EntityComparisonOperator) operator, value); } } } public static class ConditionList implements Condition { List conditionList = new LinkedList(); FlexibleStringExpander combineExdr; public ConditionList(Element conditionListElement) { this.combineExdr = new FlexibleStringExpander(conditionListElement.getAttribute("combine")); List subElements = UtilXml.childElementList(conditionListElement); Iterator subElementIter = subElements.iterator(); while (subElementIter.hasNext()) { Element subElement = (Element) subElementIter.next(); if ("condition-expr".equals(subElement.getNodeName())) { conditionList.add(new ConditionExpr(subElement)); } else if ("condition-list".equals(subElement.getNodeName())) { conditionList.add(new ConditionList(subElement)); } else if ("condition-object".equals(subElement.getNodeName())) { conditionList.add(new ConditionObject(subElement)); } else { throw new IllegalArgumentException("Invalid element with name [" + subElement.getNodeName() + "] found under a condition-list element."); } } } public EntityCondition createCondition(Map context, String entityName, GenericDelegator delegator) { if (this.conditionList.size() == 0) { return null; } if (this.conditionList.size() == 1) { Condition condition = (Condition) this.conditionList.get(0); return condition.createCondition(context, entityName, delegator); } List entityConditionList = new LinkedList(); Iterator conditionIter = conditionList.iterator(); while (conditionIter.hasNext()) { Condition curCondition = (Condition) conditionIter.next(); EntityCondition econd = curCondition.createCondition(context, entityName, delegator); if (econd != null) { entityConditionList.add(econd); } } String operatorName = combineExdr.expandString(context); EntityOperator operator = EntityOperator.lookup(operatorName); if (operator == null) { throw new IllegalArgumentException("Could not find an entity operator for the name: " + operatorName); } return new EntityConditionList(entityConditionList, (EntityJoinOperator) operator); } } public static class ConditionObject implements Condition { protected FlexibleMapAccessor fieldNameAcsr; public ConditionObject(Element conditionExprElement) { this.fieldNameAcsr = new FlexibleMapAccessor(conditionExprElement.getAttribute("field-name")); if (this.fieldNameAcsr.isEmpty()) { // no "field-name"? try "name" this.fieldNameAcsr = new FlexibleMapAccessor(conditionExprElement.getAttribute("name")); } } public EntityCondition createCondition(Map context, String entityName, GenericDelegator delegator) { EntityCondition condition = (EntityCondition) fieldNameAcsr.get(context); return condition; } } public static interface OutputHandler extends Serializable { public void handleOutput(EntityListIterator eli, Map context, FlexibleMapAccessor listAcsr); public void handleOutput(List results, Map context, FlexibleMapAccessor listAcsr); } public static class LimitRange implements OutputHandler { FlexibleStringExpander startExdr; FlexibleStringExpander sizeExdr; public LimitRange(Element limitRangeElement) { this.startExdr = new FlexibleStringExpander(limitRangeElement.getAttribute("start")); this.sizeExdr = new FlexibleStringExpander(limitRangeElement.getAttribute("size")); } int getStart(Map context) { String startStr = this.startExdr.expandString(context); try { return Integer.parseInt(startStr); } catch (NumberFormatException e) { String errMsg = "The limit-range start number \"" + startStr + "\" was not valid: " + e.toString(); Debug.logError(e, errMsg, module); throw new IllegalArgumentException(errMsg); } } int getSize(Map context) { String sizeStr = this.sizeExdr.expandString(context); try { return Integer.parseInt(sizeStr); } catch (NumberFormatException e) { String errMsg = "The limit-range size number \"" + sizeStr + "\" was not valid: " + e.toString(); Debug.logError(e, errMsg, module); throw new IllegalArgumentException(errMsg); } } public void handleOutput(EntityListIterator eli, Map context, FlexibleMapAccessor listAcsr) { int start = getStart(context); int size = getSize(context); try { listAcsr.put(context, eli.getPartialList(start, size)); } catch (GenericEntityException e) { String errMsg = "Error getting partial list in limit-range with start=" + start + " and size=" + size + ": " + e.toString(); Debug.logError(e, errMsg, module); throw new IllegalArgumentException(errMsg); } } public void handleOutput(List results, Map context, FlexibleMapAccessor listAcsr) { int start = getStart(context); int size = getSize(context); int end = start + size; if (end > results.size()) end = results.size(); listAcsr.put(context, results.subList(start, end)); } } public static class LimitView implements OutputHandler { FlexibleStringExpander viewIndexExdr; FlexibleStringExpander viewSizeExdr; public LimitView(Element limitViewElement) { this.viewIndexExdr = new FlexibleStringExpander(limitViewElement.getAttribute("view-index")); this.viewSizeExdr = new FlexibleStringExpander(limitViewElement.getAttribute("view-size")); } int getIndex(Map context) { String viewIndexStr = this.viewIndexExdr.expandString(context); try { return Integer.parseInt(viewIndexStr); } catch (NumberFormatException e) { String errMsg = "The limit-view view-index number \"" + viewIndexStr + "\" was not valid: " + e.toString(); Debug.logError(e, errMsg, module); throw new IllegalArgumentException(errMsg); } } int getSize(Map context) { String viewSizeStr = this.viewSizeExdr.expandString(context); try { return Integer.parseInt(viewSizeStr); } catch (NumberFormatException e) { String errMsg = "The limit-view view-size number \"" + viewSizeStr + "\" was not valid: " + e.toString(); Debug.logError(e, errMsg, module); throw new IllegalArgumentException(errMsg); } } public void handleOutput(EntityListIterator eli, Map context, FlexibleMapAccessor listAcsr) { int index = this.getIndex(context); int size = this.getSize(context); try { listAcsr.put(context, eli.getPartialList(((index - 1) * size) + 1, size)); } catch (GenericEntityException e) { String errMsg = "Error getting partial list in limit-view with index=" + index + " and size=" + size + ": " + e.toString(); Debug.logError(e, errMsg, module); throw new IllegalArgumentException(errMsg); } } public void handleOutput(List results, Map context, FlexibleMapAccessor listAcsr) { int index = this.getIndex(context); int size = this.getSize(context); int begin = index * size; int end = index * size + size; if (end > results.size()) end = results.size(); listAcsr.put(context, results.subList(begin, end)); } } public static class UseIterator implements OutputHandler { public UseIterator(Element useIteratorElement) { // no parameters, nothing to do } public void handleOutput(EntityListIterator eli, Map context, FlexibleMapAccessor listAcsr) { listAcsr.put(context, eli); } public void handleOutput(List results, Map context, FlexibleMapAccessor listAcsr) { throw new IllegalArgumentException("Cannot handle output with use-iterator when the query is cached, or the result in general is not an EntityListIterator"); } } public static class GetAll implements OutputHandler { public GetAll() { // no parameters, nothing to do } public void handleOutput(EntityListIterator eli, Map context, FlexibleMapAccessor listAcsr) { try { listAcsr.put(context, eli.getCompleteList()); eli.close(); } catch (GenericEntityException e) { String errorMsg = "Error getting list from EntityListIterator: " + e.toString(); Debug.logError(e, errorMsg, module); throw new IllegalArgumentException(errorMsg); } } public void handleOutput(List results, Map context, FlexibleMapAccessor listAcsr) { listAcsr.put(context, results); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -