// get the primary key parameters...
String errMsgPk = "";
Iterator<ModelField> pksIter = entity.getPksIterator();
while (pksIter.hasNext()) {
String errMsg = "";
ModelField field = pksIter.next();
ModelFieldType type = null;
try {
type = delegator.getEntityFieldType(entity, field.getType());
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
Map<String, String> messageMap = UtilMisc.toMap("fieldType", field.getType());
errMsg += UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.fatal_error_param", messageMap, locale) + ".";
}
String fval = request.getParameter(field.getName());
if (fval != null && fval.length() > 0) {
try {
findByEntity.setString(field.getName(), fval);
} catch (Exception e) {
Map<String, String> messageMap = UtilMisc.toMap("fval", fval);
errMsg = errMsg + "<li>" + field.getColName() + UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.conversion_failed", messageMap, locale) + type.getJavaType() + ".";
Debug.logWarning("[updateGeneric] " + field.getColName() + " conversion failed: \"" + fval + "\" is not a valid " + type.getJavaType() + "; entityName: " + entityName, module);
}
}
}
if (errMsgPk.length() > 0) {
request.setAttribute("_ERROR_MESSAGE_", errMsgPk);
return "error";
}
// if this is a delete, do that before getting all of the non-pk parameters and validating them
if (updateMode.equals("DELETE")) {
// Remove associated/dependent entries from other tables here
// Delete actual main entity last, just in case database is set up to do a cascading delete, caches won't get cleared
try {
delegator.removeByPrimaryKey(findByEntity.getPrimaryKey());
} catch (GenericEntityException e) {
String errMsg = UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.delete_failed", locale) + ": " + e.toString();
Debug.logWarning(e, errMsg, module);
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
return "success";
}
// get the non-primary key parameters
String errMsgNonPk = "";
Iterator<ModelField> nopksIter = entity.getNopksIterator();
while (nopksIter.hasNext()) {
ModelField field = nopksIter.next();
ModelFieldType type = null;
try {
type = delegator.getEntityFieldType(entity, field.getType());
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
Map<String, String> messageMap = UtilMisc.toMap("fieldType", field.getType());
errMsgNonPk += UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.fatal_error_param", messageMap, locale) + ".";
}
String fval = request.getParameter(field.getName());
if (fval != null && fval.length() > 0) {
try {
findByEntity.setString(field.getName(), fval);
} catch (Exception e) {
Map<String, String> messageMap = UtilMisc.toMap("fval", fval);
errMsgNonPk += field.getColName() + UtilProperties.getMessage(GenericWebEvent.err_resource,
"genericWebEvent.conversion_failed", messageMap, locale) + type.getJavaType() + ".";
Debug.logWarning("[updateGeneric] " + field.getColName() + " conversion failed: \"" + fval + "\" is not a valid " + type.getJavaType() + "; entityName: " + entityName, module);
}
} else {
findByEntity.set(field.getName(), null);
}
}
if (errMsgNonPk.length() > 0) {
request.setAttribute("_ERROR_MESSAGE_", errMsgNonPk);
return "error";
}
// if the updateMode is CREATE, check to see if an entity with the specified primary key already exists
if (updateMode.equals("CREATE")) {
GenericValue tempEntity = null;
try {
tempEntity = delegator.findOne(findByEntity.getEntityName(), findByEntity.getPrimaryKey(), false);
} catch (GenericEntityException e) {
String errMsg = UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.create_failed_by_check", locale) + ": " + e.toString();
Debug.logWarning(e, errMsg, module);
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
if (tempEntity != null) {
Map<String, String> messageMap = UtilMisc.toMap("primaryKey", findByEntity.getPrimaryKey().toString());
String errMsg = entity.getEntityName() + UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.already_exists_pk", messageMap, locale)+ ".";
Debug.logWarning("[updateGeneric] " + entity.getEntityName() + " already exists with primary key: " + findByEntity.getPrimaryKey().toString() + "; please change.", module);
}
}
// Validate parameters...
String errMsgParam = "";
Iterator<ModelField> fieldIter = entity.getFieldsIterator();
while (fieldIter.hasNext()) {
ModelField field = fieldIter.next();
for (int j = 0; j < field.getValidatorsSize(); j++) {
String curValidate = field.getValidator(j);
Class[] paramTypes = new Class[] {String.class};
Object[] params = new Object[] {findByEntity.get(field.getName()).toString()};
String className = "org.ofbiz.base.util.UtilValidate";
String methodName = curValidate;
if (curValidate.indexOf('.') > 0) {
className = curValidate.substring(0, curValidate.lastIndexOf('.'));
methodName = curValidate.substring(curValidate.lastIndexOf('.') + 1);
}
Class<?> valClass;
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
valClass = loader.loadClass(className);
} catch (ClassNotFoundException cnfe) {
Debug.logError("[updateGeneric] Could not find validation class: " + className + "; ignoring.", module);
continue;
}
Method valMethod;
try {
valMethod = valClass.getMethod(methodName, paramTypes);
} catch (NoSuchMethodException cnfe) {
Debug.logError("[updateGeneric] Could not find validation method: " + methodName + " of class " + className + "; ignoring.", module);
continue;
}
Boolean resultBool;
try {
resultBool = (Boolean) valMethod.invoke(null, params);
} catch (Exception e) {
Debug.logError("[updateGeneric] Could not access validation method: " + methodName + " of class " + className + "; returning true.", module);
resultBool = Boolean.TRUE;
}
if (!resultBool.booleanValue()) {
Field msgField;
String message;
try {
msgField = valClass.getField(curValidate + "Msg");
message = (String) msgField.get(null);
} catch (Exception e) {
Debug.logError("[updateGeneric] Could not find validation message field: " + curValidate + "Msg of class " + className + "; returning generic validation failure message.", module);
message = UtilProperties.getMessage(GenericWebEvent.err_resource, "genericWebEvent.validation_failed", locale) + ".";
}
errMsgParam += field.getColName() + " " + curValidate + " " + UtilProperties.getMessage(GenericWebEvent.err_resource,
"genericWebEvent.failed", locale) + ": " + message;
Debug.logWarning("[updateGeneric] " + field.getColName() + " " + curValidate + " failed: " + message, module);
}
}
}
if (errMsgParam.length() > 0) {