com.jk.db.dynamic.dataaccess.DynamicDataAccess Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2002-2016 Jalal Kiswani.
*
* Licensed 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.
*/
package com.jk.db.dynamic.dataaccess;
import java.io.ByteArrayInputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import com.jk.db.dataaccess.exception.JKRecordNotFoundException;
import com.jk.db.dataaccess.plain.JKAbstractPlainDataAccess;
import com.jk.db.dataaccess.plain.JKFinder;
import com.jk.db.dataaccess.plain.JKUpdater;
import com.jk.db.datasource.JKSession;
import com.jk.db.dynamic.meta.AbstractEntityMetaFactory;
import com.jk.db.dynamic.meta.MetaSqlBuilder;
import com.jk.db.util.JdbcUtil;
import com.jk.exceptions.JKDataAccessException;
import com.jk.logging.JKLogger;
import com.jk.logging.JKLoggerFactory;
import com.jk.metadata.db.meta.EntityField;
import com.jk.metadata.db.meta.FieldMeta;
import com.jk.metadata.db.meta.ForiegnKeyFieldMeta;
import com.jk.metadata.db.meta.EntityRecord;
import com.jk.metadata.db.meta.EntityMeta;
import com.jk.metadata.db.trigger.EntityTrigger;
import com.jk.security.JKAudit;
import com.jk.security.JKAuditType;
/**
* The Class DynamicDao.
*
* @author Jalal Kiswani
*/
public class DynamicDataAccess extends JKAbstractPlainDataAccess {
JKLogger logger = JKLoggerFactory.getLogger(getClass());
protected final EntityMeta tableMeta;
protected MetaSqlBuilder sqlBuilder;
/**
* Instantiates a new dynamic dao.
*
* @param tableMetaName
* the table meta name
*/
// //////////////////////////////////////////////////////////////
public DynamicDataAccess(final String tableMetaName) {
this(AbstractEntityMetaFactory.getInstance().getEntityMeta(tableMetaName));
logger.debug(tableMetaName);
}
/**
* Instantiates a new dynamic dao.
*
* @param tableMeta
* the table meta
*/
// //////////////////////////////////////////////////////////////
public DynamicDataAccess(final EntityMeta tableMeta) {
this.tableMeta = tableMeta;
this.sqlBuilder = new MetaSqlBuilder(tableMeta);
}
// // //////////////////////////////////////////////////////////////
// protected void addDeleteAudit(final Record record) {
// logger.debug(record.toString());
// final JKAudit audit = createAudit(record,
// JKAuditType.AUDIT_DELETE_RECORD);
//// addAudit(audit);
// }
//
// // //////////////////////////////////////////////////////////////
// protected void addInsertAudit(final Record record) {
// logger.debug(record.toString());
// final JKAuditType aUDIT_ADD_RECORD = JKAuditType.AUDIT_ADD_RECORD;
// final JKAudit audit = createAudit(record, aUDIT_ADD_RECORD);
//// addAudit(audit);
// }
//
// // //////////////////////////////////////////////////////////////
// protected void addUpdateAudit(final Record oldRecord, final Record
// newRecord) {
// logger.debug(oldRecord == null ? newRecord.toString() :
// oldRecord.toString());
// final JKAudit audit = createAudit(newRecord,
// JKAuditType.AUDIT_UPDATE_RECORD);
// audit.setOldValue(oldRecord.toString(true));
//// addAudit(audit);
// }
//
// //////////////////////////////////////////////////////////////
protected void callAfterAddEventOnTriggers(final EntityRecord record) {
final ArrayList triggers = this.tableMeta.getTriggers();
for (int i = 0; i < triggers.size(); i++) {
triggers.get(i).afterAdd(record);
}
}
// //////////////////////////////////////////////////////////////
protected void callAfterDeleteEventOnTriggers(final EntityRecord record) {
final ArrayList triggers = this.tableMeta.getTriggers();
for (int i = 0; i < triggers.size(); i++) {
triggers.get(i).afterDelete(record);
}
}
protected void callAfterFindEventOnTriggers(final EntityRecord record) {
final ArrayList triggers = this.tableMeta.getTriggers();
for (int i = 0; i < triggers.size(); i++) {
triggers.get(i).afterFind(record);
}
}
// //////////////////////////////////////////////////////////////
protected void callAfterUpdateEventOnTriggers(final EntityRecord oldRecord, final EntityRecord newRecord) {
final ArrayList triggers = this.tableMeta.getTriggers();
for (int i = 0; i < triggers.size(); i++) {
triggers.get(i).afterUpdate(oldRecord, newRecord);
}
}
// //////////////////////////////////////////////////////////////
protected void callBeforeAddEventOnTriggers(final EntityRecord record) {
final ArrayList triggers = this.tableMeta.getTriggers();
for (int i = 0; i < triggers.size(); i++) {
triggers.get(i).beforeAdd(record);
}
}
// //////////////////////////////////////////////////////////////
protected void callBeforeDeleteEventOnTriggers(final EntityRecord record) {
final ArrayList triggers = this.tableMeta.getTriggers();
for (int i = 0; i < triggers.size(); i++) {
triggers.get(i).beforeDelete(record);
}
}
// //////////////////////////////////////////////////////////////
protected void callBeforeUpdateEventOnTriggers(final EntityRecord oldRecord, final EntityRecord newRecord) {
final ArrayList triggers = this.tableMeta.getTriggers();
for (int i = 0; i < triggers.size(); i++) {
triggers.get(i).beforeUpdate(oldRecord, newRecord);
}
}
/**
* Clone details.
*
* @param sourceIdValue
* the source id value
* @param targetIdValue
* the target id value
*/
// ///////////////////////////////////////////////////////////////////////////////////////
public void cloneDetails(final Object sourceIdValue, final Object targetIdValue) {
// TODO : refactor this method to be extarced to facade and avoid direct
// calls from UI
final ArrayList detailFields = this.tableMeta.getDetailFields();
final JKSession session = getDataSource().createSession();
boolean commit = false;
try {
for (final ForiegnKeyFieldMeta foriegnKeyFieldMeta : detailFields) {
final DynamicDataAccess detailDao = DynamicDataAccessFactory.createDynamicDao(foriegnKeyFieldMeta.getParentTable());
// Find detaile record for the original id
final List detailRecords = detailDao.findByFieldValue(foriegnKeyFieldMeta.getName(), sourceIdValue);
// iterate over the records , change the id to the new id , and
// reset the primary key
for (final EntityRecord detailRecord : detailRecords) {
detailRecord.setIdValue(null);
detailRecord.setFieldValue(foriegnKeyFieldMeta.getName(), targetIdValue);
detailDao.insertRecord(detailRecord);
}
}
commit = true;
} finally {
session.close(commit);
}
}
// /**
// * Creates the audit.
// *
// * @param record
// * the record
// * @param auditType
// * the audit type
// * @return the JK audit
// */
// // //////////////////////////////////////////////////////////////
// public JKAudit createAudit(final Record record, final JKAuditType
// auditType) {
// final JKAudit audit = new JKAudit();
// audit.setAuditType(auditType);
// audit.setBusinessRecordId(record.getIdValueAsInteger());
// audit.setTableName(record.getTableMeta().getTableName());
// audit.setNewValue(record.toString(true));
// if (new Boolean(System.getProperty("fs.security.audit.gui", "true"))) {
// audit.setGui(record.getGui());
// }
// return audit;
// }
/**
* Creates the empty record.
*
* @param setDefaultValues
* the set default values
* @return the record
*/
// //////////////////////////////////////////////////////////////
public EntityRecord createEmptyRecord() {
return createEmptyRecord(true);
}
public EntityRecord createEmptyRecord(final boolean setDefaultValues) {
return this.tableMeta.createEmptyRecord(setDefaultValues);
}
/**
* Creates the empty record.
*
* @param setDefaultValues
* the set default values
* @param defaults
* the defaults
* @return the record
*/
// //////////////////////////////////////////////////////////////
public EntityRecord createEmptyRecord(final boolean setDefaultValues, final EntityRecord defaults) {
return this.tableMeta.createEmptyRecord(setDefaultValues, defaults);
}
/**
* Delete all records.
*/
// //////////////////////////////////////////////////////////////
public void deleteAllRecords() {
logger.debug("deleteAllRecords for tablemeta : ", tableMeta);
final JKUpdater updater = new JKUpdater() {
@Override
public String getQuery() {
return DynamicDataAccess.this.sqlBuilder.buildDelete();
}
@Override
public void setParamters(final PreparedStatement ps) throws SQLException {
}
};
executeUpdate(updater);
}
/**
* Delete by fields values.
*
* @param fieldNameToValue
* the field name to value
*/
public void deleteByFieldsValues(final HashMap fieldNameToValue) {
logger.debug(fieldNameToValue.toString());
final EntityRecord filterRecord = this.tableMeta.createEmptyRecord();
final Set keySet = fieldNameToValue.keySet();
for (final String fieldName : keySet) {
filterRecord.setFieldValue(fieldName, fieldNameToValue.get(fieldName));
}
final List lstRecords = lstRecords(filterRecord);
for (final EntityRecord record : lstRecords) {
deleteRecord(record);
}
}
/**
* Delete by field value.
*
* @param fieldName
* the field name
* @param value
* the value
*/
// //////////////////////////////////////////////////////////////
public void deleteByFieldValue(final String fieldName, final Object value) {
logger.debug(fieldName, " = ", value);
final EntityRecord record = createEmptyRecord(false);
record.setFieldValue(fieldName, value);
deleteRecord(record.getField(fieldName), false);
}
/**
* Delete record.
*
* @param field
* the field
*/
// //////////////////////////////////////////////////////////////
public void deleteRecord(final EntityField field) {
logger.debug(field.toString());
deleteRecord(field, true);
}
/**
* Delete record.
*
* @param field
* the field
* @param addAudit
* the add audit
*/
// //////////////////////////////////////////////////////////////
public void deleteRecord(final EntityField field, final boolean addAudit) {
logger.debug(field, " , add audit : ", addAudit);
final JKUpdater updater = new JKUpdater() {
@Override
public String getQuery() {
return DynamicDataAccess.this.sqlBuilder.buildDelete(field);
}
@Override
public void setParamters(final PreparedStatement ps) throws SQLException {
}
};
EntityRecord record = null;
if (addAudit) {
record = findRecord(field.getValueAsInteger());
}
executeUpdate(updater);
// if (addAudit) {
// addDeleteAudit(record);
// }
}
/**
* Delete record.
*
* @param recordId
* the record id
*/
// //////////////////////////////////////////////////////////////
public void deleteRecord(final Object recordId) {
final EntityRecord record = createEmptyRecord(false);
record.setIdValue(recordId);
deleteRecord(record);
}
/**
* Delete record.
*
* @param record
* the record
*/
// //////////////////////////////////////////////////////////////
public void deleteRecord(final EntityRecord record) {
callBeforeDeleteEventOnTriggers(record);
deleteRecord(record.getIdField());
callAfterDeleteEventOnTriggers(record);
}
/**
* Delete records.
*
* @param filter
* the filter
*/
/*
* @param filter
*
* @throws DaoException
*/
public void deleteRecords(final EntityRecord filter) {
final List findRecord = lstRecords(filter);
for (final EntityRecord record : findRecord) {
deleteRecord(record);
}
}
/**
* Execute update query.
*
* @param sql
* the sql
* @param objects
* the objects
*/
// //////////////////////////////////////////////////////////////
public void executeUpdateQuery(final String sql, final Object[] objects) {
final JKUpdater updater = new JKUpdater() {
@Override
public String getQuery() {
return sql;
}
@Override
public void setParamters(final PreparedStatement ps) throws SQLException {
for (int i = 0; i < objects.length; i++) {
ps.setObject(i + 1, objects[i]);
}
}
};
executeUpdate(updater);
}
/**
* Find by field value.
*
* @param fieldName
* the field name
* @param value
* the value
* @return the list
*/
// //////////////////////////////////////////////////////////////
public List findByFieldValue(final String fieldName, final Object value) {
final EntityRecord filterRecord = this.tableMeta.createEmptyRecord();
filterRecord.setFieldValue(fieldName, value);
return lstRecords(filterRecord);
}
/**
* Find record.
*
* @param id
* the id
* @return the record
*/
// //////////////////////////////////////////////////////////////
public EntityRecord findRecord(final Object id) {
final JKFinder finder = new JKFinder() {
@Override
public String getQuery() {
return sqlBuilder.buildFindById(id.toString());
}
@Override
public Object populate(final ResultSet rs) throws SQLException {
return readRecord(rs, DynamicDataAccess.this.tableMeta);
}
@Override
public void setParamters(final PreparedStatement ps) throws SQLException {
}
};
final EntityRecord record = (EntityRecord) findRecord(finder);
callAfterFindEventOnTriggers(record);
return record;
}
/**
* Find record.
*
* @param filter
* the filter
* @return the record
*/
// //////////////////////////////////////////////////////////////
public EntityRecord findRecord(final EntityRecord filter) {
final JKFinder finder = new JKFinder() {
@Override
public String getQuery() {
return DynamicDataAccess.this.sqlBuilder.buildFindByFilter(filter);
}
@Override
public Object populate(final ResultSet rs) throws SQLException, JKRecordNotFoundException, JKDataAccessException {
return readRecord(rs, DynamicDataAccess.this.tableMeta);
}
@Override
public void setParamters(final PreparedStatement ps) throws SQLException {
}
};
return (EntityRecord) findRecord(finder);
}
/**
* Find record by field value.
*
* @param fieldNameToValue
* the field name to value
* @return the record
*/
public EntityRecord findRecordByFieldValue(final HashMap fieldNameToValue) {
final EntityRecord filterRecord = this.tableMeta.createEmptyRecord();
for (final String fieldName : fieldNameToValue.keySet()) {
filterRecord.setFieldValue(fieldName, fieldNameToValue.get(fieldName));
}
return findRecord(filterRecord);
}
/**
* Find record by field value.
*
* @param fieldName
* the field name
* @param value
* the value
* @return the record
*/
// //////////////////////////////////////////////////////////////
public EntityRecord findRecordByFieldValue(final String fieldName, final Object value) {
final EntityRecord filterRecord = this.tableMeta.createEmptyRecord();
filterRecord.setFieldValue(fieldName, value);
return findRecord(filterRecord);
}
/**
* Gets the assigned values query.
*
* @param field1Id
* the field 1 id
* @return the assigned values query
*/
public String getAssignedValuesQuery(final int field1Id) {
final StringBuffer buf = new StringBuffer();
final ArrayList list = this.tableMeta.lstForiegnKeyFields();
final ForiegnKeyFieldMeta field1 = list.get(0);
final ForiegnKeyFieldMeta field2 = list.get(1);
final EntityMeta refTable = AbstractEntityMetaFactory.getInstance().getEntityMeta(field2.getReferenceTable());
// buf.append(refTable.getShortReportSql());
String sql = refTable.getShortReportSql();
sql += sql.toUpperCase().contains("WHERE") ? " AND " : " WHERE ";
buf.append(sql);
buf.append(field2.getReferenceField() + " IN (");
buf.append(" SELECT " + field2.getName() + " FROM " + field2.getParentTable().getTableName());
buf.append(" WHERE " + field1.getName() + " = " + field1Id);
buf.append(")");
return buf.toString();
}
/**
* Gets the first record in table.
*
* @return the first record in table
*/
// //////////////////////////////////////////////////////////////
public EntityRecord getFirstRecordInTable() {
logger.debug("First record");
final List r = lstRecords();
if (r.size() > 0) {
return r.get(0);
}
return null;
}
/**
* Gets the not assigned values query.
*
* @param field1Id
* the field 1 id
* @return the not assigned values query
*/
public String getNotAssignedValuesQuery(final int field1Id) {
final StringBuffer buf = new StringBuffer();
final ArrayList list = this.tableMeta.lstForiegnKeyFields();
final ForiegnKeyFieldMeta field1 = list.get(0);
final ForiegnKeyFieldMeta field2 = list.get(1);
final EntityMeta refTable = AbstractEntityMetaFactory.getInstance().getEntityMeta(field2.getReferenceTable());
String sql = refTable.getShortReportSql();
sql += sql.toUpperCase().contains("WHERE") ? " AND " : " WHERE ";
buf.append(sql);
buf.append(field2.getReferenceField() + " NOT IN (");
buf.append(" SELECT " + field2.getName() + " FROM " + field2.getParentTable().getTableName());
buf.append(" WHERE " + field1.getName() + " = " + field1Id);
buf.append(")");
return buf.toString();
}
/**
* Gets the sql builder.
*
* @return the sql builder
*/
// //////////////////////////////////////////////////////////////
public MetaSqlBuilder getSqlBuilder() {
return this.sqlBuilder;
}
/**
* Gets the table meta.
*
* @return the table meta
*/
public EntityMeta getTableMeta() {
return this.tableMeta;
}
/**
* Insert record.
*
* @param record
* the record
* @return the string
*/
// //////////////////////////////////////////////////////////////
public String insertRecord(final EntityRecord record) {
callBeforeAddEventOnTriggers(record);
final JKUpdater updater = new JKUpdater() {
@Override
public String getQuery() {
return DynamicDataAccess.this.sqlBuilder.buildInsert(record);
}
@Override
public void setParamters(final PreparedStatement ps) throws SQLException {
DynamicDataAccess.this.setParamters(record, ps, true);
}
};
final String id = executeUpdate(updater) + "";
record.setIdValue(id);
record.setNewRecord(false);
callAfterAddEventOnTriggers(record);
// addInsertAudit(record);
return id;
}
/**
* Insert records.
*
* @param records
* the records
*/
// //////////////////////////////////////////////////////////////
public void insertRecords(final List records) {
if (records.size() > 0) {
final String insert = this.sqlBuilder.buildFatInsert(records);
execute(insert);
}
}
/**
* Checks if is id exists.
*
* @param id
* the id
* @return true, if is id exists
*/
// //////////////////////////////////////////////////////////////
public boolean isIdExists(final Object id) {
try {
findRecord(id.toString());
return true;
} catch (final JKRecordNotFoundException e) {
// just each the exception
return false;
} catch (final JKDataAccessException e) {
throw e;
}
}
/**
* Lst records.
*
* @return the list
*/
// //////////////////////////////////////////////////////////////
public List lstRecords() {
logger.debug("lstRecords : ", tableMeta.getTableName());
return lstRecords(createEmptyRecord(false));
}
/**
* Lst records.
*
* @param filter
* the filter
* @return the list
*/
// //////////////////////////////////////////////////////////////
public List lstRecords(final EntityRecord filter) {
logger.debug(filter.toString());
final JKFinder finder = new JKFinder() {
@Override
public String getQuery() {
return sqlBuilder.buildFindByFilter(filter);
}
@Override
public Object populate(final ResultSet rs) throws SQLException {
return readRecord(rs, DynamicDataAccess.this.tableMeta);
}
@Override
public void setParamters(final PreparedStatement ps) throws SQLException {
}
};
return getList(finder);
}
/**
* Lst records by report sql.
*
* @return the list
*/
// //////////////////////////////////////////////////////////////
public List lstRecordsByReportSql() {
final JKFinder daoFinder = new JKFinder() {
@Override
public String getQuery() {
return DynamicDataAccess.this.tableMeta.getReportSql();
}
@Override
public Object populate(final ResultSet rs) throws SQLException, JKRecordNotFoundException, JKDataAccessException {
return readRecord(rs, DynamicDataAccess.this.tableMeta);
}
@Override
public void setParamters(final PreparedStatement ps) throws SQLException {
}
};
return getList(daoFinder);
}
/**
* Save record.
*
* @param record
* the record
* @return true, if successful
*/
// //////////////////////////////////////////////////////////////
public boolean saveRecord(final EntityRecord record) {
logger.debug("saveRecord() :", record.toString(true));
if (record.isNewRecord() && record.isModified()) {
logger.debug("insert()");
insertRecord(record);
return true;
} else if (!record.isNewRecord() && record.isDeleted()) {
logger.debug("deleteRecord()");
deleteRecord(record);
return true;
} else if (record.isModified()) {
logger.debug("updateRecord()");
updateRecord(record);
return true;
}
// nothing updated
logger.error("No operation to dine");
return false;
}
/**
* Save records.
*
* @param records
* the records
*/
// //////////////////////////////////////////////////////////////
public void saveRecords(final List records) {
final JKDataAccessException allExceptions = new JKDataAccessException();
for (final EntityRecord record : records) {
try {
saveRecord(record);
} catch (final JKDataAccessException e) {
allExceptions.add(e);
}
}
}
/**
* Update record.
*
* @param record
* the record
*/
// //////////////////////////////////////////////////////////////
public void updateRecord(final EntityRecord record) {
final JKUpdater updater = new JKUpdater() {
@Override
public String getQuery() {
return DynamicDataAccess.this.sqlBuilder.buildUpdate(record);
}
@Override
public void setParamters(final PreparedStatement ps) throws SQLException {
DynamicDataAccess.this.setParamters(record, ps, false);
}
};
final EntityRecord oldRecord = findRecord(record.getIdValue());
callBeforeUpdateEventOnTriggers(oldRecord, record);
executeUpdate(updater);
callAfterUpdateEventOnTriggers(oldRecord, record);
// addUpdateAudit(oldRecord, record);
}
// private Session getSession() throws DaoException {
// Session session = tableMeta.getDataSource().createSession();
// setSession(session);
// return session;
// }
// //////////////////////////////////////////////////////////////////////////////////////
protected EntityRecord readRecord(final ResultSet rs, final EntityMeta tableMeta) throws SQLException {
final EntityRecord record = tableMeta.createEmptyRecord();
record.setIdValue(rs.getObject(tableMeta.getIdField().getName()));
// Field idField = record.getIdField();
// idField.setValue(rs.getString(idField.getMeta().getName()));
final ArrayList fields = record.getFields();
for (int i = 0; i < fields.size(); i++) {
final EntityField field = fields.get(i);
final Object value = readResult(rs, field);
// Object value = rs.getObject(field.getMeta().getName());
if (value != null) {
field.setValue(value);
}
}
record.setNewRecord(false);
return record;
}
/**
* Read result.
*
* @param rs
* the rs
* @param field
* the field
* @return the object
* @throws SQLException
* the SQL exception
*/
// //////////////////////////////////////////////////////////////////////////////////////
protected Object readResult(final ResultSet rs, final EntityField field) throws SQLException {
switch (field.getMeta().getType()) {
case Types.BOOLEAN:
case Types.TINYINT:
return rs.getInt(field.getMeta().getName()) == 1;
case Types.INTEGER:
case Types.NUMERIC:
case Types.VARCHAR:
return rs.getString(field.getMeta().getName());
case Types.BLOB:
case Types.VARBINARY:
case Types.LONGVARBINARY:
case Types.BINARY:// binary
return JdbcUtil.getBinaryStream(rs, field.getMeta().getName());
case Types.DATE:
return rs.getDate(field.getMeta().getName());
case Types.TIME:
return rs.getTime(field.getMeta().getName());
}
return rs.getObject(field.getMeta().getName());
}
/**
* Sets the paramter.
*
* @param ps
* the ps
* @param index
* the index
* @param field
* the field
* @throws SQLException
* the SQL exception
*/
// //////////////////////////////////////////////////////////////////////////////////////
protected void setParamter(final PreparedStatement ps, final int index, final EntityField field) throws SQLException {
// System.out.println(field.getMeta().getName()+" - "+field.getValue());
// System.out.println(index + "=" +
// field.getValueObject()+" type:"+field.getMeta().getType());
final Object value = field.getValueObject();
if (value == null || value.toString().equals("")) {
ps.setObject(index, null);
return;
}
switch (field.getMeta().getType()) {
case Types.LONGVARBINARY:
case Types.BINARY:
final byte[] data = (byte[]) field.getValueObject();
ps.setBinaryStream(index, new ByteArrayInputStream(data), data.length);
break;
case Types.BOOLEAN:
case Types.TINYINT:
case Types.BIT:
// add support for tiny integers represented as numbers
ps.setInt(index, field.getValueAsBoolean() ? 1 : 0);
break;
case Types.DECIMAL:
ps.setDouble(index, field.getValueAsDouble());
break;
case Types.DATE:
ps.setDate(index, new java.sql.Date(((Date) field.getValueObject()).getTime()));
break;
case Types.TIME:
ps.setTime(index, new Time(((Date) field.getValueObject()).getTime()));
break;
case Types.VARCHAR:
case Types.LONGVARCHAR:
ps.setString(index, field.getValueObject().toString());
default:
// String value=fixStringValue(field.getValue());
ps.setObject(index, value);
}
}
/**
* Sets the paramters.
*
* @param record
* the record
* @param ps
* the ps
* @param includeId
* the include id
* @throws SQLException
* the SQL exception
*/
// //////////////////////////////////////////////////////////////////////////////////////
protected void setParamters(final EntityRecord record, final PreparedStatement ps, final boolean includeId) throws SQLException {
int counter = 1;
if (includeId && record.getIdValue() != null) {
setParamter(ps, counter++, record.getIdField());
}
final ArrayList fields = record.getFields();
for (int i = 0; i < fields.size(); i++) {
try {
setParamter(ps, counter++, fields.get(i));
} catch (final Exception e) {
final FieldMeta meta = fields.get(i).getMeta();
throw new SQLException("Field " + meta.getName() + " Failed to set paramter with value :" + fields.get(i).getValueObject()
+ " with type :" + meta.getType(), e);
}
}
}
/**
* Adds the audit.
*
* @param audit
* the audit
*/
// // //////////////////////////////////////////////////////////////////////
// public void addAudit(final JKAudit audit) {
// logger.debug("Audit : ", audit.toString());
// final JKUpdater updater = new JKUpdater() {
//
// @Override
// public String getQuery() {
// return "INSERT INTO sec_audits
// (audit_id,user_record_id,audit_date,audit_type_id,record_id,record_name,old_value,new_value,description,gui)
// VALUES(?,?,?,?,?,?,?,?,?,?)";
// }
//
// @Override
// public void setParamters(final PreparedStatement ps) throws SQLException,
// JKDataAccessException {
// int counter = 1;
// ps.setLong(counter++, getNextId("sec_audits", "audit_id"));
// ps.setInt(counter++, audit.getUser().getUserRecordId());
// ps.setTimestamp(counter++, new Timestamp(getSystemDate().getTime()));
// ps.setInt(counter++, audit.getAuditType().getAuditType());
// ps.setObject(counter++, audit.getBusinessRecordId());
// ps.setString(counter++, audit.getTableName());
// ps.setString(counter++, audit.getOldValue());
// ps.setString(counter++, audit.getNewValue());
// ps.setString(counter++, audit.getDescription());
// ps.setString(counter++, audit.getGui());
// }
// };
// executeUpdate(updater);
// }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy