Added code to GenericResource class to support the IS communication of VRE_Information_Space_Editor (admin) portlet

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerCore@82824 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
nikolas.laskaris 2013-10-09 14:57:36 +00:00
parent e33fd6052c
commit a7f8764145
3 changed files with 130 additions and 3 deletions

View File

@ -186,6 +186,7 @@
<artifactId>xalan</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>net.sourceforge.addressing</groupId>
<artifactId>addressing</artifactId>

View File

@ -14,6 +14,7 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
@ -36,6 +37,7 @@ import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICClient;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -45,6 +47,7 @@ import static org.gcube.resources.discovery.icclient.ICFactory.*;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resource.Type;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.common.resources.gcore.ServiceInstance;
/**
@ -148,6 +151,112 @@ public class GenericResource implements GenericResourceInfoI {
return (List<ISGenericResource>) (CachesManager.getInstance().getGenericResourceCache().get(query).getValue());
}
/**
* retrieves from the IS all the Tree collections under the current scope
* @param onlyUserCollections if true, it returns only user tree collections, otherwise it returns all tree collections
* @return a hashmap containing the {collection id, collection resource}
*/
public HashMap<String,org.gcube.common.resources.gcore.GenericResource> getAllTreeCollections(boolean onlyUserCollections) {
// maybe should add the ScopeProvider.instance.set(session.getScope()) here and not in the constructors.
HashMap<String,org.gcube.common.resources.gcore.GenericResource> pairs = new HashMap<String, org.gcube.common.resources.gcore.GenericResource>();
SimpleQuery query = null;
try {
query = queryFor(org.gcube.common.resources.gcore.GenericResource.class);
query.addCondition("$resource/Profile/SecondaryType eq 'DataSource'");
if(onlyUserCollections)
query.addCondition("$resource/Profile/Body/SourceProperties/user eq 'true'");
List<org.gcube.common.resources.gcore.GenericResource> results = client.submit(query);
if (results == null || results.size() == 0)
logger.debug("Couldn't find any tree collections within that scope! Will return empty list.");
else
logger.debug("# of Tree Collections found: "+ results.size());
for (org.gcube.common.resources.gcore.GenericResource gr : results)
pairs.put(gr.id(), gr);
} catch (Exception e) {
logger.debug("Remote Exception:" + e.toString());
}
return pairs;
}
/**
* retrieves from the IS all the OpenSearch collections under the current scope
* @param onlyUserCollections if true, it returns only user opensearch collections, otherwise it returns all opensearch collections
* @return a hashmap containing the {collection id, collection resource}
*/
public HashMap<String,org.gcube.common.resources.gcore.GenericResource> getAllOpenSearchCollections(boolean onlyUserCollections) {
// maybe should add the ScopeProvider.instance.set(session.getScope()) here and not in the constructors.
HashMap<String,org.gcube.common.resources.gcore.GenericResource> pairs = new HashMap<String, org.gcube.common.resources.gcore.GenericResource>();
SimpleQuery query = null;
try {
query = queryFor(org.gcube.common.resources.gcore.GenericResource.class);
query.addCondition("$resource/Profile/SecondaryType eq 'GCUBECollection'");
if(onlyUserCollections)
query.addCondition("$resource/Profile/Body/CollectionInfo/user eq 'true'");
List<org.gcube.common.resources.gcore.GenericResource> results = client.submit(query);
if (results == null || results.size() == 0)
logger.debug("Couldn't find any OpenSearch collections within that scope! Will return empty list.");
else
logger.debug("# of OpenSearch Collections found: "+ results.size());
for (org.gcube.common.resources.gcore.GenericResource gr : results)
pairs.put(gr.id(), gr);
} catch (Exception e) {
logger.debug("Remote Exception:" + e.toString());
}
return pairs;
}
/**
*
* @param collectionID The ID of the collection
* @return the cardinality of the collection stored within the IS
*/
public String getTreeResourceCardinality(String collectionID){
SimpleQuery query = queryFor(ServiceInstance.class);
query.addCondition("$resource/Data/*[ends-with(name(.),'ServiceName')]/text() eq 'tree-manager-service'");
query.addCondition("$resource/Data/*[ends-with(name(.),'SourceId')]/text() eq '"+collectionID+"'");
DiscoveryClient<ServiceInstance> client = clientFor(ServiceInstance.class);
List<ServiceInstance> resources = client.submit(query);
// logger.debug("Found " + resources.size() + " resources");
String cardinality = "0";
if(resources.size()==0)
logger.debug("No collection '" + collectionID +"' found within scope " + ScopeProvider.instance.get());
else if(resources.size()==1){
NodeList nodes = resources.get(0).properties().customProperties().getChildNodes();
for(int i=0;i<nodes.getLength();i++)
if(nodes.item(i).getLocalName().equalsIgnoreCase("Cardinality"))
cardinality = nodes.item(i).getTextContent();
logger.debug("Cardinality of collection '" + collectionID +"' is " + cardinality);
}
else
logger.debug("ERROR ! Found more than 1 resources '" + collectionID +"' within scope " + ScopeProvider.instance.get());
return cardinality;
}
/**
* Gets the user field property from the generic resource body (profile/body)
* @return the property value
* @throws XPathExpressionException
*/
public boolean checkIfUser(org.gcube.common.resources.gcore.GenericResource resource) throws XPathExpressionException{
XPathExpression userExpression = null;
XPath xPath = XPathFactory.newInstance().newXPath();
try {
userExpression = xPath.compile("/SourceProperties/user");
} catch (XPathExpressionException e) {
logger.debug("Could not initiate the xpath expression for the user property");
e.printStackTrace();
}
return Boolean.parseBoolean(userExpression.evaluate(new InputSource(new StringReader(resource.profile().bodyAsString()))));
}
/**
* Creates a new generic resource
@ -288,7 +397,6 @@ public class GenericResource implements GenericResourceInfoI {
return getGenericResource(query);
}
/**
* @param name the name of the generic resource
* @return a list containing the generic resources that have as name the given
@ -456,7 +564,7 @@ public class GenericResource implements GenericResourceInfoI {
private static List<String[]> retrieveGenericResourcesFromNameParts(String nameParts, String scope) throws Exception {
ScopeProvider.instance.set(scope);
System.out.println("retrieveGenericResourcesFromNameParts");
logger.debug("retrieveGenericResourcesFromNameParts");
SimpleQuery query = queryFor(GenericResource.class);
//READ COMMENT BELOW !!!
query.addCondition("$resource/Profile/Name eq '"+nameParts+"'");

View File

@ -0,0 +1,18 @@
package org.gcube.application.framework.core.util;
/**
*
* @author nikolas
*
*/
public class GenericResourcesConstants {
/**
* Generic Resource constant for IS DataSources profile name
*/
public static final String DATASOURCENAMES = "GCUBECollection";
}