From 619dd586c1317526c94f98d88cb5863075d46995 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 11 Dec 2020 17:28:56 +0100 Subject: [PATCH] Added type changelog --- .../informationsystem/types/TypeMapper.java | 13 ++++++ .../types/impl/TypeImpl.java | 45 ++++++++++++++++++- .../properties/PropertyDefinitionImpl.java | 2 +- .../types/reference/Type.java | 4 ++ .../informationsystem/utils/TypeVersion.java | 27 +++++++++++ .../utils/TypeVersionTest.java | 2 +- 6 files changed, 90 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/types/TypeMapper.java b/src/main/java/org/gcube/informationsystem/types/TypeMapper.java index a2e5ced..18571b9 100644 --- a/src/main/java/org/gcube/informationsystem/types/TypeMapper.java +++ b/src/main/java/org/gcube/informationsystem/types/TypeMapper.java @@ -106,4 +106,17 @@ public class TypeMapper { return defaultValue; } } + + @SuppressWarnings("unchecked") + public static O getStaticFieldByName(Class clz, String fieldName, O defaultValue){ + Field field; + try { + field = clz.getDeclaredField(fieldName); + field.setAccessible(true); + return (O) field.get(null); + } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { + return defaultValue; + } + } + } diff --git a/src/main/java/org/gcube/informationsystem/types/impl/TypeImpl.java b/src/main/java/org/gcube/informationsystem/types/impl/TypeImpl.java index 4f258d5..417b8c2 100644 --- a/src/main/java/org/gcube/informationsystem/types/impl/TypeImpl.java +++ b/src/main/java/org/gcube/informationsystem/types/impl/TypeImpl.java @@ -3,7 +3,9 @@ package org.gcube.informationsystem.types.impl; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.TypeVariable; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import org.gcube.com.fasterxml.jackson.annotation.JsonGetter; @@ -40,12 +42,22 @@ public class TypeImpl implements Type { public final static String DESCRIPTION = "DESCRIPTION"; public final static String VERSION = "VERSION"; + public final static String CHANGELOG = "CHANGELOG"; + + public static final Map DEFAULT_CHANGELOG_MAP; + + static { + DEFAULT_CHANGELOG_MAP = new HashMap<>(); + DEFAULT_CHANGELOG_MAP.put(new TypeVersion("1.0.0"), "First Version"); + } protected Header header; protected String name; protected String description; protected TypeVersion version; + protected Map changelog; + @JsonProperty(value="abstract") protected boolean abstractType; protected Set superClasses; @@ -138,8 +150,15 @@ public class TypeImpl implements Type { protected TypeImpl(Class clz) { this.name = TypeMapper.getType(clz); this.description = TypeMapper.getStaticStringFieldByName(clz, DESCRIPTION, ""); - String versionString = TypeMapper.getStaticStringFieldByName(clz, VERSION, "1.0.0"); + String versionString = TypeMapper.getStaticStringFieldByName(clz, VERSION, TypeVersion.MINIMAL_VERSION_STRING); this.version = new TypeVersion(versionString); + + this.changelog = TypeMapper.getStaticFieldByName(clz, CHANGELOG, DEFAULT_CHANGELOG_MAP); + + if(!changelog.containsKey(version)) { + throw new RuntimeException("The Type " + name + " does not provided the appropriated changelog Map"); + } + this.abstractType = false; if(clz.isAnnotationPresent(Abstract.class)){ @@ -184,6 +203,28 @@ public class TypeImpl implements Type { this.version = new TypeVersion(version); } + @Override + public Map getChangelog() { + return changelog; + } + + @JsonGetter(value=CHANGELOG_PROPERTY) + public Map getChangelogwithVersionAsString() { + Map map = new HashMap<>(); + for(TypeVersion typeVersion : changelog.keySet()) { + map.put(typeVersion.toString(), changelog.get(typeVersion)); + } + return map; + } + + @JsonSetter(value = CHANGELOG_PROPERTY) + public void setChangelog(Map changelog) { + this.changelog = new HashMap<>(); + for(String version : changelog.keySet()) { + this.changelog.put(new TypeVersion(version), changelog.get(version)); + } + } + @Override public boolean isAbstract() { return abstractType; @@ -199,4 +240,6 @@ public class TypeImpl implements Type { return properties; } + + } diff --git a/src/main/java/org/gcube/informationsystem/types/impl/properties/PropertyDefinitionImpl.java b/src/main/java/org/gcube/informationsystem/types/impl/properties/PropertyDefinitionImpl.java index 18dcb42..1035592 100644 --- a/src/main/java/org/gcube/informationsystem/types/impl/properties/PropertyDefinitionImpl.java +++ b/src/main/java/org/gcube/informationsystem/types/impl/properties/PropertyDefinitionImpl.java @@ -34,7 +34,7 @@ public final class PropertyDefinitionImpl implements PropertyDefinition { public final static String UUID_REGEX = "^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}){1}$"; public final static String URI_REGEX = null; public final static String URL_REGEX = null; - public final static String TYPE_VERSION_REGEX = "^(0|([1-9][0-9]{0,}))\\.(0|([1-9][0-9]{0,}))\\.(0|([1-9][0-9]{0,}))$"; + public final static String TYPE_VERSION_REGEX = "^[1-9][0-9]{0,}\\.(0|([1-9][0-9]{0,}))\\.(0|([1-9][0-9]{0,}))$"; private String name= ""; private String description= ""; diff --git a/src/main/java/org/gcube/informationsystem/types/reference/Type.java b/src/main/java/org/gcube/informationsystem/types/reference/Type.java index 411b46e..e00e70d 100644 --- a/src/main/java/org/gcube/informationsystem/types/reference/Type.java +++ b/src/main/java/org/gcube/informationsystem/types/reference/Type.java @@ -1,5 +1,6 @@ package org.gcube.informationsystem.types.reference; +import java.util.Map; import java.util.Set; import org.gcube.com.fasterxml.jackson.annotation.JsonIgnoreProperties; @@ -18,6 +19,7 @@ public interface Type extends IdentifiableElement { public static final String NAME_PROPERTY = "name"; public static final String DESCRIPTION_PROPERTY = "description"; public static final String VERSION_PROPERTY = "version"; + public static final String CHANGELOG_PROPERTY = "changelog"; public static final String ABSTRACT_PROPERTY = "abstract"; public static final String TYPE_SUPERCLASSES_PROPERTY = "superClasses"; public static final String PROPERTIES_PROPERTY = "properties"; @@ -28,6 +30,8 @@ public interface Type extends IdentifiableElement { public TypeVersion getVersion(); + public Map getChangelog(); + public boolean isAbstract(); public Set getSuperClasses(); diff --git a/src/main/java/org/gcube/informationsystem/utils/TypeVersion.java b/src/main/java/org/gcube/informationsystem/utils/TypeVersion.java index 5c8416b..733aee9 100644 --- a/src/main/java/org/gcube/informationsystem/utils/TypeVersion.java +++ b/src/main/java/org/gcube/informationsystem/utils/TypeVersion.java @@ -11,8 +11,12 @@ public class TypeVersion implements Comparable { public final static Pattern TYPE_VERSION_PATTERN; + public static final String MINIMAL_VERSION_STRING = "1.0.0"; + public static final TypeVersion MINIMAL_VERSION; + static { TYPE_VERSION_PATTERN = Pattern.compile(TYPE_VERSION_REGEX); + MINIMAL_VERSION = new TypeVersion(MINIMAL_VERSION_STRING); } @JsonIgnore @@ -32,6 +36,7 @@ public class TypeVersion implements Comparable { this.major = major; this.minor = minor; this.revision = revision; + check(); } public void setVersion(String version) { @@ -42,6 +47,18 @@ public class TypeVersion implements Comparable { this.major = Integer.valueOf(parts[0]); this.minor = Integer.valueOf(parts[1]); this.revision = Integer.valueOf(parts[2]); + check(); + } + + /* + * The REGEX does not allow a 0.X.X Version. + * Anyway I added this check in case we decide to change the minimal version and the + * regex. + */ + protected void check() { + if(this.compareTo(MINIMAL_VERSION)<0) { + throw new RuntimeException("Minimal Allowed version is " + MINIMAL_VERSION_STRING); + } } public int getMajor() { @@ -92,6 +109,16 @@ public class TypeVersion implements Comparable { return compare; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + major; + result = prime * result + minor; + result = prime * result + revision; + return result; + } + @Override public boolean equals(Object obj) { if (this == obj) diff --git a/src/test/java/org/gcube/informationsystem/utils/TypeVersionTest.java b/src/test/java/org/gcube/informationsystem/utils/TypeVersionTest.java index dc39627..ecc5a76 100644 --- a/src/test/java/org/gcube/informationsystem/utils/TypeVersionTest.java +++ b/src/test/java/org/gcube/informationsystem/utils/TypeVersionTest.java @@ -6,7 +6,7 @@ import org.junit.Test; public class TypeVersionTest { String[] validVersions = new String[] {"1.0.0", "13.23.45", "1.12.3"}; - String[] inValidVersions = new String[] {"1.0", "01.23.45", "1.02.3"}; + String[] inValidVersions = new String[] {"1.0", "01.23.45", "1.02.3", "0.1.0"}; @Test public void testPatterns() throws Exception {