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