![JAR search and dependency download from the Maven repository](/logo.png)
org.bsc.bean.AggregateBeanManager Maven / Gradle / Ivy
package org.bsc.bean;
import java.beans.BeanInfo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import org.bsc.util.Log;
/**
manage bean persistence operation on DB using BeanInfo and
PropertyDescriptorField extention and support operations create, store and remove
on the bean mapped on multiple table.
Connection conn = open connection ...
MyBean bean = new MyBean();
set properties of bean
BeanManagerBase beanManager = new BeanManagerBase(MyBean.class);
//CREATE(INSERT) BEAN ON DB
beanManager.create( conn, bean );
//STORE(UPDATE) BEAN ON DB
beanManager.store( conn, bean );
//REMOVE (DELETE) BEAN ON DB
beanManager.remove( conn, id value );
//FIND (SELECT) BEAN BY ID ON DB
MyBean bean = (MyBean)beanManager.findById( conn, id value );
//FIND (SELECT) BEAN(S) BY CONDITION ON DB
java.util.Enumeration e = beanManager.find( conn,
"property name like ?",
new Object[] { "FOR%" } );
while( e.hasMoreElements() ) {
bean = (MyBean)e.nextElement();
}
conn.close();
@see bsc.bean.PropertyDescriptorField
@see java.beans.BeanDescriptor
*/
public class AggregateBeanManager implements BeanManager {
private BeanManager base;
private BeanManager[] aggregate;
/**
*
* @param beanClass
* @param beanInfo
* @param aggregateBeanInfo
* @throws IntrospectionException
*/
@SuppressWarnings("unchecked")
public AggregateBeanManager(BeanManager base, BeanInfo[] aggregateBeanInfo) /*throws IntrospectionException*/ {
this.base = base;
BeanManagerFactory factory = BeanManagerFactory.getFactory();
aggregate = new BeanManager[aggregateBeanInfo.length];
for (int i = 0; i < aggregateBeanInfo.length; ++i) {
aggregate[i] = (BeanManager) factory.createBeanManager(base.getBeanClass(), aggregateBeanInfo[i]);
}
}
/**
*
* @param beanClass
* @param beanInfo
* @param aggregateBeanInfo
* @throws IntrospectionException
*/
@SuppressWarnings("unchecked")
public AggregateBeanManager(BeanManager base, BeanManager[] aggregateManager) /*throws IntrospectionException*/ {
this.base = base;
aggregate = new BeanManager[aggregateManager.length];
System.arraycopy(aggregateManager, 0, aggregate, 0, aggregateManager.length);
}
/**
*
* @return
*/
public BeanDescriptorEntity getBeanDescriptor() {
return base.getBeanDescriptor();
}
/**
@return array of PropertyDescriptorField inside beanInfo
@see bsc.bean.PropertyDescriptorField
*/
public PropertyDescriptorField[] getPropertyDescriptorsFields() {
return base.getPropertyDescriptorsFields();
}
public PrimaryKey getPrimaryKey() {
return base.getPrimaryKey();
}
/**
*get the bean class
*@return
*/
public Class getBeanClass() {
return base.getBeanClass();
}
/**
* enable/disable identity conditions on all find commands
*
* @param enable
* @see BeanDescriptorEntity#setIdentityConditions
*/
public void setEnableIdentityConditions(boolean enable) {
base.setEnableIdentityConditions(enable);
}
/**
create a new bean into db ( same of SQL INSERT command )
if the bean is mapped on multiple tables ( see BeanInfo.getAdditionalBeanInfo() )
this method perform INSERT on every table
@param conn database connectio
@param beans object to insert into db
@exception SQLException
*/
public int create(Connection conn, T... beans) throws java.sql.SQLException {
AbstractBeanManager _base = (AbstractBeanManager) base;
String base_create_sql = _base.getCreateStatement();
PreparedStatement base_ps = conn.prepareStatement(base_create_sql);
PreparedStatement aggregate_ps[] = new PreparedStatement[aggregate.length];
for (int i = 0; i < aggregate.length; ++i) {
AbstractBeanManager _aggregate = (AbstractBeanManager) aggregate[i];
String aggregate_sql = _aggregate.getCreateStatement();
aggregate_ps[i] = conn.prepareStatement(aggregate_sql);
}
int result = 0;
Integer aggregateOrder = base.getBeanDescriptor().getAggregateOrder();
try {
for (T bean : beans) {
if (aggregateOrder == BeanDescriptorEntity.ORDER_FIRST) {
base_ps.clearParameters();
_base.setCreateStatement(conn, base_ps, bean);
result += base_ps.executeUpdate();
}
for (int i = 0; i < aggregate.length; ++i) {
AbstractBeanManager _aggregate = (AbstractBeanManager) aggregate[i];
aggregate_ps[i].clearParameters();
_aggregate.setCreateStatement(conn, aggregate_ps[i], bean);
result += aggregate_ps[i].executeUpdate();
}
if (aggregateOrder == BeanDescriptorEntity.ORDER_LAST) {
base_ps.clearParameters();
_base.setCreateStatement(conn, base_ps, bean);
result += base_ps.executeUpdate();
}
}
} finally {
for (int i = 0; i < aggregate.length; ++i) {
DataBaseUtils.close(aggregate_ps[i]);
}
DataBaseUtils.close(base_ps);
}
return result;
}
/**
remove bean from db (same of SQL DELETE command )
if the bean is mapped on multiple tables ( see BeanInfo.getAdditionalBeanInfo() )
this method perform the DELETE on every table
@param conn database connection
@param bean instance to remove from db
@exception SQLException
*/
public int remove(Connection conn, T bean) throws java.sql.SQLException {
int result = 0;
Integer aggregateOrder = base.getBeanDescriptor().getAggregateOrder();
if (aggregateOrder == BeanDescriptorEntity.ORDER_LAST) {
result += base.remove(conn, bean);
}
for (int i = aggregate.length - 1; i >= 0; --i) {
result += aggregate[i].remove(conn, bean);
}
if (aggregateOrder == BeanDescriptorEntity.ORDER_FIRST) {
result += base.remove(conn, bean);
}
return result;
}
/**
delete bean from db using an id
@param conn database connection
@param id id value ( for composite key must be an Object array )
@exception SQLException
*/
public int removeById(Connection conn, Object... id) throws SQLException {
int result = 0;
Integer aggregateOrder = base.getBeanDescriptor().getAggregateOrder();
if (aggregateOrder == BeanDescriptorEntity.ORDER_LAST) {
result += base.removeById(conn, id);
}
for (int i = aggregate.length - 1; i >= 0; --i) {
result += aggregate[i].removeById(conn, id);
}
if (aggregateOrder == BeanDescriptorEntity.ORDER_FIRST) {
result += base.removeById(conn, id);
}
return result;
}
/**
*
* @param conn
* @return
* @throws java.sql.SQLException
*/
public int removeAll(Connection conn) throws SQLException {
int result = 0;
Integer aggregateOrder = base.getBeanDescriptor().getAggregateOrder();
if (aggregateOrder == BeanDescriptorEntity.ORDER_LAST) {
result += base.removeAll(conn);
}
for (int i = aggregate.length - 1; i >= 0; --i) {
result += aggregate[i].removeAll(conn);
}
if (aggregateOrder == BeanDescriptorEntity.ORDER_FIRST) {
result += base.removeAll(conn);
}
return result;
}
/**
store bean into db ( same of SQL UPDATE command )
if the bean is mapped on multiple tables ( see BeanInfo.getAdditionalBeanInfo() )
this method perform the UPDATE on every table
@param conn database connection
@param beans objects to update into db
@exception SQLException
*/
public int store(Connection conn, T... beans) throws java.sql.SQLException {
AbstractBeanManager _base = (AbstractBeanManager) base;
String base_store_sql = _base.getStoreStatement();
PreparedStatement base_ps = conn.prepareStatement(base_store_sql);
PreparedStatement aggregate_ps[] = new PreparedStatement[aggregate.length];
for (int i = 0; i < aggregate.length; ++i) {
AbstractBeanManager _aggregate = (AbstractBeanManager) aggregate[i];
String aggregate_sql = _aggregate.getStoreStatement();
aggregate_ps[i] = conn.prepareStatement(aggregate_sql);
}
int result = 0;
Integer aggregateOrder = base.getBeanDescriptor().getAggregateOrder();
try {
for (T bean : beans) {
if (aggregateOrder == BeanDescriptorEntity.ORDER_FIRST) {
base_ps.clearParameters();
_base.setStoreStatement(base_ps, bean);
result += base_ps.executeUpdate();
}
for (int i = 0; i < aggregate.length; ++i) {
AbstractBeanManager _aggregate = (AbstractBeanManager) aggregate[i];
aggregate_ps[i].clearParameters();
_aggregate.setStoreStatement(aggregate_ps[i], bean);
result += aggregate_ps[i].executeUpdate();
}
if (aggregateOrder == BeanDescriptorEntity.ORDER_LAST) {
base_ps.clearParameters();
_base.setStoreStatement(base_ps, bean);
result += base_ps.executeUpdate();
}
}
} finally {
for (int i = 0; i < aggregate.length; ++i) {
DataBaseUtils.close(aggregate_ps[i]);
}
DataBaseUtils.close(base_ps);
}
return result;
}
/**
update bean into db having the possibility of
include/exclude properties into update command
Ex.:
String props[] = { "prop1", "prop2", ... };
int result = manager.store( conn, myBean, props, true ); // include
OR
int result = manager.store( conn, myBean, props, false ); // exclude
@param conn database connection
@param bean object to update into db
@param properties properties to include/exclude to update
@param constraints allow to include or exclude properties from update
@exception SQLException
*/
public int store(Connection conn, T bean, StoreConstraints constraints, String... properties) throws java.sql.SQLException {
int result = 0;
Integer aggregateOrder = base.getBeanDescriptor().getAggregateOrder();
if (aggregateOrder == BeanDescriptorEntity.ORDER_FIRST) {
result += base.store(conn, bean, constraints, properties);
}
for (int i = 0; i < aggregate.length; ++i) {
try {
result += aggregate[i].store(conn, bean, constraints, properties);
} catch (PropertyNotFoundException pnfEx) {
Log.warn("Property not found!", pnfEx);
}
}
if (aggregateOrder == BeanDescriptorEntity.ORDER_LAST) {
result += base.store(conn, bean, constraints, properties);
}
return result;
}
/**
*
* @param conn
* @param bean
* @param constraints
* @param properties
* @return
* @throws java.sql.SQLException
*/
public int storeAll(Connection conn, T bean, StoreConstraints constraints, String... properties) throws java.sql.SQLException {
int result = 0;
Integer aggregateOrder = base.getBeanDescriptor().getAggregateOrder();
if (aggregateOrder == BeanDescriptorEntity.ORDER_FIRST) {
result += base.storeAll(conn, bean, constraints, properties);
}
for (int i = 0; i < aggregate.length; ++i) {
try {
result += aggregate[i].storeAll(conn, bean, constraints, properties);
} catch (PropertyNotFoundException pnfEx) {
Log.warn("Property not found!", pnfEx);
}
}
if (aggregateOrder == BeanDescriptorEntity.ORDER_LAST) {
result += base.storeAll(conn, bean, constraints, properties);
}
return result;
}
/**
select bean from db using an primary key value
Note:
if you have a composite key must pass into id parameter a
Object array that contains the PK values
Ex.:
// SINGLE KEY (string)
String id = "xxxxx";
MyBean bean = (MyBean)manager.findById( conn, id );
// MULTIPLE KEY (string,integer)
String id[] = {"xxxxx", new Integer(1) };
MyBean bean = (MyBean)manager.findById( conn, id );
@param conn database connection
@param id primary key value
@return bean | null
@exception SQLException
*/
public T findById(Connection conn, Object... id) throws SQLException {
return base.findById(conn, id);
}
/**
reload bean instance from db
@param conn database connection
@param bean bean intance
@return bean instance updated (same of parameter bean) - null if not found
@exception SQLException
@see #findById
*/
public T loadBean(Connection conn, T bean) throws SQLException {
return base.loadBean(conn, bean);
}
/**
*
* select beans from db using a where condition and cache the result into collection
*
* NB
* can close the connection before you use the collection
*
*
@param conn database connection
@param result instance of collection that will contains all the instance of beans
@param where condition formatted like PreparedStatement
@param values Object array contains a parameters value specified into where condition
create an entry for each ? into where condition
@return collection that contains all the instance of beans ( same of result parameter )
@exception SQLException
@see java.sql.PreparedStetement
*/
public Collection find(Connection conn, Collection result, String where, Object... values) throws SQLException {
return base.find(conn, result, where, values);
}
/**
*
* select beans from db using a where condition
*
* NB
* cannot close the connection before you have obtain all element from enumeration
*
*
@param conn database connection
@param where condition formatted like PreparedStatement
@param values Object array contains a parameters value specified into where condition
create an entry for each ? into where condition
@return enumeration of retreived beans
@exception SQLException
@see java.sql.PreparedStetement
*/
public BeanEnumeration find(Connection conn, String where, Object... values) throws SQLException {
return base.find(conn, where, values);
}
/**
*
* select beans from db using a custom SQL SELECT command
*
* NB
* cannot close the connection before you have obtain all element from enumeration
*
*
* @param statement custom SQL SELECT command
* @return enumeration of retreived beans
* @throws SQLException
*/
public BeanEnumeration find(PreparedStatement statement) throws SQLException {
return base.find(statement);
}
/**
*
* select beans from db using a custom SQL SELECT and cache the result into collection
*
* NB
* you can close the connection before you use the collection
*
*
* @param statement custom SQL SELECT command
* @param result instance of collection that will contains all the instance of beans
* @return collection that contains all the instance of beans ( same of result parameter )
* @throws SQLException
*/
public Collection find(PreparedStatement statement, Collection result) throws SQLException {
return base.find(statement, result);
}
/**
select all beans from db
@param conn database connection
@param collection used for store all the instance of bean
@param sqlClouse sql clouse to append command ( like order by or group by ... ) cannot enter parameters can be null
@return collection that contain all the instance of bean ( same of result parameter )
@exception SQLException
*/
public Collection findAll(Connection conn, Collection result, String sqlClouse) throws SQLException {
return base.findAll(conn, result, sqlClouse);
}
/**
select all beans from db
@param conn database connection
@param sqlClouse sql clouse to append command ( like order by or group by ... ) cannot enter parameters can be null
@return enumeration of retreived beans
@exception SQLException
*/
public BeanEnumeration findAll(Connection conn, String sqlClouse) throws SQLException {
return base.findAll(conn, sqlClouse);
}
/**
* factory method
*
* create bean instance and set the property values using ResultSet current row data
*
* @param rs
* @return
* @throws java.lang.InstantiationException
*/
public T instantiateBean() throws java.lang.InstantiationException {
return base.instantiateBean();
}
/**
*
* @param bean
* @param rs
* @return
*/
public T setBeanProperties(T bean, ResultSet rs) throws SQLException {
return base.setBeanProperties(bean, rs);
}
/**
* prepareCustomFind
*
* @param conn Connection
* @param commandKey String
* @param where String
* @return PreparedStatement
*/
public PreparedStatement prepareCustomFind(Connection conn,
String commandKey,
String where) throws SQLException {
return base.prepareCustomFind(conn, commandKey, where);
}
/**
*
* @param conn
* @param where
* @param values
* @return
* @throws java.sql.SQLException
*/
public int findAndRemove(Connection conn, String where, Object... values) throws SQLException {
throw new UnsupportedOperationException("This method is not supported for aggregateBean!");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy