This commit is contained in:
Ziver Koc 2010-11-09 17:17:53 +00:00
parent efae412a88
commit 64cf6b09bc
3 changed files with 60 additions and 27 deletions

View file

@ -10,6 +10,7 @@ import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -35,7 +36,7 @@ import zutil.log.LogUtil;
* *String
* *Character
* *DBBean
* *java.sql.Date
* *java.sql.Timestamp
* *List<DBBean>
*
* </XMP>
@ -138,11 +139,23 @@ public abstract class DBBean {
beanConfigs.put(c, config);
}
/**
* Saves the object and all the sub objects to the DB
*
* @param db is the DBMS connection
*/
public void save(DBConnection db) throws SQLException{
save( db, true );
}
/**
* Saves the Object to the DB
*
* @param db is the DBMS connection
* @param recursive is if the method should save all sub objects
*/
@SuppressWarnings("unchecked")
public void save(DBConnection db) throws SQLException{
public void save(DBConnection db, boolean recursive) throws SQLException{
if( processing )
return;
processing = true;
@ -182,7 +195,8 @@ public abstract class DBBean {
if( DBBean.class.isAssignableFrom( field.getType() )){
DBBean subobj = (DBBean)getFieldValue(field);
if(subobj != null){
subobj.save(db);
if( recursive || subobj.getId() == null )
subobj.save(db);
stmt.setObject(index, subobj.getId() );
}
else
@ -192,26 +206,7 @@ public abstract class DBBean {
// A list of DBBeans
else if( List.class.isAssignableFrom( field.getType() ) &&
field.getAnnotation( DBLinkTable.class ) != null){
List<DBBean> list = (List<DBBean>)getFieldValue(field);
if( list != null ){
DBLinkTable linkTable = field.getAnnotation( DBLinkTable.class );
String subtable = linkTable.name();
String idcol = (linkTable.column().isEmpty() ? config.tableName : linkTable.column() );
DBBeanConfig subConfig = null;
for(DBBean subobj : list){
if(subConfig == null)
subConfig = beanConfigs.get( subobj.getClass() );
// Save links in link table
PreparedStatement subStmt = db.getPreparedStatement("REPLACE INTO "+subtable+" SET ?=? id=?");
subStmt.setString(1, idcol);
subStmt.setString(2, subConfig.tableName);
subStmt.setObject(3, subobj.getId() );
DBConnection.exec(subStmt);
// Save the sub bean
subobj.save(db);
}
}
// DO NOTING
}
// Normal field
else{
@ -227,6 +222,37 @@ public abstract class DBBean {
DBConnection.exec(stmt);
if( id == null )
this.id = (Long) db.getLastInsertID();
// Save the list, after we get the object id
for(Field field : config.fields){
if( List.class.isAssignableFrom( field.getType() ) &&
field.getAnnotation( DBLinkTable.class ) != null){
List<DBBean> list = (List<DBBean>)getFieldValue(field);
if( list != null ){
DBLinkTable linkTable = field.getAnnotation( DBLinkTable.class );
String subtable = linkTable.name();
String idcol = (linkTable.column().isEmpty() ? config.tableName : linkTable.column() );
String sub_idcol = "id";
DBBeanConfig subConfig = null;
for(DBBean subobj : list){
// Save the sub bean
if( recursive || subobj.getId() == null )
subobj.save(db);
// Get the Sub object configuration
if(subConfig == null)
subConfig = beanConfigs.get( subobj.getClass() );
// Save links in link table
String subsql = "REPLACE INTO "+subtable+" SET "+idcol+"=?, "+sub_idcol+"=?";
logger.finest("List Save query: "+subsql);
PreparedStatement subStmt = db.getPreparedStatement( subsql );
subStmt.setLong(1, this.getId() );
subStmt.setLong(2, subobj.getId() );
DBConnection.exec(subStmt);
}
}
}
}
} catch (SQLException e) {
throw e;
} catch (Exception e) {
@ -349,6 +375,7 @@ public abstract class DBBean {
else if(c == boolean.class) return "BOOLEAN";
else if(c == Byte.class) return "BINARY(1)";
else if(c == byte.class) return "BINARY(1)";
else if(c == Timestamp.class) return "DATETIME";
else if(DBBean.class.isAssignableFrom(c))
return classToDBName(Long.class);
return null;
@ -393,7 +420,7 @@ public abstract class DBBean {
else
field.set(this, o);
} catch (Exception e) {
logger.log(Level.WARNING, e.getMessage(), e);
logger.log(Level.SEVERE, e.getMessage(), e);
}
}

View file

@ -19,7 +19,7 @@ import zutil.log.LogUtil;
public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
public static final Logger logger = LogUtil.getLogger();
/** This is the time to live for the cached items **/
public static final long CACHE_TTL = 1000*60*10; // in ms
public static final long CACHE_TTL = 1000*60*10; // 10 min in ms
/** A cache for detecting recursion **/
protected static HashMap<Class<?>, HashMap<Object,DBBeanCache>> cache =
new HashMap<Class<?>, HashMap<Object,DBBeanCache>>();
@ -102,6 +102,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
public T handleQueryResult(Statement stmt, ResultSet result) throws SQLException{
if( list ){
LinkedList<DBBean> bean_list = new LinkedList<DBBean>();
logger.fine("Loading new DBBean List");
while( result.next() ){
DBBean obj = createBean(result);
bean_list.add( obj );
@ -109,8 +110,9 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
return (T) bean_list;
}
else{
if( result.next() )
if( result.next() ){
return (T) createBean(result);
}
return null;
}
@ -129,6 +131,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
DBBean obj = getCachedDBBean(bean_class, id);
if( obj != null )
return obj;
logger.fine("Loading new DBBean("+bean_class.getName()+") with id: "+id);
obj = bean_class.newInstance();
cacheDBBean(obj, id);
@ -157,7 +160,9 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
String idcol = (linkTable.column().isEmpty() ? bean_config.tableName : linkTable.column() );
// Load list from link table
PreparedStatement subStmt = db.getPreparedStatement("SELECT * FROM "+subtable+" WHERE ?=?");
String subsql = "SELECT * FROM "+subtable+" WHERE ?=?";
logger.finest("List Load Query: "+subsql);
PreparedStatement subStmt = db.getPreparedStatement( subsql );
subStmt.setString(1, idcol);
subStmt.setObject(2, obj.getId() );
List<? extends DBBean> list = DBConnection.exec(subStmt,

View file

@ -64,6 +64,7 @@ public class SOAPClientFactory {
}
// Generate the methods
// TODO:
for(WSMethodDef methodDef : wsDef.getMethods()){
CtMethod method = CtNewMethod.make("public int m(int i){}", cc);
method.insertBefore("System.out.println(\"Hello.say():\");");