📄 modelscreencondition.java
字号:
/* * $Id: ModelScreenCondition.java 7061 2006-03-24 04:37:21Z jonesde $ * * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.widget.screen;import java.io.Serializable;import java.lang.reflect.Method;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.ObjectType;import org.ofbiz.base.util.UtilXml;import org.ofbiz.base.util.collections.FlexibleMapAccessor;import org.ofbiz.base.util.string.FlexibleStringExpander;import org.ofbiz.entity.GenericValue;import org.ofbiz.entityext.permission.EntityPermissionChecker;import org.ofbiz.minilang.operation.BaseCompare;import org.ofbiz.security.Security;import org.apache.oro.text.regex.MalformedPatternException;import org.apache.oro.text.regex.Pattern;import org.apache.oro.text.regex.PatternCompiler;import org.apache.oro.text.regex.PatternMatcher;import org.apache.oro.text.regex.Perl5Compiler;import org.apache.oro.text.regex.Perl5Matcher;import org.w3c.dom.Element;/** * Widget Library - Screen model condition class * * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @version $Rev: 7061 $ * @since 3.1 */public class ModelScreenCondition implements Serializable { public static final String module = ModelScreenCondition.class.getName(); protected ModelScreen modelScreen; protected ScreenCondition rootCondition; public ModelScreenCondition(ModelScreen modelScreen, Element conditionElement) { this.modelScreen = modelScreen; Element firstChildElement = UtilXml.firstChildElement(conditionElement); this.rootCondition = readCondition(modelScreen, firstChildElement); } public boolean eval(Map context) { if (rootCondition == null) { return true; } return rootCondition.eval(context); } public static abstract class ScreenCondition implements Serializable { protected ModelScreen modelScreen; public ScreenCondition(ModelScreen modelScreen, Element conditionElement) { this.modelScreen = modelScreen; } public abstract boolean eval(Map context); } public static List readSubConditions(ModelScreen modelScreen, Element conditionElement) { List condList = new LinkedList(); List subElementList = UtilXml.childElementList(conditionElement); Iterator subElementIter = subElementList.iterator(); while (subElementIter.hasNext()) { Element subElement = (Element) subElementIter.next(); condList.add(readCondition(modelScreen, subElement)); } return condList; } public static ScreenCondition readCondition(ModelScreen modelScreen, Element conditionElement) { if (conditionElement == null) { return null; } if ("and".equals(conditionElement.getNodeName())) { return new And(modelScreen, conditionElement); } else if ("xor".equals(conditionElement.getNodeName())) { return new Xor(modelScreen, conditionElement); } else if ("or".equals(conditionElement.getNodeName())) { return new Or(modelScreen, conditionElement); } else if ("not".equals(conditionElement.getNodeName())) { return new Not(modelScreen, conditionElement); } else if ("if-has-permission".equals(conditionElement.getNodeName())) { return new IfHasPermission(modelScreen, conditionElement); } else if ("if-validate-method".equals(conditionElement.getNodeName())) { return new IfValidateMethod(modelScreen, conditionElement); } else if ("if-compare".equals(conditionElement.getNodeName())) { return new IfCompare(modelScreen, conditionElement); } else if ("if-compare-field".equals(conditionElement.getNodeName())) { return new IfCompareField(modelScreen, conditionElement); } else if ("if-regexp".equals(conditionElement.getNodeName())) { return new IfRegexp(modelScreen, conditionElement); } else if ("if-empty".equals(conditionElement.getNodeName())) { return new IfEmpty(modelScreen, conditionElement); } else if ("if-entity-permission".equals(conditionElement.getNodeName())) { return new IfEntityPermission(modelScreen, conditionElement); } else { throw new IllegalArgumentException("Condition element not supported with name: " + conditionElement.getNodeName()); } } public static class And extends ScreenCondition { protected List subConditions; public And(ModelScreen modelScreen, Element condElement) { super (modelScreen, condElement); this.subConditions = readSubConditions(modelScreen, condElement); } public boolean eval(Map context) { // return false for the first one in the list that is false, basic and algo Iterator subConditionIter = this.subConditions.iterator(); while (subConditionIter.hasNext()) { ScreenCondition subCondition = (ScreenCondition) subConditionIter.next(); if (!subCondition.eval(context)) { return false; } } return true; } } public static class Xor extends ScreenCondition { protected List subConditions; public Xor(ModelScreen modelScreen, Element condElement) { super (modelScreen, condElement); this.subConditions = readSubConditions(modelScreen, condElement); } public boolean eval(Map context) { // if more than one is true stop immediately and return false; if all are false return false; if only one is true return true boolean foundOneTrue = false; Iterator subConditionIter = this.subConditions.iterator(); while (subConditionIter.hasNext()) { ScreenCondition subCondition = (ScreenCondition) subConditionIter.next(); if (subCondition.eval(context)) { if (foundOneTrue) { // now found two true, so return false return false; } else { foundOneTrue = true; } } } return foundOneTrue; } } public static class Or extends ScreenCondition { protected List subConditions; public Or(ModelScreen modelScreen, Element condElement) { super (modelScreen, condElement); this.subConditions = readSubConditions(modelScreen, condElement); } public boolean eval(Map context) { // return true for the first one in the list that is true, basic or algo Iterator subConditionIter = this.subConditions.iterator(); while (subConditionIter.hasNext()) { ScreenCondition subCondition = (ScreenCondition) subConditionIter.next(); if (subCondition.eval(context)) { return true; } } return false; } } public static class Not extends ScreenCondition { protected ScreenCondition subCondition; public Not(ModelScreen modelScreen, Element condElement) { super (modelScreen, condElement); Element firstChildElement = UtilXml.firstChildElement(condElement); this.subCondition = readCondition(modelScreen, firstChildElement); } public boolean eval(Map context) { return !this.subCondition.eval(context); } } public static class IfHasPermission extends ScreenCondition { protected FlexibleStringExpander permissionExdr; protected FlexibleStringExpander actionExdr; public IfHasPermission(ModelScreen modelScreen, Element condElement) { super (modelScreen, condElement); this.permissionExdr = new FlexibleStringExpander(condElement.getAttribute("permission")); this.actionExdr = new FlexibleStringExpander(condElement.getAttribute("action")); } public boolean eval(Map context) { // if no user is logged in, treat as if the user does not have permission GenericValue userLogin = (GenericValue) context.get("userLogin"); if (userLogin != null) { String permission = permissionExdr.expandString(context); String action = actionExdr.expandString(context); Security security = (Security) context.get("security"); if (action != null && action.length() > 0) { // run hasEntityPermission if (security.hasEntityPermission(permission, action, userLogin)) { return true; } } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -