gFeed/DataMinerAlgorithmsCrawler/src/main/java/org/gcube/data/publishing/gCatFeeder/collectors/dm/model/ckan/GCatModel.java

128 lines
4.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package org.gcube.data.publishing.gCatFeeder.collectors.dm.model.ckan;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import org.gcube.data.publishing.gCatFeeder.collectors.dm.DataMinerCollectorProperties;
import org.gcube.data.publishing.gCatFeeder.collectors.dm.model.InternalAlgorithmDescriptor;
import org.gcube.data.publishing.gCatFeeder.collectors.dm.model.Parameter;
import org.gcube.data.publishing.gCatFeeder.collectors.dm.model.UserIdentity;
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;
private static String profileID=DataMinerCollectorProperties.getProperty(DataMinerCollectorProperties.CKAN_RESOURCE_TYPE);
public static void setProfile(String toSet) {
profileXML=toSet;
}
public GCatModel(InternalAlgorithmDescriptor desc) {
item=new CkanItem();
// item.setAuthor(desc.getAuthor());
item.setDescription(desc.getDescription());
item.setTitle(desc.getName()+" in "+ContextUtils.getCurrentScopeName());
item.setLicense("CC-BY-NC-SA-4.0");
// item.setMaintainer(desc.getMaintainer());
item.setName(item.getTitle().toLowerCase().toLowerCase().replaceAll(" ", "_"));
for(String tag:desc.getTags()) {
item.getTags().add(fixTag(tag));
}
item.getTags().add(ContextUtils.getCurrentScopeName());
item.getTags().add("WPS");
item.getTags().add("Analytics");
item.getExtras().add(new CKanExtraField("system", profileID));
item.setVisibility(desc.getVisibility());
// TODO ADD PARAMETERS
for(Parameter param: desc.getParameters())
item.getExtras().add(new CKanExtraField(profileID+":Input Parameter",
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())));
UserIdentity author=desc.getAuthor();
// item.getExtras().add(new CKanExtraField(profileID+":Process Author",
// String.format("%1$s, %2$s ", args)))
// TODO ADD RESOURCES
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)));
}
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();
}
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(","));
}
}