在客户现场进行技术验证的时候,客户需要将预定义主从实体的数据复制到正式主从实体中,并进行持久化将正式数据保存到正式表中。考虑到EOS开发平台中,可以借助 Hibernate 关联操作(插入、更新)数据集中主从表数据,只需要解决如何将预定义实体的数据转移到正式实体中。实体关系如下图所示:
在EOS中的 com.eos.foundation 构件库中存在com.eos.foundation.data.DataObjectUtil.convertDataObject 方法,可以将单层的数据进行转换。通过分析,数据集的定义不能确定如何关联从表的引用,所以需要明确定义从表的引用名称需要相同(如上图红色方框)。另外,在数据集定义的时候,未明确定义 1:1 关系的引用,所以在这里明确修改 1:1 引用名为 targetHead。
下面附上代码的简单实现:
package com.primeton.eos.foundationX;import java.util.List;import com.eos.foundation.ExceptionConstant;
import com.eos.foundation.common.utils.StringUtil;
import com.eos.foundation.data.DataObjectUtil;
import com.eos.system.annotation.Bizlet;
import com.eos.system.exception.EOSRuntimeException;
import com.primeton.data.sdo.impl.TypeReference;
import com.primeton.ext.data.sdo.DataUtil;import commonj.sdo.DataObject;
import commonj.sdo.Property;/*** @author Flying Rat* @date 2015-01-13 10:59:42*/
@Bizlet("数据对象操作扩展")
public class DataObjectUtilX {@Bizlet("为数据对象指定属性填充数据")public static DataObject setProperty(DataObject dataObject, String key, String value) {if (dataObject == null) {return null;}if (StringUtil.isBlank(key)) {throw new EOSRuntimeException(ExceptionConstant.PARAM_IS_BLANK, new String[] { "key:" + key });}dataObject.set(key, value);return dataObject;}@Bizlet("为数据对象指定属性填充数据,深层填充。")public static DataObject setProperty(DataObject dataObject, String key, Object value, boolean recursion) {if (dataObject == null) {return null;}if (StringUtil.isBlank(key)) {throw new EOSRuntimeException(ExceptionConstant.PARAM_IS_BLANK, new String[] { "key:" + key });}dataObject.set(key, value);@SuppressWarnings("unchecked")List list = dataObject.getInstanceProperties();for (Property prop : list) {if (dataObject.isSet(prop)) {// 如果该属性可配置if (prop.getName().equals(key)) {if (prop.getType().isDataType()) {dataObject.set(key, DataUtil.toSDOValue(prop.getType(), value));} else {dataObject.set(key, value);}} else if (recursion && !prop.getType().isDataType()) {@SuppressWarnings("unchecked")List subObject = (List) dataObject.get(prop);for (DataObject o : subObject) {DataObjectUtilX.setProperty(o, key, value, recursion);}}}}return dataObject;}@Bizlet("将DataObject转化为目标实体类型的DataObject,深层转化。")public static DataObject convertDataObject(DataObject srcDataObject) {if (srcDataObject == null) {return null;}String targetEntityName = null;@SuppressWarnings("unchecked")List list = srcDataObject.getInstanceProperties();for (Property srcProp : list) {if (srcDataObject.isSet(srcProp)) {// 如果该属性可配置String propertyName = srcProp.getName();// 获取该属性名if ("targetHead".equals(propertyName)) {targetEntityName = ((TypeReference) srcProp.getType()).getEntityName();}}}if (StringUtil.isBlank(targetEntityName)) {throw new EOSRuntimeException(ExceptionConstant.PARAM_IS_BLANK, new String[] { "targetEntityName" });}DataObject targetDataObject = DataObjectUtil.createDataObject(targetEntityName);for (Property srcProp : list) {if (srcDataObject.isSet(srcProp)) {// 如果该属性可配置Object value = srcDataObject.get(srcProp);// 获取该属性值String propertyName = srcProp.getName();// 获取该属性名if (srcProp.getType().isDataType()) {Property targetProp = targetDataObject.getInstanceProperty(propertyName);// 实例化目标对象的该属性if (null != targetProp) {// 如果目标对象存在该属性if (targetProp.getType().isDataType()) {targetDataObject.set(propertyName, DataUtil.toSDOValue(targetProp.getType(), value));} else {targetDataObject.set(propertyName, value);}} else if (targetDataObject.getType().isOpen()) {targetDataObject.set(propertyName, value);}} else {String propName = srcProp.getName();Property targetProp = targetDataObject.getInstanceProperty(propName);if (null != targetProp) {// 如果目标对象存在该属性if (srcProp.isMany()) {DataObject[] d = DataObjectUtil.convertDataObjects(value, ((TypeReference) targetProp.getType()).getEntityName(), true);targetDataObject.set(propName, d);} else {DataObject d = DataObjectUtil.convertDataObject((DataObject) value, ((TypeReference) targetProp.getType()).getEntityName(), true);targetDataObject.set(propName, d);}}}}}return targetDataObject;}@Bizlet("将DataObject转化为目标实体类型的DataObject,深层转化。")public static DataObject convertDataObject(DataObject srcDataObject, String targetEntityName) {if (srcDataObject == null) {return null;}if (StringUtil.isBlank(targetEntityName)) {throw new EOSRuntimeException(ExceptionConstant.PARAM_IS_BLANK, new String[] { "targetEntityName" });}DataObject targetDataObject = DataObjectUtil.createDataObject(targetEntityName);@SuppressWarnings("unchecked")List list = srcDataObject.getInstanceProperties();for (Property srcProp : list) {if (srcDataObject.isSet(srcProp)) {// 如果该属性可配置Object value = srcDataObject.get(srcProp);// 获取该属性值String propertyName = srcProp.getName();// 获取该属性名if (srcProp.getType().isDataType()) {Property targetProp = targetDataObject.getInstanceProperty(propertyName);// 实例化目标对象的该属性if (null != targetProp) {// 如果目标对象存在该属性if (targetProp.getType().isDataType()) {targetDataObject.set(propertyName, DataUtil .toSDOValue(targetProp.getType(), value));} else {targetDataObject.set(propertyName, value);}} else if (targetDataObject.getType().isOpen()) {targetDataObject.set(propertyName, value);}} else {String propName = srcProp.getName();Property targetProp = targetDataObject.getInstanceProperty(propName);if (null != targetProp) {// 如果目标对象存在该属性if (srcProp.isMany()) {DataObject[] d = DataObjectUtil.convertDataObjects(value, ((TypeReference) targetProp.getType()).getEntityName(), true);targetDataObject.set(propName, d);} else {DataObject d = DataObjectUtil.convertDataObject((DataObject) value, ((TypeReference) targetProp.getType()).getEntityName(), true);targetDataObject.set(propName, d);}}}}}return targetDataObject;}
}
在上面的 DataObjectUtilX 工具类中,setProperty 方法是为 DataObject 对象赋值,convertDataObject 方法是 DataObject 数据复制到目标 DataObject 对象中。