Reorganizing classes

This commit is contained in:
Luca Frosini 2024-06-05 15:42:14 +02:00
parent 2c46cbbdc8
commit 68b868b221
4 changed files with 376 additions and 323 deletions

View File

@ -0,0 +1,316 @@
package org.gcube.informationsystem.base.impl.properties;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
import org.gcube.com.fasterxml.jackson.annotation.JsonSetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonTypeName;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.base.reference.properties.PropertyFullInfo;
import org.gcube.informationsystem.types.PropertyTypeName;
import org.gcube.informationsystem.types.PropertyTypeName.BaseType;
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
// @JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY)
@JsonTypeName(value=PropertyFullInfo.NAME)
public class PropertyFullInfoImpl extends PropertyBasicInfoImpl implements PropertyFullInfo {
/**
* Generated Serial Version UID
*/
private static final long serialVersionUID = -5925314595659292025L;
protected Logger logger = LoggerFactory.getLogger(this.getClass());
protected boolean mandatory = false;
protected boolean readonly = false;
protected boolean notnull = false;
protected Integer max= null;
protected Integer min= null;
protected String regexp= null;
protected String defaultValue=null;
protected PropertyTypeName propertyTypeName = null;
protected PropertyFullInfoImpl() {
}
public static void checkRegex(String regex, String text) {
try {
Pattern pattern = Pattern.compile(regex);
if(text!=null) {
Matcher matcher = pattern.matcher(text);
if(!matcher.find()) {
throw new RuntimeException("The value '" + text + "' does not match the requested regular expression '" + regex + "'.");
}
}
} catch (PatternSyntaxException e) {
throw new RuntimeException("'" + regex + "' is not a valid regular expression", e);
} catch (RuntimeException e) {
throw e;
}
}
public static String evaluateNullForDefaultValue(String defaultValueAsString) {
if(defaultValueAsString==null || defaultValueAsString.compareTo("null")==0) {
return null;
}
return defaultValueAsString;
}
public static Object evaluateDefaultValue(BaseType baseType, String defaultValueAsString) {
if(defaultValueAsString==null || defaultValueAsString.compareTo("null")==0) {
return null;
}
if(defaultValueAsString!=null) {
switch (baseType) {
case BOOLEAN:
return Boolean.parseBoolean(defaultValueAsString);
case INTEGER:
return Integer.parseInt(defaultValueAsString);
case SHORT:
return Short.parseShort(defaultValueAsString);
case LONG:
return Long.parseLong(defaultValueAsString);
case FLOAT:
return Float.parseFloat(defaultValueAsString);
case DOUBLE:
return Double.parseDouble(defaultValueAsString);
case DATE:
SimpleDateFormat sdf = new SimpleDateFormat(Element.DATETIME_PATTERN);
try {
return sdf.parse(defaultValueAsString);
} catch (ParseException e) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("Error while parsing annotated default ");
stringBuffer.append(Date.class.getSimpleName());
stringBuffer.append(". The provided default value is '");
stringBuffer.append(defaultValueAsString);
stringBuffer.append("' which does not match the pattern '");
stringBuffer.append(Element.DATETIME_PATTERN);
stringBuffer.append("'.");
throw new RuntimeException(stringBuffer.toString(), e);
}
case STRING:
return defaultValueAsString;
case BINARY:
return defaultValueAsString.getBytes();
case BYTE:
return Byte.parseByte(defaultValueAsString);
case PROPERTY:
return null;
default:
break;
}
}
return null;
}
@Override
public String getName() {
return name;
}
@Override
public String getDescription() {
return description;
}
@Override
public boolean isMandatory() {
return mandatory;
}
@Override
public boolean isReadonly() {
return readonly;
}
@Override
public boolean isNotnull() {
return notnull;
}
@Override
public Integer getMax() {
return max;
}
@Override
public Integer getMin() {
return min;
}
@Override
public String getRegexp() {
return regexp;
}
@Override
public String getPropertyType() {
return propertyTypeName.toString();
}
@JsonSetter(value = PropertyDefinition.PROPERTY_TYPE_PROPERTY)
public void setPropertyType(String type) {
this.propertyTypeName = new PropertyTypeName(type);
}
@JsonIgnore
public PropertyTypeName getPropertyTypeName() {
return propertyTypeName;
}
@Override
public String toString() {
return "Property ["
+ "name=" + name
+ ", description=" + description
+ ", mandatory=" + mandatory
+ ", readonly=" + readonly
+ ", notnull=" + notnull
+ ", max=" + max
+ ", min=" + min
+ ", regexpr=" + regexp
+ ", type=" + propertyTypeName.toString()
+ ", defaultValue=" + defaultValue.toString()
+ "]";
}
@Override
public int hashCode() {
return Objects.hash(name, description, mandatory, readonly, notnull, max, min, regexp, propertyTypeName.toString(), defaultValue);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
PropertyFullInfoImpl other = (PropertyFullInfoImpl) obj;
return Objects.equals(name, other.name) &&
Objects.equals(description, other.description) &&
mandatory == other.mandatory &&
readonly == other.readonly &&
notnull == other.notnull &&
Objects.equals(max, other.max) &&
Objects.equals(min, other.min) &&
Objects.equals(regexp, other.regexp) &&
Objects.equals(propertyTypeName.toString(), other.propertyTypeName.toString()) &&
Objects.equals(defaultValue, other.defaultValue);
}
protected int compareIntegers(Integer thisInt, Integer otherInt) {
Integer thisInteger = thisInt == null ? Integer.MAX_VALUE : thisInt;
Integer otherInteger = otherInt == null ? Integer.MAX_VALUE : otherInt;
return thisInteger.compareTo(otherInteger);
}
@Override
public int compareTo(PropertyFullInfo other) {
if (this == other) {
return 0;
}
if (other == null) {
return -1;
}
if (getClass() != other.getClass()) {
return -1;
}
PropertyFullInfoImpl o = (PropertyFullInfoImpl) other;
int ret = 0;
ret = name.compareTo(o.name);
if(ret != 0) {
return ret;
}
ret = description.compareTo(o.description);
if(ret != 0) {
return ret;
}
ret = Boolean.compare(mandatory, o.mandatory);
if(ret != 0) {
return ret;
}
ret = Boolean.compare(readonly, o.readonly);
if(ret != 0) {
return ret;
}
ret = Boolean.compare(notnull, o.notnull);
if(ret != 0) {
return ret;
}
ret = compareIntegers(max, o.max);
if(ret != 0) {
return ret;
}
ret = compareIntegers(min, o.min);
if(ret != 0) {
return ret;
}
if(regexp==null && o.regexp!=null) {
return -1;
}
if(o.regexp==null && regexp!=null) {
return -1;
}
if(!(regexp==null && o.regexp==null)) {
ret = regexp.compareTo(o.regexp);
if(ret != 0) {
return ret;
}
}
ret = propertyTypeName.toString().compareTo(o.propertyTypeName.toString());
if(ret != 0) {
return ret;
}
return defaultValue.compareTo(o.defaultValue);
}
}

