All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.threewks.thundr.validation.ConstraintViolationSetToMapTransformer Maven / Gradle / Ivy

The newest version!
/*
 * This file is a component of thundr, a software library from 3wks.
 * Read more: http://www.3wks.com.au/thundr
 * Copyright (C) 2013 3wks, 
 *
 * 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.threewks.thundr.validation;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.apache.commons.lang3.StringUtils;

import com.atomicleopard.expressive.ETransformer;
import com.atomicleopard.expressive.Expressive;

/**
 * Takes a Set> produced by {@link javax.validation.Validator#validate(Object, Class[])} and
 * transforms it into a serialization friendly graph of maps.
 *
 * @param  the bean type being validated
 */
public class ConstraintViolationSetToMapTransformer implements ETransformer>, Map> {

	private static final String PROPERTY_PATH_DELIMITER = ".";
	private static final String PROPERTY_PATH_DELIMITER_PATTERN = "\\.";

	/**
	 * Transform a set of constraint violations into a serialization friendly graph of maps. The map keys relate to fields being
	 * validated and the values are the corresponding error messages.
	 *
	 * @param constraintViolations a set of constraint violations
	 * @return a graph of Maps
	 */
	@Override
	public Map from(Set> constraintViolations) {
		Map errorMap = Expressive.map();
		for (ConstraintViolation violation : constraintViolations) {
			putError(errorMap, violation.getPropertyPath().toString(), violation.getMessage());
		}
		return errorMap;
	}

	/**
	 * Add an error to the error map. Nested properties are put into sub maps to make for nice property style
	 * lookups in JavaScript, EL etc.
	 */
	@SuppressWarnings("unchecked")
	private void putError(Map errorMap, String propertyPath, String message) {
		if (propertyPath.contains(PROPERTY_PATH_DELIMITER)) {
			List parts = Arrays.asList(propertyPath.split(PROPERTY_PATH_DELIMITER_PATTERN));
			String key = parts.get(0);

			Map subMap = (Map) errorMap.get(key);
			if (subMap == null) {
				subMap = Expressive.map();
				errorMap.put(key, subMap);
			}

			String subPath = StringUtils.join(parts.subList(1, parts.size()), PROPERTY_PATH_DELIMITER);
			putError(subMap, subPath, message);
			return;
		}

		errorMap.put(propertyPath, message);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy