-
-
Save klebergraciasoares/50e7319adea5b804eba0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package andrerocker.util; | |
import java.lang.reflect.Field; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import javax.persistence.Entity; | |
import javax.persistence.Transient; | |
public class PesquisaUtil | |
{ | |
public static <T> T populaParaTodosAtributos(String valor, Class<T> clazz) throws Exception | |
{ | |
T principal = clazz.newInstance(); | |
HashMap<String, Field> fields = fields(principal, null); | |
for(String key: fields.keySet()) | |
fields.get(key).set(principal, valor); | |
HashMap<String, Field> filhos = entidades(principal); | |
for(String key: filhos.keySet()) | |
{ | |
Field filho = filhos.get(key); | |
Object instanciaEntidade = filho.getType().newInstance(); | |
filho.set(principal, instanciaEntidade); | |
HashMap<String, Field> fieldsFilho = fields(instanciaEntidade, key); | |
for(String fieldKey: fieldsFilho.keySet()) | |
fieldsFilho.get(fieldKey).set(instanciaEntidade, valor); | |
} | |
return principal; | |
} | |
public static String construiQueryPesquisa(String template, Object instancia) throws Exception | |
{ | |
String hql = construiQueryPesquisa(template, instancia, null, 1); | |
contadorProfundidade = 0; | |
return hql; | |
} | |
private static int contadorProfundidade = 0; | |
private static String construiQueryPesquisa(String template, Object instancia, String pai, int profundidadeMaxima) throws Exception | |
{ | |
boolean principal = pai==null; | |
HashMap<String, Field> fields = fields(instancia, pai); | |
String where = principal?"WHERE ":""; | |
Iterator<String> it = fields.keySet().iterator(); | |
for(int i=0; it.hasNext(); i++) | |
{ | |
if(i>0) | |
where += "OR "; | |
String key = it.next(); | |
Field field = fields.get(key); | |
where += String.format("obj.%s like '%%%s%%' ", key, field.get(instancia)); | |
} | |
if(contadorProfundidade < profundidadeMaxima) | |
{ | |
contadorProfundidade++; | |
HashMap<String, Field> entidades = entidades(instancia); | |
for(String key: entidades.keySet()) | |
where += construiQueryPesquisa("OR", entidades.get(key).get(instancia), key, profundidadeMaxima); | |
} | |
return String.format("%s %s", template, where); | |
} | |
private static HashMap<String, Field> fields(Object instancia, String pai) throws Exception | |
{ | |
HashMap<String, Field> results = new HashMap<String, Field>(); | |
Field[] fields = instancia.getClass().getDeclaredFields(); | |
for(Field field: fields) | |
{ | |
field.setAccessible(true); | |
String name = field.getName(); | |
Class<?> value = field.getType(); | |
if(value.equals(String.class) && !field.isAnnotationPresent(Transient.class)) | |
{ | |
if(pai==null) | |
results.put(name, field); | |
else | |
results.put(pai+"."+name, field); | |
} | |
} | |
return results; | |
} | |
private static HashMap<String, Field> entidades(Object instancia) throws Exception | |
{ | |
HashMap<String, Field> results = new HashMap<String, Field>(); | |
Field[] fields = instancia.getClass().getDeclaredFields(); | |
for(Field field: fields) | |
{ | |
field.setAccessible(true); | |
String name = field.getName(); | |
Class<?> value = field.getType(); | |
if(value.isAnnotationPresent(Entity.class)) | |
results.put(name, field); | |
} | |
return results; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment