/*
 * Copyright 2018 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.fhir.core.model;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.b2international.snowowl.fhir.core.exceptions.ValidationException;

/**
 * Annotation based builder superclass for FHIR model classes and data types.
 * 
 * @since 6.4
 */
public abstract class ValidatingBuilder<T> {

	private ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

	public void validateModel(T model) {
		Validator validator = factory.getValidator();
		Set<ConstraintViolation<T>> violations = validator.validate(model);

		if (!violations.isEmpty()) {
			throw new ValidationException(violations);
		}
	}

	public T build() {
		T model = doBuild();
		validateModel(model);
		return model;
	}
	
	/**
 	 * Performs the actual building of the model instance.
 	 * @return T model instance built
 	 */
 	protected abstract T doBuild();

}

@@ -741,121 +741,8 @@
 ion;
-%0Aimport javax.validation.Validation;%0Aimport javax.validation.Validator;%0Aimport javax.validation.ValidatorFactory;
 %0A%0Aim
@@ -815,16 +815,87 @@
 ception;
+%0Aimport com.b2international.snowowl.hibernate.validator.ValidationUtil;
 %0A%0A/**%0A *
@@ -1044,171 +1044,43 @@
 %0A%0A%09p
-rivate ValidatorFactory factory = Validation.buildDefaultValidatorFactory();%0A%0A%09public void validateModel(T model) %7B%0A%09%09Validator validator = factory.getValidator();
+ublic void validateModel(T model) %7B
 %0A%09%09S
@@ -1119,25 +1119,43 @@
 tions = 
-v
+ValidationUtil.getV
 alidator
 .validat
@@ -1146,16 +1146,18 @@
 alidator
+()
 .validat

use ValidationUtil instead of custom ValidatorFactory