blob: 79ee07289af9eaea8af9a22a82e8624e96648919 [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.ecview.core.common.editpart.emf.validation;
import java.util.HashMap;
import java.util.Map;
import javax.validation.ValidatorFactory;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.osbp.ecview.core.common.context.IViewContext;
import org.eclipse.osbp.ecview.core.common.editpart.emf.validation.validator.BeanValidationValidator;
import org.eclipse.osbp.ecview.core.common.editpart.validation.IBeanValidationValidatorEditpart;
import org.eclipse.osbp.ecview.core.common.model.core.YField;
import org.eclipse.osbp.ecview.core.common.model.validation.ValidationPackage;
import org.eclipse.osbp.ecview.core.common.model.validation.YBeanValidationValidator;
import org.eclipse.osbp.ecview.core.common.model.validation.YMaxLengthValidationConfig;
import org.eclipse.osbp.ecview.core.common.validation.IValidationConfig;
import org.eclipse.osbp.ecview.core.common.validation.IValidator;
import org.eclipse.osbp.runtime.common.i18n.II18nService;
import org.eclipse.osbp.runtime.common.types.ITypeProviderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BeanValidationValidatorEditpart extends
ValidatorEditpart<YBeanValidationValidator> implements
IBeanValidationValidatorEditpart {
private static final Logger LOGGER = LoggerFactory
.getLogger(BeanValidationValidatorEditpart.class);
private ValidationConfigToValidatorBridge bridgeObserver;
public BeanValidationValidatorEditpart() {
super(
ValidationPackage.Literals.YBEAN_VALIDATION_VALIDATOR_CONFIG__BVAL_CLASS,
ValidationPackage.Literals.YBEAN_VALIDATION_VALIDATOR_CONFIG__BVAL_CLASS_FULLY_QUALIFIED_NAME,
ValidationPackage.Literals.YBEAN_VALIDATION_VALIDATOR_CONFIG__BVAL_PROPERTY);
}
@Override
protected IValidator createValidator() {
YBeanValidationValidator yModel = getModel();
ValidatorFactory jsr303ValidatorFactory = (ValidatorFactory) getViewContext(
yModel).getService(ValidatorFactory.class.getName());
if (jsr303ValidatorFactory == null) {
LOGGER.error("JSR303ValidatorFactory not available.");
return null;
}
Class<?> beanClass = yModel.getBvalClass();
if (beanClass == null) {
ITypeProviderService service = getViewContext(getModel())
.getService(ITypeProviderService.class.getName());
beanClass = service.forName(null, getModel()
.getBvalClassFullyQualifiedName());
}
YField yField = (YField) yModel.eContainer();
BeanValidationValidator validator = new BeanValidationValidator(
beanClass, yModel.getBvalProperty(), jsr303ValidatorFactory,
yField.getId(), yField.getLabelI18nKey());
IViewContext context = getViewContext(getModel());
validator.setI18nService((II18nService) context
.getService(II18nService.class.getName()));
validator.setLocale(context.getLocale());
return validator;
}
@Override
public void setConfig(IValidationConfig config) {
YMaxLengthValidationConfig validatable = (YMaxLengthValidationConfig) config
.getValidationSettings();
// create an observer that transfers the changes at the validatable to
// the validator
Map<EStructuralFeature, EStructuralFeature> mapping = new HashMap<EStructuralFeature, EStructuralFeature>();
mapping.put(
ValidationPackage.Literals.YBEAN_VALIDATION_VALIDATOR_CONFIG__BVAL_CLASS,
ValidationPackage.Literals.YBEAN_VALIDATION_VALIDATOR_CONFIG__BVAL_CLASS);
mapping.put(
ValidationPackage.Literals.YBEAN_VALIDATION_VALIDATOR_CONFIG__BVAL_CLASS_FULLY_QUALIFIED_NAME,
ValidationPackage.Literals.YBEAN_VALIDATION_VALIDATOR_CONFIG__BVAL_CLASS_FULLY_QUALIFIED_NAME);
mapping.put(
ValidationPackage.Literals.YBEAN_VALIDATION_VALIDATOR_CONFIG__BVAL_PROPERTY,
ValidationPackage.Literals.YBEAN_VALIDATION_VALIDATOR_CONFIG__BVAL_PROPERTY);
bridgeObserver = new ValidationConfigToValidatorBridge(validatable,
getModel(), mapping);
}
@Override
protected void internalDispose() {
try {
if (bridgeObserver != null) {
bridgeObserver.dispose();
bridgeObserver = null;
}
} finally {
super.internalDispose();
}
}
}