Hi everybody again,
This time I'm sending some sample code to retrieve ClusterNodes composed of
various fields from two different nodemanagers.
I was reading a ton of docs, including the "SearchQuery introduction"
document but it seems to be a bit outdated. Sniffing here and there, in the
document and through the apidocs, I came up with the following solution.
Could anybody take a quick look to it?
Does it follow the "best practices" for MMBase?
// Connection parameters for the remote cloud
CloudContext cloudContext =
ContextProvider.getCloudContext("rmi://127.0.0.1:1111/remotecontext");
HashMap credenciales = new HashMap();
credentials.put("username", "admin");
credentials.put("password", "admin2k");
// We connect to the cloud
Cloud cloud = cloudContext.getCloud("mmbase", "name/password",
credentials);
// Create the query to retrieve elements from the CMS repository
NodeQuery query = cloud.createNodeQuery();
// Define the object types involved in the query
NodeManager url = cloud.getNodeManager("url");
NodeManager tema = cloud.getNodeManager("tema");
// Create the steps for the query, normal steps (Step) for the usual
object types,
// special steps (RelationStep.getNext) to add related object types
through insrel.
Step source1 = query.addStep(url);
Step source2 = query.addRelationStep(tema).getNext();
// We add the desired fields from any of the sources
StepField campo_number = query.addField(source1,
url.getField("number"));
query.addField(source1, url.getField("nombre"));
query.addField(source1, url.getField("url"));
query.addField(source2, tema.getField("descripcion"));
// Set the order criteria
query.addSortOrder(campo_number, SortOrder.ORDER_DESCENDING);
// Define the max number of nodes to retrieve (never all of them since
it's a remote interface)
query.setMaxNumber(10);
// Execute the query against the CMS cloud
List listaElementos = cloud.getList(query);
Iterator iteradorElementos = listaElementos.iterator();
while(iteradorElementos.hasNext()) {
// For each element...
Node element = (Node) iteradorElementos.next();
System.out.println("(Node): " + element + "\n");
System.out.println("url: " + element.getStringValue("url.url"));
System.out.println("descripcion: " +
element.getStringValue("url.descripcion"));
System.out.println("nombre: " +
element.getStringValue("url.nombre"));
System.out.println("tema: " +
element.getStringValue("tema.descripcion"));
}
I started creating the NodeQuery this way: NodeQuery query =
cloud.getNodeManager("url").createNodeQuery();
But soon I saw the query was tied to a given NodeManager.
After that, I changed it to: NodeQuery query = cloud.createNodeQuery();
And added different fields from different sources. At first sight I thought
it couldn't work because I was not using ClusterNodes at all, so I printed
each 'element.getNodeManager()' to see which NodeManager was assigned to
that mix of fields from different sources.
What I got was: 'virtualnodes_1091717316612', a really clever answer!!
---
So, if I'm already getting multilevel info, should I use ClusterNodes?
Is this code the preferred method to do this kind of operation?
Thanks in advance,
Ignacio Renuncio.-