📄 po.java
字号:
* @param to new, not saved PO
*/
protected static void copyValues (PO from, PO to)
{
s_log.fine("From ID=" + from.get_ID() + " - To ID=" + to.get_ID());
if (from.getClass() != to.getClass())
throw new IllegalArgumentException("To class=" + to.getClass() + " NOT From=" + from.getClass());
//
for (int i = 0; i < from.m_oldValues.length; i++)
{
if (from.p_info.isVirtualColumn(i)
|| from.p_info.isKey(i)) // KeyColumn
continue;
String colName = from.p_info.getColumnName(i);
// Ignore Standard Values
if (colName.startsWith("Created")
|| colName.startsWith("Updated")
|| colName.equals("IsActive")
|| colName.equals("AD_Client_ID")
|| colName.equals("AD_Org_ID"))
; // ignore
else
to.m_newValues[i] = from.m_oldValues[i];
}
} // copy
/**************************************************************************
* Load record with ID
* @param ID ID
* @param trxName transaction name
*/
protected void load (int ID, String trxName)
{
log.finest("ID=" + ID);
if (ID > 0)
{
m_IDs = new Object[] {new Integer(ID)};
m_KeyColumns = new String[] {p_info.getTableName() + "_ID"};
load(trxName);
}
else // new
{
loadDefaults();
m_createNew = true;
setKeyInfo(); // sets m_IDs
loadComplete(true);
}
} // load
/**
* (re)Load record with m_ID[*]
*/
public boolean load (String trxName)
{
m_trxName = trxName;
boolean success = true;
StringBuffer sql = new StringBuffer("SELECT ");
int size = get_ColumnCount();
for (int i = 0; i < size; i++)
{
if (i != 0)
sql.append(",");
sql.append(p_info.getColumnSQL(i)); // Normal and Virtual Column
}
sql.append(" FROM ").append(p_info.getTableName())
.append(" WHERE ")
.append(get_WhereClause(false));
//
// int index = -1;
if (CLogMgt.isLevelFinest())
log.finest(get_WhereClause(true));
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql.toString(), m_trxName); // local trx only
for (int i = 0; i < m_IDs.length; i++)
{
Object oo = m_IDs[i];
if (oo instanceof Integer)
pstmt.setInt(i+1, ((Integer)m_IDs[i]).intValue());
else
pstmt.setString(i+1, m_IDs[i].toString());
}
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
success = load(rs);
}
else
{
log.log(Level.SEVERE, "NO Data found for " + get_WhereClause(true), new Exception());
m_IDs = new Object[] {I_ZERO};
success = false;
// throw new DBException("NO Data found for " + get_WhereClause(true));
}
rs.close();
pstmt.close();
pstmt = null;
m_createNew = false;
// reset new values
m_newValues = new Object[size];
}
catch (Exception e)
{
String msg = "";
if (m_trxName != null)
msg = "[" + m_trxName + "] - ";
msg += get_WhereClause(true)
// + ", Index=" + index
// + ", Column=" + get_ColumnName(index)
// + ", " + p_info.toString(index)
+ ", SQL=" + sql.toString();
success = false;
m_IDs = new Object[] {I_ZERO};
log.log(Level.SEVERE, msg, e);
// throw new DBException(e);
}
// Finish
try
{
if (pstmt != null)
pstmt.close();
pstmt = null;
}
catch (SQLException e1)
{
}
loadComplete(success);
return success;
} // load
/**
* Load from the current position of a ResultSet
* @param rs result set
*/
protected boolean load (ResultSet rs)
{
int size = get_ColumnCount();
boolean success = true;
int index = 0;
log.finest("(rs)");
// load column values
for (index = 0; index < size; index++)
{
String columnName = p_info.getColumnName(index);
Class clazz = p_info.getColumnClass(index);
int dt = p_info.getColumnDisplayType(index);
try
{
if (clazz == Integer.class)
m_oldValues[index] = decrypt(index, new Integer(rs.getInt(columnName)));
else if (clazz == BigDecimal.class)
m_oldValues[index] = decrypt(index, rs.getBigDecimal(columnName));
else if (clazz == Boolean.class)
m_oldValues[index] = new Boolean ("Y".equals(decrypt(index, rs.getString(columnName))));
else if (clazz == Timestamp.class)
m_oldValues[index] = decrypt(index, rs.getTimestamp(columnName));
else if (DisplayType.isLOB(dt))
m_oldValues[index] = get_LOB (rs.getObject(columnName));
else if (clazz == String.class)
m_oldValues[index] = decrypt(index, rs.getString(columnName));
else
m_oldValues[index] = loadSpecial(rs, index);
// NULL
if (rs.wasNull() && m_oldValues[index] != null)
m_oldValues[index] = null;
//
if (CLogMgt.isLevelAll())
log.finest(String.valueOf(index) + ": " + p_info.getColumnName(index)
+ "(" + p_info.getColumnClass(index) + ") = " + m_oldValues[index]);
}
catch (SQLException e)
{
if (p_info.isVirtualColumn(index)) // if rs constructor used
log.log(Level.FINER, "Virtual Column not loaded: " + columnName);
else
{
log.log(Level.SEVERE, "(rs) - " + String.valueOf(index)
+ ": " + p_info.getTableName() + "." + p_info.getColumnName(index)
+ " (" + p_info.getColumnClass(index) + ") - " + e);
success = false;
}
}
}
m_createNew = false;
setKeyInfo();
loadComplete(success);
return success;
} // load
/**
* Load from HashMap
* @param hmIn
*/
protected boolean load (HashMap<String,String> hmIn)
{
int size = get_ColumnCount();
boolean success = true;
int index = 0;
log.finest("(hm)");
// load column values
for (index = 0; index < size; index++)
{
String columnName = p_info.getColumnName(index);
String value = (String)hmIn.get(columnName);
if (value == null)
continue;
Class clazz = p_info.getColumnClass(index);
int dt = p_info.getColumnDisplayType(index);
try
{
if (clazz == Integer.class)
m_oldValues[index] = new Integer(value);
else if (clazz == BigDecimal.class)
m_oldValues[index] = new BigDecimal(value);
else if (clazz == Boolean.class)
m_oldValues[index] = new Boolean ("Y".equals(value));
else if (clazz == Timestamp.class)
m_oldValues[index] = Timestamp.valueOf(value);
else if (DisplayType.isLOB(dt))
m_oldValues[index] = null; // get_LOB (rs.getObject(columnName));
else if (clazz == String.class)
m_oldValues[index] = value;
else
m_oldValues[index] = null; // loadSpecial(rs, index);
//
if (CLogMgt.isLevelAll())
log.finest(String.valueOf(index) + ": " + p_info.getColumnName(index)
+ "(" + p_info.getColumnClass(index) + ") = " + m_oldValues[index]);
}
catch (Exception e)
{
if (p_info.isVirtualColumn(index)) // if rs constructor used
log.log(Level.FINER, "Virtual Column not loaded: " + columnName);
else
{
log.log(Level.SEVERE, "(ht) - " + String.valueOf(index)
+ ": " + p_info.getTableName() + "." + p_info.getColumnName(index)
+ " (" + p_info.getColumnClass(index) + ") - " + e);
success = false;
}
}
}
m_createNew = false;
// Overwrite
setStandardDefaults();
setKeyInfo();
loadComplete(success);
return success;
} // load
/**
* Create Hashmap with data as Strings
* @return HashMap
*/
protected HashMap<String,String> get_HashMap()
{
HashMap<String,String> hmOut = new HashMap<String,String>();
int size = get_ColumnCount();
for (int i = 0; i < size; i++)
{
Object value = get_Value(i);
// Don't insert NULL values (allows Database defaults)
if (value == null
|| p_info.isVirtualColumn(i))
continue;
// Display Type
int dt = p_info.getColumnDisplayType(i);
// Based on class of definition, not class of value
Class c = p_info.getColumnClass(i);
String stringValue = null;
if (c == Object.class)
; // saveNewSpecial (value, i));
else if (value == null || value.equals (Null.NULL))
;
else if (value instanceof Integer || value instanceof BigDecimal)
stringValue = value.toString();
else if (c == Boolean.class)
{
boolean bValue = false;
if (value instanceof Boolean)
bValue = ((Boolean)value).booleanValue();
else
bValue = "Y".equals(value);
stringValue = bValue ? "Y" : "N";
}
else if (value instanceof Timestamp)
stringValue = value.toString();
else if (c == String.class)
stringValue = (String)value;
else if (DisplayType.isLOB(dt))
;
else
; // saveNewSpecial (value, i));
//
if (stringValue != null)
hmOut.put(p_info.getColumnName(i), stringValue);
}
// Custom Columns
if (m_custom != null)
{
Iterator it = m_custom.keySet().iterator();
while (it.hasNext())
{
String column = (String)it.next();
int index = p_info.getColumnIndex(column);
String value = (String)m_custom.get(column);
if (value != null)
hmOut.put(column, value);
}
m_custom = null;
}
return hmOut;
} // get_HashMap
/**
* Load Special data (images, ..).
* To be extended by sub-classes
* @param rs result set
* @param index zero based index
* @return value value
* @throws SQLException
*/
protected Object loadSpecial (ResultSet rs, int index) throws SQLException
{
log.finest("(NOP) - " + p_info.getColumnName(index));
return null;
} // loadSpecial
/**
* Load is complete
* @param success success
* To be extended by sub-classes
*/
protected void loadComplete (boolean success)
{
} // loadComplete
/**
* Load Defaults
*/
protected void loadDefaults()
{
setStandardDefaults();
//
/** @todo defaults from Field */
// MField.getDefault(p_info.getDefaultLogic(i));
} // loadDefaults
/**
* Set Default values.
* Client, Org, Created/Updated, *By, IsActive
*/
protected void setStandardDefaults()
{
int size = get_ColumnCount();
for (int i = 0; i < size; i++)
{
if (p_info.isVirtualColumn(i))
continue;
String colName = p_info.getColumnName(i);
// Set Standard Values
if (colName.endsWith("tedBy"))
m_newValues[i] = new Integer (Env.getContextAsInt(p_ctx, "#AD_User_ID"));
else if (colName.equals("Created") || colName.equals("Updated"))
m_newValues[i] = new Timestamp (System.currentTimeMillis());
else if (colName.equals(p_info.getTableName() + "_ID")) // KeyColumn
m_newValues[i] = I_ZERO;
else if (colName.equals("IsActive"))
m_newValues[i] = new Boolean(true);
else if (colName.equals("AD_Client_ID"))
m_newValues[i] = new Integer(Env.getAD_Client_ID(p_ctx));
else if (colName.equals("AD_Org_ID"))
m_newValues[i] = new Integer(Env.getAD_Org_ID(p_ctx));
else if (colName.equals("Processed"))
m_newValues[i] = new Boolean(false);
else if (colName.equals("Processing"))
m_newValues[i] = new Boolean(false);
else if (colName.equals("Posted"))
m_newValues[i] = new Boolean(false);
}
} // setDefaults
/**
* Set Key Info (IDs and KeyColumns).
*/
private void setKeyInfo()
{
// Search for Primary Key
for (int i = 0; i < p_info.getColumnCount(); i++)
{
if (p_info.isKey(i) && p_info.getColumnName(i).endsWith("_ID"))
{
String ColumnName = p_info.getColumnName(i);
m_KeyColumns = new String[] {ColumnName};
Integer ii = (Integer)get_Value(i);
if (ii == null)
m_IDs = new Object[] {I_ZERO};
else
m_IDs = new Object[] {ii};
log.finest("(PK) " + ColumnName + "=" + ii);
return;
}
} // primary key search
// Search for Parents
ArrayList<String> columnNames = new ArrayList<String>();
for (int i = 0; i < p_info.getColumnCount(); i++)
{
if (p_info.isColumnParent(i))
columnNames.add(p_info.getColumnName(i));
}
// Set FKs
int size = columnNames.size();
if (size == 0)
throw new IllegalStateException("No PK nor FK - " + p_info.getTableName());
m_IDs = new Object[size];
m_KeyColumns = new String[size];
for (int i = 0; i < size; i++)
{
m_KeyColumns[i] = (String)columnNames.get(i);
if (m_KeyColumns[i].endsWith("_ID"))
{
Integer ii = null;
try
{
ii = (Integer)get_Value(m_KeyColumns[i]);
}
catch (Exception e)
{
log.log(Level.SEVERE, "", e);
}
if (ii != null)
m_IDs[i] = ii;
}
else
m_IDs[i] = get_Value(m_KeyColumns[i]);
log.finest("(FK) " + m_KeyColumns[i] + "=" + m_IDs[i]);
}
} // setKeyInfo
/**************************************************************************
* Are all mandatory Fields filled (i.e. can we save)?.
* Stops at first null mandatory field
* @return true if all mandatory fields are ok
*/
protected boolean isMandatoryOK()
{
int size = get_ColumnCount();
for (int i = 0; i < size; i++)
{
if (p_info.isColumnMandatory(i))
{
if (p_info.isVirtualColumn(i))
continue;
if (get_Value(i) == null || get_Value(i).equals(Null.NULL))
{
log.info(p_info.getColumnName(i));
return false;
}
}
}
return true;
} // isMandatoryOK
/**************************************************************************
* Set AD_Client
* @param AD_Client_ID client
*/
final protected void setAD_Client_ID (int AD_Client_ID)
{
set_ValueNoCheck ("AD_Client_ID", new Integer(AD_Client_ID));
} // setAD_Client_ID
/**
* Get AD_Client
* @return AD_Client_ID
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -