Added expectedtype property to keep track of original type

This commit is contained in:
Luca Frosini 2023-04-28 17:16:31 +02:00
parent 21407336bb
commit 4b06b1b6bc
13 changed files with 59 additions and 23 deletions

View File

@ -22,8 +22,9 @@ public class ElementImpl implements Element {
*/
private static final long serialVersionUID = 7338083489551084860L;
private List<String> supertypes;
protected List<String> supertypes;
protected String expectedtype;
@Override
public String getTypeName() {
return TypeUtility.getTypeName(this.getClass());
@ -34,6 +35,11 @@ public class ElementImpl implements Element {
return this.supertypes;
}
@Override
public String getExpectedtype() {
return this.expectedtype;
}
@Override
public String toString(){
StringWriter stringWriter = new StringWriter();

View File

@ -21,7 +21,7 @@ import org.gcube.informationsystem.utils.Version;
* @author Luca Frosini (ISTI - CNR)
*/
@Abstract
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY })
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, Element.EXPECTED_TYPE_PROPERTY })
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = Element.TYPE_PROPERTY)
// @JsonTypeIdResolver(ElementTypeIdResolver.class)
@TypeMetadata(name = Element.NAME, description = "This is the base type for Element", version = Version.MINIMAL_VERSION_STRING)
@ -34,6 +34,13 @@ public interface Element extends Serializable {
public static final String SUPERTYPES_PROPERTY = "supertypes";
/*
* This is the key used by the deserializer to indicate the expected type
* which instead has been deserialized using the best available
* supertype
*/
public static final String EXPECTED_TYPE_PROPERTY = "expectedtype";
/**
* DateTime Pattern to be used to serialize Dates in every element
*/
@ -46,4 +53,8 @@ public interface Element extends Serializable {
@JsonGetter(value = SUPERTYPES_PROPERTY)
public List<String> getSupertypes();
@JsonInclude(Include.NON_EMPTY)
@JsonGetter(value = EXPECTED_TYPE_PROPERTY)
public String getExpectedtype();
}

View File

@ -12,7 +12,7 @@ import org.gcube.informationsystem.model.reference.properties.Metadata;
*
* @author Luca Frosini (ISTI - CNR)
*/
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY })
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, Element.EXPECTED_TYPE_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY })
public interface IdentifiableElement extends Element {
public static final String ID_PROPERTY = "id";

View File

@ -18,7 +18,7 @@ import org.gcube.informationsystem.utils.Version;
*/
@Abstract
@JsonIgnoreProperties(ignoreUnknown=true)
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY })
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, Element.EXPECTED_TYPE_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY })
//@JsonDeserialize(as=EntityElementImpl.class) Do not uncomment to manage subclasses
@TypeMetadata(name = EntityElement.NAME, description = "This is the base type for any EntityElement", version = Version.MINIMAL_VERSION_STRING)
@Change(version = Version.MINIMAL_VERSION_STRING, description = Version.MINIMAL_VERSION_DESCRIPTION)

View File

@ -16,7 +16,7 @@ import org.gcube.informationsystem.base.reference.IdentifiableElement;
*
* @author Luca Frosini (ISTI - CNR)
*/
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY, ERElement.CONTEXTS_PROPERTY })
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, Element.EXPECTED_TYPE_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY, ERElement.CONTEXTS_PROPERTY })
public interface ERElement extends IdentifiableElement {
/**

View File

@ -19,7 +19,7 @@ import org.gcube.informationsystem.utils.Version;
* @author Luca Frosini (ISTI - CNR)
*/
@Abstract
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY, ERElement.CONTEXTS_PROPERTY })
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, Element.EXPECTED_TYPE_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY, ERElement.CONTEXTS_PROPERTY })
//@JsonDeserialize(as=EntityImpl.class) Do not uncomment to manage subclasses
@TypeMetadata(name = Entity.NAME, description = "This is the base type for any Entity", version = Version.MINIMAL_VERSION_STRING)
@Change(version = Version.MINIMAL_VERSION_STRING, description = Version.MINIMAL_VERSION_DESCRIPTION)

View File

@ -29,7 +29,7 @@ import org.gcube.informationsystem.utils.Version;
*/
@Abstract
@JsonIgnoreProperties(ignoreUnknown=true)
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY, ERElement.CONTEXTS_PROPERTY, Resource.CONSISTS_OF_PROPERTY, Resource.IS_RELATED_TO_PROPERTY })
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, Element.EXPECTED_TYPE_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY, ERElement.CONTEXTS_PROPERTY, Resource.CONSISTS_OF_PROPERTY, Resource.IS_RELATED_TO_PROPERTY })
// @JsonDeserialize(as=ResourceImpl.class) Do not uncomment to manage subclasses
@ResourceSchema(
facets={

View File

@ -26,7 +26,7 @@ import org.gcube.informationsystem.utils.Version;
* @author Luca Frosini (ISTI - CNR)
*/
// @JsonIgnoreProperties(ignoreUnknown=true)
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY })
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, Element.EXPECTED_TYPE_PROPERTY })
@JsonDeserialize(as=PropertyImpl.class)
@TypeMetadata(name = Property.NAME, description = "This is the base type for any Property", version = Version.MINIMAL_VERSION_STRING)
@Change(version = Version.MINIMAL_VERSION_STRING, description = Version.MINIMAL_VERSION_DESCRIPTION)

View File

@ -35,7 +35,7 @@ import org.gcube.informationsystem.utils.Version;
* https://wiki.gcube-system.org/gcube/Facet_Based_Resource_Model#Relation
*/
@Abstract
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY, ERElement.CONTEXTS_PROPERTY, Relation.PROPAGATION_CONSTRAINT_PROPERTY })
@JsonPropertyOrder({ Element.TYPE_PROPERTY, Element.SUPERTYPES_PROPERTY, Element.EXPECTED_TYPE_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY, ERElement.CONTEXTS_PROPERTY, Relation.PROPAGATION_CONSTRAINT_PROPERTY })
// @JsonDeserialize(as=RelationImpl.class) Do not uncomment to manage subclasses
@TypeMetadata(name = Relation.NAME, description = "This is the base type for any Relation", version = Version.MINIMAL_VERSION_STRING)
@Change(version = Version.MINIMAL_VERSION_STRING, description = Version.MINIMAL_VERSION_DESCRIPTION)

View File

@ -57,6 +57,9 @@ public class ElementDeserializer<ISM extends Element> extends StdDeserializer<IS
List<TextNode> toBeKeepSuperClasses = new ArrayList<>();
ObjectNode objectNode = (ObjectNode) treeNode;
if(!objectNode.has(Element.EXPECTED_TYPE_PROPERTY)) {
objectNode.set(Element.EXPECTED_TYPE_PROPERTY, objectNode.get(Element.TYPE_PROPERTY));
}
try {
JsonNode superClassesTreeNode = objectNode

View File

@ -204,22 +204,20 @@ public abstract class ElementMapper {
String unknownType = jsonNode.get(Element.TYPE_PROPERTY).asText();
ArrayNode arrayNode = (ArrayNode) jsonNode.get(Element.SUPERTYPES_PROPERTY);
// TODO save original class and superclasses
String candidatedSuperClass = null;
String candidatedSupertype = null;
for(int i = 0; i < arrayNode.size(); i++) {
String superClass = arrayNode.get(i).asText();
if(knownTypes.containsKey(superClass)) {
candidatedSuperClass = superClass;
String superType = arrayNode.get(i).asText();
if(knownTypes.containsKey(superType)) {
candidatedSupertype = superType;
try {
// Checking if it is one of the base type. In some cases we need to use dummy
// implementation
AccessType accessType = Enum.valueOf(AccessType.class, superClass.toUpperCase());
AccessType accessType = AccessType.getAccessType(superType);
// It is one of the BaseType.
// Looking if we need to set the dummy implementation class
if(accessType.getDummyImplementationClass()!=null) {
// This should not happen because the type has been assigned already to the dummy class.
candidatedSuperClass = accessType.getDummyImplementationClass().getSimpleName();
candidatedSupertype = accessType.getDummyImplementationClass().getSimpleName();
}
} catch(Exception ex) {
// can continue discovery
@ -228,8 +226,12 @@ public abstract class ElementMapper {
}
}
if(candidatedSuperClass!=null) {
((ObjectNode) jsonNode).set(Element.TYPE_PROPERTY, new TextNode(candidatedSuperClass));
if(candidatedSupertype!=null) {
if(!jsonNode.has(Element.EXPECTED_TYPE_PROPERTY)) {
((ObjectNode) jsonNode).set(Element.EXPECTED_TYPE_PROPERTY, jsonNode.get(Element.TYPE_PROPERTY));
}
((ObjectNode) jsonNode).set(Element.TYPE_PROPERTY, new TextNode(candidatedSupertype));
((ObjectNode) jsonNode).remove(Element.SUPERTYPES_PROPERTY);
return jsonNode;
}
@ -314,7 +316,7 @@ public abstract class ElementMapper {
JsonNode jsonNode = mapper.readTree(string);
jsonNode = analizeFullJson(jsonNode);
return ElementMapper.unmarshal(clz, mapper.writeValueAsString(jsonNode));
}
}
}
public static <El extends Element> List<El> unmarshalList(Class<El> clz, String string)

View File

@ -62,7 +62,8 @@ public class TypeImpl implements Type {
protected UUID uuid;
protected Metadata metadata;
private List<String> supertypes;
protected List<String> supertypes;
protected String expectedtype;
protected String name;
protected String description;
@ -301,4 +302,10 @@ public class TypeImpl implements Type {
public List<String> getSupertypes() {
return this.supertypes;
}
@Override
public String getExpectedtype() {
return this.expectedtype;
}
}

View File

@ -36,7 +36,8 @@ public final class PropertyDefinitionImpl implements PropertyDefinition {
public final static String URI_REGEX = null;
public final static String URL_REGEX = null;
private List<String> supertypes;
protected List<String> supertypes;
protected String expectedtype;
private String name= "";
private String description= "";
@ -294,4 +295,10 @@ public final class PropertyDefinitionImpl implements PropertyDefinition {
public List<String> getSupertypes() {
return this.supertypes;
}
@Override
public String getExpectedtype() {
return this.expectedtype;
}
}