@ -3,17 +3,20 @@ package org.gcube.data.spd.utils;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor ;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor ;
import java.io.File ;
import java.net.URISyntaxException ;
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.PreparedStatement ;
import java.sql.SQLException ;
import java.util.Collection ;
import java.util. Date ;
import java.util. GregorianCalendar ;
import java.util.List ;
import java.util.Set ;
import java.util.UUID ;
import java.util.stream.Collectors ;
import org.gcube.common.encryption. StringEncrypter;
import org.gcube.common.encryption. encrypter. StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint ;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint ;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property ;
@ -22,19 +25,23 @@ import org.gcube.data.spd.model.service.types.MetadataDetails;
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery ;
import org.gcube.spatial.data.geonetwork.LoginLevel ;
import org.gcube.spatial.data.geonetwork.iso.GcubeISOMetadata ;
import org.gcube.spatial.data.geonetwork.iso.MissingInformationException ;
import org.gcube.spatial.data.geonetwork.iso.Thesaurus ;
import org.gcube.spatial.data.geonetwork.iso.tpl.ISOMetadataByTemplate ;
import org.gcube.spatial.data.geonetwork.iso.tpl.MetadataDescriptor ;
import org.gcube.spatial.data.geonetwork.iso.tpl.codelists.ResponsiblePartyRole ;
import org.gcube.spatial.data.geonetwork.iso.tpl.codelists.RestrictionCode ;
import org.gcube.spatial.data.geonetwork.iso.tpl.constraints.LegalConstraints ;
import org.gcube.spatial.data.geonetwork.iso.tpl.constraints.ResourceConstraints ;
import org.gcube.spatial.data.geonetwork.iso.tpl.extent.BoundingBox ;
import org.gcube.spatial.data.geonetwork.iso.tpl.keys.KeywordSet ;
import org.gcube.spatial.data.geonetwork.iso.tpl.parties.ResponsibleParty ;
import org.gcube.spatial.data.geonetwork.iso.tpl.spatial.VectorRepresentation ;
import org.gcube.spatial.data.geonetwork.model.faults.EncryptionException ;
import org.gcube.spatial.data.gis.GISInterface ;
import org.gcube.spatial.data.gis.model.report.PublishResponse ;
import org.gcube.spatial.data.gis.model.report.Report.OperationState ;
import org.geotoolkit.metadata.iso.extent.DefaultExtent ;
import org.geotoolkit. xml.XML ;
import org.opengis.metadata.Metadata ;
import org.opengis.metadata.citation.PresentationForm ;
import org.opengis.metadata.identification.TopicCategory ;
import org.opengis.metadata.spatial.GeometricObjectType ;
import org.opengis.metadata.spatial.TopologyLevel ;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder ;
import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder ;
@ -110,11 +117,12 @@ public class MapUtils {
log . trace ( "Generating layer by points" ) ;
tableName = createPointTable ( db , points ) ;
log . debug ( "Created table {} in {} " , tableName , db ) ;
PublishResponse resp = createLayer ( layerOptions , metadata , tableName );
PublishResponse resp = createLayer ( layerOptions , metadata , tableName , points . size ( ) );
log . debug ( "Publish response output {} " , resp ) ;
if ( ! resp . getDataOperationResult ( ) . equals ( OperationState . COMPLETE ) ) {
throw new Exception ( "Erors while publishing layer. Messages are : "+ resp . getDataOperationMessages ( ) ) ;
throw new Exception ( "Erors while publishing layer. Messages are : Operation errors "+ resp . getDataOperationMessages ( ) + " Metadata errors " + resp . getMetaOperationMessages ( ) ) ;
} else if ( ! resp . getMetaOperationResult ( ) . equals ( OperationState . COMPLETE ) ) {
throw new Exception ( "Erors while publishing layer metadata. Messages are : " + resp . getMetaOperationMessages ( ) ) ;
} else {
@ -206,7 +214,7 @@ public class MapUtils {
}
private static final PublishResponse createLayer ( LayerCreationOptions layerOpt , MetadataDetails details , String tableName ) throws URISyntaxException , MissingInformationException , Exception {
private static final PublishResponse createLayer ( LayerCreationOptions layerOpt , MetadataDetails details , String tableName , int pointsCount ) throws URISyntaxException , MissingInformationException , Exception {
GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder ( ) ;
fte . setEnabled ( true ) ;
@ -222,8 +230,11 @@ public class MapUtils {
le . setEnabled ( true ) ;
log . debug ( "Generating meta for layer table {}. Meta parameters are {}" , tableName , details ) ;
Metadata meta = fillMeta ( details ) . getMetadata ( ) ;
MetadataDescriptor metaDescr = fillMeta ( details , pointsCount ) ;
File metaFile = ISOMetadataByTemplate . createXML ( metaDescr ) ;
Metadata meta = ( Metadata ) XML . unmarshal ( metaFile ) ;
GISInterface gis = GISInterface . get ( ) ;
@ -238,28 +249,39 @@ public class MapUtils {
private static GcubeISOMetadata fillMeta ( MetadataDetails metaDetails ) throws Exception {
GcubeISOMetadata meta = new GcubeISOMetadata ( ) ;
private static MetadataDescriptor fillMeta ( MetadataDetails metaDetails , int pointsCount ) throws Exception {
MetadataDescriptor meta = new MetadataDescriptor ( ) ;
meta . setUUIDIdentifier ( UUID . randomUUID ( ) . toString ( ) ) ;
meta . setAbstractField ( metaDetails . getAbstractField ( ) ) ;
meta . setCreationDate ( new Date ( System . currentTimeMillis ( ) ) ) ;
meta . setExtent ( ( DefaultExtent ) DefaultExtent . WORLD ) ;
meta . setGeometricObjectType ( GeometricObjectType . SURFACE ) ;
meta . setPresentationForm ( PresentationForm . MAP_DIGITAL ) ;
meta . setCreationTime ( new GregorianCalendar ( ) . getTime ( ) ) ;
meta . setPurpose ( metaDetails . getPurpose ( ) ) ;
meta . setResolution ( 0.5d ) ;
meta . getExtent ( ) . addGeographicExtent ( BoundingBox . WORLD_EXTENT ) ;
//Setting Spatial Representation
VectorRepresentation representation = new VectorRepresentation (
org . gcube . spatial . data . geonetwork . iso . tpl . codelists . TopologyLevel . GEOMETRY_ONLY , pointsCount , org . gcube . spatial . data . geonetwork . iso . tpl . codelists . GeometricObjectType . SURFACE ) ;
meta . setSpatialRepresentation ( representation ) ;
meta . addResponsibleParty ( new ResponsibleParty ( metaDetails . getAuthor ( ) , "D4Science" ,
ResponsiblePartyRole . AUTHOR ) ) ;
//Setting the Lineage Statement
meta . setLineageStatement ( "this layer has been created using different occurrence sources via Species product Discovery service" ) ;
meta . setSpatialResolution ( 0.5d ) ;
meta . setTitle ( metaDetails . getTitle ( ) ) ;
meta . setTopologyLevel ( TopologyLevel . GEOMETRY_ONLY ) ;
meta . setUser ( metaDetails . getAuthor ( ) ) ;
meta . setConstraints ( new ResourceConstraints ( "This product has been generated by aggregating and harmonizing data that are freely accessible" , new LegalConstraints ( RestrictionCode . LICENSE , "CC-BY-SA" ) , new LegalConstraints ( RestrictionCode . LICENSE , "CC-BY-SA" ) ) ) ;
meta . addCredits ( metaDetails . getCredits ( ) ) ;
List < String > keywords = metaDetails . getKeywords ( ) ;
if ( keywords ! = null & & ! keywords . isEmpty ( ) ) {
Thesaurus generalThesaurus = meta . getConfig ( ) . getThesauri ( ) . get ( "General" ) ;
for ( String key : keywords )
meta . addKeyword ( key , generalThesaurus ) ;
Set< String > keyWordSet = keywords . stream ( ) . collect ( Collectors . toSet ( ) ) ;
KeywordSet set = new KeywordSet ( keyWordSet ) ;
meta . addKeywordSet ( set ) ;
}
meta . addTopicCategory ( TopicCategory . BIOTA ) ;
meta . addTopicCategory ( org. gcube . spatial . data . geonetwork . iso . tpl . codelists . TopicCategory. BIOTA ) ;
return meta ;
}