Updated Resources

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/information-system-model@129832 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-07-04 10:05:47 +00:00
parent a99f9a4dd3
commit b66445854a
1 changed files with 43 additions and 79 deletions

View File

@ -1,14 +1,17 @@
package org.gcube.informationsystem.types; package org.gcube.informationsystem.types;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.gcube.informationsystem.model.annotations.Abstract; import org.gcube.informationsystem.model.annotations.Abstract;
import org.gcube.informationsystem.model.annotations.ISProperty;
import org.gcube.informationsystem.model.embedded.Embedded; import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.entity.Facet; import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource; import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.types.Type.OType;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -25,7 +28,7 @@ public class TypeBinder {
private static String getStaticStringFieldByName(Class<?> type, String fieldName, String defaultValue){ private static String getStaticStringFieldByName(Class<?> type, String fieldName, String defaultValue){
Field field; Field field;
try { try {
field = type.getDeclaredField(NAME); field = type.getDeclaredField(fieldName);
return (String) field.get(null); return (String) field.get(null);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
return defaultValue; return defaultValue;
@ -54,8 +57,7 @@ public class TypeBinder {
if(Resource.class.isAssignableFrom(type)){ if(Resource.class.isAssignableFrom(type)){
typeDefinition.superclasses = retrieveSuperClasses(type, Resource.class, Entity.class.getSimpleName()); typeDefinition.superclasses = retrieveSuperClasses(type, Resource.class, Entity.class.getSimpleName());
}else{ }else{
// TODO typeDefinition.properties = retrieveListOfProperties(type);
//typeDefinition.properties = retrieveListOfProperties(type);
if(Facet.class.isAssignableFrom(type)){ if(Facet.class.isAssignableFrom(type)){
typeDefinition.superclasses = retrieveSuperClasses(type, Facet.class, Entity.class.getSimpleName()); typeDefinition.superclasses = retrieveSuperClasses(type, Facet.class, Entity.class.getSimpleName());
@ -67,88 +69,44 @@ public class TypeBinder {
} }
/*
if (typeDefinition.name.equals(DEFAULT_ENTITY_SUPERCLASS)) {
typeDefinition.superclasses = Collections.singletonList("V");
} else {
String superClassName = retrieveSuperClasses(type);
typeDefinition.superclasses = Collections.singletonList(superClassName.isEmpty()?DEFAULT_ENTITY_SUPERCLASS:superClassName);
}
if (typeDefinition.name.equals(DEFAULT_RESOURCE_SUPERCLASS)) {
typeDefinition.superclasses = Collections.singletonList(DEFAULT_ENTITY_SUPERCLASS);
} else {
String superClassName = retrieveSuperClasses(type);
typeDefinition.superclasses = Collections.singletonList(superClassName.isEmpty()?DEFAULT_RESOURCE_SUPERCLASS:superClassName);
}
if (typeDefinition.name.equals(DEFAULT_FACET_SUPERCLASS)) {
typeDefinition.superclasses = Collections.singletonList(DEFAULT_ENTITY_SUPERCLASS);
} else {
String superClassName = retrieveSuperClasses(type);
typeDefinition.superclasses = Collections.singletonList(superClassName.isEmpty()?DEFAULT_FACET_SUPERCLASS:superClassName);
}
*/
logger.trace("{} TypeDefinition {} ", type, typeDefinition); logger.trace("{} TypeDefinition {} ", type, typeDefinition);
return typeDefinition; return typeDefinition;
} }
/* protected static Set<Property> retrieveListOfProperties(Class<?> type){
private static List<Property> retrieveListOfProperties(Class<?> type){ Set<Property> properties = new HashSet<>();
List<Property> properties = new ArrayList<TypeBinder.Property>(); for (Method m : type.getDeclaredMethods()){
for (Field f:type.getDeclaredFields()){ m.setAccessible(true);
f.setAccessible(true); if(m.isAnnotationPresent(ISProperty.class)){
if (f.isAnnotationPresent(ISPropertyRef.class)){ ISProperty propAnnotation = m.getAnnotation(ISProperty.class);
ISPropertyRef refAnnotation = f.getAnnotation(ISPropertyRef.class); Property prop = getProperty(propAnnotation, m);
Property prop = getProperty(refAnnotation, f);
properties.add(prop); properties.add(prop);
logger.trace("property {} retrieved in type {} ",prop, type.getSimpleName()); logger.trace("Property {} retrieved in type {} ", prop, type.getSimpleName());
}else if (f.isAnnotationPresent(ISProperty.class)){
ISProperty propAnnotation = f.getAnnotation(ISProperty.class);
Property prop = getProperty(propAnnotation, f);
properties.add(prop);
logger.trace("property {} retrieved in type {} ",prop, type.getSimpleName());
} }
} }
return properties; return properties;
} }
private static Property getProperty(ISPropertyRef refPropertyAnnotation, Field field){ protected static String getPropertyNameFromMethodName(Method method){
if (!refPropertyAnnotation.ref().isAnnotationPresent(ISEmbeddedType.class)) String name = method.getName();
throw new RuntimeException("class "+refPropertyAnnotation.ref().getSimpleName()+" must be annotated with @ISEmbeddedType"); if(name.startsWith("get")){
name = name.replace("get", "");
}
if(name.startsWith("is")){
name = name.replace("is", "");
}
ISEmbeddedType embeddedTypeAnn= refPropertyAnnotation.ref().getAnnotation(ISEmbeddedType.class); if(name.length() > 0){
String link = embeddedTypeAnn.name().isEmpty()?refPropertyAnnotation.ref().getSimpleName():embeddedTypeAnn.name(); name = Character.toLowerCase(name.charAt(0)) + (name.length() > 1 ? name.substring(1) : "");
}
String name = refPropertyAnnotation.name().isEmpty()?field.getName():refPropertyAnnotation.name(); return name;
Property prop = new Property();
prop.name = name;
prop.description = refPropertyAnnotation.description();
prop.mandatory= refPropertyAnnotation.mandatory();
prop.notnull = !refPropertyAnnotation.nullable();
prop.readonly = refPropertyAnnotation.readonly();
if(refPropertyAnnotation.max()>0) prop.max = refPropertyAnnotation.max();
if(refPropertyAnnotation.max()>=refPropertyAnnotation.min() && refPropertyAnnotation.min()>0) prop.min = refPropertyAnnotation.min();
if(!refPropertyAnnotation.regexpr().isEmpty()) prop.regexpr = refPropertyAnnotation.regexpr();
logger.trace("serching correspondance for type {}",field.getType());
if (Type.getTypeByClass(field.getType())!=null)
prop.type = Type.getTypeByClass(field.getType()).getIntValue();
prop.linkedClass = link;
if (prop.type==null)
prop.type = OType.EMBEDDED.getIntValue();
return prop;
} }
private static Property getProperty(ISProperty propertyAnnotation, Field field){ protected static Property getProperty(ISProperty propertyAnnotation, Method method){
String name = propertyAnnotation.name().isEmpty()?field.getName():propertyAnnotation.name(); String name = propertyAnnotation.name().isEmpty()?getPropertyNameFromMethodName(method):propertyAnnotation.name();
Property prop = new Property(); Property prop = new Property();
prop.name = name; prop.name = name;
prop.description = propertyAnnotation.description(); prop.description = propertyAnnotation.description();
@ -158,13 +116,21 @@ public class TypeBinder {
if(propertyAnnotation.max()>0) prop.max = propertyAnnotation.max(); if(propertyAnnotation.max()>0) prop.max = propertyAnnotation.max();
if(propertyAnnotation.max()>=propertyAnnotation.min() && propertyAnnotation.min()>0) prop.min = propertyAnnotation.min(); if(propertyAnnotation.max()>=propertyAnnotation.min() && propertyAnnotation.min()>0) prop.min = propertyAnnotation.min();
if(!propertyAnnotation.regexpr().isEmpty()) prop.regexpr = propertyAnnotation.regexpr(); if(!propertyAnnotation.regexpr().isEmpty()) prop.regexpr = propertyAnnotation.regexpr();
logger.trace("serching correspondance for type {}",field.getType());
if (Type.getTypeByClass(field.getType())!=null) logger.trace("Looking for property type type {}", method.getReturnType());
prop.type = Type.getTypeByClass(field.getType()).getIntValue(); Class<?> type = method.getReturnType();
else throw new RuntimeException("type "+field.getType().getSimpleName()+" not reconized"); if(Embedded.class.isAssignableFrom(type)){
prop.linkedClass = getStaticStringFieldByName(type, NAME, type.getSimpleName());
prop.type = OType.EMBEDDED.getIntValue();
}else if (Type.getTypeByClass(type)!=null) {
prop.type = Type.getTypeByClass(type).getIntValue();
} else {
throw new RuntimeException("Type " + type.getSimpleName() + " not reconized");
}
return prop; return prop;
} }
*/
private static Set<String> retrieveSuperClasses(Class<?> type, Class<?> baseClass, String topSuperClass){ private static Set<String> retrieveSuperClasses(Class<?> type, Class<?> baseClass, String topSuperClass){
Set<String> interfaceList = new HashSet<>(); Set<String> interfaceList = new HashSet<>();
@ -286,8 +252,6 @@ public class TypeBinder {
return type; return type;
} }
@Override @Override
public String toString() { public String toString() {
return "Property [name=" + name + ", description=" + description return "Property [name=" + name + ", description=" + description