📄 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 Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.io.*;
import java.math.*;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import org.compiere.util.*;
/**
* Persistent Object.
* Superclass for actual implementations
*
* @author Jorg Janke
* @version $Id: PO.java,v 1.131 2006/01/04 05:39:33 jjanke Exp $
*/
public abstract class PO
implements Serializable, Comparator, Evaluatee
{
/**
* Set Document Value Workflow Manager
* @param docWFMgr mgr
*/
public static void setDocWorkflowMgr (DocWorkflowMgr docWFMgr)
{
s_docWFMgr = docWFMgr;
s_log.config (s_docWFMgr.toString());
} // setDocWorkflowMgr
/** Document Value Workflow Manager */
private static DocWorkflowMgr s_docWFMgr = null;
/**************************************************************************
* Create New Persisent Object
* @param ctx context
*/
public PO (Properties ctx)
{
this (ctx, 0, null, null);
} // PO
/**
* Create & Load existing Persistent Object
* @param ID The unique ID of the object
* @param ctx context
* @param trxName transaction name
*/
public PO (Properties ctx, int ID, String trxName)
{
this (ctx, ID, trxName, null);
} // PO
/**
* Create & Load existing Persistent Object.
* @param ctx context
* @param rs optional - load from current result set position (no navigation, not closed)
* if null, a new record is created.
* @param trxName transaction name
*/
public PO (Properties ctx, ResultSet rs, String trxName)
{
this (ctx, 0, trxName, 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, Record_ID, trxName)
* or new PO (ctx, rs, get_TrxName())
* - a new single key record with new PO (ctx, 0)
* - an existing multi key record with new PO (ctx, rs, get_TrxName())
* - 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 trxName transaction name
* @param rs optional - load from current result set position (no navigation, not closed)
*/
public PO (Properties ctx, int ID, String trxName, ResultSet rs)
{
if (ctx == null)
throw new IllegalArgumentException ("No Context");
p_ctx = ctx;
p_info = initPO(ctx);
if (p_info == null || p_info.getTableName() == null)
throw new IllegalArgumentException ("Invalid PO Info - " + p_info);
//
int size = p_info.getColumnCount();
m_oldValues = new Object[size];
m_newValues = new Object[size];
m_trxName = trxName;
if (rs != null)
load(rs); // will not have virtual columns
else
load(ID, trxName);
} // 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, null); // create new
//
if (source != null)
copyValues (source, this);
setAD_Client_ID(AD_Client_ID);
setAD_Org_ID(AD_Org_ID);
} // PO
/** Logger */
protected transient CLogger log = CLogger.getCLogger (getClass());
/** Static Logger */
private static CLogger s_log = CLogger.getCLogger (PO.class);
/** Context */
protected Properties p_ctx;
/** Model Info */
protected volatile POInfo p_info = null;
/** Original Values */
private Object[] m_oldValues = null;
/** New Valies */
private Object[] m_newValues = null;
/** Record_IDs */
private Object[] m_IDs = new Object[] {I_ZERO};
/** Key Columns */
private String[] m_KeyColumns = null;
/** Create New for Multi Key */
private boolean m_createNew = false;
/** Attachment with entriess */
private MAttachment m_attachment = null;
/** Deleted ID */
private int m_idOld = 0;
/** Custom Columns */
private HashMap<String,String> m_custom = null;
/** Zero Integer */
protected static final Integer I_ZERO = new Integer(0);
/** Accounting Columns */
private ArrayList <String> s_acctColumns = null;
/** Access Level S__ 100 4 System info */
public static final int ACCESSLEVEL_SYSTEM = 4;
/** Access Level _C_ 010 2 Client info */
public static final int ACCESSLEVEL_CLIENT = 2;
/** Access Level __O 001 1 Organization info */
public static final int ACCESSLEVEL_ORG = 1;
/** Access Level SCO 111 7 System shared info */
public static final int ACCESSLEVEL_ALL = 7;
/** Access Level SC_ 110 6 System/Client info */
public static final int ACCESSLEVEL_SYSTEMCLIENT = 6;
/** Access Level _CO 011 3 Client shared info */
public static final int ACCESSLEVEL_CLIENTORG = 3;
/**
* Initialize and return PO_Info
* @param ctx context
* @return POInfo
*/
abstract protected POInfo initPO (Properties ctx);
/**
* Get Table Access Level
* @return Access Level
*/
abstract protected int get_AccessLevel();
/**
* String representation
* @return String representation
*/
public String toString()
{
StringBuffer sb = new StringBuffer("PO[")
.append(get_WhereClause(true)).append("]");
return sb.toString();
} // toString
/**
* Equals based on ID
* @param cmp comperator
* @return true if ID the same
*/
public boolean equals (Object cmp)
{
if (cmp == null)
return false;
if (!(cmp instanceof PO))
return false;
if (cmp.getClass().equals(this.getClass()))
return ((PO)cmp).get_ID() == get_ID();
return super.equals(cmp);
} // equals
/**
* Compare based on DocumentNo, Value, Name, Description
* @param o1 Object 1
* @param o2 Object 2
* @return -1 if o1 < o2
*/
public int compare (Object o1, Object o2)
{
if (o1 == null)
return -1;
else if (o2 == null)
return 1;
if (!(o1 instanceof PO))
throw new ClassCastException ("Not PO -1- " + o1);
if (!(o2 instanceof PO))
throw new ClassCastException ("Not PO -2- " + o2);
// same class
if (o1.getClass().equals(o2.getClass()))
{
int index = get_ColumnIndex("DocumentNo");
if (index == -1)
index = get_ColumnIndex("Value");
if (index == -1)
index = get_ColumnIndex("Name");
if (index == -1)
index = get_ColumnIndex("Description");
if (index != -1)
{
PO po1 = (PO)o1;
Object comp1 = po1.get_Value(index);
PO po2 = (PO)o2;
Object comp2 = po2.get_Value(index);
if (comp1 == null)
return -1;
else if (comp2 == null)
return 1;
return comp1.toString().compareTo(comp2.toString());
}
}
return o1.toString().compareTo(o2.toString());
} // compare
/**
* Get TableName.
* @return table name
*/
public String get_TableName()
{
return p_info.getTableName();
} // get_TableName
/**
* Get Key Columns.
* @return table name
*/
public String[] get_KeyColumns()
{
return m_KeyColumns;
} // get_KeyColumns
/**
* Get Table ID.
* @return table id
*/
public int get_Table_ID()
{
return p_info.getAD_Table_ID();
} // get_TableID
/**
* Return Single Key Record ID
* @return ID or 0
*/
public int get_ID()
{
Object oo = m_IDs[0];
if (oo != null && oo instanceof Integer)
return ((Integer)oo).intValue();
return 0;
} // getID
/**
* Return Deleted Single Key Record ID
* @return ID or 0
*/
public int get_IDOld()
{
return m_idOld;
} // getID
/**
* Get Context
* @return context
*/
public Properties getCtx()
{
return p_ctx;
} // getCtx
/**
* Get Logger
* @return logger
*/
public CLogger get_Logger()
{
return log;
} // getLogger
/**************************************************************************
* Get Value
* @param index index
* @return value
*/
public final Object get_Value (int index)
{
if (index < 0 || index >= get_ColumnCount())
{
log.log(Level.SEVERE, "Index invalid - " + index);
return null;
}
if (m_newValues[index] != null)
{
if (m_newValues[index].equals(Null.NULL))
return null;
return m_newValues[index];
}
return m_oldValues[index];
} // get_Value
/**
* Get Value as int
* @param index index
* @return int value or 0
*/
protected int get_ValueAsInt (int index)
{
Object value = get_Value(index);
if (value == null)
return 0;
if (value instanceof Integer)
return ((Integer)value).intValue();
try
{
return Integer.parseInt(value.toString());
}
catch (NumberFormatException ex)
{
log.warning(p_info.getColumnName(index) + " - " + ex.getMessage());
return 0;
}
} // get_ValueAsInt
/**
* Get Value
* @param columnName column name
* @return value or null
*/
public final Object get_Value (String columnName)
{
int index = get_ColumnIndex(columnName);
if (index < 0)
{
log.log(Level.SEVERE, "Column not found - " + columnName);
Trace.printStack();
return null;
}
return get_Value (index);
} // get_Value
/**
* Get Encrypted Value
* @param columnName column name
* @return value or null
*/
protected final Object get_ValueE (String columnName)
{
return get_Value (columnName);
} // get_ValueE
/**
* Get Column Value
* @param variableName name
* @return value or ""
*/
public String get_ValueAsString (String variableName)
{
Object value = get_Value (variableName);
if (value == null)
return "";
return value.toString();
} // get_ValueAsString
/**
* Get Value of Column
* @param AD_Column_ID column
* @return value or null
*/
public final Object get_ValueOfColumn (int AD_Column_ID)
{
int index = p_info.getColumnIndex(AD_Column_ID);
if (index < 0)
{
log.log(Level.SEVERE, "Not found - AD_Column_ID=" + AD_Column_ID);
return null;
}
return get_Value (index);
} // get_ValueOfColumn
/**
* Get Old Value
* @param index index
* @return value
*/
public final Object get_ValueOld (int index)
{
if (index < 0 || index >= get_ColumnCount())
{
log.log(Level.SEVERE, "Index invalid - " + index);
return null;
}
return m_oldValues[index];
} // get_ValueOld
/**
* Get Old Value
* @param columnName column name
* @return value or null
*/
public final Object get_ValueOld (String columnName)
{
int index = get_ColumnIndex(columnName);
if (index < 0)
{
log.log(Level.SEVERE, "Column not found - " + columnName);
return null;
}
return get_ValueOld (index);
} // get_ValueOld
/**
* Get Old Value as int
* @param columnName column name
* @return int value or 0
*/
protected int get_ValueOldAsInt (String columnName)
{
Object value = get_ValueOld(columnName);
if (value == null)
return 0;
if (value instanceof Integer)
return ((Integer)value).intValue();
try
{
return Integer.parseInt(value.toString());
}
catch (NumberFormatException ex)
{
log.warning(columnName + " - " + ex.getMessage());
return 0;
}
} // get_ValueOldAsInt
/**
* Is Value Changed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -