diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java index e83ec55..4ae330b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java @@ -28,6 +28,7 @@ import org.gcube.common.authorization.library.provider.CalledMethodProvider; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.base.reference.IdentifiableElement; +import org.gcube.informationsystem.model.reference.ERElement; import org.gcube.informationsystem.model.reference.properties.Metadata; import org.gcube.informationsystem.model.reference.properties.Property; import org.gcube.informationsystem.model.reference.relations.Relation; @@ -49,8 +50,8 @@ import org.gcube.informationsystem.resourceregistry.types.CachedType; import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.utils.MetadataOrient; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility; -import org.gcube.informationsystem.resourceregistry.utils.UUIDUtility; import org.gcube.informationsystem.resourceregistry.utils.OrientDBUtility; +import org.gcube.informationsystem.resourceregistry.utils.UUIDUtility; import org.gcube.informationsystem.types.reference.Type; import org.gcube.informationsystem.types.reference.entities.ResourceType; import org.gcube.informationsystem.types.reference.properties.PropertyDefinition; @@ -394,26 +395,42 @@ public abstract class ElementManagement { ObjectMapper objectMapper = new ObjectMapper(); ObjectNode objectNode = objectMapper.createObjectNode(); - objectNode.put(Element.TYPE_PROPERTY, getTypeName()); - - Collection supertypes = getCachedType().getSuperTypes(); - ArrayNode arrayNode = objectMapper.valueToTree(supertypes); - objectNode.replace(Element.SUPERTYPES_PROPERTY, arrayNode); - - OElement element = getElement(); Set keys = element.getPropertyNames(); /* Add first these key to provide an order in Json */ List keysToAddFirst = new ArrayList<>(); + keysToAddFirst.add(Element.TYPE_PROPERTY); + keysToAddFirst.add(Element.SUPERTYPES_PROPERTY); keysToAddFirst.add(IdentifiableElement.ID_PROPERTY); keysToAddFirst.add(IdentifiableElement.METADATA_PROPERTY); + if(ContextUtility.getIncludeInstanceContexts().get()) { + keysToAddFirst.add(ERElement.CONTEXTS_PROPERTY); + } keysToAddFirst.add(Relation.PROPAGATION_CONSTRAINT_PROPERTY); for(String key : keysToAddFirst) { - if(keys.contains(key)) { - analizeProperty(element, key, objectNode); + switch (key) { + case Element.TYPE_PROPERTY: + objectNode.put(Element.TYPE_PROPERTY, getTypeName()); + break; + + case Element.SUPERTYPES_PROPERTY: + Collection supertypes = getCachedType().getSuperTypes(); + ArrayNode arrayNode = objectMapper.valueToTree(supertypes); + objectNode.replace(Element.SUPERTYPES_PROPERTY, arrayNode); + break; + + case ERElement.CONTEXTS_PROPERTY: + objectNode.replace(ERElement.CONTEXTS_PROPERTY, getContextsAsObjectNode()); + break; + + default: + if(keys.contains(key)) { + analizeProperty(element, key, objectNode); + } + break; } } @@ -1165,10 +1182,8 @@ public abstract class ElementManagement { // Keeping the metadata MetadataOrient metadataOrient = MetadataUtility.getMetadataOrient((ODocument) object); JsonNode metadataJson = OrientDBUtility.toJsonNode(metadataOrient); - if(ContextUtility.getIncludeInstanceContexts().get()) { - ((ObjectNode) metadataJson).set(Metadata.__CONTEXTS, getContextsAsObjectNode()); - } + // TODO check a solution for supertypes TypesCache typesCache = TypesCache.getInstance(); @SuppressWarnings("unchecked") CachedType> metadataType = (CachedType>) typesCache.getCachedType(Metadata.NAME); @@ -1258,30 +1273,6 @@ public abstract class ElementManagement { } } - /* - protected List getSuperclasses() throws SchemaException, ResourceRegistryException { - List typeSuperTypes = new ArrayList<>(); - List allSuperClasses = getOClass().getSuperClasses(); - while(allSuperClasses.size()>0) { - List toBeAnalysed = new ArrayList<>(allSuperClasses); - allSuperClasses = new ArrayList<>(); - for(OClass oSuperClass : toBeAnalysed) { - String name = oSuperClass.getName(); - if(name.compareTo(StringFactory.V.toUpperCase()) == 0 || name.compareTo(StringFactory.E.toUpperCase()) == 0 - || name.compareTo(DatabaseEnvironment.O_RESTRICTED_CLASS) == 0) { - continue; - } - if(superClassesToBeExcluded.contains(name)) { - continue; - } - typeSuperTypes.add(typeSuperTypes.size(), name); - allSuperClasses.addAll(oSuperClass.getSuperClasses()); - } - } - return typeSuperTypes; - } - */ - protected String getNotNullErrorMessage(String fieldName) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("The type "); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/TypeManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/TypeManagement.java index 514eba5..652ef45 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/TypeManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/TypeManagement.java @@ -463,7 +463,6 @@ public class TypeManagement { return true; } - // TODO protected void updateTypeSchema(Type actualTypeDefinition, Type newTypeDefinition, AccessType baseElementAccessType) throws SchemaNotFoundException, SchemaException { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/utils/EncryptedOrient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/utils/EncryptedOrient.java index 13e39a8..405967d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/utils/EncryptedOrient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/utils/EncryptedOrient.java @@ -1,16 +1,26 @@ package org.gcube.informationsystem.resourceregistry.utils; import java.security.Key; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; import org.gcube.common.encryption.encrypter.StringEncrypter; +import org.gcube.informationsystem.base.reference.properties.PropertyElement; +import org.gcube.informationsystem.model.reference.properties.Encrypted; +import org.gcube.informationsystem.model.reference.properties.Property; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment; +import org.gcube.informationsystem.resourceregistry.types.CachedType; +import org.gcube.informationsystem.resourceregistry.types.TypesCache; +import org.gcube.informationsystem.types.reference.properties.PropertyType; +import org.gcube.informationsystem.utils.TypeUtility; import com.orientechnologies.orient.core.record.impl.ODocument; /** * @author Luca Frosini (ISTI - CNR) */ -public class EncryptedOrient extends ODocument { +public class EncryptedOrient extends ODocument implements Encrypted { public static final String NAME = "Encrypted"; public static final String VALUE = "value"; @@ -26,6 +36,25 @@ public class EncryptedOrient extends ODocument { protected EncryptedOrient(String iClassName) { super(iClassName); } + + @Override + public String getTypeName() { + return TypeUtility.getTypeName(this.getClass()); + } + + @Override + public List getSupertypes() { + TypesCache typesCache = TypesCache.getInstance(); + @SuppressWarnings("unchecked") + CachedType> cachedType = (CachedType>) typesCache.getCachedType(getTypeName()); + try { + return cachedType.getSuperTypes(); + } catch (Exception e) { + List list = new ArrayList<>(); + list.add(TypeUtility.getTypeName(Property.class)); + return list; + } + } public String getEncryptedValue() { return this.field(EncryptedOrient.VALUE); @@ -35,13 +64,6 @@ public class EncryptedOrient extends ODocument { this.field(EncryptedOrient.VALUE, encryptedValue); } - @Override - public String toJSON(String iFormat) { - String ret = super.toJSON(iFormat); - ret = OrientDBUtility.replaceType(ret); - return ret; - } - public String getDecryptedValue() { return decryptedValue; } @@ -72,5 +94,42 @@ public class EncryptedOrient extends ODocument { } } + + @Override + public Map getAdditionalProperties() { + return null; + } + + @Override + public void setAdditionalProperties(Map additionalProperties) { + + } + + @Override + public Object getAdditionalProperty(String key) { + return null; + } + + @Override + public void setAdditionalProperty(String key, Object value) { + + } + + @Override + public String toJSON(String iFormat) { + String ret = super.toJSON(iFormat); + ret = OrientDBUtility.replaceType(ret); + return ret; + } + + @Override + public String getValue() { + return getEncryptedValue(); + } + + @Override + public void setValue(String value) { + setEncryptedValue(value); + } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/utils/MetadataOrient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/utils/MetadataOrient.java index 0a860ab..e3221cc 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/utils/MetadataOrient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/utils/MetadataOrient.java @@ -85,24 +85,22 @@ public class MetadataOrient extends ODocument implements Metadata { @Override public Map getAdditionalProperties() { - // TODO Auto-generated method stub return null; } @Override public void setAdditionalProperties(Map additionalProperties) { - // TODO Auto-generated method stub + } @Override public Object getAdditionalProperty(String key) { - // TODO Auto-generated method stub return null; } @Override public void setAdditionalProperty(String key, Object value) { - // TODO Auto-generated method stub + } @Override diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java index e59c1bb..7c5f21f 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java @@ -7,7 +7,6 @@ import java.util.Set; import java.util.UUID; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; -import org.gcube.informationsystem.model.reference.properties.Metadata; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint; @@ -156,10 +155,11 @@ public class AddToContextTest extends MultiContextTest { ContextUtility.getIncludeInstanceContexts().set(true); String json = getResourceManagement(dataset).read(); + logger.trace("Resource with contexts in HierarchicalMode from server is {}", json); Dataset r1 = ElementMapper.unmarshal(Dataset.class, json); + logger.trace("Resource with contexts in HierarchicalMode with Jackson is {}", r1.toString()); - @SuppressWarnings("unchecked") - Map contextsR1 = (Map) r1.getMetadata().getAdditionalProperty(Metadata.__CONTEXTS); + Map contextsR1 = r1.getContexts(); Set contextsR1Fullname = new HashSet<>(contextsR1.values()); logger.info("Contexts of {} with UUID {} and Id {} are {}", Dataset.NAME, r1.getID(), ((IdentifierFacet)r1.getIdentificationFacets().get(0)).getValue(), contextsR1Fullname); Assert.assertTrue(contextsR1.size()==expectedContextSize);