public static void getCurrentValueWithCachedPK(HttpServletRequest request, GenericDelegator delegator, GenericPK cachedPK, String entityName) {
Map paramMap = UtilHttp.getParameterMap(request);
// Build the primary key that may have been passed in as key values
GenericValue v = delegator.makeValue(entityName);
GenericPK passedPK = v.getPrimaryKey();
Collection keyColl = passedPK.getAllKeys();
Iterator keyIt = keyColl.iterator();
while (keyIt.hasNext()) {
String attrName = (String)keyIt.next();
String attrVal = (String)request.getAttribute(attrName);
if (UtilValidate.isEmpty(attrVal)) {
attrVal = (String)paramMap.get(attrName);
}
if (UtilValidate.isNotEmpty(attrVal)) {
passedPK.put(attrName,attrVal);
}
}
// If a full passed primary key exists, it takes precedence over a cached key
// I cannot determine if the key testing utils of GenericEntity take into account
// whether or not a field is populated.
boolean useCached = false;
boolean usePassed = true;
if (cachedPK != null ) {
useCached = true;
keyColl = cachedPK.getPrimaryKey().getAllKeys();
keyIt = keyColl.iterator();
while (keyIt.hasNext()) {
String sCached = null;
String sPassed = null;
Object oPassed = null;
Object oCached = null;
String ky = (String)keyIt.next();
oPassed = passedPK.get(ky);
if (oPassed != null) {
sPassed = oPassed.toString();
if (UtilValidate.isEmpty(sPassed)) {
// If any part of passed key is not available, it can't be used
usePassed = false;
} else {
oCached = cachedPK.get(ky);
if (oCached != null) {
sCached = oCached.toString();
if (UtilValidate.isEmpty(sCached)) {
useCached = false;
} else {
}
} else {
useCached = false;
}
}
} else {
//useCached = false;
usePassed = false;
}
}
}
GenericPK currentPK = null;
if (usePassed && useCached) {
currentPK = passedPK;
} else if (usePassed && !useCached) {
currentPK = passedPK;
} else if (!usePassed && useCached) {
currentPK = cachedPK;
}
if (currentPK != null) {
request.setAttribute("currentPK", currentPK);
GenericValue currentValue = null;
try {
currentValue = delegator.findOne(currentPK.getEntityName(), currentPK, false);
} catch (GenericEntityException e) {
}
request.setAttribute("currentValue", currentValue);
}