Yevgeny Zaspitsky has uploaded a new change for review.

Change subject: restapi: add getOptionalEntity method
......................................................................

restapi: add getOptionalEntity method

Provide a way to load an optional entity from the backend and
if that's missing an Exceptioni isn't being thrown. Upon a failure the
regular failure handling logic applies.

Change-Id: I2cebffc1676cf344ee8253f1677c8470489506d8
Signed-off-by: Yevgeny Zaspitsky <[email protected]>
---
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendResource.java
1 file changed, 31 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/26/38626/1

diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendResource.java
index 214f1c1..213519a 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendResource.java
@@ -102,8 +102,25 @@
                               VdcQueryParametersBase queryParams,
                               String identifier,
                               boolean notFoundAs404) {
+        return getEntity(clz, query, queryParams, identifier, notFoundAs404, 
true);
+    }
+
+    public <T> T getOptionalEntity(Class<T> clz,
+                              VdcQueryType query,
+                              VdcQueryParametersBase queryParams,
+                              String identifier,
+                              boolean notFoundAs404) {
+        return getEntity(clz, query, queryParams, identifier, notFoundAs404, 
false);
+    }
+
+    private <T> T getEntity(Class<T> clz,
+                              VdcQueryType query,
+                              VdcQueryParametersBase queryParams,
+                              String identifier,
+                              boolean notFoundAs404,
+                              boolean isMandatory) {
         try {
-            return doGetEntity(clz, query, queryParams, identifier);
+            return doGetEntity(clz, query, queryParams, identifier, 
isMandatory);
         } catch (Exception e) {
             return handleError(clz, e, notFoundAs404);
         }
@@ -113,13 +130,25 @@
                                 VdcQueryType query,
                                 VdcQueryParametersBase queryParams,
                                 String identifier) throws 
BackendFailureException {
+        return doGetEntity(clz, query, queryParams, identifier, true);
+    }
+
+    protected <T> T doGetEntity(Class<T> clz,
+                                VdcQueryType query,
+                                VdcQueryParametersBase queryParams,
+                                String identifier,
+                                boolean isMandatory) throws 
BackendFailureException {
         VdcQueryReturnValue result = runQuery(query, queryParams);
-        if (!result.getSucceeded() || result.getReturnValue() == null) {
+        if (!result.getSucceeded() || (isMandatory && result.getReturnValue() 
== null)) {
             if (result.getExceptionString() != null) {
                 backendFailure(result.getExceptionString());
             } else {
                 throw new EntityNotFoundException(identifier);
             }
+        } else {
+            if (result.getReturnValue() == null) {
+                return null;
+            }
         }
         return castQueryResultToEntity(clz, result, identifier);
     }


-- 
To view, visit https://gerrit.ovirt.org/38626
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2cebffc1676cf344ee8253f1677c8470489506d8
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Yevgeny Zaspitsky <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to