Example usage for org.springframework.validation.beanvalidation LocalValidatorFactoryBean validate

List of usage examples for org.springframework.validation.beanvalidation LocalValidatorFactoryBean validate

Introduction

In this page you can find the example usage for org.springframework.validation.beanvalidation LocalValidatorFactoryBean validate.

Prototype

@Override
    public void validate(Object target, Errors errors, Object... validationHints) 

Source Link

Usage

From source file:org.jahia.services.content.JCRSessionWrapper.java

protected CompositeConstraintViolationException validateNodes(Collection<JCRNodeWrapper> nodes,
        CompositeConstraintViolationException ccve, final int operationType)
        throws ConstraintViolationException, RepositoryException {
    boolean isImportOperation = operationType == JCRObservationManager.IMPORT;
    for (JCRNodeWrapper node : nodes) {
        try {/*from ww  w. j a  v  a  2  s.c o  m*/
            for (String s : node.getNodeTypes()) {
                Collection<ExtendedPropertyDefinition> propDefs = NodeTypeRegistry.getInstance().getNodeType(s)
                        .getPropertyDefinitionsAsMap().values();
                for (ExtendedPropertyDefinition propertyDefinition : propDefs) {
                    String propertyName = propertyDefinition.getName();
                    if (propertyDefinition.isMandatory()
                            && propertyDefinition.getRequiredType() != PropertyType.WEAKREFERENCE
                            && propertyDefinition.getRequiredType() != PropertyType.REFERENCE
                            && !propertyDefinition.isProtected()
                            && (!propertyDefinition.isInternationalized() || getLocale() != null)
                            && (!node.hasProperty(propertyName) || (!propertyDefinition.isMultiple()
                                    && propertyDefinition.getRequiredType() != PropertyType.BINARY
                                    && StringUtils.isEmpty(node.getProperty(propertyName).getString()))

                            )) {

                        Locale errorLocale = null;
                        if (propertyDefinition.isInternationalized()) {
                            errorLocale = getLocale();
                        }

                        ccve = addError(ccve,
                                new PropertyConstraintViolationException(node,
                                        Messages.getInternal("label.error.mandatoryField",
                                                LocaleContextHolder.getLocale(), "Field is mandatory"),
                                        errorLocale, propertyDefinition));
                    }
                }
            }
        } catch (InvalidItemStateException e) {
            logger.debug("A new node can no longer be accessed to run validation checks", e);
        }

        Map<String, Constructor<?>> validators = sessionFactory.getDefaultProvider().getValidators();
        Set<ConstraintViolation<JCRNodeValidator>> constraintViolations = new LinkedHashSet<ConstraintViolation<JCRNodeValidator>>();
        for (Map.Entry<String, Constructor<?>> validatorEntry : validators.entrySet()) {
            if (node.isNodeType(validatorEntry.getKey())) {
                try {
                    JCRNodeValidator validatorDecoratedNode = (JCRNodeValidator) validatorEntry.getValue()
                            .newInstance(node);
                    LocalValidatorFactoryBean validatorFactoryBean = sessionFactory.getValidatorFactoryBean();

                    // if we are in non-import operation we enforce Default and DefaultSkipOnImportGroup;
                    // if we are in an import operation we do not enforce the DefaultSkipOnImportGroup, but rather only the Default one
                    Set<ConstraintViolation<JCRNodeValidator>> validate = !isImportOperation
                            ? validatorFactoryBean.validate(validatorDecoratedNode, Default.class,
                                    DefaultSkipOnImportGroup.class)
                            : validatorFactoryBean.validate(validatorDecoratedNode, Default.class);

                    if (validate.isEmpty()) {
                        // we enforce advanced validations only in case the default group succeeds

                        // if we are in non-import operation we enforce both AdvancedGroup and AdvancedSkipOnImportGroup;
                        // if we are in an import operation we do not enforce the AdvancedSkipOnImportGroup, but rather only the
                        // AdvancedGroup one
                        validate = !isImportOperation
                                ? validatorFactoryBean.validate(validatorDecoratedNode, AdvancedGroup.class,
                                        AdvancedSkipOnImportGroup.class)
                                : validatorFactoryBean.validate(validatorDecoratedNode, AdvancedGroup.class);
                    }

                    constraintViolations.addAll(validate);
                } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
        for (ConstraintViolation<JCRNodeValidator> constraintViolation : constraintViolations) {
            String propertyName;
            try {
                Method propertyNameGetter = constraintViolation.getConstraintDescriptor().getAnnotation()
                        .annotationType().getMethod("propertyName");
                propertyName = (String) propertyNameGetter
                        .invoke(constraintViolation.getConstraintDescriptor().getAnnotation());
            } catch (Exception e) {
                propertyName = constraintViolation.getPropertyPath().toString();
            }
            if (StringUtils.isNotBlank(propertyName)) {
                ExtendedPropertyDefinition propertyDefinition = node
                        .getApplicablePropertyDefinition(propertyName);
                if (propertyDefinition == null) {
                    propertyDefinition = node
                            .getApplicablePropertyDefinition(propertyName.replaceFirst("_", ":"));
                }
                if (propertyDefinition != null) {
                    Locale errorLocale = null;
                    if (propertyDefinition.isInternationalized()) {
                        errorLocale = getLocale();
                        if (errorLocale == null) {
                            continue;
                        }
                    }
                    ccve = addError(ccve, new PropertyConstraintViolationException(node,
                            constraintViolation.getMessage(), errorLocale, propertyDefinition));
                } else {
                    ccve = addError(ccve,
                            new NodeConstraintViolationException(node, constraintViolation.getMessage(), null));
                }
            } else {
                ccve = addError(ccve,
                        new NodeConstraintViolationException(node, constraintViolation.getMessage(), null));
            }
        }
    }

    return ccve;
}