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

158 lines
6.4 KiB
Java

package org.gcube.data.publishing.ckan2zenodo.commons;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.data.publishing.ckan2zenodo.model.faults.ConfigurationException;
import org.gcube.data.publishing.ckan2zenodo.model.parsing.Mapping;
import org.gcube.data.publishing.ckan2zenodo.model.parsing.Mapping.Regexp;
import org.gcube.data.publishing.ckan2zenodo.model.parsing.Mapping.Source;
import org.gcube.data.publishing.ckan2zenodo.model.parsing.Mapping.Source.Value;
import org.gcube.data.publishing.ckan2zenodo.model.parsing.Mapping.TargetElement;
import org.gcube.data.publishing.ckan2zenodo.model.parsing.ResourceFilter;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class IS {
public static List<GenericResource> queryForGenericResources(String secondaryType){
log.debug("Querying for Service Endpoints [secondary type : {} ]",secondaryType);
SimpleQuery query = queryFor(GenericResource.class);
query.addCondition("$resource/Profile/SecondaryType/text() eq '"+secondaryType+"'");
DiscoveryClient<GenericResource> client = clientFor(GenericResource.class);
return client.submit(query);
}
public static List<ServiceEndpoint> queryForServiceEndpoints(String category, String platformName){
log.debug("Querying for Service Endpoints [category : {} , platformName : {}]",category,platformName);
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq '"+category+"'")
.addCondition("$resource/Profile/Platform/Name/text() eq '"+platformName+"'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
return client.submit(query);
}
public static ArrayList<Mapping> readMappings(GenericResource res) throws ConfigurationException{
ArrayList<Mapping> toReturn=new ArrayList<Mapping>();
try {
Element root=res.profile().body();
NodeList mappings=root.getElementsByTagName("mapping");
for(int i = 0; i<mappings.getLength();i++) {
Element mapping=(Element) mappings.item(i);
Element sourceElement=(Element) mapping.getElementsByTagName("source").item(0);
NodeList valuesNodeList=sourceElement.getElementsByTagName("value");
Source source=new Source();
for(int j=0;j<valuesNodeList.getLength();j++) {
Element valueElement=(Element) valuesNodeList.item(j);
Value valueObject=new Value(Value.Type.valueOf(valueElement.getAttribute("type")),
valueElement.getTextContent());
if(valueElement.hasAttribute("split"))
valueObject.setSplit(valueElement.getAttribute("split"));
source.getValues().add(valueObject);
}
String targetPath=mapping.getElementsByTagName("targetPath").item(0).getTextContent();
Element targetElement=(Element) mapping.getElementsByTagName("targetElement").item(0);
TargetElement targetObject=new TargetElement(targetElement.getTextContent());
if(targetElement.hasAttribute("append"))
targetObject.setAppend(Boolean.parseBoolean(targetElement.getAttribute("append")));
HashMap<String,String> values=new HashMap<>();
NodeList valueMappings=mapping.getElementsByTagName("valueMapping");
for(int j = 0; j<valueMappings.getLength();j++) {
Element codelistMapping=(Element) valueMappings.item(j);
String sourceValue=codelistMapping.getElementsByTagName("sourceValue").item(0).getTextContent();
String targetValue=codelistMapping.getElementsByTagName("targetValue").item(0).getTextContent();
values.put(sourceValue, targetValue);
}
ArrayList<Regexp> regularExpressions=new ArrayList<>();
NodeList regexpDeclarations=mapping.getElementsByTagName("regexp");
for(int j = 0; j<regexpDeclarations.getLength();j++) {
Element regexpElement=(Element) regexpDeclarations.item(j);
String regexpTarget=regexpElement.getElementsByTagName("target").item(0).getTextContent();
String typeName=regexpElement.getAttribute("type");
Regexp regexp=new Regexp(Regexp.Type.valueOf(typeName),regexpTarget);
if(regexp.getType().equals(Regexp.Type.replace))
regexp.setReplacement(regexpElement.getElementsByTagName("replacement").item(0).getTextContent());
regularExpressions.add(regexp);
}
toReturn.add(new Mapping(source,targetPath,targetObject,values,regularExpressions));
}
return toReturn;
}catch(Throwable t) {
log.debug("Error while parsing mapping from resource "+res.id()+" name : "+res.profile().name(),t);
throw new ConfigurationException("Invaild mapping resource "+res.id()+" name : "+res.profile().name(),t);
}
}
public static ResourceFilter readResourceFilters(GenericResource res) throws ConfigurationException{
try{
ArrayList<ResourceFilter.Filter> filtersList=new ArrayList<>();
Element root=res.profile().body();
NodeList filters=((Element) root.getElementsByTagName("resourceFilters").item(0)).getElementsByTagName("filter");
for(int i=0;i<filters.getLength();i++) {
Element filter=(Element) filters.item(i);
ArrayList<String> conditions=new ArrayList<>();
NodeList conditionNodes=filter.getElementsByTagName("condition");
for(int j=0;j<conditionNodes.getLength();j++) {
conditions.add(conditionNodes.item(j).getTextContent());
}
if(!conditions.isEmpty())
filtersList.add(new ResourceFilter.Filter(conditions));
}
if(filtersList.isEmpty())
throw new Exception("No Filter actually declared.");
return new ResourceFilter(filtersList);
}catch(Throwable t) {
log.debug("Error while parsing filters from resource "+res.id()+" name : "+res.profile().name(),t);
throw new ConfigurationException("Invalid resource filters declared in "+res.id()+" name : "+res.profile().name(),t);
}
}
public static String decryptString(String toDecrypt){
try{
return StringEncrypter.getEncrypter().decrypt(toDecrypt);
}catch(Exception e) {
throw new RuntimeException("Unable to decrypt : "+toDecrypt,e);
}
}
}