106 lines
4.2 KiB
Java
106 lines
4.2 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.resources.gcore.GenericResource;
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
import org.gcube.data.publishing.ckan2zenodo.model.Mapping;
|
|
import org.gcube.data.publishing.ckan2zenodo.model.Mapping.Regexp;
|
|
import org.gcube.data.publishing.ckan2zenodo.model.Mapping.Source;
|
|
import org.gcube.data.publishing.ckan2zenodo.model.faults.ConfigurationException;
|
|
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);
|
|
Source source=new Source(Source.Type.valueOf(sourceElement.getAttribute("type")),
|
|
sourceElement.getElementsByTagName("value").item(0).getTextContent());
|
|
|
|
|
|
String targetPath=mapping.getElementsByTagName("targetPath").item(0).getTextContent();
|
|
String targetElement=mapping.getElementsByTagName("targetElement").item(0).getTextContent();
|
|
|
|
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,targetElement,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);
|
|
}
|
|
}
|
|
}
|