![JAR search and dependency download from the Maven repository](/logo.png)
com.openthinks.libs.sql.lang.reflect.ReflectEngine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openlibs.sql Show documentation
Show all versions of openlibs.sql Show documentation
The lib of java database ORM simple implementation.
The newest version!
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
* @Title: ReflectEngine.java
* @Package openthinks.libs.sql.lang.reflect
* @Description: TODO
* @author dailey
* @date 2012-11-5
* @version V1.0
*/
package com.openthinks.libs.sql.lang.reflect;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import com.openthinks.libs.sql.dhibernate.support.query.QueryFilter;
import com.openthinks.libs.sql.dhibernate.support.query.impl.FilterSQLTemplate;
import com.openthinks.libs.sql.dhibernate.support.template.FilterTemplate;
import com.openthinks.libs.sql.dhibernate.support.template.StandardSQLTemplate;
import com.openthinks.libs.sql.dhibernate.support.template.Template;
import com.openthinks.libs.sql.entity.ColumnAttributeMapping;
import com.openthinks.libs.sql.entity.Entity;
import com.openthinks.libs.sql.entity.jpa.EntityReflectHandler;
import com.openthinks.libs.sql.entity.jpa.IReflectHandler;
import com.openthinks.libs.sql.entity.jpa.JPAReflectHandler;
/**
* The engine of reflect entity, its property, mapped table and sql template
* @author dailey
*
*/
public abstract class ReflectEngine {
private static Map, IReflectHandler> map = new ConcurrentHashMap<>();
static {
register(Entity.class, new EntityReflectHandler());
register(Object.class, new JPAReflectHandler());
}
/**
* register new {@link IReflectHandler} to bind given parameter entityClass
* @param entityClass Class
* @param hander IReflectHandler
*/
public static void register(Class> entityClass, IReflectHandler hander) {
map.put(entityClass, hander);
}
/**
* set column value to corresponding entity object
* @param entity class
* @param entity entity instance
* 1. JPA annotation
* 2. Subclass of {@link Entity}
* @param columnName column name in database table
* @param columnValue column name value in database table row
* @return boolean handle success or not
*/
public static boolean propertyReflect(T entity, String columnName, Object columnValue) {
boolean isSuccess = false;
for (Entry, IReflectHandler> entry : map.entrySet()) {
Class> clzz = entry.getKey();
if (clzz.isInstance(entity)) {
try {
isSuccess = entry.getValue().handColumnField(entity, columnName, columnValue);
} catch (Exception e) {
continue;
}
if (isSuccess)
break;
}
}
return isSuccess;
}
/**
* get entity corresponding table name
* @param entity class type
* @param entityClazz Class
* 1. JPA annotation
* 2. Subclass of {@link Entity}
* @return String table name
*/
public static String getEntityTable(Class entityClazz) {
String tableName = null;
for (Entry, IReflectHandler> entry : map.entrySet()) {
Class> clzz = entry.getKey();
if (clzz.isAssignableFrom(entityClazz)) {
try {
tableName = entry.getValue().getEntityTableName(entityClazz);
} catch (Exception e) {
continue;
}
if (tableName != null)
break;
}
}
return tableName;
}
/**
* get entity corresponding table primary key
* @param entity class
* @param entityClazz Class
* 1. JPA annotation
* 2. Subclass of {@link Entity}
* @return String primary key name
*/
public static String getEntityID(Class entityClazz) {
String idName = null;
for (Entry, IReflectHandler> entry : map.entrySet()) {
Class> clzz = entry.getKey();
if (clzz.isAssignableFrom(entityClazz)) {
try {
idName = entry.getValue().getEntityIDName(entityClazz);
} catch (Exception e) {
continue;
}
if (idName != null)
break;
}
}
return idName;
}
/**
* parse entity class to {@link ColumnAttributeMapping}
* @param entity Class
* @param entityClass Class
* 1. JPA annotation
* 2. Subclass of {@link Entity}
* @return {@link ColumnAttributeMapping}
*/
public static ColumnAttributeMapping parseEntityClass(Class entityClass) {
ColumnAttributeMapping columnAttributeMapping = null;
for (Entry, IReflectHandler> entry : map.entrySet()) {
Class> clzz = entry.getKey();
if (clzz.isAssignableFrom(entityClass)) {
columnAttributeMapping = entry.getValue().parseEntityClass(entityClass);
}
}
return columnAttributeMapping;
}
/**
* create template for standard SQL by entity class
* 1. JPA annotation on param entityClass
* 2. subclass from {@link Entity}
*
* @param entity class type
* @param entityClass
* Class
* @return Template
*/
public static Template createSQLTemplate(Class entityClass) {
Template template = null;
ColumnAttributeMapping columnAttributeMapping = parseEntityClass(entityClass);
template = new StandardSQLTemplate(columnAttributeMapping, entityClass);
return template;
}
/**
* create template for standard query SQL by entity class
* 1. JPA annotation on param entityClass
* 2. subclass from {@link Entity}
* @param entity class type
* @param entityClass Class
* @param filter QueryFilter
* @return FilterTemplate
*/
public static FilterTemplate createSQLTemplate(Class entityClass, QueryFilter filter) {
FilterTemplate template = null;
ColumnAttributeMapping columnAttributeMapping = parseEntityClass(entityClass);
template = new FilterSQLTemplate(columnAttributeMapping, entityClass);
template.setFilter(filter);
return template;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy