2018-05-17 12:51:56 +02:00
|
|
|
package org.gcube.data.access.storagehub.handlers;
|
|
|
|
|
2020-04-14 18:52:59 +02:00
|
|
|
import java.util.Arrays;
|
2018-05-17 12:51:56 +02:00
|
|
|
import java.util.HashMap;
|
2020-04-14 18:52:59 +02:00
|
|
|
import java.util.List;
|
2018-05-17 12:51:56 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.gcube.common.storagehub.model.annotations.RootNode;
|
2021-05-04 11:42:02 +02:00
|
|
|
import org.gcube.common.storagehub.model.items.RootItem;
|
2018-05-17 12:51:56 +02:00
|
|
|
import org.reflections.Reflections;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
public class ClassHandler {
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
private static Logger log = LoggerFactory.getLogger(ClassHandler.class);
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-10-25 16:33:23 +02:00
|
|
|
private static ClassHandler instance = null;
|
|
|
|
|
|
|
|
public static ClassHandler instance() {
|
|
|
|
if (instance == null)
|
|
|
|
instance = new ClassHandler();
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
private Reflections reflection = new Reflections();
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2020-04-14 18:52:59 +02:00
|
|
|
private List<String> deprecatedNode = Arrays.asList("nthl:query", "nthl:aquamapsItem", "nthl:timeSeriesItem", "nthl:report", "nthl:reportTemplate", "nthl:workflowReport",
|
|
|
|
"nthl:workflowTemplate", "nthl:gCubeMetadata", "nthl:gCubeDocument", "nthl:gCubeDocumentLink", "nthl:gCubeImageDocumentLink", "nthl:gCubePDFDocumentLink",
|
|
|
|
"nthl:gCubeImageDocument", "nthl:gCubePDFDocument", "nthl:gCubeURLDocument", "nthl:gCubeAnnotation", "nthl:externalResourceLink", "nthl:tabularDataLink");
|
|
|
|
|
2021-05-04 11:42:02 +02:00
|
|
|
private Map<String, Class<? extends RootItem>> classMap = new HashMap<String, Class<? extends RootItem>>();
|
|
|
|
private Map<Class<? extends RootItem>, String> typeMap = new HashMap<Class<? extends RootItem>, String>();
|
2018-06-05 15:33:36 +02:00
|
|
|
|
|
|
|
|
2021-05-04 11:42:02 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2018-10-25 16:33:23 +02:00
|
|
|
private ClassHandler() {
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
Set<Class<?>> classesAnnotated = reflection.getTypesAnnotatedWith(RootNode.class);
|
|
|
|
for (Class<?> clazz: classesAnnotated ){
|
2021-05-04 11:42:02 +02:00
|
|
|
if (RootItem.class.isAssignableFrom(clazz)) {
|
2018-06-05 15:33:36 +02:00
|
|
|
String value = clazz.getAnnotation(RootNode.class).value();
|
|
|
|
log.debug("loading class {} with value {} ", clazz, value );
|
2021-05-04 11:42:02 +02:00
|
|
|
classMap.put(value, (Class<? extends RootItem>) clazz);
|
|
|
|
typeMap.put((Class<? extends RootItem>) clazz, value);
|
2018-06-05 15:33:36 +02:00
|
|
|
}
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2021-05-04 11:42:02 +02:00
|
|
|
public Class<? extends RootItem> get(String nodeType){
|
2018-05-17 12:51:56 +02:00
|
|
|
if (classMap.containsKey(nodeType)) return classMap.get(nodeType);
|
2021-05-04 11:42:02 +02:00
|
|
|
if (deprecatedNode.contains(nodeType)) return RootItem.class;
|
2020-04-14 18:52:59 +02:00
|
|
|
return null;
|
2018-06-05 15:33:36 +02:00
|
|
|
//throw new RuntimeException("mapping not found for nodetype "+ nodeType);
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|
2018-06-05 15:33:36 +02:00
|
|
|
|
2021-05-04 11:42:02 +02:00
|
|
|
public String getNodeType(Class<? extends RootItem> clazz){
|
2018-05-17 12:51:56 +02:00
|
|
|
if (typeMap.containsKey(clazz)) return typeMap.get(clazz);
|
|
|
|
throw new RuntimeException("mapping not found for nodetype "+ clazz.getSimpleName());
|
|
|
|
}
|
2018-06-05 15:33:36 +02:00
|
|
|
|
|
|
|
|
2018-05-17 12:51:56 +02:00
|
|
|
}
|
2018-06-05 15:33:36 +02:00
|
|
|
|
|
|
|
|