📄 po.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.util.*;
import java.sql.*;
import java.math.*;
import org.apache.log4j.Logger;
import org.compiere.util.*;
/**
* Persistent Object.
* Superclass for actual implementations
*
* @author Jorg Janke
* @version $Id: PO.java,v 1.10 2003/04/22 05:33:19 jjanke Exp $
*/
public abstract class PO
{
/**
* Create New Persisent Object
* @param ctx context
*/
public PO (Properties ctx)
{
this (ctx, 0, null);
} // PO
/**
* Create & Load existing Persistent Object
* @para ID The unique ID of the object
* @param ctx context
* @param ID Record_ID or 0 for new
*/
public PO (Properties ctx, int ID)
{
this (ctx, ID, null);
} // PO
/**
* Create & Load existing Persistent Object.
* <pre>
* You load
* - an existing single key record with new PO (ctx, Record_ID)
* or new PO (ctx, rs)
* - a new single key record with new PO (ctx, 0)
* - an existing multi key record with new PO (ctx, rs)
* - a new multi key record with new PO (ctx, null)
* The ID for new single key records is created automatically,
* you need to set the IDs for multi-key records explicitly.
* </pre>
* @param ctx context
* @param rs optional - load from current result set position (no navigation, not closed)
*/
public PO (Properties ctx, ResultSet rs)
{
this (ctx, 0, rs);
} // PO
/**
* Create & Load existing Persistent Object.
* <pre>
* You load
* - an existing single key record with new PO (ctx, Record_ID)
* or new PO (ctx, rs)
* - a new single key record with new PO (ctx, 0)
* - an existing multi key record with new PO (ctx, rs)
* - a new multi key record with new PO (ctx, null)
* The ID for new single key records is created automatically,
* you need to set the IDs for multi-key records explicitly.
* </pre>
* @param ctx context
* @param ID the ID if 0, the record defaults are applied - ignored if re exists
* @param rs optional - load from current result set position (no navigation, not closed)
*/
public PO (Properties ctx, int ID, ResultSet rs)
{
p_ctx = ctx;
p_info = initPO(ctx);
//
int size = p_info.getColumnCount();
m_oldValues = new Object[size];
m_newValues = new Object[size];
if (rs != null)
{
load(rs);
setKeyInfo();
}
else if (ID > 0)
{
m_IDs = new int[] {ID};
m_KeyColumns = new String[] {getTableName() + "_ID"};
load();
}
else // new
{
loadDefaults();
m_createNew = true;
setKeyInfo(); // sets m_IDs
}
} // PO
/**
* Create New PO by Copying existing (key not copied).
* @param ctx context
* @param source souce object
* @param AD_Client_ID client
* @param AD_Org_ID org
*/
public PO (Properties ctx, PO source, int AD_Client_ID, int AD_Org_ID)
{
this (ctx, 0, null); // create new
//
if (source != null)
copyValues (source, this);
setAD_Client_ID(AD_Client_ID);
setAD_Org_ID(AD_Org_ID);
} // PO
/** Logger */
protected Logger log = Logger.getLogger (getClass());
private static Logger s_log = Logger.getLogger (PO.class);
/** Context */
protected Properties p_ctx;
/** Model Info */
protected POInfo p_info = null;
/** Original Values */
private Object[] m_oldValues = null;
/** New Valies */
private Object[] m_newValues = null;
/** Record_IDs */
private int[] m_IDs = null;
/** Key Columns */
private String[] m_KeyColumns = null;
/** Create New for Multi Key */
private boolean m_createNew = false;
/** NULL value */
private static final Object NULL = new Object();
/**
* Initialize and return PO_Info
* @param ctx context
* @return POInfo
*/
abstract protected POInfo initPO (Properties ctx);
/**
* 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.isColumnKey(i))
{
String ColumnName = p_info.getColumnName(i);
m_KeyColumns = new String[] {ColumnName};
Integer ii = (Integer)getValue(i);
if (ii == null)
m_IDs = new int[] {0};
else
m_IDs = new int[] {ii.intValue()};
if (Log.isTraceLevel(10))
log.debug("setKeyInfo (PK) - " + ColumnName + "=" + ii);
return;
}
} // primary key search
// Search for Parents
ArrayList columnNames = new ArrayList();
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();
m_IDs = new int[size];
m_KeyColumns = new String[size];
for (int i = 0; i < size; i++)
{
m_KeyColumns[i] = (String)columnNames.get(i);
m_IDs[i] = 0;
Integer ii = (Integer)getValue(m_KeyColumns[i]);
if (ii != null)
m_IDs[i] = ii.intValue();
if (Log.isTraceLevel(10))
log.debug("PO.setKeyInfo (FK) " + m_KeyColumns[i] + "=" + ii);
}
} // setKeyInfo
/**
* String representation
* @return String representation
*/
public String toString()
{
StringBuffer sb = new StringBuffer("PO[");
if (m_IDs != null)
{
for (int i = 0; i < m_IDs.length; i++)
{
if (i > 0)
sb.append(" AND ");
sb.append(m_KeyColumns[i]).append("=").append(m_IDs[i]);
}
}
sb.append("]");
return sb.toString();
} // toString
/**
* Get TableName
* @return table name
*/
protected String getTableName()
{
return p_info.getTableName();
} // getTableName
/**
* Return Single Key Record ID
* @return ID
*/
public int getID()
{
return m_IDs[0];
} // getID
/**
* Get Context
* @return context
*/
public Properties getCtx()
{
return p_ctx;
} // getCtx
/**
* Get Value
* @param index index
* @return value
*/
protected Object getValue (int index)
{
if (index < 0 || index >= getColumnCount())
{
log.error(p_info.getTableName() + "_PO.getValue - Index invalid - " + index);
return null;
}
if (m_newValues[index] != null)
{
if (m_newValues[index].equals(NULL))
return null;
return m_newValues[index];
}
return m_oldValues[index];
} // getValue
/**
* Get Value
* @param columnName column name
* @return calue
*/
protected Object getValue (String columnName)
{
int index = getColumnIndex(columnName);
if (index < 0)
{
log.error(p_info.getTableName() + "_PO.getValue - Column not found - " + columnName);
return null;
}
return getValue (index);
} // getValue
/**
* Set Value if updateable and correct class.
* (and to NULL if not mandatory)
* @param index index
* @param value value
* @return true if value set
*/
protected final boolean setValue (int index, Object value)
{
if (index < 0 || index >= getColumnCount())
{
log.error(p_info.getTableName() + "_PO.setValue - Index invalid - " + index);
return false;
}
String colInfo = " - " + p_info.getColumnName(index);
if (p_info.isColumnUpdateable(index))
{
if (value == null)
{
if (p_info.isColumnMandatory(index))
{
log.error(p_info.getTableName() + "_PO.setValue - Cannot set mandatory column to null " + colInfo);
// Trace.printStack();
return false;
}
m_newValues[index] = NULL; // correct
if (Log.isTraceLevel(Log.l6_Database))
log.debug(p_info.getTableName() + "_PO.setValue - " + p_info.getColumnName(index) + " = null");
}
else
{
// matching class or generic object
if (value.getClass().equals(p_info.getColumnClass(index)) || p_info.getColumnClass(index) == Object.class)
m_newValues[index] = value; // correct
// Integer can be set as BigDecimal (VNumber returns always BigDecimal)
else if (value.getClass() == BigDecimal.class && p_info.getColumnClass(index) == Integer.class)
m_newValues[index] = new Integer (((BigDecimal)value).intValue());
else
{
log.error(p_info.getTableName() + "_PO.setValue - " + p_info.getColumnName(index)
+ " - Class invalid: " + value.getClass().toString()
+ ", Should be " + p_info.getColumnClass(index).toString() + colInfo);
return false;
}
if (Log.isTraceLevel(Log.l6_Database))
log.debug(p_info.getTableName() + "_PO.setValue - " + p_info.getColumnName(index) + " = " + value);
}
}
else
{
colInfo += " - NewValue=" + value + " - OldValue=" + getValue(index);
log.error(p_info.getTableName() + "_PO.setValue - Column not updateable" + colInfo);
return false;
}
return true;
} // setValue
/**
* Set Value
* @param columnName column name
* @param value value
* @return true if value set
*/
protected final boolean setValue (String columnName, Object value)
{
int index = getColumnIndex(columnName);
if (index < 0)
{
log.error(p_info.getTableName() + "_PO.setValue - Column not found - " + columnName);
return false;
}
return setValue (index, value);
} // setValue
/**
* Set Value w/o check (update, r/o, ..).
* Required for key and parent values
* @param columnName column name
* @param value value
* @return true if value set
*/
protected final boolean setValueNoCheck (String columnName, Object value)
{
int index = getColumnIndex(columnName);
if (index < 0)
{
log.error(p_info.getTableName() + "_PO.setValueNoCheck - Column not found - " + columnName);
return false;
}
m_newValues[index] = value; // write direct
if (Log.isTraceLevel(Log.l6_Database))
log.debug(p_info.getTableName() + "_PO.setValueNoCheck - " + columnName + " = " + value);
return true;
} // setValueNoCheck
/*************************************************************************/
/**
* Get Column Count
* @return column count
*/
protected int getColumnCount()
{
return p_info.getColumnCount();
} // getColumnCount
/**
* Get Column Name
* @param index index
* @return ColumnName
*/
protected String getColumnName (int index)
{
return p_info.getColumnName (index);
} // getColumnName
/**
* Get Column Label
* @param index index
* @return Column Label
*/
protected String getColumnLabel (int index)
{
return p_info.getColumnLabel (index);
} // getColumnLabel
/**
* Get Column Description
* @param index index
* @return column description
*/
protected String getColumnDescription (int index)
{
return p_info.getColumnDescription (index);
} // getColumnDescription
/**
* Is Column Mandatory
* @param index index
* @return true if column mandatory
*/
protected boolean isColumnMandatory (int index)
{
return p_info.isColumnMandatory(index);
} // isColumnNandatory
/**
* Is Column Updateable
* @param index index
* @return true if column updateable
*/
protected boolean isColumnUpdateable (int index)
{
return p_info.isColumnUpdateable(index);
} // isColumnUpdateable
/**
* Set Column Updateable
* @param index index
* @param updateable column updateable
*/
protected void setColumnUpdateable (int index, boolean updateable)
{
p_info.setColumnUpdateable(index, updateable);
} // setColumnUpdateable
/**
* Set all columns updateable
* @param updateable updateable
*/
protected void setUpdateable (boolean updateable)
{
p_info.setUpdateable (updateable);
} // setUpdateable
/**
* Get Column Index
* @param columnName column name
* @return index of column
*/
protected int getColumnIndex (String columnName)
{
return p_info.getColumnIndex(columnName);
} // getColumnIndex
/**
* Get Lookup
* @param index index
* @return Lookup or null
*/
protected Lookup getColumnLookup(int index)
{
return p_info.getColumnLookup(index);
} // getColumnLookup
/**
* Copy old values of From to new values of To.
* Does not copy Keys and AD_Client_ID/AD_Org_ID
* @param from old, existing & unchanged PO
* @param to new, not saved PO
*/
protected static void copyValues (PO from, PO to)
{
s_log.info("copyValues - From ID=" + from.getID() + " - To ID=" + to.getID());
if (from.getClass() != to.getClass())
throw new IllegalArgumentException("PO.copyValues - To class=" + to.getClass() + " NOT From=" + from.getClass());
//
for (int i = 0; i < from.m_oldValues.length; i++)
{
String colName = from.p_info.getColumnName(i);
// Ignore Standard Values
if (colName.startsWith("Created") || colName.startsWith("Updated")
// || colName.equals(from.getTableName() + "_ID") // KeyColumn
|| from.p_info.isColumnKey(i)
|| colName.equals("IsActive")
|| colName.equals("AD_Client_ID") || colName.equals("AD_Org_ID"))
; // ignore
else
to.m_newValues[i] = from.m_oldValues[i];
}
} // copy
/*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -