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

org.tinygroup.dao.util.DaoUtil Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
/**
 *  Copyright (c) 1997-2013, www.tinygroup.org ([email protected]).
 *
 *  Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html
 *
 *  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 org.tinygroup.dao.util;

import org.tinygroup.commons.namestrategy.NameStrategy;
import org.tinygroup.commons.namestrategy.impl.CamelCaseStrategy;
import org.tinygroup.dao.query.QueryCondition;
import org.tinygroup.dao.query.QueryObject;

import javax.persistence.Entity;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class DaoUtil {
	public static String QueryObjectToHql(QueryObject obj,List params) {
//		String fieldsStr = getFields(obj);
		StringBuffer sb = new StringBuffer();
//		sb.append("select ");
//		sb.append(fieldsStr);
		sb.append(" from ").append(obj.getEntityName());
		if(obj.getConditions() == null){
			return sb.toString();
		}
		StringBuffer conditionSb = new StringBuffer();
		for (QueryCondition conditon : obj.getConditions()) {
			if (conditionSb.length() == 0) {
				conditionSb.append(getCondition(conditon, obj,params));
			} else {
				conditionSb.append(" and ").append(getCondition(conditon, obj,params));
			}
		}
		if(conditionSb.length()!=0){
			sb.append(" where ");
			sb.append(conditionSb);
		}
		return sb.toString();
	}

	private static String getCondition(QueryCondition condition, QueryObject obj,List params) {
		
		StringBuffer sb = new StringBuffer();
		sb.append("(");
//		sb.append(entityName).append(" ").append(condition.getKey());
		sb.append(getMDAName(condition.getKey()));
		
		sb.append(condition.getOperator().getOperator());
		if(!condition.getOperator().isSingleOperator()){
			sb.append(" ? ");
			params.add(condition.getValue());
		}
		
		//sb.append(entityName).append(".").append(condition.getValue());
		StringBuffer subSb = new StringBuffer();
		for (QueryCondition sub : condition.getConditions()) {
			if (subSb.length() == 0) {
				subSb.append(getCondition(sub, obj,params));
			} else {
				subSb.append(" and ").append(getCondition(sub, obj,params));
			}
		}
		if (subSb.length() != 0) {
			sb.append(" or (").append(subSb).append(")");
		}
		sb.append(")");
		return sb.toString();
	}
	public static NameStrategy getNameStrategy(){
		return new CamelCaseStrategy();
	}
	public static String getMDAName(String name){
		return getNameStrategy().getPropertyName(name);
	}
	public static String getDBName(String propertyName){
		return getNameStrategy().getFieldName(propertyName);
	}
	public static String getEntityName(Class entityClass){
		Annotation annotation = entityClass.getAnnotation(Entity.class);
		try {
			String annotationValue = getAnnotationStringValue(annotation,Entity.class,"name");
			if(annotationValue==null||"".equals(annotationValue))
				return entityClass.getSimpleName();
			return annotationValue;
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e);
		} catch (NoSuchMethodException e) {
			throw new RuntimeException(e);
		}
		
	}
	
	private static String getAnnotationStringValue(Annotation annotation,
			Class annotationClazz, String name)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {
		Object[] args = null;
		Class[] argsType = null;
		return annotationClazz.getMethod(name, argsType)
				.invoke(annotation, args).toString();
	}
}