View File

@ -0,0 +1,51 @@
package org.gcube.informationsystem.base.reference.properties;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.gcube.com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.gcube.informationsystem.types.annotations.ISProperty;
import org.gcube.informationsystem.types.impl.properties.PropertyDefinitionImpl;
import org.gcube.informationsystem.types.reference.Change;
import org.gcube.informationsystem.types.reference.TypeMetadata;
import org.gcube.informationsystem.utils.Version;
/**
* @author Luca Frosini (ISTI - CNR)
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(as = PropertyDefinitionImpl.class)
@TypeMetadata(name = PropertyFullInfo.NAME, description = "This type provides full information for the definition of any properties", version = Version.MINIMAL_VERSION_STRING)
@Change(version = Version.MINIMAL_VERSION_STRING, description = Version.MINIMAL_VERSION_DESCRIPTION)
public interface PropertyFullInfo extends PropertyBasicInfo, Comparable<PropertyFullInfo> {
public static final String NAME = "PropertyFullInfo"; // PropertyFullInfo.class.getSimpleName();
public static final String MANDATORY_PROPERTY = "mandatory";
public static final String READ_ONLY_PROPERTY = "readonly";
public static final String NOT_NULL_PROPERTY = "notnull";
public static final String MAX_PROPERTY = "max";
public static final String MIN_PROPERTY = "min";
public static final String REGEX_PROPERTY = "regexp";
public static final String PROPERTY_TYPE_PROPERTY = "propertyType";
@ISProperty(name = MANDATORY_PROPERTY, readonly = false, mandatory = true, nullable = false)
public boolean isMandatory();
@ISProperty(name = READ_ONLY_PROPERTY, readonly = false, mandatory = true, nullable = false)
public boolean isReadonly();
@ISProperty(name = NOT_NULL_PROPERTY, readonly = false, mandatory = true, nullable = false)
public boolean isNotnull();
@ISProperty(name = MAX_PROPERTY, readonly = false, mandatory = true, nullable = false)
public Integer getMax();
@ISProperty(name = MIN_PROPERTY, readonly = false, mandatory = true, nullable = false)
public Integer getMin();
@ISProperty(name = REGEX_PROPERTY, readonly = false, mandatory = true, nullable = false)
public String getRegexp();
@ISProperty(name = PROPERTY_TYPE_PROPERTY, readonly = false, mandatory = true, nullable = false)
public String getPropertyType();
}

View File

@ -1,49 +1,20 @@
package org.gcube.informationsystem.types.impl.properties;
import java.lang.reflect.Method;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
import org.gcube.com.fasterxml.jackson.annotation.JsonSetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonTypeName;
import org.gcube.informationsystem.base.impl.properties.PropertyBasicInfoImpl;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.base.impl.properties.PropertyFullInfoImpl;
import org.gcube.informationsystem.types.PropertyTypeName;
import org.gcube.informationsystem.types.PropertyTypeName.BaseType;
import org.gcube.informationsystem.types.annotations.ISProperty;
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
import org.gcube.informationsystem.utils.TypeUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
// @JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY)
@JsonTypeName(value=PropertyDefinition.NAME)
public final class PropertyDefinitionImpl extends PropertyBasicInfoImpl implements PropertyDefinition {
/**
* Generated Serial Version UID
*/
private static final long serialVersionUID = -5925314595659292025L;
private static Logger logger = LoggerFactory.getLogger(PropertyDefinitionImpl.class);
private boolean mandatory = false;
private boolean readonly = false;
private boolean notnull = false;
private Integer max= null;
private Integer min= null;
private String regexp= null;
private String defaultValue=null;
private PropertyTypeName propertyTypeName = null;
public final class PropertyDefinitionImpl extends PropertyFullInfoImpl implements PropertyDefinition {
private static String getPropertyNameFromMethodName(Method method){
String name = method.getName();
@ -62,96 +33,6 @@ public final class PropertyDefinitionImpl extends PropertyBasicInfoImpl implemen
return name;
}
protected PropertyDefinitionImpl() {
}
public static void checkRegex(String regex, String text) {
try {
Pattern pattern = Pattern.compile(regex);
if(text!=null) {
Matcher matcher = pattern.matcher(text);
if(!matcher.find()) {
throw new RuntimeException("The value '" + text + "' does not match the requested regular expression '" + regex + "'.");
}
}
} catch (PatternSyntaxException e) {
throw new RuntimeException("'" + regex + "' is not a valid regular expression", e);
} catch (RuntimeException e) {
throw e;
}
}
public static String evaluateNullForDefaultValue(String defaultValueAsString) {
if(defaultValueAsString==null || defaultValueAsString.compareTo("null")==0) {
return null;
}
return defaultValueAsString;
}
public static Object evaluateDefaultValue(BaseType baseType, String defaultValueAsString) {
if(defaultValueAsString==null || defaultValueAsString.compareTo("null")==0) {
return null;
}
if(defaultValueAsString!=null) {
switch (baseType) {
case BOOLEAN:
return Boolean.parseBoolean(defaultValueAsString);
case INTEGER:
return Integer.parseInt(defaultValueAsString);
case SHORT:
return Short.parseShort(defaultValueAsString);
case LONG:
return Long.parseLong(defaultValueAsString);
case FLOAT:
return Float.parseFloat(defaultValueAsString);
case DOUBLE:
return Double.parseDouble(defaultValueAsString);
case DATE:
SimpleDateFormat sdf = new SimpleDateFormat(Element.DATETIME_PATTERN);
try {
return sdf.parse(defaultValueAsString);
} catch (ParseException e) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("Error while parsing annotated default ");
stringBuffer.append(Date.class.getSimpleName());
stringBuffer.append(". The provided default value is '");
stringBuffer.append(defaultValueAsString);
stringBuffer.append("' which does not match the pattern '");
stringBuffer.append(Element.DATETIME_PATTERN);
stringBuffer.append("'.");
throw new RuntimeException(stringBuffer.toString(), e);
}
case STRING:
return defaultValueAsString;
case BINARY:
return defaultValueAsString.getBytes();
case BYTE:
return Byte.parseByte(defaultValueAsString);
case PROPERTY:
return null;
default:
break;
}
}
return null;
}
public PropertyDefinitionImpl(ISProperty propertyAnnotation, Method method) {
String name = propertyAnnotation.name().isEmpty()?getPropertyNameFromMethodName(method):propertyAnnotation.name();
this.name = name;
@ -170,7 +51,7 @@ public final class PropertyDefinitionImpl extends PropertyBasicInfoImpl implemen
String defaultValueString = propertyAnnotation.defaultValue();
this.defaultValue = evaluateNullForDefaultValue(defaultValueString);
// The default value is evaluated to test if compliant with the declared type
PropertyDefinitionImpl.evaluateDefaultValue(propertyTypeName.getBaseType(), defaultValue);
PropertyFullInfoImpl.evaluateDefaultValue(propertyTypeName.getBaseType(), defaultValue);
Class<?> clz = method.getReturnType();
logger.trace("Return Type for method {} is {}", method, clz);
@ -198,176 +79,11 @@ public final class PropertyDefinitionImpl extends PropertyBasicInfoImpl implemen
}
}
@Override
public String getName() {
return name;
}
@Override
public String getDescription() {
return description;
}
@Override
public boolean isMandatory() {
return mandatory;
}
@Override
public boolean isReadonly() {
return readonly;
}
@Override
public boolean isNotnull() {
return notnull;
}
@Override
public Integer getMax() {
return max;
}
@Override
public Integer getMin() {
return min;
}
@Override
public String getRegexp() {
return regexp;
}
@Override
public String getPropertyType() {
return propertyTypeName.toString();
}
/**
* Generated Serial Version UID
*/
private static final long serialVersionUID = -5925314595659292025L;
@JsonSetter(value = PropertyDefinition.PROPERTY_TYPE_PROPERTY)
public void setPropertyType(String type) {
this.propertyTypeName = new PropertyTypeName(type);
}
@JsonIgnore
public PropertyTypeName getPropertyTypeName() {
return propertyTypeName;
}
@Override
public String toString() {
return "Property [name=" + name + ", description=" + description
+ ", mandatory=" + mandatory + ", readonly=" + readonly
+ ", notnull=" + notnull + ", max=" + max + ", min="
+ min + ", regexpr=" + regexp + ", type=" + propertyTypeName.toString() + "]";
}
@Override
public int hashCode() {
return Objects.hash(name, description, mandatory, readonly, notnull, max, min, regexp, propertyTypeName.toString());
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
PropertyDefinitionImpl other = (PropertyDefinitionImpl) obj;
return Objects.equals(name, other.name) &&
Objects.equals(description, other.description) &&
mandatory == other.mandatory &&
readonly == other.readonly &&
notnull == other.notnull &&
Objects.equals(max, other.max) &&
Objects.equals(min, other.min) &&
Objects.equals(regexp, other.regexp) &&
Objects.equals(propertyTypeName.toString(), other.propertyTypeName.toString());
}
protected int compareIntegers(Integer thisInt, Integer otherInt) {
Integer thisInteger = thisInt == null ? Integer.MAX_VALUE : thisInt;
Integer otherInteger = otherInt == null ? Integer.MAX_VALUE : otherInt;
return thisInteger.compareTo(otherInteger);
}
@Override
public int compareTo(PropertyDefinition other) {
if (this == other) {
return 0;
}
if (other == null) {
return -1;
}
if (getClass() != other.getClass()) {
return -1;
}
PropertyDefinitionImpl o = (PropertyDefinitionImpl) other;
int ret = 0;
ret = name.compareTo(o.name);
if(ret != 0) {
return ret;
}
ret = description.compareTo(o.description);
if(ret != 0) {
return ret;
}
ret = Boolean.compare(mandatory, o.mandatory);
if(ret != 0) {
return ret;
}
ret = Boolean.compare(readonly, o.readonly);
if(ret != 0) {
return ret;
}
ret = Boolean.compare(notnull, o.notnull);
if(ret != 0) {
return ret;
}
ret = compareIntegers(max, o.max);
if(ret != 0) {
return ret;
}
ret = compareIntegers(min, o.min);
if(ret != 0) {
return ret;
}
if(regexp==null && o.regexp!=null) {
return -1;
}
if(o.regexp==null && regexp!=null) {
return -1;
}
if(!(regexp==null && o.regexp==null)) {
ret = regexp.compareTo(o.regexp);
if(ret != 0) {
return ret;
}
}
return propertyTypeName.toString().compareTo(o.propertyTypeName.toString());
}
@Override
public String getTypeName() {
return TypeUtility.getTypeName(this.getClass());
}
}

