2019-04-01 15:30:16 +02:00
package org.gcube.data.publishing.gCatFeeder.collectors.dm.model.ckan ;
import java.io.ByteArrayOutputStream ;
import java.util.ArrayList ;
2019-04-02 17:46:32 +02:00
import org.gcube.data.publishing.gCatFeeder.collectors.dm.DataMinerCollectorProperties ;
2019-04-01 15:30:16 +02:00
import org.gcube.data.publishing.gCatFeeder.collectors.dm.model.InternalAlgorithmDescriptor ;
2019-04-03 15:59:18 +02:00
import org.gcube.data.publishing.gCatFeeder.collectors.dm.model.Parameter ;
import org.gcube.data.publishing.gCatFeeder.collectors.dm.model.UserIdentity ;
2019-04-01 15:30:16 +02:00
import org.gcube.data.publishing.gCatFeeder.model.CatalogueFormatData ;
import org.gcube.data.publishing.gCatFeeder.model.InternalConversionException ;
import org.gcube.data.publishing.gCatFeeder.utils.ContextUtils ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import lombok.Getter ;
import lombok.NoArgsConstructor ;
import lombok.Setter ;
@NoArgsConstructor
@Getter
@Setter
public class GCatModel implements CatalogueFormatData {
private static ObjectMapper mapper = new ObjectMapper ( ) ;
private static String profileXML = null ;
2019-04-03 15:59:18 +02:00
private static String profileID = DataMinerCollectorProperties . getProperty ( DataMinerCollectorProperties . CKAN_RESOURCE_TYPE ) ;
2019-04-01 15:30:16 +02:00
public static void setProfile ( String toSet ) {
profileXML = toSet ;
}
public GCatModel ( InternalAlgorithmDescriptor desc ) {
item = new CkanItem ( ) ;
2019-04-03 15:59:18 +02:00
// item.setAuthor(desc.getAuthor());
2019-04-01 15:30:16 +02:00
item . setDescription ( desc . getDescription ( ) ) ;
item . setTitle ( desc . getName ( ) + " in " + ContextUtils . getCurrentScopeName ( ) ) ;
item . setLicense ( " CC-BY-NC-SA-4.0 " ) ;
2019-04-03 15:59:18 +02:00
// item.setMaintainer(desc.getMaintainer());
2019-04-01 15:30:16 +02:00
item . setName ( item . getTitle ( ) . toLowerCase ( ) . toLowerCase ( ) . replaceAll ( " " , " _ " ) ) ;
for ( String tag : desc . getTags ( ) ) {
item . getTags ( ) . add ( fixTag ( tag ) ) ;
}
2019-04-02 19:31:57 +02:00
item . getTags ( ) . add ( ContextUtils . getCurrentScopeName ( ) ) ;
item . getTags ( ) . add ( " WPS " ) ;
item . getTags ( ) . add ( " Analytics " ) ;
2019-04-03 15:59:18 +02:00
item . getExtras ( ) . add ( new CKanExtraField ( " system " , profileID ) ) ;
2019-04-02 17:46:32 +02:00
item . setVisibility ( desc . getVisibility ( ) ) ;
2019-04-01 16:23:05 +02:00
// TODO ADD PARAMETERS
2019-04-01 16:56:06 +02:00
for ( Parameter param : desc . getParameters ( ) )
2019-04-03 15:59:18 +02:00
item . getExtras ( ) . add ( new CKanExtraField ( profileID + " :Input Parameter " ,
2019-04-01 16:56:06 +02:00
String . format ( " %1$s [%2$s] %3$s : %4$s " ,
param . getName ( ) , param . getType ( ) ,
( ( param . getValue ( ) ! = null & & ! param . getValue ( ) . isEmpty ( ) ) ? " default : " + param . getValue ( ) : " " ) ,
param . getDescription ( ) ) ) ) ;
2019-04-03 15:59:18 +02:00
UserIdentity author = desc . getAuthor ( ) ;
// item.getExtras().add(new CKanExtraField(profileID+":Process Author",
// String.format("%1$s, %2$s ", args)))
2019-04-01 16:23:05 +02:00
// TODO ADD RESOURCES
2019-04-02 19:31:57 +02:00
if ( desc . getGuiLink ( ) ! = null )
resources . add ( new CkanResource ( " Gateway Link " , desc . getGuiLink ( ) , " Link to the GUI designed to operate with DataMiner " ) ) ;
if ( desc . getWpsLink ( ) ! = null )
resources . add ( new CkanResource ( " WPS Link " , desc . getWpsLink ( ) , " WPS Link to the " + DataMinerCollectorProperties . getProperty ( DataMinerCollectorProperties . CKAN_RESOURCE_TYPE ) ) ) ;
2019-04-01 16:23:05 +02:00
2019-04-01 15:30:16 +02:00
}
private String profile = profileXML ;
private CkanItem item = null ;
private ArrayList < CkanResource > resources = new ArrayList < > ( ) ;
@Override
public String toCatalogueFormat ( ) throws InternalConversionException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ;
mapper . writeValue ( baos , this ) ;
return baos . toString ( ) ;
} catch ( Throwable t ) {
throw new InternalConversionException ( " Unable to convert " , t ) ;
}
}
/ * *
* ( Common ) Title
* ( Common ) Description
* ( Common ) Tags : free list of keywords
* ( Common ) License
* ( Common ) Visibility : either public or private
* ( Common ) Version
* ( Common ) Author : the creator of metadata . Only one occurrence is supported ;
* ( Common ) Maintainer :
* ( Method specific ) Creator : the author of the method ( with email and ORCID ) . Repeatable field ;
* ( Method specific ) Creation date : when the method has been released ;
* ( Method specific ) Input : Repeatable field ;
* ( Method specific ) Output : Repeatable field ;
* ( Method specific ) RelatedPaper : a reference to an associated paper ;
* ( Method specific ) Restrictions On Use : an optional text
* ( Method specific ) Attribution requirements : the text to use to acknowledge method usage ;
* /
static final String fixTag ( String toFix ) {
String fixedTag = toFix . replaceAll ( " : " , " " ) .
replaceAll ( " [ \\ ( \\ ) \\ \\ /] " , " - " ) . replaceAll ( " [’ '] " , " _ " ) ;
if ( fixedTag . length ( ) > 100 )
fixedTag = fixedTag . substring ( 0 , 96 ) + " ... " ;
return fixedTag . trim ( ) ;
}
2019-04-03 15:59:18 +02:00
static final String identityString ( UserIdentity id ) {
StringBuilder builder = new StringBuilder ( id . getLastName ( ) + " , " ) ;
builder . append ( id . getFirstName ( ) + " , " ) ;
if ( id . getEmail ( ) ! = null ) builder . append ( id . getEmail ( ) + " , " ) ;
if ( id . getOrcid ( ) ! = null ) builder . append ( id . getOrcid ( ) + " , " ) ;
return builder . toString ( ) . substring ( 0 , builder . lastIndexOf ( " , " ) ) ;
}
2019-04-01 15:30:16 +02:00
}