2016-10-07 18:23:23 +02:00
|
|
|
package org.gcube.data_catalogue.grsf_publish_ws.utils;
|
|
|
|
|
2016-10-08 23:10:25 +02:00
|
|
|
import java.beans.PropertyDescriptor;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CustomField;
|
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group;
|
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag;
|
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.input.Common;
|
2016-10-08 22:16:31 +02:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogueFactory;
|
2016-10-08 23:10:25 +02:00
|
|
|
import org.slf4j.LoggerFactory;
|
2016-10-08 22:16:31 +02:00
|
|
|
|
2016-10-07 18:23:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper methods
|
|
|
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
|
|
|
*/
|
|
|
|
public abstract class HelperMethods {
|
|
|
|
|
2016-10-08 23:10:25 +02:00
|
|
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(HelperMethods.class);
|
|
|
|
|
2016-10-08 22:16:31 +02:00
|
|
|
/**
|
|
|
|
* Convert a group name to its id on ckan
|
|
|
|
* @param origName
|
|
|
|
* @return
|
|
|
|
*/
|
2016-10-07 18:23:23 +02:00
|
|
|
public static String getGroupNameOnCkan(String origName){
|
2016-10-08 23:10:25 +02:00
|
|
|
|
2016-10-07 18:23:23 +02:00
|
|
|
if(origName == null)
|
|
|
|
throw new IllegalArgumentException("origName cannot be null");
|
2016-10-08 23:10:25 +02:00
|
|
|
|
2016-10-07 18:23:23 +02:00
|
|
|
return origName.trim().toLowerCase().replaceAll("[^A-Za-z0-9_.]", "_");
|
2016-10-08 23:10:25 +02:00
|
|
|
|
2016-10-07 18:23:23 +02:00
|
|
|
}
|
2016-10-08 23:10:25 +02:00
|
|
|
|
2016-10-08 22:16:31 +02:00
|
|
|
/**
|
|
|
|
* Retrieve the running instance of the data catalogue for this scope
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static DataCatalogue getDataCatalogueRunningInstance(String scope) throws Exception{
|
2016-10-08 23:10:25 +02:00
|
|
|
|
2016-10-08 22:16:31 +02:00
|
|
|
return DataCatalogueFactory.getFactory().getUtilsPerScope(scope);
|
2016-10-08 23:10:25 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the list of tags for this object
|
|
|
|
*/
|
|
|
|
public static void getTags(List<String> tags, Common record){
|
|
|
|
Class<?> current = record.getClass();
|
|
|
|
do{
|
|
|
|
Field[] fields = current.getDeclaredFields();
|
|
|
|
for (Field field : fields) {
|
|
|
|
if(field.isAnnotationPresent(Tag.class)){
|
|
|
|
try{
|
|
|
|
Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(record);
|
|
|
|
if(f != null){
|
|
|
|
|
|
|
|
tags.add(f.toString());
|
|
|
|
|
|
|
|
}
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Failed ot read value for field " + field.getName() + " skipping", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while((current = current.getSuperclass())!=null);
|
2016-10-08 22:16:31 +02:00
|
|
|
}
|
2016-10-08 23:10:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the list of groups for this object
|
|
|
|
*/
|
|
|
|
public static void getGroups(List<String> groups, Common record){
|
|
|
|
Class<?> current = record.getClass();
|
|
|
|
do{
|
|
|
|
Field[] fields = current.getDeclaredFields();
|
|
|
|
for (Field field : fields) {
|
|
|
|
if(field.isAnnotationPresent(Group.class)){
|
|
|
|
try{
|
|
|
|
Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(record);
|
|
|
|
if(f != null){
|
|
|
|
|
|
|
|
groups.add(getGroupNameOnCkan(f.toString()));
|
|
|
|
|
|
|
|
}
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Failed ot read value for field " + field.getName() + " skipping", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while((current = current.getSuperclass())!=null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the list of extras for this object
|
|
|
|
*/
|
|
|
|
public static void getExtras(Map<String, Object> extras, Common record){
|
|
|
|
Class<?> current = record.getClass();
|
|
|
|
do{
|
|
|
|
Field[] fields = current.getDeclaredFields();
|
|
|
|
for (Field field : fields) {
|
|
|
|
if(field.isAnnotationPresent(CustomField.class)){
|
|
|
|
try{
|
|
|
|
Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(record);
|
|
|
|
if(f != null){
|
|
|
|
|
|
|
|
// get the key to put into the map first
|
|
|
|
extras.put(field.getAnnotation(CustomField.class).key(), f);
|
|
|
|
|
|
|
|
}
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Failed ot read value for field " + field.getName() + " skipping", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while((current = current.getSuperclass())!=null);
|
|
|
|
}
|
|
|
|
|
2016-10-07 18:23:23 +02:00
|
|
|
}
|