
org.javabeanstack.data.DataInfo Maven / Gradle / Ivy
The newest version!
/*
* JavaBeanStack FrameWork
*
* Copyright (C) 2017 Jorge Enciso
* Email: [email protected]
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.javabeanstack.data;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.apache.log4j.Logger;
import org.javabeanstack.error.ErrorManager;
import org.javabeanstack.util.Strings;
/**
* Esta clase contiene metodos que brindan información sobre los modelos ejb
* ej. si un campo es primarykey,
*
* @author Jorge Enciso
*/
public class DataInfo {
private static final Logger LOGGER = Logger.getLogger(DataInfo.class);
/**
* Determina si el campo o miembro dado corresponde a la clave primaria.
*
* @param
* @param classType clase DataRow dado
* @param fieldname nombre del campo
* @return Verdadero si es la clave primaria.
*/
public static boolean isPrimaryKey(Class classType, String fieldname) {
try {
Field field = DataInfo.getDeclaredField(classType, fieldname);
Id id = field.getAnnotation(Id.class);
return id != null;
} catch (Exception ex) {
return false;
}
}
/**
* Determina si un campo o miembro dado es o forma parte de una clave unica.
*
* @param
* @param classType clase DataRow dado
* @param fieldname nombre del campo
* @return Verdadero si forma parte de una clave unica.
*/
public static boolean isUniqueKey(Class classType, String fieldname) {
try {
String[] fields = DataInfo.getUniqueFields(classType);
if (fields == null) {
return false;
}
for (String field : fields) {
if (field.toLowerCase().equals(fieldname.toLowerCase())) {
return true;
}
}
return false;
} catch (Exception exp) {
return false;
}
}
/**
* Determina si un campo o miembro es una clave foranea.
*
* @param
* @param classType clase DataRow dado
* @param fieldname nombre del campo
* @return Verdadero si es una clave foranea.
*/
public static boolean isForeignKey(Class classType, String fieldname) {
try {
Field field = DataInfo.getDeclaredField(classType, fieldname);
JoinColumn idforeignkey = field.getAnnotation(JoinColumn.class);
return idforeignkey != null;
} catch (Exception ex) {
return false;
}
}
/**
* Determina si un campo existe en la clase.
*
* @param
* @param classType clase DataRow dado
* @param fieldname nombre del campo
* @return Verdadero si existe.
*/
public static boolean isFieldExist(Class classType, String fieldname) {
try {
Field field = DataInfo.getDeclaredField(classType, fieldname);
if (field != null) {
return true;
}
} catch (Exception ex) {
ErrorManager.showError(ex, LOGGER);
}
return false;
}
/**
* Determina si un campo o miembro es una clave foranea cuyo fetch = Lazy
*
* @param
* @param classType clase DataRow dado
* @param fieldname nombre del campo
* @return Verdadero si fetch = Lazy
*/
public static boolean isLazyFetch(Class classType, String fieldname) {
try {
Field field = DataInfo.getDeclaredField(classType, fieldname);
OneToMany annotation = field.getAnnotation(OneToMany.class);
return annotation.fetch() == FetchType.LAZY;
} catch (Exception ex) {
return false;
}
}
/**
* Devuelve los miembros lazy
*
* @param
* @param classType clase DataRow dado
* @return una lista con los campos o miembros cuyo fetch = Lazy
*/
public static List getLazyMembers(Class classType) {
try {
Field[] fields = classType.getDeclaredFields();
List lazyFields = new ArrayList();
for (Field field : fields) {
OneToMany annotation = field.getAnnotation(OneToMany.class);
if (annotation != null) {
if (annotation.fetch() == FetchType.LAZY) {
lazyFields.add(field);
}
}
}
return lazyFields;
} catch (Exception ex) {
ErrorManager.showError(ex, LOGGER);
}
return null;
}
/**
* Devuelve el nombre de la tabla o vista a la que esta mapeada
*
* @param
* @param classType clase DataRow
* @return nombre de la tabla
*/
public static String getTableName(Class classType) {
try {
Table entity = classType.getAnnotation(Table.class);
return entity.name();
} catch (Exception ex) {
ErrorManager.showError(ex, LOGGER);
}
return "";
}
/**
* Crea una sentencia para buscar un registro especifico utilizando los
* valores de los campos unicos.
*
* @param ejb
* @return sentencia con la expresión de filtro de/los campos unicos.
*/
public static String createQueryUkCommand(IDataRow ejb) {
try {
// Crear sentencia para buscar el registro
String command = "select o from " + ejb.getClass().getSimpleName() + " o ";
if (!ejb.getIdFunctionFind().equals("")) {
command = command + " where " + "o." + DataInfo.getIdFieldName(ejb.getClass()) + " = :" + ejb.getIdFunctionFind();
return command;
}
String uniqueFilter = "";
String separador = "";
String[] uniqueList = DataInfo.getUniqueFields(ejb.getClass());
if (uniqueList == null) {
return null;
}
for (String fieldName : uniqueList) {
uniqueFilter = uniqueFilter + separador + " o." + fieldName + " = :" + fieldName;
separador = " and ";
}
command = command + " where " + uniqueFilter;
return command;
} catch (Exception ex) {
ErrorManager.showError(ex, LOGGER);
}
return null;
}
/**
* Busca y devuelve el nombre del campo clave
*
* @param
* @param classType
* @return el nombre del campo que conforma la clave primaria, nulo si no
* pudo conseguirla.
*/
public static String getIdFieldName(Class classType) {
try {
Field[] fields = classType.getDeclaredFields();
for (Field field : fields) {
Id id = field.getAnnotation(Id.class);
if (id != null) {
return field.getName();
}
}
return null;
} catch (SecurityException ex) {
ErrorManager.showError(ex, LOGGER);
}
return null;
}
/**
* Devuelve una lista conteniendo los campos que conforman la clave unica.
*
* @param
* @param classType
* @return lista conteniendo los campos que conforman la clave unica.
*/
public static String[] getUniqueFields(Class classType) {
try {
String[] uniqueConst = classType.getAnnotation(Table.class).uniqueConstraints()[0].columnNames();
if (uniqueConst != null) {
return uniqueConst;
}
} catch (Exception ex) {
ErrorManager.showError(ex, LOGGER);
}
return null;
}
/**
* Devuelve el valor de la clave primaria.
*
* @param ejb Objeto row.
* @return valor de la clave primaria.
*/
public static Object getIdvalue(IDataRow ejb) {
try {
String field = DataInfo.getIdFieldName(ejb.getClass());
if (field != null) {
return ejb.getValue(field);
}
return null;
} catch (Exception ex) {
ErrorManager.showError(ex, LOGGER);
}
return null;
}
/**
* Asigna un valor a la clave primaria.
*
* @param ejb instancia del objeto row.
* @param value valor.
* @return verdadero si tuvo exito o falso si no.
*/
public static boolean setIdvalue(IDataRow ejb, Object value) {
try {
String field = DataInfo.getIdFieldName(ejb.getClass());
if (field != null) {
ejb.setValue(field, value);
return true;
}
} catch (Exception ex) {
ErrorManager.showError(ex, LOGGER);
}
return false;
}
/**
* Devuelve el valor por defecto de un atributo de la instancia de un objeto
* row
*
* @param
* @param ejb instancia del objeto DataRow
* @param fieldname nombre del campo
* @return valor por defecto oara el campo o atributo dado.
*/
public static Object getDefaultValue(T ejb, String fieldname) {
Object valor = null;
Class> classObj = ejb.getClass();
try {
Field field = DataInfo.getDeclaredField(classObj, fieldname + "_default");
field.setAccessible(true);
valor = field.get(ejb);
} catch (SecurityException | IllegalArgumentException | IllegalAccessException ex) {
ErrorManager.showError(ex, LOGGER);
}
return valor;
}
/**
* Asigna el valor por defecto a un atributo de la instancia de un objeto
* row
*
* @param
* @param ejb instancia del objeto DataRow
* @param fieldname nombre del campo
*/
public static void setDefaultValue(T ejb, String fieldname) {
Object valor;
Class> classObj = ejb.getClass();
try {
Field field = DataInfo.getDeclaredField(classObj, fieldname);
field.setAccessible(true);
valor = DataInfo.getDefaultValue(ejb, fieldname);
if (valor != null) {
field.set(ejb, valor);
if (ejb.getAction() == 0) {
ejb.setAction(IDataRow.UPDATE);
}
}
} catch (SecurityException | IllegalArgumentException | IllegalAccessException ex) {
ErrorManager.showError(ex, LOGGER);
}
}
/**
* Devuelve el valor de un atributo que hace referencia a un objeto
* relacionado
*
* @param
* @param ejb Objeto row.
* @param objrelaName Nombre del atributo.
* @return el valor de un atributo que hace referencia a un objeto relacionado
*/
public static T getObjFk(T ejb, String objrelaName) {
try {
Field field;
try {
field = DataInfo.getDeclaredField(ejb.getClass(), objrelaName.toLowerCase());
} catch (Exception ex) {
field = null;
}
if (field == null) {
field = DataInfo.getDeclaredField(ejb.getClass(), "id" + objrelaName.toLowerCase());
}
field.setAccessible(true);
Object obj = field.get(ejb);
if (obj instanceof IDataRow) {
return (T) obj;
}
return null;
} catch (IllegalAccessException | IllegalArgumentException | SecurityException ex) {
ErrorManager.showError(ex, LOGGER);
}
return null;
}
/**
* Devuelve un campo o miembro solicitado de una clase sin importar si el
* nombre esta en mayuscula o minuscula.
*
* @param classType clase
* @param fieldname Nombre del campo o atributo.
* @return campo o miembro solicitado.
*/
public static Field getDeclaredField(Class classType, String fieldname) {
Field field = null;
try {
if (fieldname.contains(".")) {
String className = fieldname.substring(0, Strings.findString(".", fieldname));
Class classMember = classType.getDeclaredField(className).getType();
fieldname = fieldname.substring(Strings.findString(".", fieldname) + 1);
field = getDeclaredField(classMember, fieldname);
} else {
field = classType.getDeclaredField(fieldname);
}
} catch (NoSuchFieldException ex) {
field = null;
} catch (SecurityException ex) {
ErrorManager.showError(ex, LOGGER);
}
if (field != null) {
return field;
}
Field[] fields = classType.getDeclaredFields();
for (Field field1 : fields) {
if (field1.getName().toLowerCase().equals(fieldname.toLowerCase())) {
return field1;
}
}
return null;
}
/**
* Devuelve la lista de campos/atributos de una clase
* @param classType clase DataRow
* @return lista de campos
*/
public static Field[] getDeclaredFields(Class classType) {
Field[] fields;
try {
fields = classType.getDeclaredFields();
} catch (SecurityException ex) {
return null;
}
return fields;
}
/**
* Devuelve el tipo de dato de un campo o atributo solicitado
* @param classType clase DataRow
* @param fieldname nombre del campo
* @return tipo de dato de un campo o atributo solicitado
*/
public static Class getFieldType(Class classType, String fieldname) {
Class cls = null;
try {
if (fieldname.contains(".")) {
String className = fieldname.substring(0, Strings.findString(".", fieldname));
Class classMember = classType.getDeclaredField(className).getType();
fieldname = fieldname.substring(Strings.findString(".", fieldname) + 1);
cls = getFieldType(classMember, fieldname);
} else {
cls = getDeclaredField(classType, fieldname).getType();
}
} catch (NoSuchFieldException ex) {
cls = null;
} catch (SecurityException ex) {
ErrorManager.showError(ex, LOGGER);
}
return cls;
}
/**
* Devuelve el valor de un campo o atributo
* @param ejb objeto DataRow
* @param fieldname nombre del campo
* @return valor del campo solicitado.
*/
public static Object getFieldValue(Object ejb, String fieldname) {
Object value;
try {
// Si contiene un punto significa que el campo esta en uno de sus miembros
if (fieldname.contains(".")) {
String memberName = fieldname.substring(0, Strings.findString(".", fieldname));
fieldname = fieldname.substring(Strings.findString(".", fieldname) + 1);
Field field = getDeclaredField(ejb.getClass(), memberName);
field.setAccessible(true);
Object parentValue = field.get(ejb);
value = getFieldValue(parentValue,fieldname);
} else {
Field field = getDeclaredField(ejb.getClass(), fieldname);
field.setAccessible(true);
value = field.get(ejb);
}
} catch (IllegalAccessException | IllegalArgumentException | SecurityException ex) {
return null;
}
return value;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy