This commit is contained in:
Ziver Koc 2010-11-07 15:40:59 +00:00
parent b9a11aff97
commit efae412a88

View file

@ -14,6 +14,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;
import zutil.db.DBConnection;
@ -34,6 +35,7 @@ import zutil.log.LogUtil;
* *String
* *Character
* *DBBean
* *java.sql.Date
* *List<DBBean>
*
* </XMP>
@ -68,22 +70,12 @@ public abstract class DBBean {
String column() default "";
}
/**
* Sets the field as the id column in the table
*/
/*@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface DBTableID {}
*/
/**
* A Class that contains information about a bean
*/
protected static class DBBeanConfig{
/** The name of the table in the DB */
protected String tableName;
/** The id field */
//protected Field id_field;
/** All the fields in the bean */
protected ArrayList<Field> fields;
@ -103,15 +95,6 @@ public abstract class DBBean {
processing = false;
}
/**
* @return the ID field of the bean or null if there is non
*/
/*public static Field getIDField(Class<? extends DBBean> c){
if( !beanConfigs.containsKey( c ) )
initBeanConfig( c );
return beanConfigs.get( c ).id_field;
}*/
/**
* @return all the fields except the ID field
*/
@ -148,17 +131,9 @@ public abstract class DBBean {
!Modifier.isStatic( mod ) &&
!Modifier.isInterface( mod ) &&
!Modifier.isNative( mod )){
//if(field.getAnnotation(DBBean.DBTableID.class) != null)
// config.id_field = field;
config.fields.add( field );
}
}
/*try {
config.id_field = DBBean.class.getDeclaredField("id");
config.id_field.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}*/
beanConfigs.put(c, config);
}
@ -262,13 +237,15 @@ public abstract class DBBean {
/**
* Deletes the object from the DB, WARNING will not delete sub beans
*
* @throws SQLException
*/
public void delete(DBConnection db){
public void delete(DBConnection db) throws SQLException{
Class<? extends DBBean> c = this.getClass();
DBBeanConfig config = beanConfigs.get(c);
if( this.getId() == null )
throw new NoSuchElementException("ID field is null!");
try {
throw new NoSuchElementException("ID field is null( Has the bean been saved?)!");
String sql = "DELETE FROM "+config.tableName+" WHERE id=?";
logger.fine("Load query: "+sql);
PreparedStatement stmt = db.getPreparedStatement( sql );
@ -278,9 +255,6 @@ public abstract class DBBean {
// Execute the SQL
DBConnection.exec(stmt);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
@ -391,7 +365,7 @@ public abstract class DBBean {
field.setAccessible(true);
return field.get(this);
} catch (Exception e) {
e.printStackTrace();
logger.log(Level.WARNING, e.getMessage(), e);
}
return null;
}
@ -405,9 +379,21 @@ public abstract class DBBean {
protected void setFieldValue(Field field, Object o){
try {
field.setAccessible(true);
if( o == null && !Object.class.isAssignableFrom( field.getType() ) ){
logger.fine("Trying to set primitive data type to null!");
if( field.getType() == Integer.TYPE ) field.setInt(this, 0);
else if( field.getType() == Character.TYPE )field.setChar(this, (char) 0);
else if( field.getType() == Byte.TYPE ) field.setByte(this, (byte) 0);
else if( field.getType() == Short.TYPE ) field.setShort(this, (short) 0);
else if( field.getType() == Long.TYPE ) field.setLong(this, 0l);
else if( field.getType() == Float.TYPE ) field.setFloat(this, 0f);
else if( field.getType() == Double.TYPE ) field.setDouble(this, 0d);
else if( field.getType() == Boolean.TYPE ) field.setBoolean(this, false);
}
else
field.set(this, o);
} catch (Exception e) {
e.printStackTrace();
logger.log(Level.WARNING, e.getMessage(), e);
}
}