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);
}
}