Federico Simoncelli has uploaded a new change for review.

Change subject: restapi: cast list results to single entity
......................................................................

restapi: cast list results to single entity

In change 88756e3 (core: Remove usage of dynamic queries from the
Backend) the dynamic queries were removed from the restapi Backend.
The change didn't take in account that some of the queries were
used to retrieve only one item and therefore doGetEntity should
handle the case (as the old deprecated getEntity method).

An example of the problem was adding a cluster:

 # add cluster --data_center-name DataCenter1 --name Cluster1

failing with a java.lang.ClassCastException (Cannot cast ArrayList
to StoragePool).

Change-Id: I7e96a62cd7a5429a5102c7a9134bee8754b3d128
Signed-off-by: Federico Simoncelli <[email protected]>
---
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendResource.java
1 file changed, 19 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/59/28359/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 0adc8b2..665bd20 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
@@ -53,6 +53,23 @@
     public static final String JOB_ID_CONSTRAINT = "JobId";
     public static final String STEP_ID_CONSTRAINT = "StepId";
 
+    private <T> T castQueryResultToEntity(Class<T> clz, VdcQueryReturnValue 
result,
+                                          String constraint) throws 
BackendFailureException {
+        T entity;
+
+        if (List.class.isAssignableFrom(clz) && result.getReturnValue() 
instanceof List) {
+            entity = clz.cast(result.getReturnValue());
+        } else {
+            List<T> list = asCollection(clz, result.getReturnValue());
+            if (list == null || list.isEmpty()) {
+                throw new EntityNotFoundException(constraint);
+            }
+            entity = clz.cast(list.get(0));
+        }
+
+        return entity;
+    }
+
     @Deprecated
     protected <T> T getEntity(Class<T> clz, SearchType searchType, String 
constraint) {
         try {
@@ -61,18 +78,7 @@
             if (!result.getSucceeded()) {
                 backendFailure(result.getExceptionString());
             }
-
-            T entity;
-            if (List.class.isAssignableFrom(clz) && result.getReturnValue() 
instanceof List) {
-                entity = clz.cast(result.getReturnValue());
-            } else {
-                List<T> list = asCollection(clz, result.getReturnValue());
-                if (list == null || list.isEmpty()) {
-                    throw new EntityNotFoundException(constraint);
-                }
-                entity = clz.cast(list.get(0));
-            }
-            return entity;
+            return castQueryResultToEntity(clz, result, constraint);
         } catch (Exception e) {
             return handleError(clz, e, false);
         }
@@ -111,7 +117,7 @@
                 throw new EntityNotFoundException(identifier);
             }
         }
-        return clz.cast(result.getReturnValue());
+        return castQueryResultToEntity(clz, result, identifier);
     }
 
     protected <T> List<T> getBackendCollection(Class<T> clz, VdcQueryType 
query, VdcQueryParametersBase queryParams) {


-- 
To view, visit http://gerrit.ovirt.org/28359
To unsubscribe, visit http://gerrit.ovirt.org/settings

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

Reply via email to