java entity to map_Java ModelEntity.convertFieldMapInPlace方法代碼示例

import org.ofbiz.entity.model.ModelEntity; //導入方法依賴的package包/類

public static GenericValue runFind(ModelEntity modelEntity, Map context, Delegator delegator, boolean useCache, boolean autoFieldMap,

Map, Object> fieldMap, List selectFieldExpanderList) throws GeneralException {

// assemble the field map

Map entityContext = new HashMap();

if (autoFieldMap) {

// try a map called "parameters", try it first so values from here are overridden by values in the main context

Object parametersObj = context.get("parameters");

Boolean parametersObjExists = parametersObj != null && parametersObj instanceof Map, ?>;

// only need PK fields

Iterator iter = modelEntity.getPksIterator();

while (iter.hasNext()) {

ModelField curField = iter.next();

String fieldName = curField.getName();

Object fieldValue = null;

if (parametersObjExists) {

fieldValue = ((Map, ?>) parametersObj).get(fieldName);

}

if (context.containsKey(fieldName)) {

fieldValue = context.get(fieldName);

}

entityContext.put(fieldName, fieldValue);

}

}

EntityFinderUtil.expandFieldMapToContext(fieldMap, context, entityContext);

//Debug.logInfo("PrimaryKeyFinder: entityContext=" + entityContext, module);

// then convert the types...

// need the timeZone and locale for conversion, so add here and remove after

entityContext.put("locale", context.get("locale"));

entityContext.put("timeZone", context.get("timeZone"));

modelEntity.convertFieldMapInPlace(entityContext, delegator);

entityContext.remove("locale");

entityContext.remove("timeZone");

// get the list of fieldsToSelect from selectFieldExpanderList

Set fieldsToSelect = EntityFinderUtil.makeFieldsToSelect(selectFieldExpanderList, context);

//if fieldsToSelect != null and useCacheBool is true, throw an error

if (fieldsToSelect != null && useCache) {

throw new IllegalArgumentException("Error in entity-one definition, cannot specify select-field elements when use-cache is set to true");

}

try {

GenericValue valueOut = null;

GenericPK entityPK = delegator.makePK(modelEntity.getEntityName(), entityContext);

// make sure we have a full primary key, if any field is null then just log a warning and return null instead of blowing up

if (entityPK.containsPrimaryKey(true)) {

if (useCache) {

valueOut = EntityQuery.use(delegator).from(entityPK.getEntityName()).where(entityPK).cache(true).queryOne();

} else {

if (fieldsToSelect != null) {

valueOut = delegator.findByPrimaryKeyPartial(entityPK, fieldsToSelect);

} else {

valueOut = EntityQuery.use(delegator).from(entityPK.getEntityName()).where(entityPK).cache(false).queryOne();

}

}

} else {

if (Debug.infoOn()) Debug.logInfo("Returning null because found incomplete primary key in find: " + entityPK, module);

}

return valueOut;

} catch (GenericEntityException e) {

String errMsg = "Error finding entity value by primary key with entity-one: " + e.toString();

Debug.logError(e, errMsg, module);

throw new GeneralException(errMsg, e);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值