package frame.data;
|
|
import java.math.BigDecimal;
|
import java.sql.PreparedStatement;
|
import java.sql.SQLException;
|
import java.text.ParseException;
|
import java.util.Arrays;
|
import java.util.Date;
|
import java.util.List;
|
|
import frame.data.convert.Convertor;
|
import frame.data.meta.EntityMeta;
|
import frame.data.meta.MetaContainer;
|
import frame.object.data.Filter;
|
import frame.object.data.Id;
|
import frame.persist.NamedSQL;
|
import frame.persist.SQLRunner;
|
import frame.util.Util;
|
|
|
public class Entity implements IDataConsumer, IDataProvider {
|
|
private EntityMeta entityMeta;
|
private Object[] dataArray;
|
|
|
public Entity(EntityMeta tableMeta) {
|
this.entityMeta = tableMeta;
|
dataArray = new Object[tableMeta.getFieldCount()];
|
}
|
|
public Entity(String dataName) throws Exception {
|
EntityMeta tableMeta = MetaContainer.get(dataName);
|
|
if (tableMeta == null) {
|
throw new Exception("table not exists: " + dataName);
|
}
|
|
this.entityMeta = tableMeta;
|
dataArray = new Object[tableMeta.getFieldCount()];
|
}
|
|
public void set(int i, Object object) {
|
dataArray[i] = object;
|
}
|
|
public void set(String name, Object object) {
|
entityMeta.setObject(dataArray, name, object);
|
}
|
|
public void setString(String name, String value) throws ParseException {
|
entityMeta.setString(dataArray, name, value);
|
}
|
|
public Object getValue(String fieldName) {
|
int idx = entityMeta.getIndex(fieldName);
|
return dataArray[idx];
|
}
|
|
public Object getValue(int idx) {
|
return dataArray[idx];
|
}
|
|
public String getString(String fieldName) {
|
int idx = entityMeta.getIndex(fieldName);
|
return getString(idx, null);
|
}
|
|
public String getString(String fieldName, String defaultValue) {
|
int idx = entityMeta.getIndex(fieldName);
|
return getString(idx, defaultValue);
|
}
|
|
public String getString(int idx, String defaultValue) {
|
try {
|
Object value = dataArray[idx];
|
Convertor convertor = entityMeta.getConvertor(idx);
|
return convertor.toString(value);
|
}
|
catch (Exception e) {
|
return "error";
|
}
|
}
|
|
public String getQuotedString(String fieldName) {
|
int idx = entityMeta.getIndex(fieldName);
|
return getQuotedString(idx, null);
|
}
|
|
public String getQuotedString(String fieldName, String defaultValue) {
|
int idx = entityMeta.getIndex(fieldName);
|
return getQuotedString(idx, defaultValue);
|
}
|
|
public String getQuotedString(int idx, String defaultValue) {
|
String result = getString(idx, defaultValue);
|
return Util.quotedStr(result);
|
}
|
|
public String getJSONString(String fieldName) throws Exception {
|
int idx = entityMeta.getIndex(fieldName);
|
return getJSONString(idx);
|
}
|
|
public String getJSONString(int idx) throws Exception {
|
Object value = dataArray[idx];
|
Convertor convertor = entityMeta.getConvertor(idx);
|
return convertor.toJSONString(value);
|
}
|
|
public String getSQLString(String fieldName) throws Exception {
|
int idx = entityMeta.getIndex(fieldName);
|
return getSQLString(idx);
|
}
|
|
public String getSQLString(int idx) throws Exception {
|
Object value = dataArray[idx];
|
Convertor convertor = entityMeta.getConvertor(idx);
|
return convertor.toSqlString(value);
|
}
|
|
public String getSchemaString(String fieldName) throws Exception {
|
int idx = entityMeta.getIndex(fieldName);
|
return getSchemaString(idx);
|
}
|
|
public String getSchemaString(int idx) throws Exception {
|
Object value = dataArray[idx];
|
Convertor convertor = entityMeta.getConvertor(idx);
|
return convertor.toSchemaString(value);
|
}
|
|
public boolean getBoolean(String fieldName) throws Exception {
|
int idx = entityMeta.getIndex(fieldName);
|
return getBoolean(idx, false);
|
}
|
|
public boolean getBoolean(String fieldName, boolean defaultValue) throws Exception {
|
int idx = entityMeta.getIndex(fieldName);
|
return getBoolean(idx, defaultValue);
|
}
|
|
public boolean getBoolean(int idx, boolean defaultValue) throws Exception {
|
Object value = dataArray[idx];
|
Convertor convertor = entityMeta.getConvertor(idx);
|
return convertor.toBoolean(value, defaultValue);
|
}
|
|
public boolean getDate(String fieldName) throws Exception {
|
int idx = entityMeta.getIndex(fieldName);
|
return getDate(idx, null);
|
}
|
|
public boolean getDate(String fieldName, Date defaultValue) throws Exception {
|
int idx = entityMeta.getIndex(fieldName);
|
return getDate(idx, defaultValue);
|
}
|
|
public boolean getDate(int idx, Date defaultValue) throws Exception {
|
Object value = dataArray[idx];
|
Convertor convertor = entityMeta.getConvertor(idx);
|
return convertor.toDate(value, defaultValue);
|
}
|
|
public int getInt(String fieldName) throws Exception {
|
int idx = entityMeta.getIndex(fieldName);
|
return getInt(idx, 0);
|
}
|
|
public int getInt(String fieldName, int defaultValue) throws Exception {
|
int idx = entityMeta.getIndex(fieldName);
|
return getInt(idx, defaultValue);
|
}
|
|
public int getInt(int idx, int defaultValue) throws Exception {
|
Object value = dataArray[idx];
|
Convertor convertor = entityMeta.getConvertor(idx);
|
return convertor.toInteger(value, defaultValue);
|
}
|
|
public BigDecimal getBigDecimal(String fieldName) throws Exception {
|
int idx = entityMeta.getIndex(fieldName);
|
return getBigDecimal(idx, BigDecimal.ZERO);
|
}
|
|
public BigDecimal getBigDecimal(String fieldName, BigDecimal defaultValue) throws Exception {
|
int idx = entityMeta.getIndex(fieldName);
|
return getBigDecimal(idx, defaultValue);
|
}
|
|
public BigDecimal getBigDecimal(int idx, BigDecimal defaultValue) throws Exception {
|
Object value = dataArray[idx];
|
Convertor convertor = entityMeta.getConvertor(idx);
|
return convertor.toBigDecimal(value, defaultValue);
|
}
|
|
public String getDataName() {
|
return entityMeta.getName();
|
}
|
|
public int getFieldCount() {
|
return entityMeta.getFieldCount();
|
}
|
|
public String[] getLowerNames() {
|
return entityMeta.getLowerNames();
|
}
|
|
public EntityMeta getEntityMeta() {
|
return entityMeta;
|
}
|
|
public boolean isEmptyValue(String fieldName) {
|
int idx = entityMeta.getIndex(fieldName);
|
return isEmptyValue(idx);
|
}
|
|
public boolean isEmptyValue(int idx) {
|
return dataArray[idx] == null;
|
}
|
|
public void setValueToStatement(int dataIdx, PreparedStatement stmt, int paramIdx) throws SQLException {
|
entityMeta.setValueToStatement(dataArray, dataIdx, stmt, paramIdx);
|
}
|
|
public void loadInitData() {
|
//TODO
|
}
|
|
public static boolean isDBExists(Entity entity) throws Exception {
|
NamedSQL namedSQL = NamedSQL.getInstance("getCountOfId");
|
namedSQL.setTableName(entity.getDataName()).setParam("id", entity.getQuotedString("id"));
|
int cnt = SQLRunner.getInteger(namedSQL);
|
|
return cnt > 0;
|
}
|
|
public boolean isDBExists() throws Exception {
|
return isDBExists(this);
|
}
|
|
public static void loadDB(Id id, Entity entity) throws Exception {
|
NamedSQL namedSQL = NamedSQL.getInstance("getLineById");
|
namedSQL.setTableName(entity.getDataName()).setParam("id", id.getQuotedValue());
|
SQLRunner.getEntity(namedSQL, entity);
|
}
|
|
public void loadDB(Id id) throws Exception {
|
loadDB(id, this);
|
}
|
|
public static void insertDB(Entity entity) throws Exception {
|
if (entity.isEmptyValue("id")) {
|
entity.set("id", Util.newShortGUID());
|
}
|
|
NamedSQL namedSQL = NamedSQL.getInstance("insert");
|
namedSQL.setTableName(entity.getDataName()).setFieldNames(entity.getEntityMeta(), entity).setValues(entity);
|
SQLRunner.execSQL(namedSQL);
|
}
|
|
public void insertDB() throws Exception {
|
insertDB(this);
|
}
|
|
public static void updateDB(Entity entity) throws Exception {
|
if (entity.isEmptyValue("id")) {
|
entity.set("id", Util.newShortGUID());
|
}
|
|
NamedSQL namedSQL = NamedSQL.getInstance("updateById");
|
namedSQL.setTableName(entity.getDataName()).setFieldNameValues(entity);
|
namedSQL.setParam("fieldNameId", "id").setParam("id", entity.getQuotedString("id"));
|
|
SQLRunner.execSQL(namedSQL);
|
}
|
|
public void updateDB() throws Exception {
|
updateDB(this);
|
}
|
|
public void saveDB() throws Exception {
|
if (isEmptyValue("id")) {
|
set("id", Util.newShortGUID());
|
}
|
|
boolean exists = isDBExists();
|
|
if (exists) {
|
updateDB();
|
}
|
else {
|
insertDB();
|
}
|
}
|
|
public static void deleteDB(Entity entity) throws Exception {
|
NamedSQL namedSQL = NamedSQL.getInstance("deleteById");
|
namedSQL.setTableName(entity.getDataName()).setParam("id", entity.getString("id"));
|
}
|
|
public void deleteDB() throws Exception {
|
deleteDB(this);
|
}
|
|
public int getDBCount(Filter filter) {
|
// TODO Auto-generated method stub
|
return 0;
|
}
|
|
@Override
|
public Object getDataValue(String name) {
|
return getValue(name);
|
}
|
|
@Override
|
public List<String> getDataNameList() {
|
String[] lowerNames = entityMeta.getLowerNames();
|
return Arrays.asList(lowerNames);
|
}
|
|
@Override
|
public boolean containsData(String name) {
|
return entityMeta.contains(name);
|
}
|
|
@Override
|
public void setDataValue(String name, Object value) {
|
set(name, value);
|
}
|
|
}
|