/**
* Class BusinessObject 是所有核算对象的基础
* 它用来表达每一个核算对象以及核算对象的关联子对象.
*
cjyu 2014年5月12日13:48:03 增加构造函数,如果未传入jboclass 时将根据resultSet构造一个对象
* @author xjzhao
* @version 1.0, 13/03/13
* @see com.amarsoft.are.jbo.BizObject
* @see com.amarsoft.are.jbo.BizObjectClass
* @since JDK1.6
*/
package com.amarsoft.app.base.businessobject;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.antlr.v4.runtime.tree.ParseTree;
import org.jdom.output.XMLOutputter;
import com.amarsoft.app.base.config.impl.XMLConfig;
import com.amarsoft.app.base.script.ScriptConfig;
import com.amarsoft.app.base.util.ACCOUNT_CONSTANTS;
import com.amarsoft.are.ARE;
import com.amarsoft.are.jbo.BizObject;
import com.amarsoft.are.jbo.BizObjectClass;
import com.amarsoft.are.jbo.BizObjectKey;
import com.amarsoft.are.jbo.JBOException;
import com.amarsoft.are.jbo.JBOFactory;
import com.amarsoft.are.jbo.impl.StateBizObject;
import com.amarsoft.are.lang.DataElement;
import com.amarsoft.are.lang.Element;
import com.amarsoft.are.lang.StringX;
import com.amarsoft.are.util.Arith;
import com.amarsoft.are.util.json.JSONElement;
import com.amarsoft.are.util.json.JSONEncoder;
import com.amarsoft.are.util.json.JSONException;
import com.amarsoft.are.util.json.JSONObject;
import com.amarsoft.are.util.xml.Attribute;
public class BusinessObject extends StateBizObject{
private static final long serialVersionUID = 1L;
/**
* 默认的对象类型
*/
public static final String OBJECT_TYPE_DEFAULT_NAME = "SYS_GEN_OBJECTTYPE";
/**
* 本业务对象存在多字段主键,在体现ObjectNo时多字段值之间的分隔符
*/
public static final String OBJECT_KEY_DEFAULT_SPLIT_CHARACTOR = ",";
/**
* 默认主键ID,当不使用JBOFacotory创建对象,且未指定主键时,自动使用此字段作为主键
*/
public static final String OBJECT_KEY_DEFAULT_ATTRIBUTEID = "SYS_SERIALNO";
/**
* 扩展属性集合
*/
private Map ex_attributes = new HashMap();
private Map attributeIDMap = new HashMap();//为了不区分大小写,ex_attributes中的Key全部转为大写保存
/**
* 根据BizObjectClass创建一个新的业务对象
* @param bizObjectClass
*/
protected BusinessObject(BizObjectClass bizObjectClass) {
super(bizObjectClass);
}
/**
* 设置对象编号
* @param objectNo
* @throws JBOException
*/
public void setKey(Object... objectNo) throws JBOException{
String[] keyAttributeArray = this.getBizObjectClass().getKeyAttributes();
if(keyAttributeArray==null||keyAttributeArray.length==0) return ;
if(keyAttributeArray.length!=objectNo.length) throw new JBOException("设置主键时,传入参数个数不匹配!");
for(int i=0;i=0) return true;
else return false;
}
}
/**
* 获得对象的所有属性ID
* @return
*/
public String[] getAttributeIDArray(){
List list = new ArrayList();
Element[] dataElements = this.getAttributes();
for(int i=0;i ex_attributesID = ex_attributes.keySet();
for(String attributeID:ex_attributesID){
list.add(this.attributeIDMap.get(attributeID));
}
String[] s = new String[list.size()];
list.toArray(s);
return s;
}
/**
* 获取对应Object
* @param attributeID
* @return long
* @throws JBOException
*/
public Object getObject(String attributeID) throws JBOException{
if(this.indexOfAttribute(attributeID)>=0){
DataElement value = this.getAttribute(attributeID);
return value.getValue();
}
else{
return this.ex_attributes.get(attributeID.toUpperCase());
}
}
/**
* 获取对应属性的字符串值
* @param attributeID
* @return String
* @throws JBOException
*/
public String getString(String attributeID) throws JBOException{
Object value = this.getObject(attributeID);
if(value==null) return "";
if (value instanceof String) {
return (String)value;
}
else {
return value.toString();
}
}
/**
* 获取对应属性的浮点数
* @param attributeID
* @return double
* @throws JBOException
*/
public double getDouble(String attributeID) throws JBOException{
Object value = this.getObject(attributeID);
if(value==null) return 0d;
if (value instanceof Number) {
return ((Number)value).doubleValue();
}
else if (value instanceof String){
String stringValue= (String)value;
if(StringX.isEmpty(stringValue)) return 0d;
return Double.valueOf(stringValue.replaceAll(",", ""));
}
else {// 金额类型字段处理
throw new JBOException(this.getBizClassName()+"."+attributeID+"的值不是数字!");
}
}
/**
* 获取对应属性的浮点数据(保留小数点后2位,本系统中只有最终金额使用,计算中间数值变量请勿用以免丢失精度)
* @param attributeID
* @return double
* @throws JBOException
*/
public double getDouble(String attributeID,int precision) throws JBOException{
double d=this.getDouble(attributeID);
return Arith.round(d, precision);
}
/**
* 获取对应属性的浮点数(保留小数点后8位,本系统中只有利率才使用)
* @param attributeID
* @return double
* @throws JBOException
*/
public double getRate(String attributeID) throws JBOException{
return this.getDouble(attributeID,ACCOUNT_CONSTANTS.Number_Precision_Rate);
}
/**
* 获取对应属性的整数
* @param attributeID
* @return int
* @throws JBOException
*/
public int getInt(String attributeID) throws JBOException{
Object value = this.getObject(attributeID);
if(value==null) return 0;
if (value instanceof Number) {
return ((Number)value).intValue();
}
else if (value instanceof String){
String stringValue= (String)value;
if(StringX.isEmpty(stringValue)) return 0;
return Integer.valueOf(stringValue.replaceAll(",", ""));
}
else {// 金额类型字段处理
throw new JBOException(this.getBizClassName()+"."+attributeID+"的值不是数字!");
}
}
/**
* 获取对应属性的长整型数值
* @param attributeID
* @return long
* @throws JBOException
*/
public long getLong(String attributeID) throws JBOException{
Object value = this.getObject(attributeID);
if(value==null) return 0l;
if (value instanceof Number) {
return ((Number)value).intValue();
}
else if (value instanceof String){
String stringValue= (String)value;
if(StringX.isEmpty(stringValue)) return 0l;
return Long.valueOf(stringValue.replaceAll(",", ""));
}
else {// 金额类型字段处理
throw new JBOException(this.getBizClassName()+"."+attributeID+"的值不是数字!");
}
}
/**
* 获取该对象的编号(唯一表示该对象的字符串,及JBO中定义主键以逗号分隔的拼接)
* @param void
* @return String Key1Value,Key2Value,Key3Value,……
* @throws Exception
*/
public String getKeyString() throws JBOException{
return getKeyString(BusinessObject.OBJECT_KEY_DEFAULT_SPLIT_CHARACTOR);
}
/**
* 获取该对象的编号(唯一表示该对象的字符串,及JBO中定义主键以指定字符分隔的拼接)
* @param void
* @return String Key1Value,Key2Value,Key3Value,……
* @throws JBOException
*/
public String getKeyString(String split) throws JBOException{
String[] keyAttributeArray = this.getBizObjectClass().getKeyAttributes();
if(keyAttributeArray==null||keyAttributeArray.length==0){
return "";
}
String keyString = "";
for(int i=0;i mapValue) throws JBOException{
for(Iterator it=mapValue.keySet().iterator();it.hasNext();){
String key = it.next();
this.setAttributeValue(key, mapValue.get(key));
}
}
/**
* 首先将当前对象的所有值清空,然后将一个businessObject的所有属性归并到当前对象,对于当前对象不具有的属性,自动作为扩展属性处理,不做忽略
* BizObject中的setAttributeValue会自动忽略本省不拥有的属性
* @param businessObject
* @throws JBOException
*/
public void setAttributes(BusinessObject businessObject) throws JBOException{
String[] attributeIDs = businessObject.getAttributeIDArray();
for(String attributeID:attributeIDs){
Object value = businessObject.getObject(attributeID);
this.setAttributeValue(attributeID, value);
}
}
/**
* 设置属性值
* @param dataElement
* @return
* @throws JBOException
*/
public BusinessObject setAttributeValue(DataElement dataElement) throws JBOException{
return this.setAttributeValue(dataElement.getName(), dataElement.getValue());
}
/*
* 设置属性值
* @see com.amarsoft.are.jbo.BizObject#setAttributeValue(java.lang.String, java.lang.Object)
*/
public BusinessObject setAttributeValue(String attributeID,Object value) throws JBOException{
if(this.indexOfAttribute(attributeID)>=0){
super.setAttributeValue(attributeID,value);
}
else{
this.ex_attributes.put(attributeID.toUpperCase(), value);
this.attributeIDMap.put(attributeID.toUpperCase(), attributeID);
}
return this;
}
/*
* 设置属性值
* @see com.amarsoft.are.jbo.BizObject#setAttributeValue(java.lang.String, java.lang.Object)
*/
public BusinessObject setAttributeValue(String attributeID,int value) throws JBOException{
if(this.indexOfAttribute(attributeID)>=0){
super.setAttributeValue(attributeID, value);
}
else{
this.ex_attributes.put(attributeID.toUpperCase(), value);
this.attributeIDMap.put(attributeID.toUpperCase(), attributeID);
}
return this;
}
/*
* 设置属性值
* @see com.amarsoft.are.jbo.BizObject#setAttributeValue(java.lang.String, java.lang.Object)
*/
public BusinessObject setAttributeValue(String attributeID,double value) throws JBOException{
if(this.indexOfAttribute(attributeID)>=0){
super.setAttributeValue(attributeID, value);
}
else{
this.ex_attributes.put(attributeID.toUpperCase(), value);
this.attributeIDMap.put(attributeID.toUpperCase(), attributeID);
}
return this;
}
/**
* 设置关联对象
* @param attributeID
* @param value
* @return
* @throws JBOException
*/
public BusinessObject setAttributeValue(String attributeID,BusinessObject[] value) throws JBOException{
this.ex_attributes.remove(attributeID.toUpperCase());
this.attributeIDMap.put(attributeID.toUpperCase(), attributeID);
for(BusinessObject o:value){
this.appendBusinessObject(attributeID, o);
}
return this;
}
/**
* 设置关联对象
* @param attributeID
* @param value
* @return
* @throws JBOException
*/
public BusinessObject setAttributeValue(String attributeID,Collection c) throws JBOException{
this.ex_attributes.remove(attributeID.toUpperCase());
this.attributeIDMap.put(attributeID.toUpperCase(), attributeID);
this.appendBusinessObjects(attributeID, c);
return this;
}
/**
* 设置关联对象
* @param attributeID
* @param value
* @return
* @throws JBOException
*/
public BusinessObject setAttributeValue(String attributeID,BusinessObject value) throws JBOException{
this.ex_attributes.remove(attributeID.toUpperCase());
this.attributeIDMap.put(attributeID.toUpperCase(), attributeID);
this.appendBusinessObject(attributeID, value);
return this;
}
/**
* 将该对象的主键设置为空
* @param void
* @return void
* @throws JBOException
*/
public BusinessObject setKeyNull() throws JBOException{
DataElement[] keys = this.getKey().getAttributes();
for(int k = 0; k < keys.length; k++){
super.setAttributeValue(keys[k].getName(), null);
}
return this;
}
/**
* 取指定对象类型的1:1关联对象,当存在多个关联对象时报错
* @param objectType
* @return
* @throws Exception
*/
public BusinessObject getBusinessObject(String attributeID) throws JBOException {
List l=this.getBusinessObjects(attributeID);
if(l.isEmpty()) return null;
if(l.size()>1){
String desc ="对象{"+this.getBizClassName()+"-"+this.getKeyString()+"}的关联对象{"+attributeID+"}存在多个,不能使用此方法!";
throw new JBOException(desc);
}
return l.get(0);
}
/**
* 获取满足筛选条件的关联对象记录
* @param objectType
* @param as
* @return
* @throws Exception
*/
public BusinessObject getBusinessObjectByAttributes(String attributeID,Map filterParameters) throws Exception {
List values = this.getBusinessObjects(attributeID);
return BusinessObjectHelper.getBusinessObjectByAttributes(values, filterParameters);
}
public BusinessObject getBusinessObjectByKey(String attributeID,Object... parameters) throws JBOException {
List t=this.getBusinessObjects(attributeID);
if(t.isEmpty()) return null;
String[] keys=t.get(0).getBizObjectClass().getKeyAttributes();
Map parameterMap=new HashMap();
for(int i=0;i l=this.getBusinessObjectsByAttributes(attributeID,parameterMap);
if(l.isEmpty()) return null;
/*if(l.size()>1){
String desc ="对象{"+this.getBizClassName()+"-"+this.getKeyString()+"}的关联对象{"+attributeID+"}存在多个,不能使用此方法!";
throw new JBOException(desc);
}*/
return l.get(0);
}
public BusinessObject getBusinessObjectByAttributes(String attributeID,Object... parameters) throws JBOException {
List l=this.getBusinessObjectsByAttributes(attributeID,parameters);
if(l.isEmpty()) return null;
if(l.size()>1){
String desc ="对象{"+this.getBizClassName()+"-"+this.getKeyString()+"}的关联对象{"+attributeID+"}存在多个,不能使用此方法!";
throw new JBOException(desc);
}
return l.get(0);
}
/**
* 取指定对象类型的1:1关联对象,当存在多个关联对象时报错
* @param attributeID
* @param filterParameters:过滤条件,格式parameter1=value1;parameter2=value2;.....
* @return
* @throws JBOException
*/
public BusinessObject getBusinessObjectBySql(String attributeID,String querySql,Object... parameters) throws JBOException {
List l=this.getBusinessObjectsBySql(attributeID,querySql,parameters);
if(l.isEmpty()) return null;
if(l.size()>1){
String desc ="对象{"+this.getBizClassName()+"-"+this.getKeyString()+"}的关联对象{"+attributeID+"}存在多个,不能使用此方法!";
throw new JBOException(desc);
}
return l.get(0);
}
public BusinessObject getBusinessObject(String attributeID,BizObjectKey bizObjectKey) throws JBOException {
List l = this.getBusinessObjects(attributeID);
for(BusinessObject o:l){
if(bizObjectKey.equals(o.getKey())){
return o;
}
}
return null;
}
/**
* 取指定数组属性
* @param objectType
* @return
* @throws JBOException
* @throws Exception
*/
public List getBusinessObjects(String attributeID) throws JBOException{
List l=new ArrayList();
Object object = this.getObject(attributeID);
if(object==null) return l;
if(object instanceof List){
@SuppressWarnings("unchecked")
List