2016-06-09 10:56:14 +02:00
|
|
|
package org.gcube.informationsystem.types;
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
2019-03-22 15:18:23 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2020-01-30 10:26:43 +01:00
|
|
|
import org.gcube.informationsystem.base.reference.Element;
|
2019-10-24 11:57:21 +02:00
|
|
|
import org.gcube.informationsystem.types.impl.TypeDefinitionImpl;
|
2019-10-23 18:19:55 +02:00
|
|
|
import org.gcube.informationsystem.types.reference.TypeDefinition;
|
2019-10-25 17:46:13 +02:00
|
|
|
import org.gcube.informationsystem.utils.ISMapper;
|
2016-06-09 10:56:14 +02:00
|
|
|
|
2019-03-22 15:18:23 +01:00
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
2016-06-09 10:56:14 +02:00
|
|
|
public class TypeBinder {
|
|
|
|
|
2019-03-22 15:18:23 +01:00
|
|
|
private final static String NAME = "NAME";
|
2016-06-09 10:56:14 +02:00
|
|
|
|
2019-10-28 17:36:09 +01:00
|
|
|
public static String serializeTypeDefinition(TypeDefinition typeDefinition) throws Exception{
|
2019-10-25 17:46:13 +02:00
|
|
|
String json = ISMapper.marshal(typeDefinition);
|
2016-06-09 10:56:14 +02:00
|
|
|
return json;
|
|
|
|
}
|
2019-03-22 15:18:23 +01:00
|
|
|
|
2020-01-30 10:26:43 +01:00
|
|
|
public static <ISM extends Element> String serializeType(Class<ISM> type) throws Exception{
|
2019-11-04 18:06:46 +01:00
|
|
|
TypeDefinition typeDefinition = createTypeDefinition(type);
|
2019-10-25 17:46:13 +02:00
|
|
|
return ISMapper.marshal(typeDefinition);
|
2019-03-22 15:18:23 +01:00
|
|
|
}
|
2016-06-09 10:56:14 +02:00
|
|
|
|
2019-10-28 17:36:09 +01:00
|
|
|
public static TypeDefinition deserializeTypeDefinition(String json) throws Exception{
|
|
|
|
TypeDefinition readValue = ISMapper.unmarshal(TypeDefinition.class, json);
|
2019-10-24 11:26:49 +02:00
|
|
|
return readValue;
|
2019-03-22 15:18:23 +01:00
|
|
|
}
|
|
|
|
|
2020-01-30 10:26:43 +01:00
|
|
|
public static <ISM extends Element> String serializeTypeDefinitions(List<TypeDefinition> typeDefinitions) throws Exception{
|
2019-10-28 17:36:09 +01:00
|
|
|
String json = ISMapper.marshal(typeDefinitions);
|
|
|
|
return json;
|
|
|
|
|
2019-03-22 15:18:23 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 17:36:09 +01:00
|
|
|
public static List<TypeDefinition> deserializeTypeDefinitions(String json) throws Exception{
|
|
|
|
List<TypeDefinition> list = ISMapper.unmarshalList(TypeDefinition.class, json);
|
|
|
|
return list;
|
2019-03-22 15:18:23 +01:00
|
|
|
}
|
|
|
|
|
2020-01-30 10:26:43 +01:00
|
|
|
public static <ISM extends Element> TypeDefinition createTypeDefinition(Class<ISM> clz) {
|
2019-11-04 18:06:46 +01:00
|
|
|
TypeDefinition typeDefinition = TypeDefinitionImpl.getInstance(clz);
|
2016-06-09 10:56:14 +02:00
|
|
|
return typeDefinition;
|
|
|
|
}
|
2016-07-01 16:02:37 +02:00
|
|
|
|
2020-01-30 10:26:43 +01:00
|
|
|
public static String getType(Class<? extends Element> clz){
|
2019-10-23 18:19:55 +02:00
|
|
|
return getStaticStringFieldByName(clz, NAME, clz.getSimpleName());
|
2016-06-09 10:56:14 +02:00
|
|
|
}
|
2016-07-04 12:05:47 +02:00
|
|
|
|
2020-01-30 10:26:43 +01:00
|
|
|
public static String getStaticStringFieldByName(Class<? extends Element> type, String fieldName, String defaultValue){
|
2019-10-23 18:19:55 +02:00
|
|
|
Field field;
|
|
|
|
try {
|
|
|
|
field = type.getDeclaredField(fieldName);
|
|
|
|
field.setAccessible(true);
|
|
|
|
return (String) field.get(null);
|
|
|
|
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
|
|
|
|
return defaultValue;
|
2016-06-09 10:56:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|