2012-09-12 12:47:59 +02:00
package org.gcube.application.framework.core.util ;
2013-10-15 16:39:25 +02:00
2012-09-12 12:47:59 +02:00
import java.io.StringReader ;
import java.rmi.RemoteException ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.LinkedList ;
import java.util.List ;
import java.util.Vector ;
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
import javax.xml.xpath.XPath ;
2013-10-09 16:57:36 +02:00
import javax.xml.xpath.XPathExpression ;
2012-09-12 12:47:59 +02:00
import javax.xml.xpath.XPathExpressionException ;
import javax.xml.xpath.XPathFactory ;
import org.gcube.application.framework.core.GenericResourceInfoI ;
import org.gcube.application.framework.core.cache.CachesManager ;
import org.gcube.application.framework.core.genericresources.model.ISGenericResource ;
import org.gcube.application.framework.core.session.ASLSession ;
import org.gcube.application.framework.core.session.SessionManager ;
import org.gcube.application.framework.core.util.CacheEntryConstants ;
import org.gcube.application.framework.core.util.QueryString ;
import org.gcube.application.framework.core.util.SessionConstants ;
2013-08-22 12:28:07 +02:00
import org.gcube.common.resources.gcore.Resources ;
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.informationsystem.publisher.RegistryPublisher ;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory ;
import org.gcube.informationsystem.publisher.ScopedPublisher ;
import org.gcube.informationsystem.publisher.stubs.registry.faults.PublisherException ;
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery ;
2012-09-12 12:47:59 +02:00
import org.w3c.dom.Document ;
2014-06-05 12:03:56 +02:00
import org.w3c.dom.Element ;
2013-10-09 16:57:36 +02:00
import org.w3c.dom.NodeList ;
2012-09-12 12:47:59 +02:00
import org.xml.sax.InputSource ;
2013-02-22 17:52:54 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2014-01-28 12:48:25 +01:00
2013-08-22 12:28:07 +02:00
import static org.gcube.resources.discovery.icclient.ICFactory.* ;
import org.gcube.common.resources.gcore.Resource ;
2013-10-09 16:57:36 +02:00
import org.gcube.common.resources.gcore.ServiceInstance ;
2013-08-22 12:28:07 +02:00
2012-09-12 12:47:59 +02:00
/ * *
2013-08-22 12:28:07 +02:00
* @author Valia Tsagkalidou ( NKUA )
* @author Nikolas Laskaris ( NKUA ) - - refactored August 2013 to conform to FeatherWeight stack standards
2012-09-12 12:47:59 +02:00
* /
2013-09-03 11:43:58 +02:00
public class GenericResource implements GenericResourceInfoI {
2013-02-22 17:52:54 +01:00
/** The logger. */
2013-09-03 11:43:58 +02:00
private static final Logger logger = LoggerFactory . getLogger ( GenericResource . class ) ;
2012-09-12 12:47:59 +02:00
/ * *
2013-08-22 12:28:07 +02:00
* A static IS publisher in order to communicate with IS ( FeatherWeight )
2012-09-12 12:47:59 +02:00
* /
2013-08-22 12:28:07 +02:00
// protected static ISPublisher publisher = null;
// protected static ISClient client = null;
protected static ScopedPublisher scopedPublisher = null ;
protected static RegistryPublisher publisher = null ;
2013-09-03 11:43:58 +02:00
protected static DiscoveryClient < org . gcube . common . resources . gcore . GenericResource > client = null ;
2013-08-22 12:28:07 +02:00
2012-09-12 12:47:59 +02:00
/ * *
* The D4Science session to be used
* /
ASLSession session ;
/ * *
* Constructs a GenericReosurce object
* @param extrenalSessionID the external session id which is used for the D4ScienceSession ( usually the session . getId ( ) of the HttpSession or PortletSession )
* @param username the username of the user that called this constructor
* /
2013-09-03 11:43:58 +02:00
public GenericResource ( String extrenalSessionID , String username )
2012-09-12 12:47:59 +02:00
{
2013-08-22 12:28:07 +02:00
2012-09-12 12:47:59 +02:00
session = SessionManager . getInstance ( ) . getASLSession ( extrenalSessionID , username ) ;
try {
2013-08-22 12:28:07 +02:00
ScopeProvider . instance . set ( session . getScope ( ) ) ;
scopedPublisher = RegistryPublisherFactory . scopedPublisher ( ) ;
publisher = RegistryPublisherFactory . create ( ) ;
2012-09-12 12:47:59 +02:00
} catch ( Exception e ) {
2013-02-22 17:52:54 +01:00
logger . error ( " Exception: " , e ) ;
2012-09-12 12:47:59 +02:00
}
if ( client = = null )
{
try {
2013-09-03 11:43:58 +02:00
client = clientFor ( org . gcube . common . resources . gcore . GenericResource . class ) ; //GHNContext.getImplementation(ISClient.class);
2012-09-12 12:47:59 +02:00
} catch ( Exception e ) {
2013-02-22 17:52:54 +01:00
logger . error ( " Exception: " , e ) ;
2012-09-12 12:47:59 +02:00
client = null ;
}
}
}
/ * *
* Constructs a GenericReosurce object
* @param session the D4Science session to be used for retrieving information needed
* /
2013-09-03 11:43:58 +02:00
public GenericResource ( ASLSession session ) {
2012-09-12 12:47:59 +02:00
super ( ) ;
this . session = session ;
try {
2013-08-22 12:28:07 +02:00
ScopeProvider . instance . set ( session . getScope ( ) ) ;
scopedPublisher = RegistryPublisherFactory . scopedPublisher ( ) ;
publisher = RegistryPublisherFactory . create ( ) ;
2012-09-12 12:47:59 +02:00
} catch ( Exception e ) {
2013-02-22 17:52:54 +01:00
logger . error ( " Exception: " , e ) ;
2012-09-12 12:47:59 +02:00
}
if ( client = = null )
{
try {
2013-09-03 11:43:58 +02:00
client = clientFor ( org . gcube . common . resources . gcore . GenericResource . class ) ;
2012-09-12 12:47:59 +02:00
} catch ( Exception e ) {
// TODO Auto-generated catch block
2013-02-22 17:52:54 +01:00
logger . error ( " Exception: " , e ) ;
2012-09-12 12:47:59 +02:00
client = null ;
}
}
}
/ * *
* @return the name of the active VRE
* /
protected String getDLName ( )
{
//SOS
//return session.getOriginalScopeName();
return session . getOriginalScopeName ( ) ;
}
/ * *
* @param query the query to retrieve generic resources from cache
* @return a list of generic resources
* /
protected List < ISGenericResource > getGenericResource ( QueryString query )
{
2014-02-06 16:44:51 +01:00
List < ISGenericResource > resList = ( List < ISGenericResource > ) ( CachesManager . getInstance ( ) . getGenericResourceCache ( ) . get ( query ) . getValue ( ) ) ;
if ( resList . isEmpty ( ) )
logger . debug ( " no generic resources in cache " + query . get ( " name " ) ) ;
return resList ;
2012-09-12 12:47:59 +02:00
}
2013-10-09 16:57:36 +02:00
/ * *
* 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 ) ;
2014-06-05 12:03:56 +02:00
query . addCondition ( " $resource/Profile/SecondaryType eq 'DataSource' " ) ; //this brings all collections, need to filter out the opensearch ones.
2013-10-09 16:57:36 +02:00
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. " ) ;
for ( org . gcube . common . resources . gcore . GenericResource gr : results )
pairs . put ( gr . id ( ) , gr ) ;
} catch ( Exception e ) {
logger . debug ( " Remote Exception: " + e . toString ( ) ) ;
}
2014-06-05 12:03:56 +02:00
//remove from all collections set, the opensearch ones !
for ( String key : pairs . keySet ( ) ) {
org . gcube . common . resources . gcore . GenericResource collection = pairs . get ( key ) ;
Element body = collection . profile ( ) . body ( ) ;
if ( body . getElementsByTagName ( " type " ) . getLength ( ) = = 0 )
pairs . remove ( key ) ;
else
logger . debug ( " Found tree collection: " + key ) ;
}
logger . debug ( " # of Tree Collections found: " + pairs . size ( ) ) ;
2013-10-09 16:57:36 +02:00
return pairs ;
}
2012-09-12 12:47:59 +02:00
2013-10-09 16:57:36 +02:00
/ * *
* 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 ) ;
2014-06-05 12:03:56 +02:00
// query.addCondition("$resource/Profile/SecondaryType eq 'GCUBECollection'");
query . addCondition ( " $resource/Profile/SecondaryType eq 'DataSource' " ) ; //changed from GCUBECollection to DataSource (2 be same as the tree collections)
query . addCondition ( " $resource/Profile/Body/SourceProperties/type eq 'opensearch' " ) ;
2013-10-09 16:57:36 +02:00
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 ) ;
2013-10-15 16:39:25 +02:00
query . addCondition ( " $resource/Data/child::*[local-name()='ServiceName']/string() eq 'tree-manager-service' " ) ;
query . addCondition ( " $resource/Data/child::*[local-name()='SourceId']/string() eq ' " + collectionID + " ' " ) ;
2013-10-09 16:57:36 +02:00
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 ;
}
2013-10-15 16:39:25 +02:00
/ * *
* Retrieves the cardinality of all tree collections on the current scope
*
* @return a HashMap containing the ( collectionID , cardinality ) pairs
* /
public HashMap < String , String > getTreeResourcesCardinalities ( ) {
HashMap < String , String > outputResults = new HashMap < String , String > ( ) ; //to hold (collectionID,cardinality)
DiscoveryClient < CardinalitiesResultBean > client = clientFor ( CardinalitiesResultBean . class ) ;
SimpleQuery query = queryFor ( ServiceInstance . class ) ;
query . addCondition ( " $resource/Data/child::*[local-name()='ServiceName']/string() eq 'tree-manager-service' " ) ;
query . setResult ( " <Result> " +
" <CollectionID>{$resource/Data/child::*[local-name()='SourceId']/string()}</CollectionID> " +
" <Cardinality>{$resource/Data/child::*[local-name()='Cardinality']/string()}</Cardinality> " +
" </Result> " ) ;
List < CardinalitiesResultBean > beanResults = client . submit ( query ) ;
if ( beanResults . size ( ) = = 0 )
logger . debug ( " There are no tree collection cardinality results " ) ;
for ( CardinalitiesResultBean beanResult : beanResults ) {
outputResults . put ( beanResult . collectionID , beanResult . cardinality ) ;
// logger.debug("Collection: " + beanResult.collectionID + " has cardinality: " + beanResult.cardinality);
}
return outputResults ;
}
2013-10-09 16:57:36 +02:00
/ * *
* 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 ( ) ) ) ) ) ;
}
2012-09-12 12:47:59 +02:00
/ * *
* Creates a new generic resource
* @param genericResource the new generic resource
* @return the ID of the newly created resource
* @throws RemoteException when an error has occurred while communicating with IS
* /
2013-08-22 12:28:07 +02:00
public String createGenericResource ( ISGenericResource genericResource ) throws RemoteException {
2012-09-12 12:47:59 +02:00
try {
2013-08-22 12:28:07 +02:00
//maybe should add the ScopeProvider.instance.set(session.getScope()); here and not in the constructors.
2014-01-28 12:48:25 +01:00
org . gcube . common . resources . gcore . GenericResource gCubeRes = new org . gcube . common . resources . gcore . GenericResource ( ) ;
gCubeRes . newProfile ( ) ;
2013-08-22 12:28:07 +02:00
gCubeRes . profile ( ) . name ( genericResource . getName ( ) ) ;
gCubeRes . profile ( ) . description ( genericResource . getDescription ( ) ) ;
gCubeRes . profile ( ) . type ( genericResource . getSecondaryType ( ) ) ;
2014-01-28 12:48:25 +01:00
gCubeRes . profile ( ) . newBody ( genericResource . getBody ( ) ) ;
2013-08-22 12:28:07 +02:00
//maybe should change the visibility (into public) of addScope() within the Resource class of common-gcore-resources package in order to use it here.
List < String > scopes = new ArrayList < String > ( ) ;
scopes . add ( session . getScope ( ) ) ;
2014-01-28 12:48:25 +01:00
org . gcube . common . resources . gcore . GenericResource res = scopedPublisher . create ( gCubeRes , scopes ) ;
2013-10-01 16:11:43 +02:00
logger . info ( " Created Generic Resource with id: " + res . id ( ) + " on scope: " + scopes . toString ( ) ) ;
2014-01-28 12:48:25 +01:00
// 1-oct-2013 -- adding generic resource to cache
2013-10-01 16:11:43 +02:00
logger . info ( " Adding it also in cache... " ) ;
List < ISGenericResource > genRes = new ArrayList < ISGenericResource > ( ) ;
genRes . add ( genericResource ) ;
QueryString query1 = new QueryString ( ) ;
query1 . addParameter ( CacheEntryConstants . id , genericResource . getId ( ) ) ;
2013-12-05 15:30:04 +01:00
query1 . addParameter ( CacheEntryConstants . vre , session . getScope ( ) ) ;
2013-10-01 16:11:43 +02:00
CachesManager . getInstance ( ) . getGenericResourceCache ( ) . put ( new net . sf . ehcache . Element ( query1 , genRes ) ) ;
logger . info ( " ...added in cache " ) ;
2013-08-22 12:28:07 +02:00
return res . id ( ) ;
2012-09-12 12:47:59 +02:00
} catch ( Exception e ) {
2013-02-22 17:52:54 +01:00
logger . error ( " Exception: " , e ) ;
2012-09-12 12:47:59 +02:00
throw new RemoteException ( ) ;
}
}
2014-01-28 12:48:25 +01:00
// public String createGenericResourceOLD(ISGenericResource genericResource) throws RemoteException {
// try {
// //maybe should add the ScopeProvider.instance.set(session.getScope()); here and not in the constructors.
// org.gcube.common.resources.gcore.GenericResource gCubeRes = Resources.unmarshal(org.gcube.common.resources.gcore.GenericResource.class, org.gcube.common.resources.gcore.GenericResource.class.getClassLoader().getResourceAsStream("generic.xml"));
// gCubeRes.profile().name(genericResource.getName());
// gCubeRes.profile().description(genericResource.getDescription());
// gCubeRes.profile().body().setTextContent(genericResource.getBody());
// gCubeRes.profile().type(genericResource.getSecondaryType());
// //maybe should change the visibility (into public) of addScope() within the Resource class of common-gcore-resources package in order to use it here.
// List<String> scopes=new ArrayList<String>();
// scopes.add(session.getScope());
// Resource res = scopedPublisher.create(gCubeRes, scopes);
// logger.info("Created Generic Resource with id: "+res.id()+" on scope: "+scopes.toString());
//
// //NIK-- 1/oct/2013 -- adding generic resource to cache
// logger.info("Adding it also in cache...");
// List<ISGenericResource> genRes = new ArrayList<ISGenericResource>();
// genRes.add(genericResource);
// QueryString query1 = new QueryString();
// query1.addParameter(CacheEntryConstants.id, genericResource.getId());
// query1.addParameter(CacheEntryConstants.vre, session.getScope());
// CachesManager.getInstance().getGenericResourceCache().put(new net.sf.ehcache.Element(query1, genRes));
// logger.info("...added in cache");
//
// return res.id();
// } catch (Exception e) {
// logger.error("Exception:", e);
// throw new RemoteException();
// }
// }
2012-09-12 12:47:59 +02:00
2014-01-28 13:21:08 +01:00
/ * *
2014-02-06 16:44:51 +01:00
* @return a list containing all generic resources
2014-01-28 13:21:08 +01:00
* @throws RemoteException when an error has occurred while communicating with IS
* /
public List < org . gcube . common . resources . gcore . GenericResource > getAllGenericResources ( ) throws RemoteException {
SimpleQuery queryMan = null ;
try {
queryMan = queryFor ( org . gcube . common . resources . gcore . GenericResource . class ) ;
return client . submit ( queryMan ) ;
} catch ( Exception e ) {
logger . error ( " Exception: " , e ) ;
throw new RemoteException ( ) ;
}
}
2012-09-12 12:47:59 +02:00
/ * *
2013-11-30 01:09:14 +01:00
* Updates a generic resource based on its ID
2012-09-12 12:47:59 +02:00
* @param genericResource the generic resource to be updated
* @throws RemoteException when an error has occurred while communicating with IS
* /
2013-08-22 12:28:07 +02:00
public void updateGenericResourceByID ( ISGenericResource genericResource ) throws RemoteException {
2012-09-12 12:47:59 +02:00
try {
2013-08-22 12:28:07 +02:00
//maybe should add the ScopeProvider.instance.set(session.getScope()); here and not in the constructors.
logger . info ( " Updating generic resource by ID " ) ;
2013-09-03 11:43:58 +02:00
SimpleQuery query = queryFor ( org . gcube . common . resources . gcore . GenericResource . class ) ;
2013-08-22 12:28:07 +02:00
query . addCondition ( " $resource/ID eq ' " + genericResource . getId ( ) + " ' " ) ;
2012-09-12 12:47:59 +02:00
2013-09-03 11:43:58 +02:00
List < org . gcube . common . resources . gcore . GenericResource > result = client . submit ( query ) ;
2012-09-12 12:47:59 +02:00
if ( result = = null | | result . size ( ) = = 0 ) {
2013-08-22 12:28:07 +02:00
logger . info ( " Error during updateGenericResourceByID: Couldn't find the resource to update " ) ;
2012-09-12 12:47:59 +02:00
return ;
}
2013-09-03 11:43:58 +02:00
org . gcube . common . resources . gcore . GenericResource gCubeRes = result . get ( 0 ) ;
2013-12-02 14:48:39 +01:00
logger . debug ( " Updating generic resource body from: " + gCubeRes . profile ( ) . bodyAsString ( ) ) ;
logger . debug ( " Updating generic resource body to: " + genericResource . getBody ( ) ) ;
2013-08-22 12:28:07 +02:00
gCubeRes . profile ( ) . name ( genericResource . getName ( ) ) ;
gCubeRes . profile ( ) . description ( genericResource . getDescription ( ) ) ;
2013-11-30 01:09:14 +01:00
gCubeRes . profile ( ) . newBody ( genericResource . getBody ( ) ) ;
2013-08-22 12:28:07 +02:00
gCubeRes . profile ( ) . type ( genericResource . getSecondaryType ( ) ) ;
2012-09-12 12:47:59 +02:00
try {
2013-11-30 01:09:14 +01:00
//SOS!!!! uncomment this and erase the two lines below
2012-09-12 12:47:59 +02:00
//publisher.updateGCUBEResource(gCubeRes, session.getScope(), new PortalSecurityManager(session));
2013-08-22 12:28:07 +02:00
List < String > scopes = new ArrayList < String > ( ) ;
for ( String scope : gCubeRes . scopes ( ) . asCollection ( ) )
scopes . add ( scope ) ;
2013-09-25 10:30:02 +02:00
// Resource res = scopedPublisher.update(gCubeRes, scopes);
Resource res = publisher . update ( gCubeRes ) ; //alternative of the above, if we don't want to define a scope -- better use scopedPublisher
2013-10-01 16:11:43 +02:00
logger . info ( " Updated generic resource on IS ! New ID: " + res . id ( ) ) ;
2012-09-12 12:47:59 +02:00
} catch ( Exception e ) {
2013-08-22 12:28:07 +02:00
logger . error ( " Exception during update of generic resource: " , e ) ;
2012-09-12 12:47:59 +02:00
}
QueryString query1 = new QueryString ( ) ;
2013-12-05 15:30:04 +01:00
query1 . addParameter ( CacheEntryConstants . name , genericResource . getName ( ) ) ;
query1 . addParameter ( CacheEntryConstants . vre , session . getScope ( ) ) ;
2012-09-12 12:47:59 +02:00
if ( CachesManager . getInstance ( ) . getGenericResourceCache ( ) . isElementInMemory ( query1 ) | | CachesManager . getInstance ( ) . getGenericResourceCache ( ) . isElementOnDisk ( query1 ) )
{
2013-12-05 15:30:04 +01:00
logger . info ( " Removing old generic resource from cache... " ) ;
2013-10-01 16:11:43 +02:00
CachesManager . getInstance ( ) . getGenericResourceCache ( ) . get ( query1 ) . setTimeToLive ( 0 ) ;
2012-09-12 12:47:59 +02:00
}
else {
2013-10-01 16:11:43 +02:00
logger . info ( " The generic resource to replace in cache was not found " ) ;
2012-09-12 12:47:59 +02:00
}
2013-12-05 15:30:04 +01:00
logger . info ( " ...and adding new generic resource in cache. " ) ;
QueryString query2 = new QueryString ( ) ;
2013-10-01 16:11:43 +02:00
List < ISGenericResource > genRes = new ArrayList < ISGenericResource > ( ) ;
genRes . add ( genericResource ) ;
2013-12-05 15:30:04 +01:00
query2 . addParameter ( CacheEntryConstants . id , genericResource . getId ( ) ) ;
query2 . addParameter ( CacheEntryConstants . vre , session . getScope ( ) ) ;
CachesManager . getInstance ( ) . getGenericResourceCache ( ) . put ( new net . sf . ehcache . Element ( query2 , genRes ) ) ;
2013-10-01 16:11:43 +02:00
2012-09-12 12:47:59 +02:00
} catch ( Exception e ) {
2013-02-22 17:52:54 +01:00
logger . error ( " Exception: " , e ) ;
2012-09-12 12:47:59 +02:00
throw new RemoteException ( ) ;
}
}
2013-08-22 12:28:07 +02:00
2012-09-12 12:47:59 +02:00
/ * *
* @return a list containing pairs of ( name , id ) of the available generic resources
* @throws RemoteException when an error has occurred while communicating with IS
* /
public List < Pair > getAvailableGenericResourceNames ( ) throws RemoteException {
2013-08-22 12:28:07 +02:00
//maybe should add the ScopeProvider.instance.set(session.getScope()); here and not in the constructors.
2012-09-12 12:47:59 +02:00
List < Pair > pairs = new ArrayList < Pair > ( ) ;
2013-08-22 12:28:07 +02:00
SimpleQuery queryMan = null ;
2012-09-12 12:47:59 +02:00
try {
2013-09-03 11:43:58 +02:00
queryMan = queryFor ( org . gcube . common . resources . gcore . GenericResource . class ) ;
List < org . gcube . common . resources . gcore . GenericResource > results = client . submit ( queryMan ) ;
for ( org . gcube . common . resources . gcore . GenericResource gr : results )
2013-08-22 12:28:07 +02:00
pairs . add ( new Pair ( gr . profile ( ) . name ( ) , gr . id ( ) ) ) ;
2012-09-12 12:47:59 +02:00
return pairs ;
} catch ( Exception e ) {
2013-02-22 17:52:54 +01:00
logger . error ( " Exception: " , e ) ;
2012-09-12 12:47:59 +02:00
throw new RemoteException ( ) ;
}
}
2014-02-18 10:37:01 +01:00
public List < ISGenericResource > getGenericResourcesByType ( String type ) throws RemoteException {
2014-02-20 15:45:58 +01:00
return getGenericResourcesByType ( session . getScope ( ) , type ) ;
}
public static List < ISGenericResource > getGenericResourcesByType ( String scope , String type ) throws RemoteException {
ScopeProvider . instance . set ( scope ) ;
2014-02-18 10:37:01 +01:00
List < ISGenericResource > output = new ArrayList < ISGenericResource > ( ) ;
SimpleQuery queryMan = null ;
try {
2014-02-20 15:45:58 +01:00
client = clientFor ( org . gcube . common . resources . gcore . GenericResource . class ) ;
2014-02-18 10:37:01 +01:00
queryMan = queryFor ( org . gcube . common . resources . gcore . GenericResource . class ) ;
queryMan . addCondition ( " $resource/Profile/SecondaryType eq ' " + type + " ' " ) ;
List < org . gcube . common . resources . gcore . GenericResource > results = client . submit ( queryMan ) ;
for ( org . gcube . common . resources . gcore . GenericResource res : results )
output . add ( new ISGenericResource ( res . id ( ) , res . profile ( ) . name ( ) , res . profile ( ) . description ( ) , res . profile ( ) . bodyAsString ( ) , res . profile ( ) . type ( ) ) ) ;
return output ;
} catch ( Exception e ) {
logger . error ( " Exception: " , e ) ;
throw new RemoteException ( ) ;
}
}
2014-02-20 15:45:58 +01:00
2012-09-12 12:47:59 +02:00
/ * *
* @param id the id of the generic resource
* @return a list containing the corresponding generic resources
* @throws RemoteException when an error has occurred while communicating with IS
* /
public List < ISGenericResource > getGenericResourceByID ( String id )
throws RemoteException {
QueryString query = new QueryString ( ) ;
query . put ( CacheEntryConstants . id , id ) ;
query . put ( CacheEntryConstants . vre , getDLName ( ) ) ;
return getGenericResource ( query ) ;
}
2014-02-06 16:44:51 +01:00
2012-09-12 12:47:59 +02:00
/ * *
* @param name the name of the generic resource
* @return a list containing the generic resources that have as name the given
* @throws RemoteException when an error has occurred while communicating with IS
* /
2014-02-06 16:44:51 +01:00
public List < ISGenericResource > getGenericResourceByName ( String name ) throws RemoteException {
//caching is done by (id,vre) as key pairs. so we cannot use cache to retrieve objects.
List < org . gcube . common . resources . gcore . GenericResource > allRes = getAllGenericResources ( ) ;
List < ISGenericResource > results = new ArrayList < ISGenericResource > ( ) ;
for ( org . gcube . common . resources . gcore . GenericResource res : allRes )
if ( res . profile ( ) . name ( ) . equalsIgnoreCase ( name ) )
results . add ( new ISGenericResource ( res . id ( ) , res . profile ( ) . name ( ) , res . profile ( ) . description ( ) , res . profile ( ) . bodyAsString ( ) , res . profile ( ) . type ( ) ) ) ;
return results ;
2012-09-12 12:47:59 +02:00
}
/ * *
* @return a list containing the generic resources that describe which collections are part of the active VRE as well as their hierarchical structure ( the name of this generic resource is " ScenarioCollectionInfo " )
* @throws RemoteException when an error has occurred while communicating with IS
* /
public List < ISGenericResource > getGenericResourceForScenario ( )
throws RemoteException {
QueryString query = new QueryString ( ) ;
query . put ( CacheEntryConstants . name , SessionConstants . ScenarioSchemaInfo ) ;
query . put ( CacheEntryConstants . vre , getDLName ( ) ) ;
return getGenericResource ( query ) ;
}
/ * *
* Removes an existing generic resource
* @param genericResource the generic resource to be removed
* @throws RemoteException when an error has occurred while communicating with IS
* /
2013-08-22 12:28:07 +02:00
public void removeGenericResource ( ISGenericResource genericResource ) throws RemoteException {
2012-09-12 12:47:59 +02:00
try {
2013-08-22 12:28:07 +02:00
//maybe should add the ScopeProvider.instance.set(session.getScope()); here and not in the constructors.
2013-09-03 11:43:58 +02:00
SimpleQuery query = queryFor ( org . gcube . common . resources . gcore . GenericResource . class ) ;
2013-08-22 12:28:07 +02:00
query . addCondition ( " $resource/ID eq ' " + genericResource . getId ( ) + " ' " ) ;
2013-09-03 11:43:58 +02:00
List < org . gcube . common . resources . gcore . GenericResource > result = client . submit ( query ) ;
2013-08-22 12:28:07 +02:00
if ( result = = null | | result . size ( ) = = 0 ) {
logger . error ( " Couldn't find the resource to delete! Returning... " ) ;
return ;
}
2013-09-03 11:43:58 +02:00
org . gcube . common . resources . gcore . GenericResource gCubeRes = result . get ( 0 ) ;
2013-08-22 12:28:07 +02:00
publisher = RegistryPublisherFactory . create ( ) ;
Resource res = publisher . remove ( gCubeRes ) ;
logger . info ( " Removed generic resource from IS " ) ;
QueryString queryString = new QueryString ( ) ;
queryString . addParameter ( CacheEntryConstants . id , genericResource . getId ( ) ) ;
2013-12-05 15:30:04 +01:00
queryString . addParameter ( CacheEntryConstants . vre , session . getScope ( ) ) ;
2013-08-22 12:28:07 +02:00
if ( CachesManager . getInstance ( ) . getGenericResourceCache ( ) . isElementInMemory ( queryString ) | | CachesManager . getInstance ( ) . getGenericResourceCache ( ) . isElementOnDisk ( queryString ) ) {
genericResource = ( ( List < ISGenericResource > ) CachesManager . getInstance ( ) . getGenericResourceCache ( ) . get ( queryString ) . getValue ( ) ) . get ( 0 ) ;
2013-02-25 17:32:15 +01:00
//TODO
2013-08-22 12:28:07 +02:00
CachesManager . getInstance ( ) . getGenericResourceCache ( ) . get ( queryString ) . setTimeToLive ( 0 ) ;
2012-09-12 12:47:59 +02:00
}
2013-12-05 15:30:04 +01:00
// queryString.clear();
// queryString.put(CacheEntryConstants.name, genericResource.getName());
// queryString.addParameter(CacheEntryConstants.vre, session.getScope());
// if(CachesManager.getInstance().getGenericResourceCache().isElementInMemory(queryString) || CachesManager.getInstance().getGenericResourceCache().isElementOnDisk(queryString)){
// //TODO
// CachesManager.getInstance().getGenericResourceCache().get(queryString).setTimeToLive(0);
// }
2013-08-22 12:28:07 +02:00
} catch ( PublisherException e ) {
2013-02-22 17:52:54 +01:00
logger . error ( " Exception: " , e ) ;
2012-09-12 12:47:59 +02:00
throw new RemoteException ( ) ;
}
}
/ * *
* Parses the xml and returns it as DOM object
* @param XMLdoc the xml as a string
* @return xml parsed as a Document
* /
static Document parseXMLFileToDOM ( String XMLdoc ) {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory . newInstance ( ) ;
DocumentBuilder builder ;
try {
builder = dbFactory . newDocumentBuilder ( ) ;
Document doc = builder . parse ( new InputSource ( new StringReader ( XMLdoc ) ) ) ;
return doc ;
} catch ( Exception e ) {
return null ;
}
}
/ * *
*
* @param xsltType Presentation or Metadata . The type of the xslt
* @return A vector which contains all the generic resources , xslts of this type . The xslts are sorted by the schema .
* /
public HashMap < String , Vector < String [ ] > > getAllXslts ( String xsltType ) {
HashMap < String , Vector < String [ ] > > schemas_xslts = new HashMap < String , Vector < String [ ] > > ( ) ;
Vector < String [ ] > xslts = null ;
List < String [ ] > result = null ;
try {
2013-09-03 11:43:58 +02:00
result = GenericResource . retrieveGenericResourcesFromNameParts ( xsltType , session . getScope ( ) ) ;
2012-09-12 12:47:59 +02:00
} catch ( Exception e ) {
2013-02-22 17:52:54 +01:00
logger . error ( " Exception: " , e ) ;
2012-09-12 12:47:59 +02:00
}
if ( result = = null ) {
return null ;
}
/ * if ( xsltType . equals ( " MetadataXSLT " ) ) {
for ( int i = 0 ; i < result . size ( ) ; i + + ) {
String fullName = result . get ( i ) [ 0 ] ;
String id = result . get ( i ) [ 1 ] ;
String tmp = fullName . substring ( xsltType . length ( ) + 1 , fullName . length ( ) ) ;
String parts [ ] = tmp . split ( " _ " ) ;
String schemaName = parts [ 0 ] ;
String xsltName = parts [ 1 ] ;
xslts = schemas_xslts . get ( schemaName . replaceFirst ( " %5f " , " _ " ) ) ;
if ( xslts = = null ) {
xslts = new Vector < String [ ] > ( ) ;
schemas_xslts . put ( schemaName . replaceFirst ( " %5f " , " _ " ) , xslts ) ;
}
// add the xslt name and id
String s [ ] = new String [ 2 ] ;
s [ 0 ] = xsltName ;
s [ 1 ] = id ;
xslts . add ( s ) ;
}
return schemas_xslts ;
} * /
// else {
// Presentation xslts
2013-02-22 17:52:54 +01:00
logger . info ( " Inside getAllXslts " ) ;
logger . info ( " The type is: " + xsltType ) ;
2012-09-12 12:47:59 +02:00
for ( int i = 0 ; i < result . size ( ) ; i + + ) {
String fullName = result . get ( i ) [ 0 ] ;
2013-02-22 17:52:54 +01:00
logger . info ( " fullname: " + fullName ) ;
2012-09-12 12:47:59 +02:00
String id = result . get ( i ) [ 1 ] ;
2013-02-22 17:52:54 +01:00
logger . info ( " id: " + id ) ;
2012-09-12 12:47:59 +02:00
// String tmp = fullName.substring(xsltType.length()+1,fullName.length());
String tmp = fullName . substring ( xsltType . length ( ) + 3 , fullName . length ( ) ) ;
2013-02-22 17:52:54 +01:00
logger . info ( " tmp: " + tmp ) ;
2012-09-12 12:47:59 +02:00
String parts [ ] = tmp . split ( " - \\ |- " ) ;
String schemaName = parts [ 0 ] ;
for ( int k = 0 ; k < parts . length ; k + + ) {
2013-02-22 17:52:54 +01:00
logger . info ( " part: " + parts [ k ] ) ;
2012-09-12 12:47:59 +02:00
}
2013-02-22 17:52:54 +01:00
logger . info ( " schemaName: " + schemaName ) ;
2012-09-12 12:47:59 +02:00
String xsltName = parts [ 1 ] ;
2013-02-22 17:52:54 +01:00
logger . info ( " xsltName: " + xsltName ) ;
2012-09-12 12:47:59 +02:00
xslts = schemas_xslts . get ( schemaName ) ;
if ( xslts = = null ) {
xslts = new Vector < String [ ] > ( ) ;
schemas_xslts . put ( schemaName , xslts ) ;
}
// add the xslt name and id
String s [ ] = new String [ 2 ] ;
s [ 0 ] = xsltName ;
s [ 1 ] = id ;
xslts . add ( s ) ;
}
return schemas_xslts ;
// }
}
2013-08-22 12:28:07 +02:00
private static List < String [ ] > retrieveGenericResourcesFromNameParts ( String nameParts , String scope ) throws Exception {
ScopeProvider . instance . set ( scope ) ;
2013-10-09 16:57:36 +02:00
logger . debug ( " retrieveGenericResourcesFromNameParts " ) ;
2013-08-22 12:28:07 +02:00
SimpleQuery query = queryFor ( GenericResource . class ) ;
//READ COMMENT BELOW !!!
query . addCondition ( " $resource/Profile/Name eq ' " + nameParts + " ' " ) ;
// on the above, we check if it is equal. what we want in order to have the previous functionality is to seaarch "like", not "eq"
2013-09-03 11:43:58 +02:00
List < org . gcube . common . resources . gcore . GenericResource > queryResults = null ;
2013-08-22 12:28:07 +02:00
queryResults = client . submit ( query ) ;
if ( queryResults = = null | | queryResults . size ( ) = = 0 ) {
logger . error ( " Couldn't find the resource from name! Returning null " ) ;
return null ;
}
List < String [ ] > results = new LinkedList < String [ ] > ( ) ;
2013-09-03 11:43:58 +02:00
for ( org . gcube . common . resources . gcore . GenericResource gr : queryResults ) {
2013-08-22 12:28:07 +02:00
String [ ] nameAndID = { gr . profile ( ) . name ( ) , gr . id ( ) } ;
results . add ( nameAndID ) ;
}
return results ;
2014-01-28 12:48:25 +01:00
}
2012-09-12 12:47:59 +02:00
}