Yes, I wondered about that.
In my case the following simplified code would work, because it's used before
running the service.
I guess it's how it should be used (why running isValid() before running?). So
I guess we can simply use that
public boolean isValid(Map<String, Object> context, String mode, Locale
locale) {
try {
validate(context, mode, locale);
} catch (ServiceValidationException e) {
return false;
}
return true;
}
Other opinions?
Jacques
Le 28/12/2016 à 21:48, Scott Gray a écrit :
I guess it depends on how this will be used but I think there is a big
difference between " // do not validate results with errors" and returning
false from an isValid method. IMO an error output response is perfectly
valid.
Regards
Scott
On 28 December 2016 at 21:47, <[email protected]> wrote:
Author: jleroux
Date: Wed Dec 28 08:47:25 2016
New Revision: 1776243
URL: http://svn.apache.org/viewvc?rev=1776243&view=rev
Log:
Implemented: Add a isValid() method to the ModelService class
(OFBIZ-9158)
The idea is to use validate() to render a boolean result. I needed that in
a
custom project, I think it's worth contributing.
Modified:
ofbiz/trunk/framework/service/src/main/java/org/apache/
ofbiz/service/ModelService.java
Modified: ofbiz/trunk/framework/service/src/main/java/org/apache/
ofbiz/service/ModelService.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/
src/main/java/org/apache/ofbiz/service/ModelService.
java?rev=1776243&r1=1776242&r2=1776243&view=diff
============================================================
==================
--- ofbiz/trunk/framework/service/src/main/java/org/apache/
ofbiz/service/ModelService.java (original)
+++ ofbiz/trunk/framework/service/src/main/java/org/apache/
ofbiz/service/ModelService.java Wed Dec 28 08:47:25 2016
@@ -600,6 +600,32 @@ public class ModelService extends Abstra
}
/**
+ * Validates a Map against the IN or OUT parameter information
+ * Same than validate() with same signature but returns a boolean
instead of exceptions
+ * @param context the context
+ * @param mode Test either mode IN or mode OUT
+ * @param locale the actual locale to use
+ */
+ public boolean isValid(Map<String, Object> context, String mode,
Locale locale) {
+ boolean verboseOn = Debug.verboseOn();
+ if (verboseOn) Debug.logVerbose("[ModelService.validate] : {" +
this.name + "} : Validating context - " + context, module);
+
+ // do not validate results with errors
+ if (mode.equals(OUT_PARAM) && context != null &&
context.containsKey(RESPONSE_MESSAGE)) {
+ if (RESPOND_ERROR.equals(context.get(RESPONSE_MESSAGE)) ||
RESPOND_FAIL.equals(context.get(RESPONSE_MESSAGE))) {
+ if (verboseOn) Debug.logVerbose("[ModelService.validate]
: {" + this.name + "} : response was an error, not validating.", module);
+ return false;
+ }
+ }
+ try {
+ validate(context, mode, locale);
+ } catch (ServiceValidationException e) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
* Validates a map of name, object types to a map of name, objects
* @param info The map of name, object types
* @param test The map to test its value types.