2016-06-07 18:29:19 +02:00
package org.gcube.datacatalogue.metadatadiscovery.reader ;
import static org.gcube.resources.discovery.icclient.ICFactory.client ;
import java.io.StringReader ;
import java.util.ArrayList ;
import java.util.List ;
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
2017-03-24 16:05:52 +01:00
import javax.xml.xpath.XPath ;
import javax.xml.xpath.XPathConstants ;
import javax.xml.xpath.XPathExpression ;
import javax.xml.xpath.XPathFactory ;
2016-06-07 18:29:19 +02:00
import org.gcube.common.resources.gcore.utils.XPathHelper ;
import org.gcube.common.scope.impl.ScopeBean ;
2017-03-24 16:05:52 +01:00
import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile ;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat ;
2016-06-07 18:29:19 +02:00
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube.resources.discovery.client.queries.api.Query ;
import org.gcube.resources.discovery.client.queries.impl.QueryBox ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2017-03-24 16:05:52 +01:00
import org.w3c.dom.Document ;
2016-06-07 18:29:19 +02:00
import org.w3c.dom.Node ;
import org.xml.sax.InputSource ;
2016-06-08 11:36:32 +02:00
2016-06-07 18:29:19 +02:00
/ * *
2016-06-08 11:36:32 +02:00
* The Class MedataFormatDiscovery .
2016-06-07 18:29:19 +02:00
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
2016-06-08 11:36:32 +02:00
* Jun 8 , 2016
2016-06-07 18:29:19 +02:00
* /
2017-03-24 12:43:57 +01:00
public class MetadataFormatDiscovery {
2016-06-07 18:29:19 +02:00
2017-03-24 12:43:57 +01:00
private static Logger logger = LoggerFactory . getLogger ( MetadataFormatDiscovery . class ) ;
2016-06-07 18:29:19 +02:00
protected static final String DATA_CATALOGUE_METADATA_SECONDARY_TYPE = " DataCatalogueMetadata " ;
private String secondaryType ;
private ScopeBean scope ;
2017-03-24 16:05:52 +01:00
private List < MetadataProfile > metadataProfiles ;
2016-06-07 18:29:19 +02:00
/ * *
* Instantiates a new medata format reader .
*
* @param scope - the scope to be searched
* @throws Exception the exception
* /
2017-03-24 12:43:57 +01:00
public MetadataFormatDiscovery ( ScopeBean scope ) throws Exception {
2016-06-07 18:29:19 +02:00
this . scope = scope ;
this . secondaryType = DATA_CATALOGUE_METADATA_SECONDARY_TYPE ;
2017-03-24 16:05:52 +01:00
this . metadataProfiles = readMetadataProfilesFromInfrastrucure ( ) ;
2016-06-07 18:29:19 +02:00
}
/ * *
* this method looks up the applicationProfile profile among the ones available in the infrastructure .
*
* @return the applicationProfile profile
* @throws Exception the exception
* /
2017-03-24 16:05:52 +01:00
private List < MetadataProfile > readMetadataProfilesFromInfrastrucure ( ) throws Exception {
2016-06-07 18:29:19 +02:00
logger . trace ( " read secondary type: " + secondaryType ) ;
if ( this . scope = = null )
throw new Exception ( " Scope is null " ) ;
String scopeString = this . scope . toString ( ) ;
logger . trace ( " read scope: " + scopeString ) ;
2017-03-24 16:05:52 +01:00
List < MetadataProfile > list = new ArrayList < MetadataProfile > ( ) ;
2016-06-07 18:29:19 +02:00
try {
2016-06-13 16:42:33 +02:00
// ScopeProvider.instance.set(scopeString);
// logger.info("scope provider set instance: "+scopeString);
// ScopeProvider.instance.set(scopeString);
logger . info ( " Using scope from ScopeProvider: " + scopeString ) ;
2016-06-07 18:29:19 +02:00
String queryString = getGcubeGenericQueryString ( secondaryType ) ;
logger . trace ( " queryString: " + queryString ) ;
Query q = new QueryBox ( queryString ) ;
DiscoveryClient < String > client = client ( ) ;
2017-03-24 16:05:52 +01:00
List < String > metaProfile = client . submit ( q ) ;
2016-06-07 18:29:19 +02:00
2017-03-24 16:05:52 +01:00
if ( metaProfile = = null | | metaProfile . size ( ) = = 0 )
throw new MetadataProfileNotFoundException ( " Resource/s with secondaryType: " + secondaryType + " is/are not registered in the scope: " + scopeString ) ;
2016-06-07 18:29:19 +02:00
else {
2017-03-24 16:05:52 +01:00
for ( String elem : metaProfile ) {
2016-06-07 18:29:19 +02:00
try {
DocumentBuilder docBuilder = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) ;
2017-03-24 16:05:52 +01:00
Document doc = docBuilder . parse ( new InputSource ( new StringReader ( elem ) ) ) ;
Node node = doc . getDocumentElement ( ) ;
2016-06-07 18:29:19 +02:00
XPathHelper helper = new XPathHelper ( node ) ;
2017-03-24 16:05:52 +01:00
MetadataProfile meta = getMetadataProfileFromResource ( doc , helper ) ;
2016-06-07 18:29:19 +02:00
list . add ( meta ) ;
} catch ( Exception e ) {
2017-03-24 16:05:52 +01:00
logger . error ( " Error while trying to fetch resource with secondary type " + secondaryType + " from the infrastructure, scope: " + scopeString , e ) ;
2016-06-07 18:29:19 +02:00
}
}
}
} catch ( Exception e ) {
logger . error ( " Error while trying to fetch applicationProfile " + secondaryType + " from the infrastructure, " + e ) ;
return list ;
}
return list ;
}
/ * *
* Gets the metadata type from resource .
*
* @param helper the helper
* @return the metadata type from resource
2017-03-24 16:05:52 +01:00
* @throws MetadataProfileNotFoundException the application profile not found exception
2016-06-07 18:29:19 +02:00
* /
2017-03-24 16:05:52 +01:00
private MetadataProfile getMetadataProfileFromResource ( Document doc , XPathHelper helper ) throws MetadataProfileNotFoundException {
2016-06-07 18:29:19 +02:00
try {
2017-03-24 16:05:52 +01:00
MetadataProfile metadataProfile = new MetadataProfile ( ) ;
2016-06-07 18:29:19 +02:00
List < String > id = helper . evaluate ( " /Resource/ID/text() " ) ;
if ( id = = null | | id . isEmpty ( ) )
2017-03-24 16:05:52 +01:00
throw new MetadataProfileNotFoundException ( " Resource ID not found for " + helper . toString ( ) ) ;
2016-06-07 18:29:19 +02:00
else {
2017-03-24 16:05:52 +01:00
metadataProfile . setId ( id . get ( 0 ) ) ;
2016-06-07 18:29:19 +02:00
}
List < String > name = helper . evaluate ( " /Resource/Profile/Name/text() " ) ;
if ( name = = null | | name . isEmpty ( ) )
2017-03-24 16:05:52 +01:00
throw new MetadataProfileNotFoundException ( " Resource Name not found for Resource Id: " + metadataProfile . getId ( ) ) ;
2016-06-07 18:29:19 +02:00
else
2017-03-24 16:05:52 +01:00
metadataProfile . setName ( name . get ( 0 ) ) ;
2016-06-07 18:29:19 +02:00
List < String > description = helper . evaluate ( " /Resource/Profile/Description/text() " ) ;
if ( description = = null | | description . isEmpty ( ) )
2017-03-24 16:05:52 +01:00
logger . info ( " Description not found for Resource ID: " + metadataProfile . getId ( ) + " , continuing.. " ) ;
2016-06-07 18:29:19 +02:00
else
2017-03-24 16:05:52 +01:00
metadataProfile . setDescription ( description . get ( 0 ) ) ;
2016-06-07 18:29:19 +02:00
2017-03-24 16:05:52 +01:00
XPathFactory xpathFactory = XPathFactory . newInstance ( ) ;
XPath xpath = xpathFactory . newXPath ( ) ;
XPathExpression expr = xpath . compile ( " /Resource/Profile/Body/metadataformat/@ " + MetadataFormat . LOCAL_NAME_METADATA_TYPE ) ;
String metadataType = ( String ) expr . evaluate ( doc , XPathConstants . STRING ) ;
if ( metadataType = = null | | metadataType . isEmpty ( ) )
throw new MetadataProfileNotFoundException ( " Required attribute ' " + MetadataFormat . LOCAL_NAME_METADATA_TYPE + " ' not found in the element ' " + MetadataFormat . LOCAL_NAME_METADATA_FORMAT + " ' for Metadata Profile (with Resource) Id: " + metadataProfile . getId ( ) ) ;
else
metadataProfile . setMetadataType ( metadataType ) ;
return metadataProfile ;
2016-06-07 18:29:19 +02:00
} catch ( Exception e ) {
2017-03-24 16:05:52 +01:00
logger . error ( " An error occurred in getMetadataProfileFromResource " , e ) ;
2016-06-07 18:29:19 +02:00
return null ;
}
}
/ * *
2016-06-08 11:36:32 +02:00
* Gets the gcube generic query string .
2016-06-07 18:29:19 +02:00
*
* @param secondaryType the secondary type
2016-06-08 11:36:32 +02:00
* @return the gcube generic query string
2016-06-07 18:29:19 +02:00
* /
public synchronized String getGcubeGenericQueryString ( String secondaryType ) {
return " for $profile in collection('/db/Profiles/GenericResource')//Resource " +
" where $profile/Profile/SecondaryType/string() eq ' " + secondaryType + " ' " +
" return $profile " ;
}
/ * *
* Gets the secondary type .
*
* @return the secondary type
* /
public String getSecondaryType ( ) {
return secondaryType ;
}
/ * *
* Gets the scope .
*
* @return the scope
* /
public ScopeBean getScope ( ) {
return scope ;
}
/ * *
* Gets the metadata types .
*
* @return the metadataTypes
* /
2017-03-24 16:05:52 +01:00
public List < MetadataProfile > getMetadataProfiles ( ) {
2016-06-07 18:29:19 +02:00
2017-03-24 16:05:52 +01:00
return metadataProfiles ;
2016-06-07 18:29:19 +02:00
}
}