grsf-publisher-ws/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java

127 lines
3.5 KiB
Java
Raw Normal View History

package org.gcube.data_catalogue.grsf_publish_ws.utils;
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;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogueFactory;
import org.slf4j.LoggerFactory;
/**
* Helper methods
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public abstract class HelperMethods {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(HelperMethods.class);
/**
* Convert a group name to its id on ckan
* @param origName
* @return
*/
public static String getGroupNameOnCkan(String origName){
if(origName == null)
throw new IllegalArgumentException("origName cannot be null");
return origName.trim().toLowerCase().replaceAll("[^A-Za-z0-9_.]", "_");
}
/**
* Retrieve the running instance of the data catalogue for this scope
* @return
* @throws Exception
*/
public static DataCatalogue getDataCatalogueRunningInstance(String scope) throws Exception{
return DataCatalogueFactory.getFactory().getUtilsPerScope(scope);
}
/**
* 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);
}
/**
* 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);
}
}