View File

@ -2,9 +2,8 @@ package org.gcube.informationsystem.types.reference.properties;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.gcube.com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.gcube.informationsystem.base.reference.properties.PropertyBasicInfo;
import org.gcube.informationsystem.base.reference.properties.PropertyFullInfo;
import org.gcube.informationsystem.types.annotations.Final;
import org.gcube.informationsystem.types.annotations.ISProperty;
import org.gcube.informationsystem.types.impl.properties.PropertyDefinitionImpl;
import org.gcube.informationsystem.types.reference.Change;
import org.gcube.informationsystem.types.reference.TypeMetadata;
@ -18,37 +17,8 @@ import org.gcube.informationsystem.utils.Version;
@TypeMetadata(name = PropertyDefinition.NAME, description = "This type provides information for the definition of any properties", version = Version.MINIMAL_VERSION_STRING)
@Change(version = Version.MINIMAL_VERSION_STRING, description = Version.MINIMAL_VERSION_DESCRIPTION)
@Final
public interface PropertyDefinition extends PropertyBasicInfo, Comparable<PropertyDefinition> {
public interface PropertyDefinition extends PropertyFullInfo {
public static final String NAME = "PropertyDefinition"; // PropertyDefinition.class.getSimpleName();
public static final String MANDATORY_PROPERTY = "mandatory";
public static final String READ_ONLY_PROPERTY = "readonly";
public static final String NOT_NULL_PROPERTY = "notnull";
public static final String MAX_PROPERTY = "max";
public static final String MIN_PROPERTY = "min";
public static final String REGEX_PROPERTY = "regexp";
public static final String PROPERTY_TYPE_PROPERTY = "propertyType";
@ISProperty(name = MANDATORY_PROPERTY, readonly = false, mandatory = true, nullable = false)
public boolean isMandatory();
@ISProperty(name = READ_ONLY_PROPERTY, readonly = false, mandatory = true, nullable = false)
public boolean isReadonly();
@ISProperty(name = NOT_NULL_PROPERTY, readonly = false, mandatory = true, nullable = false)
public boolean isNotnull();
@ISProperty(name = MAX_PROPERTY, readonly = false, mandatory = true, nullable = false)
public Integer getMax();
@ISProperty(name = MIN_PROPERTY, readonly = false, mandatory = true, nullable = false)
public Integer getMin();
@ISProperty(name = REGEX_PROPERTY, readonly = false, mandatory = true, nullable = false)
public String getRegexp();
@ISProperty(name = PROPERTY_TYPE_PROPERTY, readonly = false, mandatory = true, nullable = false)
public String getPropertyType();
}