@ -6,11 +6,9 @@ package org.gcube.datacatalogue.metadatadiscovery.reader;
import static org.gcube.resources.discovery.icclient.ICFactory.client ;
import java.io.ByteArrayInputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.StringReader ;
import java.util.List ;
import java.util.Properties ;
import javax.xml.bind.JAXBContext ;
import javax.xml.bind.Unmarshaller ;
@ -37,13 +35,12 @@ import org.xml.sax.InputSource;
* /
public class NamespaceCategoryReader {
/ * *
*
* /
public static final String NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES = "NamespacesCatalogueCategories.properties" ;
private static Logger logger = LoggerFactory . getLogger ( NamespaceCategoryReader . class ) ;
private static final String GENERIC_RESOURCE_NAMESPACES_NAME = "Namespaces Catalogue Categories" ;
private static final String GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE = "DataCatalogueNamespace" ;
private ScopeBean scope ;
private String resourceID ;
private NamespaceCategories namespaces ;
@ -51,16 +48,11 @@ public class NamespaceCategoryReader {
* Instantiates a new medata format reader .
*
* @param scope the scope
* @param resourceID the resource id
* @throws Exception the exception
* /
public NamespaceCategoryReader ( ScopeBean scope , String resourceID ) throws Exception {
public NamespaceCategoryReader ( ScopeBean scope ) throws Exception {
this . scope = scope ;
this . resourceID = resourceID ;
if ( resourceID = = null | | resourceID . isEmpty ( ) )
throw new Exception ( "Please, specify a valid ResourceID in the property file: " + NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES ) ;
readNamespaceCategoryByResourceID ( ) ;
readNamespaceCategory ( ) ;
}
/ * *
@ -69,8 +61,8 @@ public class NamespaceCategoryReader {
* @return the metadata format by id
* @throws Exception the exception
* /
private void readNamespaceCategory ByResourceID ( ) throws Exception {
logger . trace ( "Getting Namespace Category for resourceID: "+ resourceID ) ;
private void readNamespaceCategory ( ) throws Exception {
logger . trace ( "Getting Namespace Category ") ;
if ( this . scope = = null )
throw new Exception ( "Scope is null" ) ;
@ -84,26 +76,23 @@ public class NamespaceCategoryReader {
logger . info ( "Using scope from ScopeProvider: " + scopeString ) ;
String queryString = QueryForResourceUtil . queryForGenericResourceById( resourceID ) ;
String queryString = QueryForResourceUtil . getGcubeGenericQueryStringForSecondaryTypeAndName( GENERIC_RESOURCE_NAMESPACES_NAME , GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE ) ;
logger . trace ( "queryString: " + queryString ) ;
Query q = new QueryBox ( queryString ) ;
DiscoveryClient < String > client = client ( ) ;
List < String > appProfile = client . submit ( q ) ;
List < String > appProfile = client . submit ( q ) ;
if ( appProfile = = null | | appProfile . size ( ) = = 0 )
throw new MetadataProfileNotFoundException ( "Your resource ID "+ resourceID + " is not registered in the infrastructure, the scope is "+ scopeString ) ;
throw new MetadataProfileNotFoundException ( "Your resource is not registered in the infrastructure, the scope is "+ scopeString ) ;
else {
String theResource = null ;
try {
theResource = appProfile . get ( 0 ) ;
// logger.trace("Resource with resourceID "+resourceID+" matched "+theResource);
logger . debug ( "Resource (Namespaces Catalogue Categories) within resourceID " + resourceID + " found" ) ;
logger . debug ( "Resource (Namespaces Catalogue Categories) found" ) ;
DocumentBuilder docBuilder = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) ;
Node node = docBuilder . parse ( new InputSource ( new StringReader ( theResource ) ) ) . getDocumentElement ( ) ;
XPathHelper helper = new XPathHelper ( node ) ;
// List<MetadataField> fields = getMetadataFieldsFromResource(helper);
// mf.setMetadataFields(fields);
logger . debug ( "Unmarshalling it.." ) ;
readNamespaceCategoryFromResource ( helper ) ;
} catch ( Exception e ) {
@ -112,10 +101,11 @@ public class NamespaceCategoryReader {
}
} catch ( Exception e ) {
logger . error ( "Error while trying to fetch resourceID " + resourceID + " from the infrastructure, the scope is " + scopeString , e ) ;
throw new Exception ( "Error while trying to fetch resourceID " + resourceID + " from the infrastructure, the scope is " + scopeString , e ) ;
logger . error ( "Error while trying to fetch resource with name " + GENERIC_RESOURCE_NAMESPACES_NAME + " and secondary type " + GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE +
" from the infrastructure, the scope is " + scopeString , e ) ;
throw new Exception ( "Error while trying to fetch resource with name " + GENERIC_RESOURCE_NAMESPACES_NAME + " and secondary type " + GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE +
" from the infrastructure, the scope is " + scopeString , e ) ;
} finally {
// ScopeProvider.instance.reset();
}
}
@ -133,13 +123,13 @@ public class NamespaceCategoryReader {
List < String > namespaceSources = helper . evaluate ( "/Resource/Profile/Body/namespaces" ) ;
JAXBContext jaxbContext = JAXBContext . newInstance ( NamespaceCategories . class ) ;
Unmarshaller jaxbUnmarshaller = jaxbContext . createUnmarshaller ( ) ;
Unmarshaller jaxbUnmarshaller = jaxbContext . createUnmarshaller ( ) ;
if ( namespaceSources = = null | | namespaceSources . size ( ) = = 0 ) {
throw new Exception ( "Resource does not contain <namespaces> <namespace>...</namespace> </namespaces> in the body" ) ;
}
if ( namespaceSources = = null | | namespaceSources . size ( ) = = 0 ) {
throw new Exception ( "Resource does not contain <namespaces> <namespace>...</namespace> </namespaces> in the body" ) ;
}
InputStream stream = new ByteArrayInputStream ( namespaceSources . get ( 0 ) . getBytes ( ) ) ;
InputStream stream = new ByteArrayInputStream ( namespaceSources . get ( 0 ) . getBytes ( ) ) ;
namespaces = ( NamespaceCategories ) jaxbUnmarshaller . unmarshal ( stream ) ;
} catch ( Exception e ) {
@ -149,31 +139,6 @@ public class NamespaceCategoryReader {
}
}
/ * *
* Namespaces catalogue categories resource id .
*
* @return the string
* @throws PropertyFileNotFoundException the property file not found exception
* /
public static String NamespacesCatalogueCategoriesResourceID ( ) throws PropertyFileNotFoundException {
Properties prop = new Properties ( ) ;
try {
InputStream in = NamespaceCategoryReader . class . getResourceAsStream ( NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES ) ;
// load a properties file
prop . load ( in ) ;
return prop . getProperty ( "RESOURCE_ID" ) ;
} catch ( IOException e ) {
logger . error ( "An error occurred on read property file " + NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES , e ) ;
throw new PropertyFileNotFoundException ( "An error occurred on read property file " + NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES + ": " + e ) ;
}
}
/ * *
* Gets the namespaces .
*
@ -193,8 +158,6 @@ public class NamespaceCategoryReader {
StringBuilder builder = new StringBuilder ( ) ;
builder . append ( "NamespaceCategoryReader [scope=" ) ;
builder . append ( scope ) ;
builder . append ( ", resourceID=" ) ;
builder . append ( resourceID ) ;
builder . append ( ", namespaces=" ) ;
builder . append ( namespaces ) ;
builder . append ( "]" ) ;