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

com.wesleyhome.dao.processor.DAOAnnotaionValidator Maven / Gradle / Ivy

/*
 * @(#)DAOAnnotaionValidator.java
 * 
 * (C) Copyright 2014 by Travelers
 * All Rights Reserved.
 * 
 * This software is the confidential and proprietary information
 * of the Travelers Corporation. ("Confidential Information").
 * Redistribution of the source code or binary form is not permitted
 * without prior authorization from Travelers.
 */
package com.wesleyhome.dao.processor;

import java.util.Date;
import java.util.List;
import javax.annotation.processing.Messager;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.VariableElement;
import javax.tools.Diagnostic.Kind;
import com.wesleyhome.dao.annotations.QueryParameterType;
import com.wesleyhome.dao.processor.model.EntityInfo;
import com.wesleyhome.dao.processor.model.QueryParameterInfo;

/**
 * The DAOAnnotaionValidator class is a
 * 
 * @author
 * @since
 */
public class DAOAnnotaionValidator {

	public static final DAOAnnotaionValidator instance = new DAOAnnotaionValidator();
	private Boolean valid;

	/**
	 * 
	 */
	private DAOAnnotaionValidator() {
	}

	/**
	 * @param entityInfo
	 * @param messager
	 * @return
	 */
	public boolean validate(final EntityInfo entityInfo, final Messager messager) {
		if (valid == null) {
			valid = validateQueryParameters(entityInfo, messager);
		}
		return valid.booleanValue();
	}

	/**
	 * @param entityInfo
	 * @param messager
	 * @return
	 */
	private boolean validateQueryParameters(final EntityInfo entityInfo, final Messager messager) {
		List queryParameterInfoList = entityInfo.queryParameterInfoList;
		boolean good = true;
		for (QueryParameterInfo queryParameterInfo : queryParameterInfoList) {
			good = validateType(queryParameterInfo, messager) && good;
		}
		return good;
	}

	/**
	 * @param queryParameterInfo
	 * @param messages
	 */
	private boolean validateType(final QueryParameterInfo queryParameterInfo, final Messager messager) {
		Class classType;
		try {
			classType = Class.forName(queryParameterInfo.fieldTypeClassName);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
		QueryParameterType queryParameterType = queryParameterInfo.queryParameterType;
		VariableElement fieldElement = queryParameterInfo.fieldElement;
		AnnotationMirror queryParameterAnnotation = queryParameterInfo.queryParameterAnnotation;
		AnnotationValue queryParameterTypeValue = queryParameterInfo.queryParameterTypeValue;
		switch(queryParameterType) {
			case STARTS_WITH:
			case ENDS_WITH:
			case CONTAINS:
				if (!String.class.equals(classType)) {
					String message = String.format("%s can only be used for String variables", queryParameterType);
					messager.printMessage(Kind.ERROR, message, fieldElement, queryParameterAnnotation, queryParameterTypeValue);
					return false;
				}
				return true;
			case BEFORE:
			case AFTER:
			case ON_OR_AFTER:
			case ON_OR_BEFORE:
				if (!Date.class.isAssignableFrom(classType)) {
					String message = String.format("%s can only be used for Date variables", queryParameterType);
					messager.printMessage(Kind.ERROR, message, fieldElement, queryParameterAnnotation, queryParameterTypeValue);
					return false;
				}
				return true;
			case BETWEEN:
			case LESS_THAN:
			case GREATER_THAN:
			case LESS_THAN_OR_EQUAL:
			case GREATER_THAN_OR_EQUAL:
				if (!Comparable.class.isAssignableFrom(classType)) {
					String message = String.format("%s can only be used for Comparable variables", queryParameterType);
					messager.printMessage(Kind.ERROR, message, fieldElement, queryParameterAnnotation, queryParameterTypeValue);
					return false;
				}
				return true;
			case EQUALS:
			case IN:
			default:
				return true;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy