com.talk2object.plum.repository.meta.Entity Maven / Gradle / Ivy
package com.talk2object.plum.repository.meta;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class Entity {
private Class entityClass;
private String name;
private List fields;
private String idFieldName;
private Class idFieldClass;
public Entity(Class entityClass) {
this.entityClass = entityClass;
this.name = entityClass.getSimpleName();
fields = new ArrayList();
}
public T getNewInstance() {
try {
return (T) entityClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public void addField(Field field) {
fields.add(field);
}
public String getName() {
return name;
}
public Class getEntityClass() {
return entityClass;
}
public List getFields() {
return fields;
}
public String getIdFieldName() {
return idFieldName;
}
public void setIdFieldName(String idFieldName) {
this.idFieldName = idFieldName;
}
public Class getIdFieldClass() {
return idFieldClass;
}
public void setIdFieldClass(Class idFieldClass) {
this.idFieldClass = idFieldClass;
}
public void setEntityClass(Class entityClass) {
this.entityClass = entityClass;
}
public void setName(String name) {
this.name = name;
}
public void setFields(List fields) {
this.fields = fields;
}
public Serializable getId(Object obj) {
try {
return (Serializable) PropertyUtils.getProperty(obj, idFieldName);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void setId(Object entity, Serializable id) {
try {
PropertyUtils.setProperty(entity, idFieldName, id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public boolean idEquals(Object obj1, Object obj2) {
if (obj1 != null && obj2 != null) {
String s1 = getId(obj1).toString();
String s2 = getId(obj2).toString();
return s1.equals(s2);
} else
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy