Example usage for javax.el ValueReference getBase

List of usage examples for javax.el ValueReference getBase

Introduction

In this page you can find the example usage for javax.el ValueReference getBase.

Prototype

public Object getBase() 

Source Link

Usage

From source file:org.nuxeo.ecm.platform.ui.web.validator.DocumentConstraintValidator.java

@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    if (context == null) {
        throw new NullPointerException();
    }/*  w  w w .j a va 2s .com*/
    if (component == null) {
        throw new NullPointerException();
    }
    ValueExpression ve = component.getValueExpression("value");
    if (ve == null) {
        return;
    }

    ValueExpressionAnalyzer expressionAnalyzer = new ValueExpressionAnalyzer(ve);
    ValueReference vref = expressionAnalyzer.getReference(context.getELContext());

    if (log.isDebugEnabled()) {
        log.debug(String.format("Validating  value '%s' for expression '%s', base=%s, prop=%s", value,
                ve.getExpressionString(), vref.getBase(), vref.getProperty()));
    }

    if (isResolvable(vref, ve)) {
        List<ConstraintViolation> violations = doValidate(context, vref, ve, value);
        if (violations != null && !violations.isEmpty()) {
            Locale locale = context.getViewRoot().getLocale();
            if (violations.size() == 1) {
                ConstraintViolation v = violations.iterator().next();
                String msg = v.getMessage(locale);
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
            } else {
                Set<FacesMessage> messages = new LinkedHashSet<FacesMessage>(violations.size());
                for (ConstraintViolation v : violations) {
                    String msg = v.getMessage(locale);
                    messages.add(new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
                }
                throw new ValidatorException(messages);
            }
        }
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.validator.DocumentConstraintValidator.java

@SuppressWarnings("rawtypes")
private boolean isResolvable(ValueReference ref, ValueExpression ve) {
    if (ve == null || ref == null) {
        return false;
    }/*www  .j  av a2 s .  co m*/
    Object base = ref.getBase();
    if (base != null) {
        Class baseClass = base.getClass();
        if (baseClass != null) {
            if (DocumentPropertyContext.class.isAssignableFrom(baseClass)
                    || (Property.class.isAssignableFrom(baseClass))
                    || (ProtectedEditableModel.class.isAssignableFrom(baseClass))
                    || (ListItemMapper.class.isAssignableFrom(baseClass))) {
                return true;
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(String.format("NOT validating %s, base=%s, prop=%s", ve.getExpressionString(), base,
                ref.getProperty()));
    }
    return false;
}

From source file:org.nuxeo.ecm.platform.ui.web.validator.DocumentConstraintValidator.java

protected List<ConstraintViolation> doValidate(FacesContext context, ValueReference vref, ValueExpression e,
        Object value) {//from   w  ww .j  ava  2  s.c o  m
    DocumentValidationService validationService = Framework.getService(DocumentValidationService.class);
    // document validation
    DocumentValidationReport report = null;
    if (!validationService.isActivated(CTX_JSFVALIDATOR, null)) {
        return null;
    }
    XPathAndField field = resolveField(context, vref, e);
    if (field != null) {
        boolean validateSubs = getHandleSubProperties().booleanValue();
        // use the xpath to validate the field
        // this allow to get the custom message defined for field if there's error
        report = validationService.validate(field.xpath, value, validateSubs);
        if (log.isDebugEnabled()) {
            log.debug(String.format("VALIDATED  value '%s' for expression '%s', base=%s, prop=%s", value,
                    e.getExpressionString(), vref.getBase(), vref.getProperty()));
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug(String.format("NOT Validating  value '%s' for expression '%s', base=%s, prop=%s", value,
                    e.getExpressionString(), vref.getBase(), vref.getProperty()));
        }
    }

    if (report != null && report.hasError()) {
        return report.asList();
    }

    return null;
}

From source file:org.nuxeo.ecm.platform.ui.web.validator.DocumentConstraintValidator.java

protected XPathAndField resolveField(FacesContext context, ValueReference vref, ValueExpression ve) {
    Object base = vref.getBase();
    Object propObj = vref.getProperty();
    if (propObj != null && !(propObj instanceof String)) {
        // ignore cases where prop would not be a String
        return null;
    }/*from   w ww .j a  va 2 s .  c om*/
    String xpath = null;
    Field field = null;
    String prop = (String) propObj;
    Class<?> baseClass = base.getClass();
    if (DocumentPropertyContext.class.isAssignableFrom(baseClass)) {
        DocumentPropertyContext dc = (DocumentPropertyContext) base;
        xpath = dc.getSchema() + ":" + prop;
        field = getField(xpath);
    } else if (Property.class.isAssignableFrom(baseClass)) {
        xpath = ((Property) base).getPath() + "/" + prop;
        field = getField(((Property) base).getField(), prop);
    } else if (ProtectedEditableModel.class.isAssignableFrom(baseClass)) {
        ProtectedEditableModel model = (ProtectedEditableModel) base;
        ValueExpression listVe = model.getBinding();
        ValueExpressionAnalyzer expressionAnalyzer = new ValueExpressionAnalyzer(listVe);
        ValueReference listRef = expressionAnalyzer.getReference(context.getELContext());
        if (isResolvable(listRef, listVe)) {
            XPathAndField parentField = resolveField(context, listRef, listVe);
            if (parentField != null) {
                field = getField(parentField.field, "*");
                if (parentField.xpath == null) {
                    xpath = field.getName().getLocalName();
                } else {
                    xpath = parentField.xpath + "/" + field.getName().getLocalName();
                }
            }
        }
    } else if (ListItemMapper.class.isAssignableFrom(baseClass)) {
        ListItemMapper mapper = (ListItemMapper) base;
        ProtectedEditableModel model = mapper.getModel();
        ValueExpression listVe;
        if (model.getParent() != null) {
            // move one level up to resolve parent list binding
            listVe = model.getParent().getBinding();
        } else {
            listVe = model.getBinding();
        }
        ValueExpressionAnalyzer expressionAnalyzer = new ValueExpressionAnalyzer(listVe);
        ValueReference listRef = expressionAnalyzer.getReference(context.getELContext());
        if (isResolvable(listRef, listVe)) {
            XPathAndField parentField = resolveField(context, listRef, listVe);
            if (parentField != null) {
                field = getField(parentField.field, prop);
                if (field == null || field.getName() == null) {
                    // it should not happen but still, just in case
                    return null;
                }
                if (parentField.xpath == null) {
                    xpath = field.getName().getLocalName();
                } else {
                    xpath = parentField.xpath + "/" + field.getName().getLocalName();
                }
            }
        }
    } else {
        log.error(String.format("Cannot validate expression '%s, base=%s'", ve.getExpressionString(), base));
    }
    // cleanup / on begin or at end
    if (xpath != null) {
        xpath = StringUtils.strip(xpath, "/");
    } else if (field == null && xpath == null) {
        return null;
    }
    return new XPathAndField(field, xpath);
}