2019-11-27 18:21:01 +01:00
|
|
|
package org.gcube.data.publishing.ckan2zenodo;
|
|
|
|
|
2019-12-03 12:50:36 +01:00
|
|
|
import java.io.IOException;
|
2019-12-02 18:02:11 +01:00
|
|
|
import java.util.ArrayList;
|
2019-12-03 12:50:36 +01:00
|
|
|
import java.util.List;
|
2019-12-02 18:02:11 +01:00
|
|
|
|
2019-12-03 12:50:36 +01:00
|
|
|
import org.gcube.data.publishing.ckan2zenodo.commons.Parsing;
|
2019-11-27 18:21:01 +01:00
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.CkanItemDescriptor;
|
2019-12-03 12:50:36 +01:00
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.Mapping;
|
2019-12-02 18:02:11 +01:00
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.Contributor;
|
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.DepositionMetadata;
|
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.DepositionMetadata.AccessRights;
|
2019-12-03 12:50:36 +01:00
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.RelatedIdentifier;
|
2019-11-27 18:21:01 +01:00
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.ZenodoDeposition;
|
|
|
|
|
2019-12-03 12:50:36 +01:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.jayway.jsonpath.DocumentContext;
|
|
|
|
import com.jayway.jsonpath.JsonPath;
|
|
|
|
|
|
|
|
import lombok.NonNull;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
2019-12-02 18:02:11 +01:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
@Slf4j
|
2019-12-03 12:50:36 +01:00
|
|
|
@RequiredArgsConstructor
|
2019-11-27 18:21:01 +01:00
|
|
|
public class Transformer {
|
|
|
|
|
2019-12-03 12:50:36 +01:00
|
|
|
@NonNull
|
|
|
|
private List<Mapping> mappings;
|
2019-12-02 18:02:11 +01:00
|
|
|
|
2019-11-27 18:21:01 +01:00
|
|
|
|
2019-12-03 12:50:36 +01:00
|
|
|
|
|
|
|
public ZenodoDeposition transform(CkanItemDescriptor toTransform, ZenodoDeposition deposition) throws IOException {
|
2019-12-02 18:02:11 +01:00
|
|
|
log.debug("Transforming "+toTransform+". Existing Deposition is : "+deposition);
|
|
|
|
|
|
|
|
if(deposition==null) deposition=new ZenodoDeposition();
|
|
|
|
|
|
|
|
// default mappings
|
|
|
|
DepositionMetadata meta=deposition.getMetadata();
|
|
|
|
if(meta==null)
|
|
|
|
meta=new DepositionMetadata();
|
|
|
|
|
|
|
|
meta.setTitle(toTransform.getTitle());
|
|
|
|
meta.setDescription(toTransform.getNotes());
|
|
|
|
|
|
|
|
// Access Right
|
|
|
|
if(toTransform.isOpen())
|
|
|
|
meta.setAccess_right(AccessRights.open);
|
|
|
|
else {
|
|
|
|
meta.setAccess_right(AccessRights.restricted);
|
|
|
|
meta.setAccess_conditions("Visit the VRE "+toTransform.getVRE()+" to access it.");
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.setLicense(toTransform.getLicenseId());
|
|
|
|
|
|
|
|
meta.setKeywords(new ArrayList<String>(toTransform.getTags()));
|
|
|
|
|
|
|
|
//Related Identifiers
|
|
|
|
String itemUrl=toTransform.getItemUrl();
|
|
|
|
ArrayList<RelatedIdentifier> relatedIdentifiers=new ArrayList<>();
|
|
|
|
if(itemUrl!=null) relatedIdentifiers.add(new RelatedIdentifier(itemUrl,RelatedIdentifier.Relation.compiles));
|
|
|
|
|
2019-12-03 12:50:36 +01:00
|
|
|
meta.setRelated_identifiers(relatedIdentifiers);
|
|
|
|
|
2019-12-02 18:02:11 +01:00
|
|
|
//Contributors
|
|
|
|
ArrayList<Contributor> contributors=new ArrayList<>();
|
|
|
|
String authorName=toTransform.getAuthor();
|
|
|
|
if(authorName!=null) {
|
|
|
|
Contributor author=new Contributor(Contributor.Type.Producer);
|
|
|
|
author.setName(authorName);
|
|
|
|
contributors.add(author);
|
|
|
|
}
|
|
|
|
|
|
|
|
String maintainerName=toTransform.getAuthor();
|
|
|
|
if(maintainerName!=null) {
|
|
|
|
Contributor maintainer=new Contributor(Contributor.Type.DataCurator);
|
|
|
|
maintainer.setName(maintainerName);
|
|
|
|
contributors.add(maintainer);
|
|
|
|
}
|
|
|
|
meta.setContributors(contributors);
|
|
|
|
|
|
|
|
meta.setVersion(toTransform.getVersion());
|
|
|
|
|
|
|
|
|
|
|
|
// Dates
|
|
|
|
|
|
|
|
|
|
|
|
deposition.setMetadata(meta);
|
|
|
|
|
|
|
|
// profile specific mappings
|
2019-12-03 12:50:36 +01:00
|
|
|
return applyMappings(toTransform, deposition);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ZenodoDeposition applyMappings(CkanItemDescriptor source, ZenodoDeposition target) throws IOException {
|
|
|
|
ObjectMapper mapper=Parsing.getMapper();
|
|
|
|
DocumentContext sourceCtx=JsonPath.using(Parsing.JSON_PATH_ALWAYS_LIST_CONFIG).parse(source.getContent());
|
|
|
|
DocumentContext targetCtx=JsonPath.using(Parsing.JSON_PATH_ALWAYS_LIST_CONFIG).parse(mapper.writeValueAsString(target));
|
|
|
|
|
|
|
|
for(Mapping mapping:mappings) {
|
|
|
|
List<String> sourceValues=sourceCtx.read(mapping.getSource());
|
|
|
|
if(sourceValues!=null)
|
|
|
|
for(String sourceValue:sourceValues) {
|
|
|
|
String targetValue =mapping.getValueMapping().getOrDefault(sourceValue, sourceValue);
|
|
|
|
targetCtx.add(mapping.getTarget(),targetValue);
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 18:21:01 +01:00
|
|
|
|
2019-12-03 12:50:36 +01:00
|
|
|
return mapper.readValue(targetCtx.jsonString(), ZenodoDeposition.class);
|
2019-11-27 18:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|