Once we have the registry instance in place (embedded registry or remote registry), navigation is straightforward. It is the same as working with the Registry API. Say we have a resource called "/foo/c1"
in the Registry and you can access it using the registry instance as given below:
Resource resource = registry.get("/foo/c1");
The resource object will represent the actual resource object in the Registry. But if the resource is a collection, then the object will, of course, represent the collection.
As you can see in the code sample above, once we have a registry instance we do not need to pass a complete URL for all invocations. It is adequate to pass only a relative path to a resource.
To add a resource to the registry instance, first thing we need to do is to create a Resource object and then to call its put method:
First let's try to cerate a collection called "/c1/c2"
Collection collection = registry.newCollection();
registry.put("/c1/c2", collection);
If you call the get method then you'd be able to access that resource created. Now let's try to add a resource with content. For that, we need to first create a Resource object and then set content.
Resource r1 = registry.newResource();
String str = "My File Content";
r1.setContent(str.getBytes());
registry.put("/c1/c2/r1", r1);
We can use the following code to confirm whether the resource exists:
boolean value = registry.resourceExists("/c1/c2/r1");
If the resource does exist, a boolean value of true will be returned, else false.
Additionally, we can use the registry instance to delete resources. Deleting a resource is a matter of calling the delete method of the registry. Let's just say we want to delete "/c1/c2/r1". Then we can use the following code:
registry.delete("/c1/c2/r1");
We can rename individual resources or collections. To rename a resource, use the following lines of code. Same code can be used to rename a collection as well.
registry.rename("/c1/c2/r1", "/c1/c2/r2");
Above line of code renames "/c1/c2/r1" to "/c1/c2/r2".
Resources are automatically versioned when they are added or updated. But collections are not versioned automatically due to performance considerations. However, you can create versions of collections using the API.
It is recommended that collection versions are created only for making checkpoints of the subtree you are working on.
registry.createVersion("/c1/c2");
We can list all of the versions of a given resource using the code given below. The result would be an array of String, containing links to the different versions of the resource.
String [] versions = registry.getVersions("/c1/c2");
Since the Registry comes with versioning, we can restore a resource to any of its versions. This can be done using the registry as well. In the previous section, we discussed how to retrieve versions for a give resource.
The following line of code demonstrates we can restore back an old version of the registry instance.
registry.restoreVersion ("/c1/c2;version=2");
We can perform tagging operations using the registry. To tag a resource, we need the resource path and the tagging words. Let's say we need to tag a resource named "/c1/c2/r2" as "rename resource". We can do this as:
registry.applyTag("/c1/c2/r2" , "rename resource");
We can use the registry to retrieve tags for a give resource. It will return an array of Tag type and we can iterate the array to see the content.
Tag[] tags = registry.getTags("/c1/c2/r2");
We can remove a tag using the tag name. Use the following code:
registry.removeTag("/c1/c2/r2","rename resource");
We can also comment on a resource using the registry. Here, we will create a comment object and call the registry instance. Following lines of code illustrates how we can achieve this:
Comment c1 = new Comment();
c1.setText("This is my comment");
String commentpath = registry.addComment("/c1/c2/r2", c1), c1);
The above lines of code will add a comment to the resource named: "c1/c2/r2 ".
We can also make changes to comments we have already made using the registry instance. Here, we need the path and new text for the comment we are adding. Say we want to change from "This is my comment" to "This is cool",
For that, we can do the following:
registry.editComment(commentpath,"This is cool");
In order to rate a resource based on our judgment, we can again use the registry instance. Rating a resource can be done using following line of code:
registry.rateResource("c1/c2/r2 " , 4);
In addition to the above operations, there are a number of others that we can perform using the registry instance. In this document we have discussed only the most commonly used operations.