Reorganizing property order

This commit is contained in:
Luca Frosini 2023-04-28 14:52:46 +02:00
parent 7d7b9478bb
commit 3776bdae50
5 changed files with 100 additions and 53 deletions

View File

@ -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<El extends OElement, T extends Type> {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put(Element.TYPE_PROPERTY, getTypeName());
Collection<String> supertypes = getCachedType().getSuperTypes();
ArrayNode arrayNode = objectMapper.valueToTree(supertypes);
objectNode.replace(Element.SUPERTYPES_PROPERTY, arrayNode);
OElement element = getElement();
Set<String> keys = element.getPropertyNames();
/* Add first these key to provide an order in Json */
List<String> 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<String> 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<El extends OElement, T extends Type> {
// 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<PropertyType<Property>> metadataType = (CachedType<PropertyType<Property>>) typesCache.getCachedType(Metadata.NAME);
@ -1258,30 +1273,6 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
}
}
/*
protected List<String> getSuperclasses() throws SchemaException, ResourceRegistryException {
List<String> typeSuperTypes = new ArrayList<>();
List<OClass> allSuperClasses = getOClass().getSuperClasses();
while(allSuperClasses.size()>0) {
List<OClass> 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 ");

View File

@ -463,7 +463,6 @@ public class TypeManagement {
return true;
}
// TODO
protected void updateTypeSchema(Type actualTypeDefinition, Type newTypeDefinition, AccessType baseElementAccessType)
throws SchemaNotFoundException, SchemaException {

View File

@ -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<String> getSupertypes() {
TypesCache typesCache = TypesCache.getInstance();
@SuppressWarnings("unchecked")
CachedType<PropertyType<PropertyElement>> cachedType = (CachedType<PropertyType<PropertyElement>>) typesCache.getCachedType(getTypeName());
try {
return cachedType.getSuperTypes();
} catch (Exception e) {
List<String> 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<String, Object> getAdditionalProperties() {
return null;
}
@Override
public void setAdditionalProperties(Map<String, Object> 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);
}
}

View File

@ -85,24 +85,22 @@ public class MetadataOrient extends ODocument implements Metadata {
@Override
public Map<String, Object> getAdditionalProperties() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setAdditionalProperties(Map<String, Object> 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

View File

@ -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<String,String> contextsR1 = (Map<String, String>) r1.getMetadata().getAdditionalProperty(Metadata.__CONTEXTS);
Map<UUID,String> contextsR1 = r1.getContexts();
Set<String> 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);