Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ModelScreenCondition.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ModelScreenCondition.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ModelScreenCondition.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ModelScreenCondition.java Mon Jan 5 08:50:30 2015 @@ -18,557 +18,70 @@ *******************************************************************************/ package org.ofbiz.widget.screen; -import java.io.Serializable; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; import java.util.Map; -import java.util.TimeZone; -import org.apache.oro.text.regex.MalformedPatternException; -import org.apache.oro.text.regex.Pattern; -import org.apache.oro.text.regex.PatternMatcher; -import org.apache.oro.text.regex.Perl5Matcher; -import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.GeneralException; -import org.ofbiz.base.util.ObjectType; -import org.ofbiz.base.util.PatternFactory; import org.ofbiz.base.util.UtilGenerics; -import org.ofbiz.base.util.UtilValidate; -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.ofbiz.service.DispatchContext; -import org.ofbiz.service.GenericServiceException; -import org.ofbiz.service.LocalDispatcher; -import org.ofbiz.service.ModelService; -import org.ofbiz.service.ServiceUtil; +import org.ofbiz.widget.ModelWidget; +import org.ofbiz.widget.ModelWidgetCondition; import org.w3c.dom.Element; /** - * Widget Library - Screen model condition class + * Models the <condition> element. + * + * @see <code>widget-screen.xsd</code> */ @SuppressWarnings("serial") -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<String, Object> 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<String, Object> context); - } - - public static List<ScreenCondition> readSubConditions(ModelScreen modelScreen, Element conditionElement) { - List<? extends Element> subElementList = UtilXml.childElementList(conditionElement); - List<ScreenCondition> condList = new ArrayList<ScreenCondition>(subElementList.size()); - for (Element subElement: subElementList) { - 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-service-permission".equals(conditionElement.getNodeName())) { - return new IfServicePermission(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 if ("if-empty-section".equals(conditionElement.getNodeName())) { - return new IfEmptySection(modelScreen, conditionElement); - } else { - throw new IllegalArgumentException("Condition element not supported with name: " + conditionElement.getNodeName()); - } - } - - public static class And extends ScreenCondition { - protected List<ScreenCondition> subConditions; - - public And(ModelScreen modelScreen, Element condElement) { - super (modelScreen, condElement); - this.subConditions = readSubConditions(modelScreen, condElement); - } - - @Override - public boolean eval(Map<String, Object> context) { - // return false for the first one in the list that is false, basic and algo - for (ScreenCondition subCondition: this.subConditions) { - if (!subCondition.eval(context)) { - return false; - } - } - return true; - } - } - - public static class Xor extends ScreenCondition { - protected List<ScreenCondition> subConditions; - - public Xor(ModelScreen modelScreen, Element condElement) { - super (modelScreen, condElement); - this.subConditions = readSubConditions(modelScreen, condElement); - } - - @Override - public boolean eval(Map<String, Object> 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; - for (ScreenCondition subCondition: this.subConditions) { - 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<ScreenCondition> subConditions; - - public Or(ModelScreen modelScreen, Element condElement) { - super (modelScreen, condElement); - this.subConditions = readSubConditions(modelScreen, condElement); - } - - @Override - public boolean eval(Map<String, Object> context) { - // return true for the first one in the list that is true, basic or algo - for (ScreenCondition subCondition: this.subConditions) { - 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); - } - - @Override - public boolean eval(Map<String, Object> context) { - return !this.subCondition.eval(context); - } - } - - public static class IfServicePermission extends ScreenCondition { - protected FlexibleStringExpander serviceExdr; - protected FlexibleStringExpander actionExdr; - protected FlexibleStringExpander ctxMapExdr; - protected FlexibleStringExpander resExdr; - - public IfServicePermission(ModelScreen modelScreen, Element condElement) { - super(modelScreen, condElement); - this.serviceExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("service-name")); - this.actionExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("main-action")); - this.ctxMapExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("context-map")); - this.resExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("resource-description")); - } - - @Override - public boolean eval(Map<String, Object> 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 serviceName = serviceExdr.expandString(context); - String mainAction = actionExdr.expandString(context); - String contextMap = ctxMapExdr.expandString(context); - String resource = resExdr.expandString(context); - if (UtilValidate.isEmpty(resource)) { - resource = serviceName; - } - - if (UtilValidate.isEmpty(serviceName)) { - Debug.logWarning("No permission service-name specified!", module); - return false; - } - - Map<String, Object> serviceContext = UtilGenerics.toMap(context.get(contextMap)); - if (serviceContext != null) { - // copy the required internal fields - serviceContext.put("userLogin", context.get("userLogin")); - serviceContext.put("locale", context.get("locale")); - } else { - serviceContext = context; - } - - // get the service objects - LocalDispatcher dispatcher = this.modelScreen.getDispatcher(context); - DispatchContext dctx = dispatcher.getDispatchContext(); - - // get the service - ModelService permService; - try { - permService = dctx.getModelService(serviceName); - } catch (GenericServiceException e) { - Debug.logError(e, module); - return false; - } - - if (permService != null) { - // build the context - Map<String, Object> svcCtx = permService.makeValid(serviceContext, ModelService.IN_PARAM); - svcCtx.put("resourceDescription", resource); - if (UtilValidate.isNotEmpty(mainAction)) { - svcCtx.put("mainAction", mainAction); - } - - // invoke the service - Map<String, Object> resp; - try { - resp = dispatcher.runSync(permService.name, svcCtx, 300, true); - } catch (GenericServiceException e) { - Debug.logError(e, module); - return false; - } - if (ServiceUtil.isError(resp) || ServiceUtil.isFailure(resp)) { - Debug.logError(ServiceUtil.getErrorMessage(resp), module); - return false; - } - Boolean hasPermission = (Boolean) resp.get("hasPermission"); - if (hasPermission != null) { - return hasPermission.booleanValue(); - } - } - } - return false; - } - } - - public static class IfHasPermission extends ScreenCondition { - protected FlexibleStringExpander permissionExdr; - protected FlexibleStringExpander actionExdr; - - public IfHasPermission(ModelScreen modelScreen, Element condElement) { - super (modelScreen, condElement); - this.permissionExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("permission")); - this.actionExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("action")); - } - - @Override - public boolean eval(Map<String, Object> 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 (UtilValidate.isNotEmpty(action)) { - // run hasEntityPermission - if (security.hasEntityPermission(permission, action, userLogin)) { - return true; - } - } else { - // run hasPermission - if (security.hasPermission(permission, userLogin)) { - return true; - } - } - } - return false; - } - } - - public static class IfValidateMethod extends ScreenCondition { - protected FlexibleMapAccessor<Object> fieldAcsr; - protected FlexibleStringExpander methodExdr; - protected FlexibleStringExpander classExdr; - - public IfValidateMethod(ModelScreen modelScreen, Element condElement) { - super (modelScreen, condElement); - this.fieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("field")); - if (this.fieldAcsr.isEmpty()) this.fieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("field-name")); - this.methodExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("method")); - this.classExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("class")); - } - - @Override - public boolean eval(Map<String, Object> context) { - String methodName = this.methodExdr.expandString(context); - String className = this.classExdr.expandString(context); - - Object fieldVal = this.fieldAcsr.get(context); - String fieldString = null; - if (fieldVal != null) { - try { - fieldString = (String) ObjectType.simpleTypeConvert(fieldVal, "String", null, (TimeZone) context.get("timeZone"), (Locale) context.get("locale"), true); - } catch (GeneralException e) { - Debug.logError(e, "Could not convert object to String, using empty String", module); - } - } - - // always use an empty string by default - if (fieldString == null) fieldString = ""; +public class ModelScreenCondition extends ModelWidgetCondition { - Class<?>[] paramTypes = new Class[] {String.class}; - Object[] params = new Object[] {fieldString}; - - Class<?> valClass; - try { - valClass = ObjectType.loadClass(className); - } catch (ClassNotFoundException cnfe) { - Debug.logError("Could not find validation class: " + className, module); - return false; - } - - Method valMethod; - try { - valMethod = valClass.getMethod(methodName, paramTypes); - } catch (NoSuchMethodException cnfe) { - Debug.logError("Could not find validation method: " + methodName + " of class " + className, module); - return false; - } - - Boolean resultBool = Boolean.FALSE; - try { - resultBool = (Boolean) valMethod.invoke(null, params); - } catch (Exception e) { - Debug.logError(e, "Error in IfValidationMethod " + methodName + " of class " + className + ", defaulting to false ", module); - } - - return resultBool.booleanValue(); - } - } + /* + * ----------------------------------------------------------------------- * + * DEVELOPERS PLEASE READ + * ----------------------------------------------------------------------- * + * + * This model is intended to be a read-only data structure that represents + * an XML element. Outside of object construction, the class should not + * have any behaviors. + * + * Instances of this class will be shared by multiple threads - therefore + * it is immutable. DO NOT CHANGE THE OBJECT'S STATE AT RUN TIME! + * + */ - public static class IfCompare extends ScreenCondition { - protected FlexibleMapAccessor<Object> fieldAcsr; - protected FlexibleStringExpander valueExdr; - - protected String operator; - protected String type; - protected FlexibleStringExpander formatExdr; - - public IfCompare(ModelScreen modelScreen, Element condElement) { - super (modelScreen, condElement); - this.fieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("field")); - if (this.fieldAcsr.isEmpty()) this.fieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("field-name")); - this.valueExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("value")); - - this.operator = condElement.getAttribute("operator"); - this.type = condElement.getAttribute("type"); - - this.formatExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("format")); - } - - @Override - public boolean eval(Map<String, Object> context) { - String value = this.valueExdr.expandString(context); - String format = this.formatExdr.expandString(context); - - Object fieldVal = this.fieldAcsr.get(context); - - // always use an empty string by default - if (fieldVal == null) { - fieldVal = ""; - } - - List<Object> messages = new LinkedList<Object>(); - Boolean resultBool = BaseCompare.doRealCompare(fieldVal, value, operator, type, format, messages, null, null, true); - if (messages.size() > 0) { - messages.add(0, "Error with comparison in if-compare between field [" + fieldAcsr.toString() + "] with value [" + fieldVal + "] and value [" + value + "] with operator [" + operator + "] and type [" + type + "]: "); - - StringBuilder fullString = new StringBuilder(); - for (Object item: messages) { - fullString.append(item.toString()); - } - Debug.logWarning(fullString.toString(), module); - - throw new IllegalArgumentException(fullString.toString()); - } + public static final String module = ModelScreenCondition.class.getName(); + public static final ConditionFactory SCREEN_CONDITION_FACTORY = new ScreenConditionFactory(); - return resultBool.booleanValue(); - } + public ModelScreenCondition(ModelScreen modelScreen, Element conditionElement) { + super(SCREEN_CONDITION_FACTORY, modelScreen, conditionElement); } - public static class IfCompareField extends ScreenCondition { - protected FlexibleMapAccessor<Object> fieldAcsr; - protected FlexibleMapAccessor<Object> toFieldAcsr; - - protected String operator; - protected String type; - protected FlexibleStringExpander formatExdr; - - public IfCompareField(ModelScreen modelScreen, Element condElement) { - super (modelScreen, condElement); - this.fieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("field")); - if (this.fieldAcsr.isEmpty()) this.fieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("field-name")); - this.toFieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("to-field")); - if (this.toFieldAcsr.isEmpty()) this.toFieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("to-field-name")); + public static class IfEmptySection extends ModelWidgetCondition implements Condition { + private final FlexibleStringExpander sectionExdr; - this.operator = condElement.getAttribute("operator"); - this.type = condElement.getAttribute("type"); - - this.formatExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("format")); + private IfEmptySection(ConditionFactory factory, ModelWidget modelWidget, Element condElement) { + super (factory, modelWidget, condElement); + this.sectionExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("section-name")); } @Override public boolean eval(Map<String, Object> context) { - String format = this.formatExdr.expandString(context); - - Object fieldVal = this.fieldAcsr.get(context); - Object toFieldVal = this.toFieldAcsr.get(context); - - // always use an empty string by default - if (fieldVal == null) { - fieldVal = ""; - } - - List<Object> messages = new LinkedList<Object>(); - Boolean resultBool = BaseCompare.doRealCompare(fieldVal, toFieldVal, operator, type, format, messages, null, null, false); - if (messages.size() > 0) { - messages.add(0, "Error with comparison in if-compare-field between field [" + fieldAcsr.toString() + "] with value [" + fieldVal + "] and to-field [" + toFieldAcsr.toString() + "] with value [" + toFieldVal + "] with operator [" + operator + "] and type [" + type + "]: "); - - StringBuilder fullString = new StringBuilder(); - for (Object item: messages) { - fullString.append(item.toString()); - } - Debug.logWarning(fullString.toString(), module); - - throw new IllegalArgumentException(fullString.toString()); - } - - return resultBool.booleanValue(); + Map<String, Object> sectionsMap = UtilGenerics.toMap(context.get("sections")); + return !sectionsMap.containsKey(this.sectionExdr.expandString(context)); } } - public static class IfRegexp extends ScreenCondition { - protected FlexibleMapAccessor<Object> fieldAcsr; - protected FlexibleStringExpander exprExdr; - - public IfRegexp(ModelScreen modelScreen, Element condElement) { - super (modelScreen, condElement); - this.fieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("field")); - if (this.fieldAcsr.isEmpty()) this.fieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("field-name")); - this.exprExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("expr")); - } + public static class ScreenConditionFactory extends DefaultConditionFactory { @Override - public boolean eval(Map<String, Object> context) { - Object fieldVal = this.fieldAcsr.get(context); - String expr = this.exprExdr.expandString(context); - Pattern pattern; - - try { - pattern = PatternFactory.createOrGetPerl5CompiledPattern(expr, true); - } catch (MalformedPatternException e) { - String errMsg = "Error in evaluation in if-regexp in screen: " + e.toString(); - Debug.logError(e, errMsg, module); - throw new IllegalArgumentException(errMsg); + public Condition newInstance(ModelWidget modelWidget, Element conditionElement) { + if (conditionElement == null) { + return DefaultConditionFactory.TRUE; } - - String fieldString = null; - try { - fieldString = (String) ObjectType.simpleTypeConvert(fieldVal, "String", null, (TimeZone) context.get("timeZone"), (Locale) context.get("locale"), true); - } catch (GeneralException e) { - Debug.logError(e, "Could not convert object to String, using empty String", module); + if ("if-empty-section".equals(conditionElement.getNodeName())) { + return new IfEmptySection(this, modelWidget, conditionElement); + } else { + return super.newInstance(modelWidget,conditionElement); } - // always use an empty string by default - if (fieldString == null) fieldString = ""; - - PatternMatcher matcher = new Perl5Matcher(); - return matcher.matches(fieldString, pattern); - } - } - - public static class IfEmpty extends ScreenCondition { - protected FlexibleMapAccessor<Object> fieldAcsr; - - public IfEmpty(ModelScreen modelScreen, Element condElement) { - super (modelScreen, condElement); - this.fieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("field")); - if (this.fieldAcsr.isEmpty()) this.fieldAcsr = FlexibleMapAccessor.getInstance(condElement.getAttribute("field-name")); - } - - @Override - public boolean eval(Map<String, Object> context) { - Object fieldVal = this.fieldAcsr.get(context); - return ObjectType.isEmpty(fieldVal); - } - } - public static class IfEntityPermission extends ScreenCondition { - protected EntityPermissionChecker permissionChecker; - - public IfEntityPermission(ModelScreen modelScreen, Element condElement) { - super (modelScreen, condElement); - this.permissionChecker = new EntityPermissionChecker(condElement); - } - - @Override - public boolean eval(Map<String, Object> context) { - return permissionChecker.runPermissionCheck(context); - } - } - - public static class IfEmptySection extends ScreenCondition { - protected FlexibleStringExpander sectionExdr; - - public IfEmptySection(ModelScreen modelScreen, Element condElement) { - super (modelScreen, condElement); - this.sectionExdr = FlexibleStringExpander.getInstance(condElement.getAttribute("section-name")); - } - - @Override - public boolean eval(Map<String, Object> context) { - Map<String, Object> sectionsList = UtilGenerics.toMap(context.get("sections")); - return !sectionsList.containsKey(this.sectionExdr.expandString(context)); } } } Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java Mon Jan 5 08:50:30 2015 @@ -33,16 +33,14 @@ import javax.xml.parsers.ParserConfigura import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; -import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.UtilCodec; import org.ofbiz.base.util.UtilGenerics; -import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilXml; import org.ofbiz.base.util.collections.MapStack; import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; -import org.ofbiz.entity.condition.EntityCondition; import org.ofbiz.entity.util.EntityQuery; import org.ofbiz.widget.ModelWidget; import org.ofbiz.widget.ModelWidgetAction; @@ -607,7 +605,7 @@ public abstract class ModelScreenWidget public String getTitle(Map<String, Object> context) { String title = this.titleExdr.expandString(context); - StringUtil.SimpleEncoder simpleEncoder = (StringUtil.SimpleEncoder) context.get("simpleEncoder"); + UtilCodec.SimpleEncoder simpleEncoder = (UtilCodec.SimpleEncoder) context.get("simpleEncoder"); if (simpleEncoder != null) { title = simpleEncoder.encode(title); } @@ -893,7 +891,8 @@ public abstract class ModelScreenWidget public String getText(Map<String, Object> context) { String text = this.textExdr.expandString(context); - StringUtil.SimpleEncoder simpleEncoder = (StringUtil.SimpleEncoder) context.get("simpleEncoder"); + // FIXME: Encoding should be done by the renderer, not by the model. + UtilCodec.SimpleEncoder simpleEncoder = (UtilCodec.SimpleEncoder) context.get("simpleEncoder"); if (simpleEncoder != null) { text = simpleEncoder.encode(text); } @@ -1042,16 +1041,7 @@ public abstract class ModelScreenWidget Debug.logError(e, errMsg, module); throw new RuntimeException(errMsg); } - StringBuffer renderBuffer = new StringBuffer(); - modelTree.renderTreeString(renderBuffer, context, treeStringRenderer); - try { - writer.append(renderBuffer.toString()); - } catch (IOException e) { - String errMsg = "Error rendering included tree named [" + name + "] at location [" + location + "]: " + e.toString(); - Debug.logError(e, errMsg, module); - throw new RuntimeException(errMsg); - } - + modelTree.renderTreeString(writer, context, treeStringRenderer); if (protectScope) { UtilGenerics.<MapStack<String>>cast(context).pop(); } @@ -1470,7 +1460,8 @@ public abstract class ModelScreenWidget public String getText(Map<String, Object> context) { String text = this.textExdr.expandString(context); - StringUtil.SimpleEncoder simpleEncoder = (StringUtil.SimpleEncoder) context.get("simpleEncoder"); + // FIXME: Encoding should be done by the renderer, not by the model. + UtilCodec.SimpleEncoder simpleEncoder = (UtilCodec.SimpleEncoder) context.get("simpleEncoder"); if (simpleEncoder != null) { text = simpleEncoder.encode(text); } @@ -1487,9 +1478,9 @@ public abstract class ModelScreenWidget public String getTarget(Map<String, Object> context) { Map<String, Object> expanderContext = context; - StringUtil.SimpleEncoder simpleEncoder = context == null ? null : (StringUtil.SimpleEncoder) context.get("simpleEncoder"); + UtilCodec.SimpleEncoder simpleEncoder = context == null ? null : (UtilCodec.SimpleEncoder) context.get("simpleEncoder"); if (simpleEncoder != null) { - expanderContext = StringUtil.HtmlEncodingMapWrapper.getHtmlEncodingMapWrapper(context, simpleEncoder); + expanderContext = UtilCodec.HtmlEncodingMapWrapper.getHtmlEncodingMapWrapper(context, simpleEncoder); } return this.targetExdr.expandString(expanderContext); } @@ -1632,7 +1623,8 @@ public abstract class ModelScreenWidget public String getAlt(Map<String, Object> context) { String alt = this.alt.expandString(context); - StringUtil.SimpleEncoder simpleEncoder = (StringUtil.SimpleEncoder) context.get("simpleEncoder"); + // FIXME: Encoding should be done by the renderer, not by the model. + UtilCodec.SimpleEncoder simpleEncoder = (UtilCodec.SimpleEncoder) context.get("simpleEncoder"); if (simpleEncoder != null) { alt = simpleEncoder.encode(alt); } @@ -1671,8 +1663,10 @@ public abstract class ModelScreenWidget portalPage = PortalPageWorker.getPortalPage(expandedPortalPageId, context); } else { try { - portalPage = EntityQuery.use(delegator).from("PortalPage").where("portalPageId", expandedPortalPageId) - .cache().queryOne(); + portalPage = EntityQuery.use(delegator) + .from("PortalPage") + .where("portalPageId", expandedPortalPageId) + .cache().queryOne(); } catch (GenericEntityException e) { throw new RuntimeException(e); } @@ -1695,9 +1689,13 @@ public abstract class ModelScreenWidget List<GenericValue> portletAttributes = null; GenericValue portalPage = getPortalPageValue(context); String actualPortalPageId = portalPage.getString("portalPageId"); - portalPageColumns = delegator.findByAnd("PortalPageColumn", UtilMisc.toMap("portalPageId", actualPortalPageId), - UtilMisc.toList("columnSeqId"), true); - + portalPageColumns = EntityQuery.use(delegator) + .from("PortalPageColumn") + .where("portalPageId", actualPortalPageId) + .orderBy("columnSeqId") + .cache(true) + .queryList(); + // Renders the portalPage header screenStringRenderer.renderPortalPageBegin(writer, context, this); @@ -1714,8 +1712,11 @@ public abstract class ModelScreenWidget screenStringRenderer.renderPortalPageColumnBegin(writer, context, this, columnValue); // Get the Portlets located in the current column - portalPagePortlets = delegator.findByAnd("PortalPagePortletView", UtilMisc.toMap("portalPageId", portalPage.getString("portalPageId"), "columnSeqId", columnSeqId), UtilMisc.toList("sequenceNum"), false); - + portalPagePortlets = EntityQuery.use(delegator) + .from("PortalPagePortletView") + .where("portalPageId", portalPage.getString("portalPageId"), "columnSeqId", columnSeqId) + .orderBy("sequenceNum") + .queryList(); // First Portlet in a Column has no previous Portlet String prevPortletId = ""; String prevPortletSeqId = ""; @@ -1748,9 +1749,11 @@ public abstract class ModelScreenWidget context.put("nextColumnSeqId", nextColumnSeqId); // Get portlet's attributes - portletAttributes = delegator.findList("PortletAttribute", - EntityCondition.makeCondition(UtilMisc.toMap("portalPageId", portletValue.get("portalPageId"), "portalPortletId", portletValue.get("portalPortletId"), "portletSeqId", portletValue.get("portletSeqId"))), - null, null, null, false); + portletAttributes = EntityQuery.use(delegator) + .from("PortletAttribute") + .where("portalPageId", portletValue.get("portalPageId"), "portalPortletId", portletValue.get("portalPortletId"), "portletSeqId", portletValue.get("portletSeqId")) + .queryList(); + ListIterator <GenericValue>attributesIterator = portletAttributes.listIterator(); while (attributesIterator.hasNext()) { GenericValue attribute = attributesIterator.next(); Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ScreenFopViewHandler.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ScreenFopViewHandler.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ScreenFopViewHandler.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/screen/ScreenFopViewHandler.java Mon Jan 5 08:50:30 2015 @@ -32,7 +32,7 @@ import javax.xml.transform.stream.Stream import org.apache.fop.apps.Fop; import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.UtilCodec; import org.ofbiz.base.util.UtilProperties; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.Delegator; @@ -57,6 +57,7 @@ public class ScreenFopViewHandler extend /** * @see org.ofbiz.webapp.view.ViewHandler#init(javax.servlet.ServletContext) */ + @Override public void init(ServletContext context) throws ViewHandlerException { this.servletContext = context; } @@ -64,9 +65,10 @@ public class ScreenFopViewHandler extend /** * @see org.ofbiz.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ + @Override public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException { - Delegator delegator = (Delegator) request.getAttribute("delegator"); + Delegator delegator = (Delegator) request.getAttribute("delegator"); // render and obtain the XSL-FO Writer writer = new StringWriter(); try { @@ -80,7 +82,7 @@ public class ScreenFopViewHandler extend // this is the object used to render forms from their definitions screens.getContext().put("formStringRenderer", formStringRenderer); - screens.getContext().put("simpleEncoder", StringUtil.getEncoder(EntityUtilProperties.getPropertyValue("widget", getName() + ".encoder", delegator))); + screens.getContext().put("simpleEncoder", UtilCodec.getEncoder(EntityUtilProperties.getPropertyValue("widget", getName() + ".encoder", delegator))); screens.render(page); } catch (Exception e) { renderError("Problems with the response writer/output stream", e, "[Not Yet Rendered]", request, response); Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/tree/MacroTreeRenderer.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/tree/MacroTreeRenderer.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/tree/MacroTreeRenderer.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/tree/MacroTreeRenderer.java Mon Jan 5 08:50:30 2015 @@ -55,10 +55,8 @@ import freemarker.template.TemplateExcep public class MacroTreeRenderer implements TreeStringRenderer { public static final String module = MacroTreeRenderer.class.getName(); - ScreenStringRenderer screenStringRenderer = null; private Template macroLibrary; private Environment environment; - protected boolean widgetCommentsEnabled = false; public MacroTreeRenderer(String macroLibraryPath, Appendable writer) throws TemplateException, IOException { @@ -67,29 +65,20 @@ public class MacroTreeRenderer implement this.environment = FreeMarkerWorker.renderTemplate(this.macroLibrary, input, writer); } - private void executeMacro(Appendable writer, String macro) throws IOException { + private void executeMacro(String macro) throws IOException { try { Reader templateReader = new StringReader(macro); // FIXME: I am using a Date as an hack to provide a unique name for the template... - Template template = new Template((new java.util.Date()).toString(), templateReader, FreeMarkerWorker.getDefaultOfbizConfig()); + Template template = new Template((new java.util.Date()).toString(), templateReader, + FreeMarkerWorker.getDefaultOfbizConfig()); templateReader.close(); - if (writer != null) { - Map<String, Object> input = UtilMisc.toMap("key", null); - Environment tmpEnvironment = FreeMarkerWorker.renderTemplate(this.macroLibrary, input, writer); - tmpEnvironment.include(template); - } else { - this.environment.include(template); - } + this.environment.include(template); } catch (TemplateException e) { - Debug.logError(e, "Error rendering screen thru ftl", module); + Debug.logError(e, "Error rendering tree thru ftl", module); } catch (IOException e) { - Debug.logError(e, "Error rendering screen thru ftl", module); + Debug.logError(e, "Error rendering tree thru ftl", module); } } - - private void executeMacro(String macro) throws IOException { - executeMacro(null, macro); - } /** * Renders the beginning boundary comment string. @@ -98,18 +87,16 @@ public class MacroTreeRenderer implement * @param modelWidget The widget */ public void renderBeginningBoundaryComment(Appendable writer, String widgetType, ModelWidget modelWidget) throws IOException { - if (this.widgetCommentsEnabled) { - StringWriter sr = new StringWriter(); - sr.append("<@formatBoundaryComment "); - sr.append(" boundaryType=\""); - sr.append("Begin"); - sr.append("\" widgetType=\""); - sr.append(widgetType); - sr.append("\" widgetName=\""); - sr.append(modelWidget.getBoundaryCommentName()); - sr.append("\" />"); - executeMacro(sr.toString()); - } + StringWriter sr = new StringWriter(); + sr.append("<@formatBoundaryComment "); + sr.append(" boundaryType=\""); + sr.append("Begin"); + sr.append("\" widgetType=\""); + sr.append(widgetType); + sr.append("\" widgetName=\""); + sr.append(modelWidget.getBoundaryCommentName()); + sr.append("\" />"); + executeMacro(sr.toString()); } /** @@ -119,28 +106,27 @@ public class MacroTreeRenderer implement * @param modelWidget The widget */ public void renderEndingBoundaryComment(Appendable writer, String widgetType, ModelWidget modelWidget) throws IOException { - if (this.widgetCommentsEnabled) { - StringWriter sr = new StringWriter(); - sr.append("<@formatBoundaryComment "); - sr.append(" boundaryType=\""); - sr.append("End"); - sr.append("\" widgetType=\""); - sr.append(widgetType); - sr.append("\" widgetName=\""); - sr.append(modelWidget.getBoundaryCommentName()); - sr.append("\" />"); - executeMacro(sr.toString()); - } - } + StringWriter sr = new StringWriter(); + sr.append("<@formatBoundaryComment "); + sr.append(" boundaryType=\""); + sr.append("End"); + sr.append("\" widgetType=\""); + sr.append(widgetType); + sr.append("\" widgetName=\""); + sr.append(modelWidget.getBoundaryCommentName()); + sr.append("\" />"); + executeMacro(sr.toString()); + } public void renderNodeBegin(Appendable writer, Map<String, Object> context, ModelTree.ModelNode node, int depth) throws IOException { String currentNodeTrailPiped = null; List<String> currentNodeTrail = UtilGenerics.toList(context.get("currentNodeTrail")); String style = ""; - if (node.isRootNode()) { - this.widgetCommentsEnabled = ModelWidget.widgetBoundaryCommentsEnabled(context); - renderBeginningBoundaryComment(writer, "Tree Widget", node.getModelTree()); + if (node.isRootNode()) { + if (ModelWidget.widgetBoundaryCommentsEnabled(context)) { + renderBeginningBoundaryComment(writer, "Tree Widget", node.getModelTree()); + } style = "basic-tree"; } @@ -151,7 +137,7 @@ public class MacroTreeRenderer implement sr.append("\" />"); executeMacro(sr.toString()); - String pkName = node.getPkName(); + String pkName = node.getPkName(context); String entityId = null; String entryName = node.getEntryName(); if (UtilValidate.isNotEmpty(entryName)) { @@ -162,9 +148,10 @@ public class MacroTreeRenderer implement } boolean hasChildren = node.hasChildren(context); - ModelTree.ModelNode.Link expandCollapseLink = new ModelTree.ModelNode.Link(); // check to see if this node needs to be expanded. if (hasChildren && node.isExpandCollapse()) { + // FIXME: Using a widget model in this way is an ugly hack. + ModelTree.ModelNode.Link expandCollapseLink = null; String targetEntityId = null; List<String> targetNodeTrail = UtilGenerics.toList(context.get("targetNodeTrail")); if (depth < targetNodeTrail.size()) { @@ -178,7 +165,6 @@ public class MacroTreeRenderer implement context.put("processChildren", Boolean.FALSE); //expandCollapseLink.setText(" + "); currentNodeTrailPiped = StringUtil.join(currentNodeTrail, "|"); - expandCollapseLink.setStyle("collapsed"); StringBuilder target = new StringBuilder(node.getModelTree().getExpandCollapseRequest(context)); String trailName = node.getModelTree().getTrailName(context); if (target.indexOf("?") < 0) { @@ -187,7 +173,7 @@ public class MacroTreeRenderer implement target.append("&"); } target.append(trailName).append("=").append(currentNodeTrailPiped); - expandCollapseLink.setTarget(target.toString()); + expandCollapseLink = new ModelTree.ModelNode.Link("collapsed", target.toString(), " "); } } else { context.put("processChildren", Boolean.TRUE); @@ -197,7 +183,6 @@ public class MacroTreeRenderer implement if (currentNodeTrailPiped == null) { currentNodeTrailPiped = ""; } - expandCollapseLink.setStyle("expanded"); StringBuilder target = new StringBuilder(node.getModelTree().getExpandCollapseRequest(context)); String trailName = node.getModelTree().getTrailName(context); if (target.indexOf("?") < 0) { @@ -206,32 +191,35 @@ public class MacroTreeRenderer implement target.append("&"); } target.append(trailName).append("=").append(currentNodeTrailPiped); - expandCollapseLink.setTarget(target.toString()); + expandCollapseLink = new ModelTree.ModelNode.Link("expanded", target.toString(), " "); // add it so it can be remove in renderNodeEnd currentNodeTrail.add(lastContentId); } - renderLink(writer, context, expandCollapseLink); + if (expandCollapseLink != null) { + renderLink(writer, context, expandCollapseLink); + } } else if (!hasChildren) { context.put("processChildren", Boolean.FALSE); - expandCollapseLink.setStyle("leafnode"); + ModelTree.ModelNode.Link expandCollapseLink = new ModelTree.ModelNode.Link("leafnode", "", " "); renderLink(writer, context, expandCollapseLink); } } public void renderNodeEnd(Appendable writer, Map<String, Object> context, ModelTree.ModelNode node) throws IOException { Boolean processChildren = (Boolean) context.get("processChildren"); - if (node.isRootNode()) { - renderEndingBoundaryComment(writer, "Tree Widget", node.getModelTree()); - } - StringWriter sr = new StringWriter(); - sr.append("<@renderNodeEnd "); + sr.append("<@renderNodeEnd "); sr.append(" processChildren="); sr.append(Boolean.toString(processChildren.booleanValue())); sr.append(" isRootNode="); sr.append(Boolean.toString(node.isRootNode())); sr.append(" />"); - executeMacro(sr.toString()); + executeMacro(sr.toString()); + if (node.isRootNode()) { + if (ModelWidget.widgetBoundaryCommentsEnabled(context)) { + renderEndingBoundaryComment(writer, "Tree Widget", node.getModelTree()); + } + } } public void renderLastElement(Appendable writer, Map<String, Object> context, ModelTree.ModelNode node) throws IOException { @@ -369,14 +357,14 @@ public class MacroTreeRenderer implement sr.append("\" urlString=\""); sr.append(urlString); sr.append("\" />"); - executeMacro(writer, sr.toString()); + executeMacro(sr.toString()); } public ScreenStringRenderer getScreenStringRenderer(Map<String, Object> context) { ScreenRenderer screenRenderer = (ScreenRenderer)context.get("screens"); if (screenRenderer != null) { - this.screenStringRenderer = screenRenderer.getScreenStringRenderer(); + return screenRenderer.getScreenStringRenderer(); } - return this.screenStringRenderer; + return null; } } |
Free forum by Nabble | Edit this page |