📄 domainobjectfactory.java
字号:
package org.julp;import java.util.*;import java.sql.*;import java.beans.*;import java.lang.reflect.*;public abstract class DomainObjectFactory implements java.io.Serializable, Cloneable { public DomainObjectFactory() { if (Boolean.getBoolean("debug-julp") || Boolean.getBoolean("debug-" + getClass().getName())){ debug = true; } } public DomainObjectFactory(boolean synchronizedAccess){ this.setSynchronizedAccess(synchronizedAccess); } public DomainObjectFactory(Class requestor, boolean synchronizedAccess){ this.requestor = requestor; this.setSynchronizedAccess(synchronizedAccess); } public DomainObjectFactory(Class requestor) { this.requestor = requestor; } /*================================== members start ===========================================*/ protected boolean debug = false; protected MetaData metaData = null; /* Mapping of DB Table columns to object fields */ protected Map mapping = null; /* Object to persist */ protected Class requestor = null; /* objectId */ protected int objectId = 0; /* Collection of Objects (data) */ protected List objectList = new ArrayList(); /* Collection of Objects to delete from DB */ protected List removedObjects = new ArrayList(); /* Useful to send only modified objects to another tier */ protected boolean discardUnmodifiedObjects; /* Set sequence or disable update/insert/delete */ protected char[] dataModificationSequence; public static final char DATA_MODIFICATION_SEQUENCE_DELETE = 'D'; public static final char DATA_MODIFICATION_SEQUENCE_INSERT = 'I'; public static final char DATA_MODIFICATION_SEQUENCE_UPDATE = 'U'; /* Disable DB modifications*/ protected boolean readOnly = false; /* Optimistic lock settings ****************************************/ /* Generates WHERE stastement with key only */ public static final char KEY_COLUMNS = 'K'; /* Generates WHERE stastement with key and columns modified */ public static final char KEY_AND_MODIFIED_COLUMNS = 'M'; /* Generates WHERE stastement with key and all updateble columns - this is the strictest concurrency check */ public static final char KEY_AND_UPDATEBLE_COLUMNS = 'U'; /* Current Optimistic lock setting. Default is KEY_COLUMNS (Primary Key only) */ protected char optimisticLock = KEY_COLUMNS; /* Throw exception if PreparedStatement.executeUpdate() does not return 1 for each DomainObject. It means that the row in DB Table most likely was modified or deleted by another user/process. */ protected boolean throwOptimisticLockDeleteException = true; protected boolean throwOptimisticLockUpdateException = true; /* END of Optimistic lock settings *********************************/ /* Target DB catalog */ protected String catalog = null; /* Target DB schema */ protected String schema = null; /* Target DB Table */ protected String table = null; /* This object generates SQL for data modification */ protected DataWriter dataWriter = null; /* This is JDBC utility */ protected DBServices dbServices = null; /* used as argument in method.invoke(obj, readArg)*/ protected Object[] readArg = new Object[0]; /* use synchronized Collections */ protected boolean synchronizedAccess = false; /* If lazyLoding == true than set only original values vs. original and current */ protected boolean lazyLoading = false; /* Records per page */ protected int pageSize = 0; /* Do not execute INSERTS/UPDATES/DELETES - just generate SQL and parameters */ protected boolean generateSQLOnly = false; /* control if load() append objects to objectList )*/ protected boolean append = false; /* Some databases (like hsqdb 1.7.1) failed when UPDATE statement is using TABLE_NAME.COLUMN_NAME in SET clause. INSERT also failes */ protected boolean noFullColumnName = false; /*================================== members end===========================================*/ public int load(ResultSet rs) throws java.sql.SQLException{ String columnName = null; String fieldName = null; ResultSetMetaData rsmd = rs.getMetaData(); int rsColCount = rsmd.getColumnCount(); List rsColumns = new ArrayList(rsColCount); for (int i = 1;i <= rsColCount;i++){ rsColumns.add(rsmd.getColumnName(i)); } int columnCount = 0; if (!isAppend()){ this.objectList.clear(); if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::load()::requestor.getName()" + "::objectId 1: " + objectId); this.objectId = 0; if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() +"::load()::requestor.getName()" + "::objectId 2: " + objectId); } try{ //long start1 = System.currentTimeMillis(); if (this.metaData == null){ populateMetaData(); } //long end1 = System.currentTimeMillis(); //if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::populateMetaData: " + (end1 - start1)); columnCount = this.metaData.getColumnCount(); if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::load()::requestor.getName()" + "::columnCount: " + columnCount); while (rs.next()){ DataHolder originalValues = null; if (!isReadOnly()){ originalValues = new DataHolder(columnCount); } Object obj = requestor.newInstance(); if (!isLazyLoading()){ ((DomainObject) obj).setLoading(true); } for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++){ fieldName = this.metaData.getFieldName(columnIndex); if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::load()::fieldName: " + fieldName); if (fieldName != null){ // ResultSet may have more columns then this Object fields, so skip unneeded column columnName = this.metaData.getColumnName(columnIndex); if (!rsColumns.contains(columnName)){ // ResultSet has less columns then DomainObject fields this.metaData.setWritable(columnIndex, false); continue; } Object value = null; Object[] convertedValue = null; String fieldClassName = this.metaData.getFieldClassName(columnIndex); // So far there is support only for listed DataTypes. if (fieldClassName.equals("java.lang.String")){ convertedValue = new String[1]; value = rs.getString(columnName); if (rs.wasNull()){ convertedValue[0] = (String) null; }else{ convertedValue[0] = value; } }else if (fieldClassName.equals("java.lang.Integer")){ convertedValue = new Integer[1]; value = new Integer(rs.getInt(columnName)); if (rs.wasNull()){ convertedValue[0] = (Integer) null; }else{ convertedValue[0] = value; } }else if (fieldClassName.equals("java.lang.Long")){ convertedValue = new Long[1]; value = new Long(rs.getLong(columnName)); if (rs.wasNull()){ convertedValue[0] = (Long) null; }else{ convertedValue[0] = value; } }else if (fieldClassName.equals("java.lang.Double")){ convertedValue = new Double[1]; value = new Double(rs.getDouble(columnName)); if (rs.wasNull()){ convertedValue[0] = (Double) null; }else{ convertedValue[0] = value; } }else if (fieldClassName.equals("java.lang.Float")){ convertedValue = new Float[1]; value = new Float(rs.getFloat(columnName)); if (rs.wasNull()){ convertedValue[0] = (Float) null; }else{ convertedValue[0] = value; } }else if (fieldClassName.equals("java.lang.Short")){ convertedValue = new Short[1]; value = new Short(rs.getShort(columnName)); if (rs.wasNull()){ convertedValue[0] = (Short) null; }else{ convertedValue[0] = value; } }else if (fieldClassName.equals("java.sql.Time")){ convertedValue = new java.sql.Time[1]; value = (java.sql.Time) rs.getTime(columnName); //value = new java.sql.Time(rs.getTime(columnName).getTime()); if (rs.wasNull()){ convertedValue[0] = (java.sql.Time) null; }else{ convertedValue[0] = value; } }else if (fieldClassName.equals("java.sql.Date")){ convertedValue = new java.sql.Date[1]; value = (java.sql.Date) rs.getDate(columnName); //value = new java.sql.Date(rs.getDate(columnName).getTime()); if (rs.wasNull()){ convertedValue[0] = (java.sql.Date) null; }else{ convertedValue[0] = value; } }else if (fieldClassName.equals("java.util.Date")){ convertedValue = new java.util.Date[1]; java.sql.Date d = rs.getDate(columnName); //value = new java.util.Date(rs.getDate(columnName).getTime()); if (rs.wasNull()){ convertedValue[0] = (java.util.Date) null; }else{ value = new java.util.Date(d.getTime()); convertedValue[0] = value; } }else if (fieldClassName.equals("java.math.BigDecimal")){ convertedValue = new java.math.BigDecimal[1]; value = rs.getBigDecimal(columnName); if (rs.wasNull()){ convertedValue[0] = (java.math.BigDecimal) null; }else{ convertedValue[0] = value; } }else if (fieldClassName.equals("java.math.BigInteger")){ convertedValue = new java.math.BigInteger[1]; long longValue = 0; longValue = rs.getLong(columnName); if (rs.wasNull()){ convertedValue[0] = (java.math.BigInteger) null; }else{ value = java.math.BigInteger.valueOf(longValue); convertedValue[0] = value; } }else if (fieldClassName.equals("boolean")){ convertedValue = new Boolean[1]; boolean booleanValue = false; booleanValue = rs.getBoolean(columnName); if (rs.wasNull()){ convertedValue[0] = new Boolean(false); }else{ value = new Boolean(booleanValue); convertedValue[0] = value; } }else if (fieldClassName.equals("byte")){ convertedValue = new Byte[1]; byte byteValue = 0; byteValue = rs.getByte(columnName); if (rs.wasNull()){ byte emptyByte = 0; convertedValue[0] = new Byte(emptyByte); }else{ value = new Byte(byteValue); convertedValue[0] = value; } }else if (fieldClassName.equals("char")){ convertedValue = new Character[1]; String stringValue = null; stringValue = rs.getString(columnName); if (rs.wasNull()){ char emptyChar = '\u0000'; convertedValue[0] = new Character(emptyChar); }else{ value = new Character(stringValue.charAt(0)); convertedValue[0] = value; } }else if (fieldClassName.equals("double")){ convertedValue = new Double[1]; double doubleValue = 0.0; doubleValue = rs.getDouble(columnName); if (rs.wasNull()){ convertedValue[0] = new Double(0.0); }else{ value = new Double(doubleValue); convertedValue[0] = value; } }else if (fieldClassName.equals("float")){ convertedValue = new Float[1]; float floatValue = 0; floatValue = rs.getFloat(columnName); if (rs.wasNull()){ convertedValue[0] = new Float(0.0); }else{ value = new Float(floatValue); convertedValue[0] = value; } }else if (fieldClassName.equals("int")){ convertedValue = new Integer[1]; int intValue = 0; intValue = rs.getInt(columnName); if (rs.wasNull()){ convertedValue[0] = new Integer(0); }else{ value = new Integer(intValue); convertedValue[0] = value; } }else if (fieldClassName.equals("long")){ convertedValue = new Long[1]; long longValue = 0; longValue = rs.getLong(columnName); if (rs.wasNull()){ convertedValue[0] = new Long(0); }else{ value = new Long(longValue); convertedValue[0] = value; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -