Compare commits

...

5 Commits

7 changed files with 175 additions and 26 deletions

View File

@ -41,7 +41,6 @@ public class PropertyElementImpl extends ElementImpl implements Property {
super();
this.additionalProperties = new HashMap<>();
this.allowedAdditionalKeys = new HashSet<>();
this.allowedAdditionalKeys.add(SUPERTYPES_PROPERTY);
}
@Override

View File

@ -4,17 +4,25 @@
package org.gcube.informationsystem.contexts.impl.entities;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
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.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.informationsystem.base.impl.entities.EntityElementImpl;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.contexts.impl.relations.IsParentOfImpl;
import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.contexts.reference.relations.IsParentOf;
import org.gcube.informationsystem.model.impl.properties.MetadataImpl;
import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.utils.UUIDManager;
/**
@ -34,10 +42,21 @@ public final class ContextImpl extends EntityElementImpl implements Context {
protected IsParentOf parent;
protected List<IsParentOf> children;
@JsonIgnore
protected Map<String, Object> additionalProperties;
@JsonIgnore
/**
* Used to allow to have an additional property starting with '_' or '@'
*/
protected final Set<String> allowedAdditionalKeys;
protected ContextImpl() {
super();
this.parent = null;
this.children = new ArrayList<>();
this.additionalProperties = new HashMap<>();
this.allowedAdditionalKeys = new HashSet<>();
}
public ContextImpl(UUID uuid) {
@ -158,4 +177,55 @@ public final class ContextImpl extends EntityElementImpl implements Context {
children.add(isParentOf);
}
@Override
public Map<String, Object> getAdditionalProperties() {
return additionalProperties;
}
@Override
public void setAdditionalProperties(Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
}
@Override
public Object getAdditionalProperty(String key) {
return additionalProperties.get(key);
}
@Override
public void setAdditionalProperty(String key, Object value) {
if(!allowedAdditionalKeys.contains(key)){
if(key.startsWith("_")) {
return;
}
if(key.startsWith("@")) {
return;
}
}
/*
Additional properties are not deserialized to the proper property type.
The first attempt was to try to write a specific deserializer but it fails.
This fix the issue.
*/
try {
if(value instanceof Map<?,?>) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String,Object>) value;
if(map.containsKey(Element.TYPE_PROPERTY)) {
String reserialized = ElementMapper.getObjectMapper().writeValueAsString(map);
Property property = ElementMapper.unmarshal(Property.class, reserialized);
value = property;
}
}
}catch (Throwable e) {
e.getMessage();
// Any type of error/exception must be catched
}
/* END of fix to properly deserialize Property types*/
this.additionalProperties.put(key, value);
}
}

View File

@ -4,19 +4,25 @@
package org.gcube.informationsystem.contexts.reference.entities;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.annotation.JsonAnyGetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonAnySetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonGetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.gcube.com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.gcube.com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.gcube.com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.base.reference.SchemaMixedElement;
import org.gcube.informationsystem.base.reference.entities.EntityElement;
import org.gcube.informationsystem.contexts.impl.entities.ContextImpl;
import org.gcube.informationsystem.contexts.reference.relations.IsParentOf;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.serialization.AdditionalPropertiesSerializer;
import org.gcube.informationsystem.types.annotations.ISProperty;
import org.gcube.informationsystem.types.reference.Change;
import org.gcube.informationsystem.types.reference.TypeMetadata;
@ -31,14 +37,59 @@ import org.gcube.informationsystem.utils.Version;
@JsonPropertyOrder({ Element.TYPE_PROPERTY, IdentifiableElement.ID_PROPERTY, IdentifiableElement.METADATA_PROPERTY})
@TypeMetadata(name = Context.NAME, description = "This type is the used to define a Context", version = Version.MINIMAL_VERSION_STRING)
@Change(version = Version.MINIMAL_VERSION_STRING, description = Version.MINIMAL_VERSION_DESCRIPTION)
public interface Context extends EntityElement {
public interface Context extends EntityElement, SchemaMixedElement {
public static final String NAME = "Context"; // Context.class.getSimpleName();
public static final String NAME_PROPERTY = "name";
public static final String PARENT_PROPERTY = "parent";
public static final String CHILDREN_PROPERTY = "children";
/**
* {
* ...
* "availableAt" : [
* "https://i-marine.d4science.org/group/alienandinvasivespecies",
* "https://services.d4science.org/group/alienandinvasivespecies"
* ]
* ...
* }
* For non VRE context this field could be null or could have multiple value
* For VRE it is normally one value only (but some exception could exists
*/
public static final String AVAILABLE_AT_PROPERTY = "availableAt";
//
/**
* This information is provided to allowed user only (by role)
* {
* ...
* "fullpaths" : [
* {
* "fullpath": "/gcube/devsec/myTest",
* "time": "2023-12-11 15:35:41.289 +0000", // Same of creationTime in metadata
* },
* {
* "fullpath": "/gcube/devsec/myTestVRE",
* "time": "2023-12-13 16:13:12.336 +0000",
* }
* {
* "fullpath": "/gcube/devsec/myVRE",
* "time": "2023-12-18 12:37:48.172 +0000", // Same of lastUpdateTime in metadata
* }
* ]
* ...
* }
*/
public static final String CONTEXT_FULLPATH_CHANGES_LOG_PROPERTY = "fullpathChangelog";
/**
* This information is provided to allowed user only (by role)
* The symmetric key for the context
*/
public static final String CONTEXT_KEY_PROPERTY = "key";
@ISProperty(name = NAME_PROPERTY, mandatory = true, nullable = false)
public String getName();
@ -66,5 +117,20 @@ public interface Context extends EntityElement {
public void addChild(Context child);
public void addChild(IsParentOf isParentOf);
@JsonAnyGetter
@JsonSerialize(using = AdditionalPropertiesSerializer.class)
@Override
public Map<String,Object> getAdditionalProperties();
@Override
public void setAdditionalProperties(Map<String,Object> additionalProperties);
@Override
public Object getAdditionalProperty(String key);
@JsonAnySetter
@Override
public void setAdditionalProperty(String key, Object value);
}

View File

@ -11,7 +11,6 @@ import java.util.Set;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
import org.gcube.com.fasterxml.jackson.annotation.JsonTypeName;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.model.reference.ModelElement;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.serialization.ElementMapper;
@ -40,8 +39,6 @@ public abstract class FacetImpl extends EntityImpl implements Facet {
super();
this.additionalProperties = new HashMap<>();
this.allowedAdditionalKeys = new HashSet<>();
this.allowedAdditionalKeys.add(ModelElement.SUPERTYPES_PROPERTY);
this.allowedAdditionalKeys.add(ModelElement.EXPECTED_TYPE_PROPERTY);
}
@Override

View File

@ -53,7 +53,6 @@ public abstract class RelationImpl<S extends Resource, T extends Entity>
super();
this.additionalProperties = new HashMap<>();
this.allowedAdditionalKeys = new HashSet<>();
this.allowedAdditionalKeys.add(SUPERTYPES_PROPERTY);
}
protected RelationImpl(S source, T target,

View File

@ -25,6 +25,7 @@ import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.SerializationFeature;
import org.gcube.com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.JsonNodeType;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.com.fasterxml.jackson.databind.node.TextNode;
import org.gcube.informationsystem.base.reference.AccessType;
@ -32,6 +33,7 @@ import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.discovery.Discovery;
import org.gcube.informationsystem.discovery.knowledge.Knowledge;
import org.gcube.informationsystem.model.reference.ModelElement;
import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import org.slf4j.Logger;
@ -207,24 +209,26 @@ public abstract class ElementMapper {
ArrayNode arrayNode = (ArrayNode) objectNode.get(ModelElement.SUPERTYPES_PROPERTY);
String candidatedSupertype = null;
for(int i = 0; i < arrayNode.size(); i++) {
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 = 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.
candidatedSupertype = accessType.getDummyImplementationClass().getSimpleName();
if(arrayNode!=null) {
for(int i = 0; i < arrayNode.size(); i++) {
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 = 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.
candidatedSupertype = accessType.getDummyImplementationClass().getSimpleName();
}
} catch(Exception ex) {
// can continue discovery
}
} catch(Exception ex) {
// can continue discovery
break;
}
break;
}
}
@ -243,7 +247,17 @@ public abstract class ElementMapper {
}
protected static JsonNode analizeTypes(ObjectNode objectNode) {
String cls = objectNode.get(Element.TYPE_PROPERTY).asText();
String cls = null;
JsonNode typeJN = objectNode.get(Element.TYPE_PROPERTY);
if(typeJN !=null) {
cls = objectNode.get(Element.TYPE_PROPERTY).asText();
}
if(cls==null && objectNode.getNodeType()==JsonNodeType.OBJECT) {
cls = Property.NAME;
}
if(!knownTypes.containsKey(cls)) {
objectNode = setTypeToBestAvailable(objectNode);
}
@ -267,7 +281,7 @@ public abstract class ElementMapper {
}
return objectNode;
}
protected static ArrayNode analizeTypes(ArrayNode arrayNode) {
ArrayNode ret = mapper.createArrayNode();

View File

@ -33,7 +33,11 @@ public class UUIDUtility {
}
public static String getUUIDAsString(JsonNode jsonNode){
return getUUID(jsonNode).toString();
UUID uuid = getUUID(jsonNode);
if(uuid!=null) {
return uuid.toString();
}
return null;
}
public static String getUUIDAsString(String json) throws JsonProcessingException, IOException{