ckan2zenodo-library/ckan2zenodo-library/src/main/java/org/gcube/data/publishing/ckan2zenodo/Transformer.java

140 lines
4.4 KiB
Java
Raw Normal View History

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-03 17:20:12 +01:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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-03 17:20:12 +01:00
import org.gcube.data.publishing.ckan2zenodo.model.Mapping.Regexp;
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.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) {
2019-12-03 17:20:12 +01:00
String resultingValue=sourceValue;
// apply regexps
for(Regexp regexp:mapping.getRegexp()) {
switch(regexp.getType()) {
case extract : {
Pattern p=Pattern.compile(regexp.getTarget());
Matcher m = p.matcher(resultingValue);
if(m.find())
resultingValue=m.group();
else resultingValue=null;
break;
}
case replace : {
resultingValue=resultingValue.replaceAll(regexp.getTarget(), regexp.getReplacement());
break;
}
}
}
// apply value mapping
resultingValue =mapping.getValueMapping().getOrDefault(sourceValue, sourceValue);
targetCtx.add(mapping.getTarget(),resultingValue);
2019-12-03 12:50:36 +01:00
}
}
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
}
}