Juan Hernandez has uploaded a new change for review.

Change subject: restapi: Recursively scan models when adding links
......................................................................

restapi: Recursively scan models when adding links

One of the steps to generate the output document from a model is to add
links to the referenced resources. We do this using reflection to
enumerate the properties of the model, checking which are resources and
adding "href" attributes computed from the identifiers. This works fine
but there are some situations where the referencs to the resources are
not direct children of the model, but grandchildren or potentially
deeper. For example, the work to add template versions will include a
model whose representation will that looks like this:

  <template id="...">
    ...
    <version>
      <base_resource id="..." href=".."/>
      <version_number>3</version_number>
      <version_name>three</version_number>
    </version>
  </template>

In this case the "base_resource" element is a reference to a resource
that isn't a direct child of the template resource, so the method we use
to create the links doesn't add the "href" attribute. This patch changes
the LinkHelper class so that it will recursively scan the model and add
the links in all the levels.

Change-Id: Ibff32c7a2ec65ac2f95bb1dd64361b3c98ef049b
Signed-off-by: Juan Hernandez <[email protected]>
---
M 
backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/LinkHelper.java
1 file changed, 18 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/54/23254/1

diff --git 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/LinkHelper.java
 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/LinkHelper.java
index 662626a..bca9d01 100644
--- 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/LinkHelper.java
+++ 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/LinkHelper.java
@@ -466,15 +466,25 @@
         ArrayList<BaseResource> ret = new ArrayList<BaseResource>();
 
         for (Method method : obj.getClass().getMethods()) {
-            if (method.getName().startsWith("get") &&
-                BaseResource.class.isAssignableFrom(method.getReturnType())) {
-                try {
-                    BaseResource inline = (BaseResource)method.invoke(obj);
-                    if (inline != null) {
-                        ret.add(inline);
+            if (method.getName().startsWith("get")) {
+                // We need to recursively scan everything that is in the model 
package, as there may be references
+                // to resources deeply nested:
+                if (method.getReturnType().getPackage() == 
BaseResource.class.getPackage()) {
+                    Object inline = null;
+                    try {
+                        inline = method.invoke(obj);
                     }
-                } catch (Exception e) {
-                    // invocation target exception should not occur on simple 
getter
+                    catch (Exception e) {
+                        // invocation target exception should not occur on 
simple getter
+                    }
+                    if (inline != null) {
+                        if (inline instanceof BaseResource) {
+                            ret.add((BaseResource) inline);
+                        }
+                        else {
+                            ret.addAll(getInlineResources(inline));
+                        }
+                    }
                 }
             }
         }


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

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

Reply via email to