Difference between revisions of "GCube Document Library"
(→Trying to get the document content I get this exception: java.net.MalformedURLException: unknown protocol: sms) |
|||
(54 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
= Design Overview = | = Design Overview = | ||
− | The | + | The gCube Document Library (gDL) is a library that works on top of the Content Manager. This library offers a simpler way for client to access documents in the d4science system. The major change in the ContentManager is the document model (more generic, more extensible), the gDL reduces the development costs for using this new feature. |
== Document Model == | == Document Model == | ||
− | [[Image:cmlModel.png| the <code> | + | [[Image:cmlModel.png| the <code>gDL Object Model</code>]] |
= Implementation Overview = | = Implementation Overview = | ||
− | The | + | The gDL is composed by tree types of access interfaces: |
+ | *the '''CollectionManager''' is used to manage the Collections in the system. | ||
*the '''CMReader''' is used to read document from a collection. it offers methods to get a document or part of it (Metadata, Annotations, Parts, Alternatives); | *the '''CMReader''' is used to read document from a collection. it offers methods to get a document or part of it (Metadata, Annotations, Parts, Alternatives); | ||
*the '''CMWriter''' is used to add document or update it or a part of it; | *the '''CMWriter''' is used to add document or update it or a part of it; | ||
Line 13: | Line 14: | ||
+ | = Accessing a collection = | ||
+ | The GCube Document Library offers a set of utilities to help the client to interact with the Content Manager. | ||
+ | ==Reading a collection== | ||
+ | The <code>CMReader</code> let the client to read elements from a collection. There are methods to read single document or part of it and methods to read multiple documents returned as GCubeDocument iterator. | ||
+ | Method to access GCubeDocument lets filter and project the result. Both operations are achieved through the Projection system. | ||
+ | ===Projection=== | ||
+ | The Projection lets filter and project the results of a "query". | ||
+ | |||
+ | There is a Projection class for each element of model. | ||
+ | |||
+ | For each element is possible to define which fields get back through the <code>includeXXX(boolean include)</code> method. | ||
+ | |||
+ | For each element is also possible to set a constraint through the <code>constrainXXX(Constraint constraint)</code> method, or remove it with <code>resetXXXConstraint()</code> one. | ||
+ | |||
+ | The <code>ProjectionsFactory</code> class provide a set of utility method for fast retrieving element dedicated projection. | ||
+ | ==Writing into a collection== | ||
+ | The <code>CMWriter</code> class offers method to write, update and delete document from a collection. | ||
+ | |||
+ | The write methods accept new document and returns the new document id. | ||
+ | |||
+ | The update method accept a preceding retrieved document on which updates are made. | ||
+ | |||
+ | Finally the document remotion requires only the document id. | ||
+ | |||
+ | Each method have both a single element operation than a multiple element operation using an iterator as input and as output. | ||
+ | |||
+ | ==Managing collections and views== | ||
+ | The GCube Document Library offers utility classes to manage the collections and the views in the system. | ||
+ | |||
+ | The <code>CollectionManager</code> class is an utility to manage a single collection. It can return the collection Reader and Writer for respectively read and write the collection. It can also manage the collection Views with facilities method to retrieve and manage it. | ||
+ | |||
+ | The <code>Collections</code> class is an utility to manage the CM collections. The class provide methods for find, delete and create collections. | ||
+ | |||
+ | The <code>CMViews</code> class is an utility for find the Views on the system. | ||
+ | |||
+ | =Configuring the environment= | ||
+ | ==Supporting the SMS protocol== | ||
+ | The Storage Management Service introduces a new protocol, called ''sms'', with an handler implementation. | ||
+ | |||
+ | To let know the JVM about the existence of a new protocol handler you can use one of this solutions: | ||
+ | * Specify a new java property at JVM startup: ''-Djava.protocol.handler.pkgs=org.gcube.contentmanagement.storagelayer.storagemanagementservice.stubs.protocol'' | ||
+ | ** if you are developing a service and are you using the last distribution you don't have to change anything because the new GHN distribution already do it. | ||
+ | ** if you are developing a portlet or a portal library check if your portal instance have the option specified in the environment variable ''CATALINA_OPTS''. | ||
+ | ** if you are running your client locally on Eclipse you can specify the options on run configuration, Arguments tab, VM arguments section. | ||
+ | * Use this sentence in your client testing program: | ||
+ | <source lang="java5"> | ||
+ | System.setProperty("java.protocol.handler.pkgs", "org.gcube.contentmanagement.storagelayer.storagemanagementservice.stubs.protocol"); | ||
+ | </source> | ||
+ | |||
+ | = Examples = | ||
+ | Here some examples on gDL use. More examples are on ''examples'' folder in gDL project on SVN. | ||
+ | |||
+ | ==Managing Collections examples== | ||
+ | We shows some examples on managing Collections. | ||
+ | === List all collections === | ||
+ | This example shows how to list all collection on a scope. | ||
+ | <source lang="java5"> | ||
+ | GCUBEScope scope = GCUBEScope.getScope("/gcube"); | ||
+ | |||
+ | System.out.println("Collections in "+scope+" :"); | ||
+ | for (CMCollection collection:Collections.list(scope)){ | ||
+ | System.out.println(collection); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | === Find collection by name === | ||
+ | This example shows how to find a collection using his name. | ||
+ | <source lang="java5"> | ||
+ | GCUBEScope scope = GCUBEScope.getScope("/gcube"); | ||
+ | |||
+ | String collectionName = "My Test Collection"; | ||
+ | |||
+ | System.out.println("Collections in "+scope+" with name \""+collectionName+"\":"); | ||
+ | for (CMCollection collection:Collections.findByName(scope, collectionName)){ | ||
+ | System.out.println(collection); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | === Create a GCube collection === | ||
+ | This code creates a new collection with the given name and description. | ||
+ | |||
+ | The create method returns a list of collection references because a create command can produce more than one collection (for example OAI-Plugin). | ||
+ | |||
+ | <source lang="java5"> | ||
+ | GCUBEScope scope = GCUBEScope.getScope("/gcube"); | ||
+ | |||
+ | //collection informations | ||
+ | String collectionName = "MyTestCollection2"; | ||
+ | String collectionDescription = "This is my test collection"; | ||
+ | boolean userCollection = true; | ||
+ | |||
+ | //we don't want to propagate the request to others CM | ||
+ | boolean propagateRequest = false; | ||
+ | |||
+ | //we want the collection to be readable and writable | ||
+ | boolean readable = true; | ||
+ | boolean writable = true; | ||
+ | |||
+ | //finally we create the collection | ||
+ | List<CollectionReference> collectionReferences = Collections.createGCubeCollection(propagateRequest, collectionName, collectionDescription, userCollection, readable, writable, scope); | ||
+ | |||
+ | System.out.println("Created collections:"); | ||
+ | for (CollectionReference collectionReference:collectionReferences){ | ||
+ | System.out.println("Collection id: "+collectionReference.getCollectionID()); | ||
+ | if (collectionReference.getReader()!=null) System.out.println("Reader EPR: "+collectionReference.getReader()); | ||
+ | if (collectionReference.getWriter()!=null) System.out.println("Writer EPR: "+collectionReference.getWriter()); | ||
+ | System.out.println(); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | === Wrap a collection using SMS-Plugin === | ||
+ | This example shows how to wrap an existing collection on SMS. | ||
+ | To run this example the SMS-Plugin jar is required. | ||
+ | <source lang="java5"> | ||
+ | GCUBEScope scope = GCUBEScope.getScope("/gcube"); | ||
+ | |||
+ | //the collection id on SMS (the id of collection IO on the SMS) | ||
+ | String collectionId = "74fe4120-8f5b-11df-8083-c574cd534edf"; | ||
+ | |||
+ | //we don't want to propagate the request to others CM | ||
+ | boolean propagateRequest = false; | ||
+ | |||
+ | //we want the collection to be readable and writable | ||
+ | boolean readable = true; | ||
+ | boolean writable = true; | ||
+ | |||
+ | //finally we wrap the collection | ||
+ | List<CollectionReference> collectionReferences = SMSUtil.wrapCollection(scope, propagateRequest, collectionId, readable, writable); | ||
+ | |||
+ | System.out.println("Collections wrapped:"); | ||
+ | for (CollectionReference collectionReference:collectionReferences){ | ||
+ | System.out.println("Collection id: "+collectionReference.getCollectionID()); | ||
+ | if (collectionReference.getReader()!=null) System.out.println("Reader EPR: "+collectionReference.getReader()); | ||
+ | if (collectionReference.getWriter()!=null) System.out.println("Writer EPR: "+collectionReference.getWriter()); | ||
+ | System.out.println(); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | === Delete a GCube collection === | ||
+ | This code deletes a GCube collection with the given id. | ||
+ | |||
+ | <source lang="java5"> | ||
+ | GCUBEScope scope = GCUBEScope.getScope("/gcube/devNext"); | ||
+ | |||
+ | String collectionId = "8fb9df30-2de8-11df-893d-add683599cad"; | ||
+ | |||
+ | //we want to delete the collection profile | ||
+ | boolean deleteProfile = true; | ||
+ | |||
+ | System.out.println("Requesting delete:"); | ||
+ | List<Future<RPDocument>> results = Collections.deleteGCubeCollection(collectionId, deleteProfile, scope); | ||
+ | |||
+ | System.out.println("Results:"); | ||
+ | for (Future<RPDocument> result:results){ | ||
+ | RPDocument doc = result.get(); | ||
+ | System.out.println("Deleted delegate for collection "+doc.getKey()); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ==Reading examples== | ||
+ | We shows some examples on reading documents. | ||
+ | |||
+ | === Get a single document === | ||
+ | This example shows how to get a single document using his id. | ||
+ | |||
+ | <source lang="java5"> | ||
+ | //we instantiate the CMReader | ||
+ | CMReader cmReader = new CMReader(collectionId, scope); | ||
+ | |||
+ | //then we retrieve the document, we are not specifying projections, therefore the entire document is returned. | ||
+ | GCubeDocument document = cmReader.getDocument(documentId); | ||
+ | |||
+ | System.out.println("Retrieved document:"); | ||
+ | System.out.println("id: " + document.getId()); | ||
+ | System.out.println("name: " + document.getName()); | ||
+ | System.out.println("mimeType: " + document.getMimeType()); | ||
+ | </source> | ||
+ | |||
+ | |||
+ | === Get documents by name === | ||
+ | This example shows how to get documents from a collection using a name constraint. | ||
+ | |||
+ | <source lang="java5"> | ||
+ | String collectionId = "74fe4120-8f5b-11df-8083-c574cd534edf"; | ||
+ | System.out.println("collectionId: "+collectionId); | ||
+ | System.out.println(); | ||
+ | |||
+ | String documentName = "Column dedication by Eumachos and Amias"; | ||
+ | |||
+ | //we instantiate the CMReader | ||
+ | CMReader cmReader = new CMReader(collectionId, scope); | ||
+ | |||
+ | //we define a gcube main document projection | ||
+ | GCubeMainDocumentProjection projection = ProjectionsFactory.MAIN_DOCUMENT(); | ||
+ | //we set a constraint over the name field | ||
+ | projection.constrainName(is(documentName)); | ||
+ | |||
+ | //we are asking for all documents having the specified name | ||
+ | Iterator<GCubeDocument> documentIterator = cmReader.getDocuments(projection); | ||
+ | |||
+ | int i = 0; | ||
+ | System.out.println("Retrieved documents:"); | ||
+ | |||
+ | //finally we iterate over the collection | ||
+ | while(documentIterator.hasNext()){ | ||
+ | GCubeDocument document = documentIterator.next(); | ||
+ | System.out.println(i+") "+document); | ||
+ | i++; | ||
+ | } | ||
+ | |||
+ | System.out.println(); | ||
+ | System.out.println("Found "+i+" documents"); | ||
+ | </source> | ||
+ | |||
+ | To use the <code>is</code> function remember to add this import: | ||
+ | <source lang="java5"> | ||
+ | import static org.gcube.contentmanagement.contentmanager.stubs.model.constraints.Constraints.*; | ||
+ | </source> | ||
+ | |||
+ | === Get all documents in a collection === | ||
+ | This example shows how to get all documents from a collection. | ||
+ | |||
+ | <source lang="java5"> | ||
+ | //we instantiate the CMReader | ||
+ | CMReader cmReader = new CMReader(collectionId, scope); | ||
+ | |||
+ | //then we retrieve all collection documents | ||
+ | //we are not specifying projections, therefore the entire document is returned. | ||
+ | Iterator<GCubeDocument> documentIterator = cmReader.getDocuments(); | ||
+ | |||
+ | int i = 0; | ||
+ | System.out.println("Collection documents:"); | ||
+ | |||
+ | //finally we iterate over the collection | ||
+ | while(documentIterator.hasNext()){ | ||
+ | GCubeDocument document = documentIterator.next(); | ||
+ | |||
+ | String id = document.getId(); | ||
+ | int parts = document.getParts().size(); | ||
+ | int alternatives = document.getAlternatives().size(); | ||
+ | int metadata = document.getMetadata().size(); | ||
+ | int annotations = document.getAnnotations().size(); | ||
+ | |||
+ | System.out.printf("%d id: %s parts: %d alternatives: %d metadata: %d annotations: %d %n", i, id, parts, alternatives, metadata, annotations); | ||
+ | i++; | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | === Get a document using projections === | ||
+ | This example shows how to use projections during document retrieving. | ||
+ | <source lang="java5"> | ||
+ | //we instantiate the CMReader | ||
+ | CMReader cmReader = new CMReader(collectionId, scope); | ||
+ | |||
+ | //then we retrieve the document, | ||
+ | //we are specifying the MAIN_DOCUMENT and the METADATA projections, the document returned will have main document data and all related metadata | ||
+ | GCubeDocument document = cmReader.getDocument(documentId, ProjectionsFactory.MAIN_DOCUMENT(), ProjectionsFactory.METADATA()); | ||
+ | |||
+ | System.out.println("Retrieved document:"); | ||
+ | System.out.println("id: " + document.getId()); | ||
+ | System.out.println("name: " + document.getName()); | ||
+ | System.out.println("mimeType: " + document.getMimeType()); | ||
+ | System.out.println("length: " + document.getLength()); | ||
+ | System.out.println("contentURL: " + document.getContentURL()); | ||
+ | System.out.println(); | ||
+ | |||
+ | for (GCubeMetadata metadata:document.getMetadata()){ | ||
+ | System.out.println("Metadata:"); | ||
+ | System.out.println(" id: " + metadata.getId()); | ||
+ | System.out.println(" mimeType: " + metadata.getMimeType()); | ||
+ | System.out.println(" length: " + metadata.getLength()); | ||
+ | System.out.println(" Metadata info:"); | ||
+ | System.out.println(" name: " + metadata.getMetadataName()); | ||
+ | System.out.println(" schema: " + metadata.getMetadataSchema()); | ||
+ | System.out.println(" language: " + metadata.getMetadataLanguage()); | ||
+ | System.out.println(); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ===Get a document content=== | ||
+ | This example shows how to get a document content. | ||
+ | <source lang="java5"> | ||
+ | //we instantiate the CMReader | ||
+ | CMReader cmReader = new CMReader(collectionId, scope); | ||
+ | |||
+ | //then we retrieve the document, we are requesting only the main document | ||
+ | GCubeDocument document = cmReader.getDocument(documentId, ProjectionsFactory.MAIN_DOCUMENT()); | ||
+ | |||
+ | System.out.println("Retrieved document:"); | ||
+ | System.out.println("id: " + document.getId()); | ||
+ | System.out.println("name: " + document.getName()); | ||
+ | System.out.println("contentURL: " + document.getContentURL()); | ||
+ | System.out.println(); | ||
+ | |||
+ | System.out.println("Retrieving content..."); | ||
+ | //the content is retrieved by the CML library through the content URL | ||
+ | InputStream is = document.getContent(); | ||
+ | |||
+ | //finally we copy the content to a tmp file | ||
+ | File tmpFile = File.createTempFile("mycontent", ".tmp"); | ||
+ | IOUtils.copy(is, new FileOutputStream(tmpFile)); | ||
+ | |||
+ | System.out.println("Content saved in tmp file: " + tmpFile.getAbsolutePath()); | ||
+ | </source> | ||
+ | |||
+ | ===Get Collection dynamic properties=== | ||
+ | This example shows how to get dynamic properties for a given collection. | ||
+ | <source lang="java5"> | ||
+ | String collectionId = "18611ed0-f162-11dd-96f7-b87cc0f0b075"; | ||
+ | System.out.println("collectionId: "+collectionId); | ||
+ | |||
+ | CollectionManager collectionManager = new CollectionManager(collectionId, scope); | ||
+ | System.out.println("Cardinality: "+collectionManager.getCardinality()); | ||
+ | SimpleDateFormat sdf = new SimpleDateFormat(); | ||
+ | System.out.println("Last Update: "+sdf.format(collectionManager.getLatUpdate().getTime())); | ||
+ | System.out.println(); | ||
+ | </source> | ||
+ | |||
+ | ==Writing examples== | ||
+ | We shows some examples on manipulating collections. | ||
+ | |||
+ | ===Add a new document to a collection=== | ||
+ | Add a new document to a collection. | ||
+ | <source lang="java5"> | ||
+ | //we instantiate the CMWriter | ||
+ | CMWriter cmWriter = new CMWriter(collectionId, scope); | ||
+ | |||
+ | //we create an instance of gcube document | ||
+ | GCubeDocument myNewDocument = new GCubeDocument("My Test Document", "application/octet-stream"); | ||
+ | |||
+ | //then we ask for adding the new document | ||
+ | String myNewDocumentId = cmWriter.add(myNewDocument); | ||
+ | System.out.println("document created, id: " + myNewDocumentId); | ||
+ | |||
+ | |||
+ | //finally we retrieve the created document | ||
+ | CMReader cmReader = new CMReader(collectionId, scope); | ||
+ | GCubeDocument document = cmReader.getDocument(myNewDocumentId); | ||
+ | |||
+ | System.out.println("Created document:"); | ||
+ | System.out.println("id: " + document.getId()); | ||
+ | System.out.println("name: " + document.getName()); | ||
+ | System.out.println("mimeType: " + document.getMimeType()); | ||
+ | System.out.println("length: " + document.getLength()); | ||
+ | System.out.println("contentURL: " + document.getContentURL()); | ||
+ | |||
+ | System.out.println("parts: " + document.getParts().size()); | ||
+ | System.out.println("alternatives:" + document.getAlternatives().size()); | ||
+ | System.out.println("metadata: " + document.getMetadata().size()); | ||
+ | System.out.println("annotations: " + document.getAnnotations().size()); | ||
+ | </source> | ||
+ | |||
+ | ===Add a new alternative to a document=== | ||
+ | Add a new alternative to a document. | ||
+ | <source lang="java5"> | ||
+ | //first we retrieve the document to update | ||
+ | CMReader cmReader = new CMReader(collectionId, scope); | ||
+ | GCubeMainDocumentProjection projection = ProjectionsFactory.MAIN_DOCUMENT(); | ||
+ | GCubeDocument document = cmReader.getDocument(documentId, projection); | ||
+ | |||
+ | System.out.println("Document to update:"); | ||
+ | System.out.printf("id: %s%n", document.getId()); | ||
+ | System.out.println(); | ||
+ | |||
+ | //we create the alternative to add | ||
+ | GCubeAlternative alternative = new GCubeAlternative("text/xml"); | ||
+ | |||
+ | //we add the alternative to the document | ||
+ | document.addAlternative(alternative); | ||
+ | |||
+ | //we instantiate the CMWriter | ||
+ | CMWriter cmWriter = new CMWriter(collectionId, scope); | ||
+ | |||
+ | //we ask for an update | ||
+ | cmWriter.update(document); | ||
+ | System.out.println("document updated"); | ||
+ | </source> | ||
+ | |||
+ | ===Remove a document from a collection=== | ||
+ | Remove a document from a collection. | ||
+ | <source lang="java5"> | ||
+ | //we instantiate the CMWriter | ||
+ | CMWriter cmWriter = new CMWriter(collectionId, scope); | ||
+ | |||
+ | writer.remove(documentId); | ||
+ | |||
+ | System.out.println("document removed."); | ||
+ | </source> | ||
+ | |||
+ | ==Views examples== | ||
+ | We shows some examples on handling views. | ||
+ | |||
+ | ===Create a Metadata View=== | ||
+ | Shows how to create a metadata view. | ||
+ | <source lang="java5"> | ||
+ | String collectionId = "1c583800-d5d8-11df-9cd6-b72b661eb83e"; | ||
+ | System.out.println("collectionId: "+collectionId); | ||
+ | System.out.println(); | ||
+ | |||
+ | MetadataView myView = new MetadataView(); | ||
+ | myView.setName("MyView"); | ||
+ | myView.setDescription("This is my test view"); | ||
+ | myView.setEditable(false); | ||
+ | myView.setIndexable(false); | ||
+ | myView.setUserCollection(false); | ||
+ | myView.setMetadaName("aquamaps"); | ||
+ | myView.setMetadataLanguage("en"); | ||
+ | myView.setMetadataSchema("http://aquamaps.xsd"); | ||
+ | |||
+ | CollectionManager manager = new CollectionManager(collectionId, scope); | ||
+ | manager.createView(myView); | ||
+ | |||
+ | System.out.println("View created: "+myView); | ||
+ | |||
+ | MetadataViewReader reader = new MetadataViewReader(myView, scope); | ||
+ | |||
+ | System.out.println("Metadata:"); | ||
+ | Iterator<GCubeMetadata> iterator = reader.getAll(); | ||
+ | while(iterator.hasNext()) System.out.println(iterator.next()); | ||
+ | </source> | ||
+ | |||
+ | ===List collection views=== | ||
+ | List all the views for a certain collection. | ||
+ | <source lang="java5"> | ||
+ | //we instantiate a collection manager. | ||
+ | CollectionManager collectionManager = new CollectionManager(collectionId, scope); | ||
+ | |||
+ | //then we ask for all the available views | ||
+ | List<CMView<?>> views = collectionManager.findViews(); | ||
+ | |||
+ | System.out.println("found "+views.size()+" views:"); | ||
+ | for (CMView<?> view:views) System.out.println(view); | ||
+ | </source> | ||
+ | |||
+ | ===Get Metadata elements=== | ||
+ | Get a metadata view for a collection and the all the elements of the view. | ||
+ | <source lang="java5"> | ||
+ | //first we instantiate a collection manager | ||
+ | CollectionManager collectionManager = new CollectionManager(collectionId, scope); | ||
+ | |||
+ | //then we create a view kind, in this case a MetadataView without specifying any property | ||
+ | MetadataView myMedataView = new MetadataView(); | ||
+ | |||
+ | //finally we ask to the collection manager to list all views of the kind specified. | ||
+ | List<MetadataView> views = collectionManager.findViews(myMedataView); | ||
+ | System.out.println("Found views: "+views.size()); | ||
+ | |||
+ | //in case we not found views | ||
+ | if (views.size()==0) throw new Exception("No views found"); | ||
+ | |||
+ | //we get the first of the list | ||
+ | MetadataView view = views.get(0); | ||
+ | |||
+ | //we instantiate the view reader | ||
+ | MetadataViewReader metadataReader = new MetadataViewReader(view, scope); | ||
+ | |||
+ | //we request all the elements | ||
+ | Iterator<GCubeMetadata> metadata = metadataReader.getAll(); | ||
+ | |||
+ | //and print it | ||
+ | while (metadata.hasNext()) System.out.println(" >"+metadata.next()); | ||
+ | </source> | ||
+ | |||
+ | ===Get Metadata elements using only the metadata view ID=== | ||
+ | Get a metadata view for a collection and all the elements of the view without knowing the collection ID. | ||
+ | <source lang="java5"> | ||
+ | //Use the metadata view ID to find the view | ||
+ | List<MetadataView> views = CMViews.findViews(scope, new MetadataView(metadataViewID)); | ||
+ | if (views.isEmpty()){ | ||
+ | System.out.println("Metadata view not found. View ID: " + metadataViewID); | ||
+ | } | ||
+ | |||
+ | //Just get the first view | ||
+ | MetadataView myMedataView = views.get(0); | ||
+ | MetadataViewReader metadataReader = new MetadataViewReader(myMedataView, scope); | ||
+ | </source> | ||
+ | |||
+ | ===Get Metadata elements specifying a condition=== | ||
+ | Get all the elements of the view specifying a condition. | ||
+ | <source lang="java5"> | ||
+ | MetadataViewReader metadataReader = new MetadataViewReader(view, scope); | ||
+ | |||
+ | GCubeMetadataProjection projection = (GCubeMetadataProjection) view.getProjection(); | ||
+ | |||
+ | //2/3/09 5:06 PM | ||
+ | Calendar c1 = Calendar.getInstance(); | ||
+ | c1.set(2009, 1, 3, 17,6,0); | ||
+ | |||
+ | //2/3/09 5:07 PM | ||
+ | Calendar c2 = Calendar.getInstance(); | ||
+ | c2.set(2009, 1, 3, 17,7,0); | ||
+ | projection.constrainCreationTime(all(after(c1),before(c2))); | ||
+ | |||
+ | //we request all the elements | ||
+ | Iterator<GCubeMetadata> metadata = metadataReader.getAll(projection); | ||
+ | |||
+ | //and print it | ||
+ | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); | ||
+ | System.out.println("Metadata elements created from "+sdf.format(c1.getTime())+" to "+sdf.format(c2.getTime())+":"); | ||
+ | while (metadata.hasNext()) { | ||
+ | GCubeMetadata m = metadata.next(); | ||
+ | |||
+ | System.out.println(" >"+m); | ||
+ | System.out.println(); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ==Managing URI== | ||
+ | The CML library offers an helper class ''URIS'' which expose a set of methods to manage a document URI. | ||
+ | |||
+ | Here some examples: | ||
+ | |||
+ | * String '''collectionID'''(URI uri): Returns the collection identifier in the URI. | ||
+ | * String '''documentID'''(URI uri): Returns the document identifier in the URI. | ||
+ | * String '''nodeID'''(URI uri): Returns the identifier of the node identified by a content URI. (e.g. the GCubeMetadata id, the GCubeAlternative) | ||
+ | * URI '''parentURI'''(URI uri): Returns the element parent. (e.g. from a GCubeMetadata URI you will get the Main Document URI) | ||
= F.A.Q. = | = F.A.Q. = | ||
− | + | == Running the examples I get the exception ''GHNClientContext: gHN could not complete initialisation'' == | |
+ | Running the examples I get the following exception: | ||
<source lang="java5"> | <source lang="java5"> | ||
[main] FATAL contexts.GHNClientContext - [0.0s] GHNClientContext: gHN could not complete initialisation | [main] FATAL contexts.GHNClientContext - [0.0s] GHNClientContext: gHN could not complete initialisation | ||
Line 47: | Line 565: | ||
at org.gcube.contentmanagement.contentmanager.stubs.calls.ReadManagerCall.<init>(ReadManagerCall.java:97) | at org.gcube.contentmanagement.contentmanager.stubs.calls.ReadManagerCall.<init>(ReadManagerCall.java:97) | ||
at org.gcube.contentmanagement.contentmanagerlibrary.CMReader.<init>(CMReader.java:62) | at org.gcube.contentmanagement.contentmanagerlibrary.CMReader.<init>(CMReader.java:62) | ||
− | at org.gcube.contentmanager. | + | ... |
+ | </source> | ||
+ | |||
+ | In this case you have to configure your eclipse running configuration properly (the variable GLOBUS_LOCATION is missing on environment) [https://gcore.wiki.gcube-system.org/gCube/index.php/The_Development_Cycle#A_Minimal_Client]. | ||
+ | |||
+ | == Running the examples I get the exception an AxisFault with nested exception ''org.globus.wsrf.ResourceException: resource identifier is missing'' == | ||
+ | Running the examples I get the following exception: | ||
+ | <source lang="java5"> | ||
+ | [main] ERROR calls.ReadManagerCall - ReadManagerCall: failed @ instance http://node23.d.d4science.research-infrastructures.eu:8080/wsrf/services/gcube/contentmanagement/contentmanager/readmanager | ||
+ | AxisFault | ||
+ | faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.generalException | ||
+ | faultSubcode: | ||
+ | faultString: | ||
+ | faultActor: | ||
+ | faultNode: | ||
+ | faultDetail: | ||
+ | {http://gcube-system.org/namespaces/common/core/faults}GCUBEFault:<ns2:Timestamp xmlns:ns2="http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-BaseFaults-1.2-draft-01.xsd">2011-01-14T18:07:38.603Z</ns2:Timestamp><ns1:FaultMessage>; nested exception is: | ||
+ | org.globus.wsrf.ResourceException: ; nested exception is: | ||
+ | org.globus.wsrf.ResourceException: resource identifier is missing</ns1:FaultMessage><ns1:FaultType>RETRY_EQUIVALENT</ns1:FaultType> | ||
+ | {http://xml.apache.org/axis/}exceptionName:org.gcube.common.core.faults.GCUBERetryEquivalentFault | ||
+ | {http://xml.apache.org/axis/}hostname:node23.d.d4science.research-infrastructures.eu | ||
+ | {}string:org.gcube.common.core.faults.GCUBERetryEquivalentException: ; nested exception is: | ||
+ | org.globus.wsrf.ResourceException: ; nested exception is: | ||
+ | org.globus.wsrf.ResourceException: resource identifier is missing | ||
+ | at org.gcube.common.core.contexts.GCUBEServiceContext.getDefaultException(GCUBEServiceContext.java:288) | ||
+ | at org.gcube.contentmanagement.contentmanager.porttypes.ReadManagerPT.get(ReadManagerPT.java:101) | ||
+ | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | ||
+ | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) | ||
+ | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) | ||
+ | at java.lang.reflect.Method.invoke(Method.java:585) | ||
+ | at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:384) | ||
+ | at org.globus.axis.providers.RPCProvider.invokeMethodSub(RPCProvider.java:107) | ||
+ | at org.globus.axis.providers.RPCProvider.invokeMethod(RPCProvider.java:90) | ||
+ | at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:281) | ||
+ | at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:319) | ||
+ | at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32) | ||
+ | at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118) | ||
+ | at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83) | ||
+ | at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:450) | ||
+ | at org.apache.axis.server.AxisServer.invoke(AxisServer.java:285) | ||
+ | at org.globus.wsrf.container.ServiceThread.doPost(ServiceThread.java:664) | ||
+ | at org.globus.wsrf.container.ServiceThread.process(ServiceThread.java:382) | ||
+ | at org.globus.wsrf.container.ServiceThread.run(ServiceThread.java:291) | ||
+ | Caused by: org.globus.wsrf.ResourceException: ; nested exception is: | ||
+ | org.globus.wsrf.ResourceException: resource identifier is missing | ||
+ | at org.gcube.common.core.state.GCUBEWSHome.find(GCUBEWSHome.java:263) | ||
+ | at org.gcube.contentmanagement.contentmanager.porttypes.ReadManagerPT.getManager(ReadManagerPT.java:41) | ||
+ | at org.gcube.contentmanagement.contentmanager.porttypes.ReadManagerPT.get(ReadManagerPT.java:95) | ||
+ | ... 17 more | ||
+ | Caused by: org.globus.wsrf.ResourceException: resource identifier is missing | ||
+ | at org.gcube.common.core.state.GCUBEResourceHome.find(GCUBEResourceHome.java:506) | ||
+ | at org.gcube.common.core.state.GCUBEWSHome.find(GCUBEWSHome.java:269) | ||
+ | at org.gcube.common.core.state.GCUBEWSHome.find(GCUBEWSHome.java:55) | ||
+ | at org.globus.wsrf.impl.ResourceContextImpl.getResource(ResourceContextImpl.java:164) | ||
+ | at org.gcube.common.core.state.GCUBEWSHome.find(GCUBEWSHome.java:262) | ||
+ | ... 19 more | ||
+ | |||
+ | |||
+ | |||
+ | at org.gcube.common.core.contexts.GCUBERemotePortTypeContext$GCUBEMethodInterceptor.intercept(GCUBERemotePortTypeContext.java:401) | ||
+ | at org.gcube.contentmanagement.contentmanager.stubs.bindings.ReadManagerPortTypeSOAPBindingStub$$EnhancerByCGLIB$$3b40c9a0.get(<generated>) | ||
+ | at org.gcube.contentmanagement.contentmanager.stubs.calls.ReadManagerCall$3.call(ReadManagerCall.java:247) | ||
+ | at org.gcube.contentmanagement.contentmanager.stubs.calls.BaseCall$CallHandler.interact(BaseCall.java:72) | ||
+ | at org.gcube.contentmanagement.contentmanager.stubs.calls.BaseCall$CallHandler._interact(BaseCall.java:86) | ||
+ | at org.gcube.common.core.utils.handlers.GCUBEServiceHandler.tryInstances(GCUBEServiceHandler.java:195) | ||
+ | at org.gcube.common.core.utils.handlers.GCUBEServiceHandler.run(GCUBEServiceHandler.java:111) | ||
+ | at org.gcube.contentmanagement.contentmanager.stubs.calls.BaseCall$CallHandler.run(BaseCall.java:95) | ||
+ | at org.gcube.contentmanagement.contentmanager.stubs.calls.ReadManagerCall.get(ReadManagerCall.java:252) | ||
+ | at org.gcube.contentmanagement.gcubedocumentlibrary.CMReader.getDocuments(CMReader.java:98) | ||
+ | ... | ||
+ | </source> | ||
+ | |||
+ | In this case you have to configure your eclipse running configuration properly (the variable GLOBUS_LOCATION is missing on classpath) [https://gcore.wiki.gcube-system.org/gCube/index.php/The_Development_Cycle#A_Minimal_Client]. | ||
+ | |||
+ | == Trying to get the document content I get this exception: ''java.net.MalformedURLException: unknown protocol: sms'' == | ||
+ | |||
+ | <source lang="java5"> | ||
+ | Exception in thread "main" java.net.MalformedURLException: unknown protocol: sms | ||
+ | at java.net.URL.<init>(URL.java:574) | ||
+ | at java.net.URL.<init>(URL.java:464) | ||
+ | at java.net.URL.<init>(URL.java:413) | ||
+ | at org.gcube.contentmanagement.contentmanagerlibrary.model.GCubeDocument.getContent(GCubeDocument.java:227) | ||
+ | ... | ||
+ | </source> | ||
+ | |||
+ | Check your configuration about [[GCube_Document_Library#Supporting_the_SMS_protocol|SMS protocol]] | ||
+ | |||
+ | == Eclipse don't find some functions in the examples == | ||
+ | |||
+ | Eclipse don't find some functions in the example code (like ''is'', ''matches'', ''all'', ''either'', ''not'', ''more'', ''less'', ''after'', ''before''). | ||
+ | |||
+ | Those are helper function from the CM and are defined on ''Constraints'' class. | ||
+ | You can statically import it with this statement: | ||
+ | |||
+ | <source lang="java5"> | ||
+ | import static org.gcube.contentmanagement.contentmanager.stubs.model.constraints.Constraints.*; | ||
+ | </source> | ||
+ | |||
+ | == Trying to interact with the CM I get this exception: ''org.apache.commons.discovery.DiscoveryException: org.gcube.common.core.utils.handlers.GCUBEServiceHandler$NoQueryResultException'' == | ||
+ | |||
+ | <source lang="java5"> | ||
+ | org.apache.commons.discovery.DiscoveryException: org.gcube.common.core.utils.handlers.GCUBEServiceHandler$NoQueryResultException | ||
+ | ***** | ||
+ | org.gcube.common.core.utils.handlers.GCUBEServiceHandler$NoQueryResultException | ||
+ | at org.gcube.common.core.utils.handlers.GCUBEStatefulServiceHandler.findInstances(GCUBEStatefulServiceHandler.java:38) | ||
+ | at org.gcube.common.core.utils.handlers.GCUBEServiceHandler.getInstances(GCUBEServiceHandler.java:140) | ||
+ | at org.gcube.common.core.utils.handlers.GCUBEServiceHandler.run(GCUBEServiceHandler.java:110) | ||
+ | at org.gcube.contentmanagement.contentmanager.stubs.calls.BaseCall$CallHandler.run(BaseCall.java:93) | ||
+ | at org.gcube.contentmanagement.contentmanager.stubs.calls.WriteManagerCall.add(WriteManagerCall.java:98) | ||
+ | at org.gcube.contentmanagement.contentmanager.stubs.calls.WriteManagerCall.add(WriteManagerCall.java:75) | ||
+ | at org.gcube.contentmanagement.gcubedocumentlibrary.CMWriter.add(CMWriter.java:84) | ||
+ | at org.gcube.personalization.userprofileaccess.impl.CMSUtils.createDocument(CMSUtils.java:67) | ||
+ | at org.gcube.personalization.userprofileaccess.impl.UserProfileAccessFactoryService.createResource(UserProfileAccessFactoryService.java:331) | ||
</source> | </source> | ||
− | + | The exception means that there is not plugin instantiated for the specified collection. Check if the collection id is correct. Otherwise contact the support team. |
Latest revision as of 15:22, 14 March 2011
Contents
- 1 Design Overview
- 2 Implementation Overview
- 3 Accessing a collection
- 4 Configuring the environment
- 5 Examples
- 6 F.A.Q.
- 6.1 Running the examples I get the exception GHNClientContext: gHN could not complete initialisation
- 6.2 Running the examples I get the exception an AxisFault with nested exception org.globus.wsrf.ResourceException: resource identifier is missing
- 6.3 Trying to get the document content I get this exception: java.net.MalformedURLException: unknown protocol: sms
- 6.4 Eclipse don't find some functions in the examples
- 6.5 Trying to interact with the CM I get this exception: org.apache.commons.discovery.DiscoveryException: org.gcube.common.core.utils.handlers.GCUBEServiceHandler$NoQueryResultException
Design Overview
The gCube Document Library (gDL) is a library that works on top of the Content Manager. This library offers a simpler way for client to access documents in the d4science system. The major change in the ContentManager is the document model (more generic, more extensible), the gDL reduces the development costs for using this new feature.
Document Model
Implementation Overview
The gDL is composed by tree types of access interfaces:
- the CollectionManager is used to manage the Collections in the system.
- the CMReader is used to read document from a collection. it offers methods to get a document or part of it (Metadata, Annotations, Parts, Alternatives);
- the CMWriter is used to add document or update it or a part of it;
- the CMView is used to get documents from a specific view of a collection.
Accessing a collection
The GCube Document Library offers a set of utilities to help the client to interact with the Content Manager.
Reading a collection
The CMReader
let the client to read elements from a collection. There are methods to read single document or part of it and methods to read multiple documents returned as GCubeDocument iterator.
Method to access GCubeDocument lets filter and project the result. Both operations are achieved through the Projection system.
Projection
The Projection lets filter and project the results of a "query".
There is a Projection class for each element of model.
For each element is possible to define which fields get back through the includeXXX(boolean include)
method.
For each element is also possible to set a constraint through the constrainXXX(Constraint constraint)
method, or remove it with resetXXXConstraint()
one.
The ProjectionsFactory
class provide a set of utility method for fast retrieving element dedicated projection.
Writing into a collection
The CMWriter
class offers method to write, update and delete document from a collection.
The write methods accept new document and returns the new document id.
The update method accept a preceding retrieved document on which updates are made.
Finally the document remotion requires only the document id.
Each method have both a single element operation than a multiple element operation using an iterator as input and as output.
Managing collections and views
The GCube Document Library offers utility classes to manage the collections and the views in the system.
The CollectionManager
class is an utility to manage a single collection. It can return the collection Reader and Writer for respectively read and write the collection. It can also manage the collection Views with facilities method to retrieve and manage it.
The Collections
class is an utility to manage the CM collections. The class provide methods for find, delete and create collections.
The CMViews
class is an utility for find the Views on the system.
Configuring the environment
Supporting the SMS protocol
The Storage Management Service introduces a new protocol, called sms, with an handler implementation.
To let know the JVM about the existence of a new protocol handler you can use one of this solutions:
- Specify a new java property at JVM startup: -Djava.protocol.handler.pkgs=org.gcube.contentmanagement.storagelayer.storagemanagementservice.stubs.protocol
- if you are developing a service and are you using the last distribution you don't have to change anything because the new GHN distribution already do it.
- if you are developing a portlet or a portal library check if your portal instance have the option specified in the environment variable CATALINA_OPTS.
- if you are running your client locally on Eclipse you can specify the options on run configuration, Arguments tab, VM arguments section.
- Use this sentence in your client testing program:
System.setProperty("java.protocol.handler.pkgs", "org.gcube.contentmanagement.storagelayer.storagemanagementservice.stubs.protocol");
Examples
Here some examples on gDL use. More examples are on examples folder in gDL project on SVN.
Managing Collections examples
We shows some examples on managing Collections.
List all collections
This example shows how to list all collection on a scope.
GCUBEScope scope = GCUBEScope.getScope("/gcube"); System.out.println("Collections in "+scope+" :"); for (CMCollection collection:Collections.list(scope)){ System.out.println(collection); }
Find collection by name
This example shows how to find a collection using his name.
GCUBEScope scope = GCUBEScope.getScope("/gcube"); String collectionName = "My Test Collection"; System.out.println("Collections in "+scope+" with name \""+collectionName+"\":"); for (CMCollection collection:Collections.findByName(scope, collectionName)){ System.out.println(collection); }
Create a GCube collection
This code creates a new collection with the given name and description.
The create method returns a list of collection references because a create command can produce more than one collection (for example OAI-Plugin).
GCUBEScope scope = GCUBEScope.getScope("/gcube"); //collection informations String collectionName = "MyTestCollection2"; String collectionDescription = "This is my test collection"; boolean userCollection = true; //we don't want to propagate the request to others CM boolean propagateRequest = false; //we want the collection to be readable and writable boolean readable = true; boolean writable = true; //finally we create the collection List<CollectionReference> collectionReferences = Collections.createGCubeCollection(propagateRequest, collectionName, collectionDescription, userCollection, readable, writable, scope); System.out.println("Created collections:"); for (CollectionReference collectionReference:collectionReferences){ System.out.println("Collection id: "+collectionReference.getCollectionID()); if (collectionReference.getReader()!=null) System.out.println("Reader EPR: "+collectionReference.getReader()); if (collectionReference.getWriter()!=null) System.out.println("Writer EPR: "+collectionReference.getWriter()); System.out.println(); }
Wrap a collection using SMS-Plugin
This example shows how to wrap an existing collection on SMS. To run this example the SMS-Plugin jar is required.
GCUBEScope scope = GCUBEScope.getScope("/gcube"); //the collection id on SMS (the id of collection IO on the SMS) String collectionId = "74fe4120-8f5b-11df-8083-c574cd534edf"; //we don't want to propagate the request to others CM boolean propagateRequest = false; //we want the collection to be readable and writable boolean readable = true; boolean writable = true; //finally we wrap the collection List<CollectionReference> collectionReferences = SMSUtil.wrapCollection(scope, propagateRequest, collectionId, readable, writable); System.out.println("Collections wrapped:"); for (CollectionReference collectionReference:collectionReferences){ System.out.println("Collection id: "+collectionReference.getCollectionID()); if (collectionReference.getReader()!=null) System.out.println("Reader EPR: "+collectionReference.getReader()); if (collectionReference.getWriter()!=null) System.out.println("Writer EPR: "+collectionReference.getWriter()); System.out.println(); }
Delete a GCube collection
This code deletes a GCube collection with the given id.
GCUBEScope scope = GCUBEScope.getScope("/gcube/devNext"); String collectionId = "8fb9df30-2de8-11df-893d-add683599cad"; //we want to delete the collection profile boolean deleteProfile = true; System.out.println("Requesting delete:"); List<Future<RPDocument>> results = Collections.deleteGCubeCollection(collectionId, deleteProfile, scope); System.out.println("Results:"); for (Future<RPDocument> result:results){ RPDocument doc = result.get(); System.out.println("Deleted delegate for collection "+doc.getKey()); }
Reading examples
We shows some examples on reading documents.
Get a single document
This example shows how to get a single document using his id.
//we instantiate the CMReader CMReader cmReader = new CMReader(collectionId, scope); //then we retrieve the document, we are not specifying projections, therefore the entire document is returned. GCubeDocument document = cmReader.getDocument(documentId); System.out.println("Retrieved document:"); System.out.println("id: " + document.getId()); System.out.println("name: " + document.getName()); System.out.println("mimeType: " + document.getMimeType());
Get documents by name
This example shows how to get documents from a collection using a name constraint.
String collectionId = "74fe4120-8f5b-11df-8083-c574cd534edf"; System.out.println("collectionId: "+collectionId); System.out.println(); String documentName = "Column dedication by Eumachos and Amias"; //we instantiate the CMReader CMReader cmReader = new CMReader(collectionId, scope); //we define a gcube main document projection GCubeMainDocumentProjection projection = ProjectionsFactory.MAIN_DOCUMENT(); //we set a constraint over the name field projection.constrainName(is(documentName)); //we are asking for all documents having the specified name Iterator<GCubeDocument> documentIterator = cmReader.getDocuments(projection); int i = 0; System.out.println("Retrieved documents:"); //finally we iterate over the collection while(documentIterator.hasNext()){ GCubeDocument document = documentIterator.next(); System.out.println(i+") "+document); i++; } System.out.println(); System.out.println("Found "+i+" documents");
To use the is
function remember to add this import:
import static org.gcube.contentmanagement.contentmanager.stubs.model.constraints.Constraints.*;
Get all documents in a collection
This example shows how to get all documents from a collection.
//we instantiate the CMReader CMReader cmReader = new CMReader(collectionId, scope); //then we retrieve all collection documents //we are not specifying projections, therefore the entire document is returned. Iterator<GCubeDocument> documentIterator = cmReader.getDocuments(); int i = 0; System.out.println("Collection documents:"); //finally we iterate over the collection while(documentIterator.hasNext()){ GCubeDocument document = documentIterator.next(); String id = document.getId(); int parts = document.getParts().size(); int alternatives = document.getAlternatives().size(); int metadata = document.getMetadata().size(); int annotations = document.getAnnotations().size(); System.out.printf("%d id: %s parts: %d alternatives: %d metadata: %d annotations: %d %n", i, id, parts, alternatives, metadata, annotations); i++; }
Get a document using projections
This example shows how to use projections during document retrieving.
//we instantiate the CMReader CMReader cmReader = new CMReader(collectionId, scope); //then we retrieve the document, //we are specifying the MAIN_DOCUMENT and the METADATA projections, the document returned will have main document data and all related metadata GCubeDocument document = cmReader.getDocument(documentId, ProjectionsFactory.MAIN_DOCUMENT(), ProjectionsFactory.METADATA()); System.out.println("Retrieved document:"); System.out.println("id: " + document.getId()); System.out.println("name: " + document.getName()); System.out.println("mimeType: " + document.getMimeType()); System.out.println("length: " + document.getLength()); System.out.println("contentURL: " + document.getContentURL()); System.out.println(); for (GCubeMetadata metadata:document.getMetadata()){ System.out.println("Metadata:"); System.out.println(" id: " + metadata.getId()); System.out.println(" mimeType: " + metadata.getMimeType()); System.out.println(" length: " + metadata.getLength()); System.out.println(" Metadata info:"); System.out.println(" name: " + metadata.getMetadataName()); System.out.println(" schema: " + metadata.getMetadataSchema()); System.out.println(" language: " + metadata.getMetadataLanguage()); System.out.println(); }
Get a document content
This example shows how to get a document content.
//we instantiate the CMReader CMReader cmReader = new CMReader(collectionId, scope); //then we retrieve the document, we are requesting only the main document GCubeDocument document = cmReader.getDocument(documentId, ProjectionsFactory.MAIN_DOCUMENT()); System.out.println("Retrieved document:"); System.out.println("id: " + document.getId()); System.out.println("name: " + document.getName()); System.out.println("contentURL: " + document.getContentURL()); System.out.println(); System.out.println("Retrieving content..."); //the content is retrieved by the CML library through the content URL InputStream is = document.getContent(); //finally we copy the content to a tmp file File tmpFile = File.createTempFile("mycontent", ".tmp"); IOUtils.copy(is, new FileOutputStream(tmpFile)); System.out.println("Content saved in tmp file: " + tmpFile.getAbsolutePath());
Get Collection dynamic properties
This example shows how to get dynamic properties for a given collection.
String collectionId = "18611ed0-f162-11dd-96f7-b87cc0f0b075"; System.out.println("collectionId: "+collectionId); CollectionManager collectionManager = new CollectionManager(collectionId, scope); System.out.println("Cardinality: "+collectionManager.getCardinality()); SimpleDateFormat sdf = new SimpleDateFormat(); System.out.println("Last Update: "+sdf.format(collectionManager.getLatUpdate().getTime())); System.out.println();
Writing examples
We shows some examples on manipulating collections.
Add a new document to a collection
Add a new document to a collection.
//we instantiate the CMWriter CMWriter cmWriter = new CMWriter(collectionId, scope); //we create an instance of gcube document GCubeDocument myNewDocument = new GCubeDocument("My Test Document", "application/octet-stream"); //then we ask for adding the new document String myNewDocumentId = cmWriter.add(myNewDocument); System.out.println("document created, id: " + myNewDocumentId); //finally we retrieve the created document CMReader cmReader = new CMReader(collectionId, scope); GCubeDocument document = cmReader.getDocument(myNewDocumentId); System.out.println("Created document:"); System.out.println("id: " + document.getId()); System.out.println("name: " + document.getName()); System.out.println("mimeType: " + document.getMimeType()); System.out.println("length: " + document.getLength()); System.out.println("contentURL: " + document.getContentURL()); System.out.println("parts: " + document.getParts().size()); System.out.println("alternatives:" + document.getAlternatives().size()); System.out.println("metadata: " + document.getMetadata().size()); System.out.println("annotations: " + document.getAnnotations().size());
Add a new alternative to a document
Add a new alternative to a document.
//first we retrieve the document to update CMReader cmReader = new CMReader(collectionId, scope); GCubeMainDocumentProjection projection = ProjectionsFactory.MAIN_DOCUMENT(); GCubeDocument document = cmReader.getDocument(documentId, projection); System.out.println("Document to update:"); System.out.printf("id: %s%n", document.getId()); System.out.println(); //we create the alternative to add GCubeAlternative alternative = new GCubeAlternative("text/xml"); //we add the alternative to the document document.addAlternative(alternative); //we instantiate the CMWriter CMWriter cmWriter = new CMWriter(collectionId, scope); //we ask for an update cmWriter.update(document); System.out.println("document updated");
Remove a document from a collection
Remove a document from a collection.
//we instantiate the CMWriter CMWriter cmWriter = new CMWriter(collectionId, scope); writer.remove(documentId); System.out.println("document removed.");
Views examples
We shows some examples on handling views.
Create a Metadata View
Shows how to create a metadata view.
String collectionId = "1c583800-d5d8-11df-9cd6-b72b661eb83e"; System.out.println("collectionId: "+collectionId); System.out.println(); MetadataView myView = new MetadataView(); myView.setName("MyView"); myView.setDescription("This is my test view"); myView.setEditable(false); myView.setIndexable(false); myView.setUserCollection(false); myView.setMetadaName("aquamaps"); myView.setMetadataLanguage("en"); myView.setMetadataSchema("http://aquamaps.xsd"); CollectionManager manager = new CollectionManager(collectionId, scope); manager.createView(myView); System.out.println("View created: "+myView); MetadataViewReader reader = new MetadataViewReader(myView, scope); System.out.println("Metadata:"); Iterator<GCubeMetadata> iterator = reader.getAll(); while(iterator.hasNext()) System.out.println(iterator.next());
List collection views
List all the views for a certain collection.
//we instantiate a collection manager. CollectionManager collectionManager = new CollectionManager(collectionId, scope); //then we ask for all the available views List<CMView<?>> views = collectionManager.findViews(); System.out.println("found "+views.size()+" views:"); for (CMView<?> view:views) System.out.println(view);
Get Metadata elements
Get a metadata view for a collection and the all the elements of the view.
//first we instantiate a collection manager CollectionManager collectionManager = new CollectionManager(collectionId, scope); //then we create a view kind, in this case a MetadataView without specifying any property MetadataView myMedataView = new MetadataView(); //finally we ask to the collection manager to list all views of the kind specified. List<MetadataView> views = collectionManager.findViews(myMedataView); System.out.println("Found views: "+views.size()); //in case we not found views if (views.size()==0) throw new Exception("No views found"); //we get the first of the list MetadataView view = views.get(0); //we instantiate the view reader MetadataViewReader metadataReader = new MetadataViewReader(view, scope); //we request all the elements Iterator<GCubeMetadata> metadata = metadataReader.getAll(); //and print it while (metadata.hasNext()) System.out.println(" >"+metadata.next());
Get Metadata elements using only the metadata view ID
Get a metadata view for a collection and all the elements of the view without knowing the collection ID.
//Use the metadata view ID to find the view List<MetadataView> views = CMViews.findViews(scope, new MetadataView(metadataViewID)); if (views.isEmpty()){ System.out.println("Metadata view not found. View ID: " + metadataViewID); } //Just get the first view MetadataView myMedataView = views.get(0); MetadataViewReader metadataReader = new MetadataViewReader(myMedataView, scope);
Get Metadata elements specifying a condition
Get all the elements of the view specifying a condition.
MetadataViewReader metadataReader = new MetadataViewReader(view, scope); GCubeMetadataProjection projection = (GCubeMetadataProjection) view.getProjection(); //2/3/09 5:06 PM Calendar c1 = Calendar.getInstance(); c1.set(2009, 1, 3, 17,6,0); //2/3/09 5:07 PM Calendar c2 = Calendar.getInstance(); c2.set(2009, 1, 3, 17,7,0); projection.constrainCreationTime(all(after(c1),before(c2))); //we request all the elements Iterator<GCubeMetadata> metadata = metadataReader.getAll(projection); //and print it SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); System.out.println("Metadata elements created from "+sdf.format(c1.getTime())+" to "+sdf.format(c2.getTime())+":"); while (metadata.hasNext()) { GCubeMetadata m = metadata.next(); System.out.println(" >"+m); System.out.println(); }
Managing URI
The CML library offers an helper class URIS which expose a set of methods to manage a document URI.
Here some examples:
- String collectionID(URI uri): Returns the collection identifier in the URI.
- String documentID(URI uri): Returns the document identifier in the URI.
- String nodeID(URI uri): Returns the identifier of the node identified by a content URI. (e.g. the GCubeMetadata id, the GCubeAlternative)
- URI parentURI(URI uri): Returns the element parent. (e.g. from a GCubeMetadata URI you will get the Main Document URI)
F.A.Q.
Running the examples I get the exception GHNClientContext: gHN could not complete initialisation
Running the examples I get the following exception:
[main] FATAL contexts.GHNClientContext - [0.0s] GHNClientContext: gHN could not complete initialisation java.io.FileNotFoundException: null/config/GHNConfig.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at org.gcube.common.core.contexts.GHNContext.configureGHN(GHNContext.java:314) at org.gcube.common.core.contexts.GHNClientContext.configureGHN(GHNClientContext.java:35) at org.gcube.common.core.contexts.GHNClientContext.initialise(GHNClientContext.java:27) at org.gcube.common.core.contexts.GHNContext.<clinit>(GHNContext.java:252) at org.gcube.common.core.utils.calls.WSCall.getInitQuery(WSCall.java:55) at org.gcube.common.core.utils.calls.WSCall.getInitQuery(WSCall.java:25) at org.gcube.common.core.utils.calls.GCUBECall.<init>(GCUBECall.java:72) at org.gcube.common.core.utils.calls.GCUBECall.<init>(GCUBECall.java:84) at org.gcube.common.core.utils.calls.WSCall.<init>(WSCall.java:43) at org.gcube.contentmanagement.contentmanager.stubs.calls.BaseCall.<init>(BaseCall.java:25) at org.gcube.contentmanagement.contentmanager.stubs.calls.ManagerCall.<init>(ManagerCall.java:24) at org.gcube.contentmanagement.contentmanager.stubs.calls.ManagerCall.<init>(ManagerCall.java:35) at org.gcube.contentmanagement.contentmanager.stubs.calls.ReadManagerCall.<init>(ReadManagerCall.java:97) at org.gcube.contentmanagement.contentmanagerlibrary.CMReader.<init>(CMReader.java:62) at org.gcube.contentmanager.contentmanagerlibrary.GetDocumentsWithProjections.main(GetDocumentsWithProjections.java:34) Exception in thread "main" java.lang.NullPointerException at org.gcube.common.core.utils.calls.WSCall.getInitQuery(WSCall.java:55) at org.gcube.common.core.utils.calls.WSCall.getInitQuery(WSCall.java:25) at org.gcube.common.core.utils.calls.GCUBECall.<init>(GCUBECall.java:72) at org.gcube.common.core.utils.calls.GCUBECall.<init>(GCUBECall.java:84) at org.gcube.common.core.utils.calls.WSCall.<init>(WSCall.java:43) at org.gcube.contentmanagement.contentmanager.stubs.calls.BaseCall.<init>(BaseCall.java:25) at org.gcube.contentmanagement.contentmanager.stubs.calls.ManagerCall.<init>(ManagerCall.java:24) at org.gcube.contentmanagement.contentmanager.stubs.calls.ManagerCall.<init>(ManagerCall.java:35) at org.gcube.contentmanagement.contentmanager.stubs.calls.ReadManagerCall.<init>(ReadManagerCall.java:97) at org.gcube.contentmanagement.contentmanagerlibrary.CMReader.<init>(CMReader.java:62) ...
In this case you have to configure your eclipse running configuration properly (the variable GLOBUS_LOCATION is missing on environment) [1].
Running the examples I get the exception an AxisFault with nested exception org.globus.wsrf.ResourceException: resource identifier is missing
Running the examples I get the following exception:
[main] ERROR calls.ReadManagerCall - ReadManagerCall: failed @ instance http://node23.d.d4science.research-infrastructures.eu:8080/wsrf/services/gcube/contentmanagement/contentmanager/readmanager AxisFault faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.generalException faultSubcode: faultString: faultActor: faultNode: faultDetail: {http://gcube-system.org/namespaces/common/core/faults}GCUBEFault:<ns2:Timestamp xmlns:ns2="http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-BaseFaults-1.2-draft-01.xsd">2011-01-14T18:07:38.603Z</ns2:Timestamp><ns1:FaultMessage>; nested exception is: org.globus.wsrf.ResourceException: ; nested exception is: org.globus.wsrf.ResourceException: resource identifier is missing</ns1:FaultMessage><ns1:FaultType>RETRY_EQUIVALENT</ns1:FaultType> {http://xml.apache.org/axis/}exceptionName:org.gcube.common.core.faults.GCUBERetryEquivalentFault {http://xml.apache.org/axis/}hostname:node23.d.d4science.research-infrastructures.eu {}string:org.gcube.common.core.faults.GCUBERetryEquivalentException: ; nested exception is: org.globus.wsrf.ResourceException: ; nested exception is: org.globus.wsrf.ResourceException: resource identifier is missing at org.gcube.common.core.contexts.GCUBEServiceContext.getDefaultException(GCUBEServiceContext.java:288) at org.gcube.contentmanagement.contentmanager.porttypes.ReadManagerPT.get(ReadManagerPT.java:101) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:384) at org.globus.axis.providers.RPCProvider.invokeMethodSub(RPCProvider.java:107) at org.globus.axis.providers.RPCProvider.invokeMethod(RPCProvider.java:90) at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:281) at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:319) at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32) at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118) at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83) at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:450) at org.apache.axis.server.AxisServer.invoke(AxisServer.java:285) at org.globus.wsrf.container.ServiceThread.doPost(ServiceThread.java:664) at org.globus.wsrf.container.ServiceThread.process(ServiceThread.java:382) at org.globus.wsrf.container.ServiceThread.run(ServiceThread.java:291) Caused by: org.globus.wsrf.ResourceException: ; nested exception is: org.globus.wsrf.ResourceException: resource identifier is missing at org.gcube.common.core.state.GCUBEWSHome.find(GCUBEWSHome.java:263) at org.gcube.contentmanagement.contentmanager.porttypes.ReadManagerPT.getManager(ReadManagerPT.java:41) at org.gcube.contentmanagement.contentmanager.porttypes.ReadManagerPT.get(ReadManagerPT.java:95) ... 17 more Caused by: org.globus.wsrf.ResourceException: resource identifier is missing at org.gcube.common.core.state.GCUBEResourceHome.find(GCUBEResourceHome.java:506) at org.gcube.common.core.state.GCUBEWSHome.find(GCUBEWSHome.java:269) at org.gcube.common.core.state.GCUBEWSHome.find(GCUBEWSHome.java:55) at org.globus.wsrf.impl.ResourceContextImpl.getResource(ResourceContextImpl.java:164) at org.gcube.common.core.state.GCUBEWSHome.find(GCUBEWSHome.java:262) ... 19 more at org.gcube.common.core.contexts.GCUBERemotePortTypeContext$GCUBEMethodInterceptor.intercept(GCUBERemotePortTypeContext.java:401) at org.gcube.contentmanagement.contentmanager.stubs.bindings.ReadManagerPortTypeSOAPBindingStub$$EnhancerByCGLIB$$3b40c9a0.get(<generated>) at org.gcube.contentmanagement.contentmanager.stubs.calls.ReadManagerCall$3.call(ReadManagerCall.java:247) at org.gcube.contentmanagement.contentmanager.stubs.calls.BaseCall$CallHandler.interact(BaseCall.java:72) at org.gcube.contentmanagement.contentmanager.stubs.calls.BaseCall$CallHandler._interact(BaseCall.java:86) at org.gcube.common.core.utils.handlers.GCUBEServiceHandler.tryInstances(GCUBEServiceHandler.java:195) at org.gcube.common.core.utils.handlers.GCUBEServiceHandler.run(GCUBEServiceHandler.java:111) at org.gcube.contentmanagement.contentmanager.stubs.calls.BaseCall$CallHandler.run(BaseCall.java:95) at org.gcube.contentmanagement.contentmanager.stubs.calls.ReadManagerCall.get(ReadManagerCall.java:252) at org.gcube.contentmanagement.gcubedocumentlibrary.CMReader.getDocuments(CMReader.java:98) ...
In this case you have to configure your eclipse running configuration properly (the variable GLOBUS_LOCATION is missing on classpath) [2].
Trying to get the document content I get this exception: java.net.MalformedURLException: unknown protocol: sms
Exception in thread "main" java.net.MalformedURLException: unknown protocol: sms at java.net.URL.<init>(URL.java:574) at java.net.URL.<init>(URL.java:464) at java.net.URL.<init>(URL.java:413) at org.gcube.contentmanagement.contentmanagerlibrary.model.GCubeDocument.getContent(GCubeDocument.java:227) ...
Check your configuration about SMS protocol
Eclipse don't find some functions in the examples
Eclipse don't find some functions in the example code (like is, matches, all, either, not, more, less, after, before).
Those are helper function from the CM and are defined on Constraints class. You can statically import it with this statement:
import static org.gcube.contentmanagement.contentmanager.stubs.model.constraints.Constraints.*;
Trying to interact with the CM I get this exception: org.apache.commons.discovery.DiscoveryException: org.gcube.common.core.utils.handlers.GCUBEServiceHandler$NoQueryResultException
org.apache.commons.discovery.DiscoveryException: org.gcube.common.core.utils.handlers.GCUBEServiceHandler$NoQueryResultException ***** org.gcube.common.core.utils.handlers.GCUBEServiceHandler$NoQueryResultException at org.gcube.common.core.utils.handlers.GCUBEStatefulServiceHandler.findInstances(GCUBEStatefulServiceHandler.java:38) at org.gcube.common.core.utils.handlers.GCUBEServiceHandler.getInstances(GCUBEServiceHandler.java:140) at org.gcube.common.core.utils.handlers.GCUBEServiceHandler.run(GCUBEServiceHandler.java:110) at org.gcube.contentmanagement.contentmanager.stubs.calls.BaseCall$CallHandler.run(BaseCall.java:93) at org.gcube.contentmanagement.contentmanager.stubs.calls.WriteManagerCall.add(WriteManagerCall.java:98) at org.gcube.contentmanagement.contentmanager.stubs.calls.WriteManagerCall.add(WriteManagerCall.java:75) at org.gcube.contentmanagement.gcubedocumentlibrary.CMWriter.add(CMWriter.java:84) at org.gcube.personalization.userprofileaccess.impl.CMSUtils.createDocument(CMSUtils.java:67) at org.gcube.personalization.userprofileaccess.impl.UserProfileAccessFactoryService.createResource(UserProfileAccessFactoryService.java:331)
The exception means that there is not plugin instantiated for the specified collection. Check if the collection id is correct. Otherwise contact the support team.