package org.gcube.data.publishing.ckan2zenodo; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.gcube.data.publishing.ckan2zenodo.commons.Parsing; import org.gcube.data.publishing.ckan2zenodo.model.CkanItemDescriptor; import org.gcube.data.publishing.ckan2zenodo.model.Mapping; import org.gcube.data.publishing.ckan2zenodo.model.Mapping.Regexp; import org.gcube.data.publishing.ckan2zenodo.model.faults.TransformationException; 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; import org.gcube.data.publishing.ckan2zenodo.model.zenodo.RelatedIdentifier; import org.gcube.data.publishing.ckan2zenodo.model.zenodo.ZenodoDeposition; import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.JsonPath; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @Slf4j @RequiredArgsConstructor public class Transformer { @NonNull private List mappings; public ZenodoDeposition transform(CkanItemDescriptor toTransform, ZenodoDeposition deposition) throws TransformationException { 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(toTransform.getTags())); //Related Identifiers String itemUrl=toTransform.getItemUrl(); ArrayList relatedIdentifiers=new ArrayList<>(); if(itemUrl!=null) relatedIdentifiers.add(new RelatedIdentifier(itemUrl,RelatedIdentifier.Relation.compiles)); meta.setRelated_identifiers(relatedIdentifiers); //Contributors ArrayList 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 return applyMappings(toTransform, deposition); } private ZenodoDeposition applyMappings(CkanItemDescriptor source, ZenodoDeposition target) throws TransformationException { try{ 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) { try { // extract source List sourceValues=new ArrayList<>(); switch(mapping.getSource().getType()) { case constant : { sourceValues.add(mapping.getSource().getValue()); break; } case jsonPath : { sourceValues.addAll(sourceCtx.read(mapping.getSource().getValue())); break; } } for(String sourceValue:sourceValues) { 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 : { if(resultingValue!=null) { String replacement=regexp.getReplacement()!=null?regexp.getReplacement():""; resultingValue=resultingValue.replaceAll(regexp.getTarget(), replacement); break; } } } } // apply value mappings resultingValue =mapping.getValueMapping().getOrDefault(sourceValue, resultingValue); // apply resulting value targetCtx.put(mapping.getTargetPath(),mapping.getTargetElement(),resultingValue); } }catch(Throwable t) { throw new TransformationException("Exception while applying "+mapping,t); } } return mapper.readValue(targetCtx.jsonString(), ZenodoDeposition.class); }catch(Throwable t) { log.error("Unable to transform "+source+" using previous "+target,t); throw new TransformationException("Unable to translate "+source.getName(),t); } } }