Redesigning E/R instance definition

This commit is contained in:
Luca Frosini 2023-04-18 17:52:07 +02:00
parent f382c91db7
commit 79c0c9a0ab
22 changed files with 239 additions and 408 deletions

View File

@ -31,7 +31,7 @@
<dependency> <dependency>
<groupId>org.gcube.distribution</groupId> <groupId>org.gcube.distribution</groupId>
<artifactId>gcube-smartgears-bom</artifactId> <artifactId>gcube-smartgears-bom</artifactId>
<version>2.3.0</version> <version>2.5.0-SNAPSHOT</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>

View File

@ -10,7 +10,6 @@ import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.contexts.reference.relations.IsParentOf; import org.gcube.informationsystem.contexts.reference.relations.IsParentOf;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextException;
@ -27,7 +26,6 @@ import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.record.ODirection; import com.orientechnologies.orient.core.record.ODirection;
import com.orientechnologies.orient.core.record.OVertex; import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.executor.OResult; import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.sql.executor.OResultSet; import com.orientechnologies.orient.core.sql.executor.OResultSet;
@ -125,9 +123,7 @@ public class ContextUtility {
OVertex contextVertex = getContextVertexByFullName(oDatabaseDocument, fullName); OVertex contextVertex = getContextVertexByFullName(oDatabaseDocument, fullName);
ODocument oDocument = contextVertex.getProperty(IdentifiableElement.HEADER_PROPERTY); uuid = UUID.fromString(contextVertex.getProperty(IdentifiableElement.UUID_PROPERTY));
uuid = UUID.fromString(oDocument.getProperty(Header.UUID_PROPERTY));
securityContext = getSecurityContextByUUID(uuid, contextVertex); securityContext = getSecurityContextByUUID(uuid, contextVertex);

View File

@ -12,9 +12,9 @@ import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.NullNode; import org.gcube.com.fasterxml.jackson.databind.node.NullNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.contexts.reference.relations.IsParentOf; import org.gcube.informationsystem.contexts.reference.relations.IsParentOf;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
@ -127,9 +127,7 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
select.append(getName()); select.append(getName());
select.append("\""); select.append("\"");
select.append(QueryLogicalOperator.AND.getLogicalOperator()); select.append(QueryLogicalOperator.AND.getLogicalOperator());
select.append(Context.HEADER_PROPERTY); select.append(IdentifiableElement.UUID_PROPERTY);
select.append(".");
select.append(Header.UUID_PROPERTY);
select.append(QueryConditionalOperator.NE.getConditionalOperator()); select.append(QueryConditionalOperator.NE.getConditionalOperator());
select.append("\""); select.append("\"");
select.append(parentContext.uuid); select.append(parentContext.uuid);

View File

@ -297,7 +297,7 @@ public class SecurityContext {
String[] list = name.split("_"); String[] list = name.split("_");
if (list.length == 2) { if (list.length == 2) {
String contextUUID = list[1]; String contextUUID = list[1];
if (!UUIDManager.isReservedUUID(contextUUID)) { if (!UUIDManager.getInstance().isReservedUUID(contextUUID)) {
contexts.add(contextUUID); contexts.add(contextUUID);
} }
} }

View File

@ -21,7 +21,7 @@ import org.gcube.informationsystem.base.reference.relations.RelationElement;
import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.discovery.Discovery; import org.gcube.informationsystem.discovery.Discovery;
import org.gcube.informationsystem.discovery.RegistrationProvider; import org.gcube.informationsystem.discovery.RegistrationProvider;
import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.gcube.informationsystem.model.reference.properties.Property; import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal;
@ -198,7 +198,7 @@ public class DatabaseEnvironment {
List<Class<? extends Element>> definitionToBeCreated = new ArrayList<>(); List<Class<? extends Element>> definitionToBeCreated = new ArrayList<>();
definitionToBeCreated.add(PropertyElement.class); definitionToBeCreated.add(PropertyElement.class);
definitionToBeCreated.add(Property.class); definitionToBeCreated.add(Property.class);
definitionToBeCreated.add(Header.class); definitionToBeCreated.add(Metadata.class);
definitionToBeCreated.add(PropertyDefinition.class); definitionToBeCreated.add(PropertyDefinition.class);
definitionToBeCreated.add(PropertyType.class); definitionToBeCreated.add(PropertyType.class);
definitionToBeCreated.add(LinkedEntity.class); definitionToBeCreated.add(LinkedEntity.class);
@ -215,7 +215,7 @@ public class DatabaseEnvironment {
TypeManagement typeManagement = new TypeManagement(); TypeManagement typeManagement = new TypeManagement();
typeManagement.setTypeAndTypeName(clz); typeManagement.setTypeAndTypeName(clz);
if(clz.equals(Property.class) || clz.equals(Header.class) ) { if(clz.equals(Property.class) || clz.equals(Metadata.class) ) {
((TypeManagement) typeManagement).setSkipTypeDefinitionCreation(true); ((TypeManagement) typeManagement).setSkipTypeDefinitionCreation(true);
} }
@ -231,17 +231,17 @@ public class DatabaseEnvironment {
} }
/* /*
* We have already created Property and Header * We have already created Property and Metadata
* because Header is needed to create * because Metadata is needed to create
* types for internal use (i.e. Context, EntityType). * types for internal use (i.e. Context, EntityType).
* *
* For Property and Header we also need * For Property and Metadata we also need
* to create the instance in TypeSecurityContext * to create the instance in TypeSecurityContext
* as we will do for any other Property specialization. * as we will do for any other Property specialization.
*/ */
List<Class<? extends Element>> schemaToBeCreated = new ArrayList<>(); List<Class<? extends Element>> schemaToBeCreated = new ArrayList<>();
schemaToBeCreated.add(Property.class); schemaToBeCreated.add(Property.class);
schemaToBeCreated.add(Header.class); schemaToBeCreated.add(Metadata.class);
for(Class<? extends Element> clazz : schemaToBeCreated) { for(Class<? extends Element> clazz : schemaToBeCreated) {
ElementManagement<? extends OElement,?> erManagement = new PropertyTypeDefinitionManagement(); ElementManagement<? extends OElement,?> erManagement = new PropertyTypeDefinitionManagement();
erManagement.setJson(TypeMapper.serializeType(clazz)); erManagement.setJson(TypeMapper.serializeType(clazz));

View File

@ -28,7 +28,7 @@ import org.gcube.common.authorization.library.provider.CalledMethodProvider;
import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.gcube.informationsystem.model.reference.properties.Property; import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
@ -46,8 +46,8 @@ import org.gcube.informationsystem.resourceregistry.instances.base.properties.Pr
import org.gcube.informationsystem.resourceregistry.instances.model.Operation; import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
import org.gcube.informationsystem.resourceregistry.types.CachedType; import org.gcube.informationsystem.resourceregistry.types.CachedType;
import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.types.TypesCache;
import org.gcube.informationsystem.resourceregistry.utils.HeaderOrient; import org.gcube.informationsystem.resourceregistry.utils.MetadataOrient;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility;
import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.gcube.informationsystem.types.reference.Type; import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.types.reference.entities.ResourceType; import org.gcube.informationsystem.types.reference.entities.ResourceType;
@ -136,7 +136,7 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
this.accessType = accessType; this.accessType = accessType;
this.ignoreKeys = new HashSet<String>(); this.ignoreKeys = new HashSet<String>();
this.ignoreStartWithKeys = new HashSet<String>(); this.ignoreStartWithKeys = new HashSet<String>();
this.ignoreStartWithKeys.add(ElementManagement.AT); this.ignoreStartWithKeys.add(ElementManagement.AT);
@ -350,15 +350,8 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
} }
protected void checkUUIDMatch() throws ResourceRegistryException { protected void checkUUIDMatch() throws ResourceRegistryException {
Header header = null; if(jsonNode.has(IdentifiableElement.UUID_PROPERTY)) {
try { UUID resourceUUID = UUID.fromString(jsonNode.get(IdentifiableElement.UUID_PROPERTY).asText());
header = HeaderUtility.getHeader(jsonNode, false);
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
if(header != null) {
UUID resourceUUID = header.getUUID();
if(resourceUUID.compareTo(uuid) != 0) { if(resourceUUID.compareTo(uuid) != 0) {
String error = String.format( String error = String.format(
"UUID provided in header (%s) differs from the one (%s) used to identify the %s instance", "UUID provided in header (%s) differs from the one (%s) used to identify the %s instance",
@ -440,14 +433,15 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
reallyCreate(); reallyCreate();
Header entityHeader = HeaderUtility.getHeader(jsonNode, true); Metadata entityMetadata = MetadataUtility.getMetadata(jsonNode);
if(entityHeader != null) { if(entityMetadata != null) {
element.setProperty(IdentifiableElement.HEADER_PROPERTY, entityHeader); element.setProperty(IdentifiableElement.METADATA_PROPERTY, entityMetadata);
} else { } else {
entityHeader = HeaderUtility.addHeader(element, null); entityMetadata = MetadataUtility.addMetadata(element);
uuid = entityHeader.getUUID();
} }
this.uuid = Utility.getUUID(element, true);
getWorkingContext().addElement(element, oDatabaseDocument); getWorkingContext().addElement(element, oDatabaseDocument);
element.save(); element.save();
@ -470,7 +464,7 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
reallyUpdate(); reallyUpdate();
HeaderUtility.updateModifiedByAndLastUpdate(element); MetadataUtility.updateModifiedByAndLastUpdate(element);
element.save(); element.save();
@ -505,7 +499,7 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
throw new ResourceRegistryException("Trying to set null " + elementClass.getSimpleName() + " in " + this); throw new ResourceRegistryException("Trying to set null " + elementClass.getSimpleName() + " in " + this);
} }
this.element = element; this.element = element;
this.uuid = HeaderUtility.getHeader(element).getUUID(); this.uuid = Utility.getUUID(element);
OClass oClass = getOClass(); OClass oClass = getOClass();
this.typeName = oClass.getName(); this.typeName = oClass.getName();
} }
@ -1078,8 +1072,8 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
for(String key : properties.keySet()) { for(String key : properties.keySet()) {
try { try {
if(key.compareTo(IdentifiableElement.HEADER_PROPERTY)==0) { if(key.compareTo(IdentifiableElement.METADATA_PROPERTY)==0) {
// We never update the header with the value provided // We never update the metadata with the value provided
continue; continue;
} }
@ -1145,23 +1139,23 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
return (JsonNode) object; return (JsonNode) object;
} }
if(key.compareTo(IdentifiableElement.HEADER_PROPERTY) == 0) { if(key.compareTo(IdentifiableElement.METADATA_PROPERTY) == 0) {
// Keeping the header // Keeping the metadata
HeaderOrient headerOrient = HeaderUtility.getHeaderOrient((ODocument) object); MetadataOrient metadataOrient = MetadataUtility.getMetadataOrient((ODocument) object);
JsonNode headerJson = Utility.toJsonNode(headerOrient, false); JsonNode metadataJson = Utility.toJsonNode(metadataOrient, false);
if(ContextUtility.getIncludeInstanceContexts().get()) { if(ContextUtility.getIncludeInstanceContexts().get()) {
((ObjectNode) headerJson).set(Header.__CONTEXTS, getContextsAsObjectNode()); ((ObjectNode) metadataJson).set(Metadata.__CONTEXTS, getContextsAsObjectNode());
} }
TypesCache typesCache = TypesCache.getInstance(); TypesCache typesCache = TypesCache.getInstance();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
CachedType<PropertyType<Property>> headerType = (CachedType<PropertyType<Property>>) typesCache.getCachedType(Header.NAME); CachedType<PropertyType<Property>> headerType = (CachedType<PropertyType<Property>>) typesCache.getCachedType(Metadata.NAME);
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
Collection<String> superClasses = headerType.getSuperTypes(); Collection<String> superClasses = headerType.getSuperTypes();
ArrayNode arrayNode = objectMapper.valueToTree(superClasses); ArrayNode arrayNode = objectMapper.valueToTree(superClasses);
((ObjectNode) headerJson).replace(Element.SUPERCLASSES_PROPERTY, arrayNode); ((ObjectNode) metadataJson).replace(Element.SUPERCLASSES_PROPERTY, arrayNode);
return headerJson; return metadataJson;
} }
if(ignoreKeys.contains(key)) { if(ignoreKeys.contains(key)) {

View File

@ -43,7 +43,7 @@ public abstract class EntityElementManagement<E extends EntityElement, ET extend
protected EntityElementManagement(AccessType accessType) { protected EntityElementManagement(AccessType accessType) {
super(accessType); super(accessType);
this.ignoreKeys.add(EntityElement.HEADER_PROPERTY); this.ignoreKeys.add(EntityElement.METADATA_PROPERTY);
this.ignoreStartWithKeys.add(IN_PREFIX.toLowerCase()); this.ignoreStartWithKeys.add(IN_PREFIX.toLowerCase());
this.ignoreStartWithKeys.add(OUT_PREFIX.toLowerCase()); this.ignoreStartWithKeys.add(OUT_PREFIX.toLowerCase());

View File

@ -4,6 +4,7 @@ import java.security.Key;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
@ -13,9 +14,9 @@ import org.gcube.common.encryption.encrypter.StringEncrypter;
import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.base.reference.properties.PropertyElement; import org.gcube.informationsystem.base.reference.properties.PropertyElement;
import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.properties.Encrypted;
import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.gcube.informationsystem.model.reference.properties.Property; import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.model.reference.properties.Vault;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
@ -24,8 +25,8 @@ import org.gcube.informationsystem.resourceregistry.instances.base.ElementManage
import org.gcube.informationsystem.resourceregistry.types.CachedType; import org.gcube.informationsystem.resourceregistry.types.CachedType;
import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.types.TypesCache;
import org.gcube.informationsystem.resourceregistry.utils.EncryptedOrient; import org.gcube.informationsystem.resourceregistry.utils.EncryptedOrient;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility;
import org.gcube.informationsystem.resourceregistry.utils.VaultOrient; import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.gcube.informationsystem.types.reference.properties.PropertyType; import org.gcube.informationsystem.types.reference.properties.PropertyType;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -84,13 +85,23 @@ public class PropertyElementManagement {
} }
try { try {
Header header = HeaderUtility.getHeader(jsonNode, false); UUID uuid = Utility.getUUID(jsonNode);
if(header != null) { if(uuid != null) {
throw new ResourceRegistryException("A property object cannot have an Header"); throw new ResourceRegistryException("A property object cannot have an UUID");
} }
} catch(Exception e) { } catch(Exception e) {
logger.warn("An invalid Header has been provided. Anyway property object cannot have an Header."); logger.warn("An invalid UUID has been provided. Anyway property object cannot have an UUID.");
throw new ResourceRegistryException("An property object cannot have an Header"); throw new ResourceRegistryException("An property object cannot have an UUID");
}
try {
Metadata metadata = MetadataUtility.getMetadata(jsonNode);
if(metadata != null) {
throw new ResourceRegistryException("A property object cannot have a Metadata");
}
} catch(Exception e) {
logger.warn("An invalid Metadata has been provided. Anyway property object cannot have a Metadata.");
throw new ResourceRegistryException("An property object cannot have a Metadata");
} }
@ -99,7 +110,7 @@ public class PropertyElementManagement {
* Resource Registry must decrypt the value with the Context Key and Encrypt it with DB key. * Resource Registry must decrypt the value with the Context Key and Encrypt it with DB key.
* The opposite operation is done when the value is read by clients. * The opposite operation is done when the value is read by clients.
*/ */
if(oClass.isSubClassOf(EncryptedOrient.NAME)) { if(oClass.isSubClassOf(Encrypted.NAME)) {
EncryptedOrient encrypted = new EncryptedOrient(); EncryptedOrient encrypted = new EncryptedOrient();
oDocument = encrypted; oDocument = encrypted;
oDocument.fromJSON(jsonNode.toString()); oDocument.fromJSON(jsonNode.toString());
@ -111,25 +122,7 @@ public class PropertyElementManagement {
encrypted.setDecryptedValue(decryptedValue, false); encrypted.setDecryptedValue(decryptedValue, false);
} catch(Exception e) { } catch(Exception e) {
throw new ResourceRegistryException("Unable to manage " + EncryptedOrient.NAME + " " + org.gcube.informationsystem.model.reference.properties.Property.NAME); throw new ResourceRegistryException("Unable to manage " + Encrypted.NAME + " " + org.gcube.informationsystem.model.reference.properties.Property.NAME);
}
return oDocument;
}
if(oClass.isSubClassOf(Vault.NAME)) {
VaultOrient vault = new VaultOrient();
oDocument = vault;
oDocument.fromJSON(jsonNode.toString());
try {
String contextEncryptedValue = vault.getValue();
// Decrypting with Context Key (default key)
String decryptedValue = StringEncrypter.getEncrypter().decrypt(contextEncryptedValue);
vault.setDecryptedValue(decryptedValue, false);
} catch(Exception e) {
throw new ResourceRegistryException("Unable to manage " + Vault.NAME + " " + org.gcube.informationsystem.model.reference.properties.Property.NAME);
} }
return oDocument; return oDocument;
} }
@ -174,16 +167,16 @@ public class PropertyElementManagement {
* The opposite operation is done when the value is set from clients. * The opposite operation is done when the value is set from clients.
* see {@link PropertyManagement#getPropertyDocument(JsonNode) getPropertyDocument()} * see {@link PropertyManagement#getPropertyDocument(JsonNode) getPropertyDocument()}
*/ */
if(oClass.isSubClassOf(EncryptedOrient.NAME)) { if(oClass.isSubClassOf(Encrypted.NAME)) {
try { try {
EncryptedOrient encrypted = null; EncryptedOrient encrypted = null;
String encryptedValue = (String) oDocument.getProperty(EncryptedOrient.VALUE); String encryptedValue = (String) oDocument.getProperty(Encrypted.VALUE);
if(oDocument instanceof EncryptedOrient) { if(oDocument instanceof EncryptedOrient) {
encrypted = (EncryptedOrient) oDocument; encrypted = (EncryptedOrient) oDocument;
if(encrypted.getDbEncryptedValue().compareTo(encryptedValue)==0) { if(encrypted.getDbEncryptedValue().compareTo(encryptedValue)==0) {
((ObjectNode) jsonNode).put(EncryptedOrient.VALUE, encrypted.getContextEncryptedValue()); ((ObjectNode) jsonNode).put(Encrypted.VALUE, encrypted.getContextEncryptedValue());
} }
}else { }else {
encrypted = new EncryptedOrient(); encrypted = new EncryptedOrient();
@ -197,7 +190,7 @@ public class PropertyElementManagement {
String contextEncryptedValue = StringEncrypter.getEncrypter().encrypt(decryptedValue); String contextEncryptedValue = StringEncrypter.getEncrypter().encrypt(decryptedValue);
// Setting the value encrypted with DB key // Setting the value encrypted with DB key
((ObjectNode) jsonNode).put(EncryptedOrient.VALUE, contextEncryptedValue); ((ObjectNode) jsonNode).put(Encrypted.VALUE, contextEncryptedValue);
} }
}catch (Exception e) { }catch (Exception e) {
@ -205,38 +198,6 @@ public class PropertyElementManagement {
} }
} }
if(oClass.isSubClassOf(Vault.NAME)) {
try {
VaultOrient vaultOrient = null;
String encryptedValue = (String) oDocument.getProperty(Vault.VALUE);
if(oDocument instanceof VaultOrient) {
vaultOrient = (VaultOrient) oDocument;
if(vaultOrient.getDbEncryptedValue().compareTo(encryptedValue)==0) {
((ObjectNode) jsonNode).put(Vault.VALUE, vaultOrient.getContextEncryptedValue());
}
}else {
vaultOrient = new VaultOrient();
oDocument = (ODocument) vaultOrient;
// Decrypting with DB Key
Key databaseKey = DatabaseEnvironment.getDatabaseKey();
String decryptedValue = StringEncrypter.getEncrypter().decrypt(encryptedValue, databaseKey);
// Encrypting with Context Key (default key)
String contextEncryptedValue = StringEncrypter.getEncrypter().encrypt(decryptedValue);
// Setting the value encrypted with DB key
((ObjectNode) jsonNode).put(Vault.VALUE, contextEncryptedValue);
}
}catch (Exception e) {
throw new ResourceRegistryException("Errror while managing " + EncryptedOrient.NAME+ " "+ Property.NAME, e);
}
}
return jsonNode; return jsonNode;
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {

View File

@ -46,7 +46,6 @@ public abstract class RelationElementManagement<SEM extends EntityElementManagem
protected RelationElementManagement(AccessType accessType, Class<? extends EntityElement> sourceEntityClass, Class<? extends EntityElement> targetEntityClass) { protected RelationElementManagement(AccessType accessType, Class<? extends EntityElement> sourceEntityClass, Class<? extends EntityElement> targetEntityClass) {
super(accessType); super(accessType);
this.ignoreKeys.add(Relation.HEADER_PROPERTY);
this.ignoreKeys.add(Relation.SOURCE_PROPERTY); this.ignoreKeys.add(Relation.SOURCE_PROPERTY);
this.ignoreKeys.add(Relation.TARGET_PROPERTY); this.ignoreKeys.add(Relation.TARGET_PROPERTY);
this.ignoreKeys.add(IN.toLowerCase()); this.ignoreKeys.add(IN.toLowerCase());

View File

@ -18,7 +18,6 @@ import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.model.reference.entities.Entity; import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
@ -40,7 +39,7 @@ import org.gcube.informationsystem.resourceregistry.instances.model.ERManagement
import org.gcube.informationsystem.resourceregistry.instances.model.Operation; import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.RelationManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.RelationManagement;
import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.types.TypesCache;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility;
import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.gcube.informationsystem.types.reference.entities.EntityType; import org.gcube.informationsystem.types.reference.entities.EntityType;
@ -147,8 +146,6 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
protected EntityManagement(AccessType accessType) { protected EntityManagement(AccessType accessType) {
super(accessType); super(accessType);
this.ignoreKeys.add(Entity.HEADER_PROPERTY);
this.ignoreStartWithKeys.add(IN_PREFIX.toLowerCase()); this.ignoreStartWithKeys.add(IN_PREFIX.toLowerCase());
this.ignoreStartWithKeys.add(OUT_PREFIX.toLowerCase()); this.ignoreStartWithKeys.add(OUT_PREFIX.toLowerCase());
this.ignoreStartWithKeys.add(IN_PREFIX.toUpperCase()); this.ignoreStartWithKeys.add(IN_PREFIX.toUpperCase());
@ -338,7 +335,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
setOperation(Operation.ADD_TO_CONTEXT); setOperation(Operation.ADD_TO_CONTEXT);
reallyAddToContext(); reallyAddToContext();
if(!skipped) { if(!skipped) {
HeaderUtility.updateModifiedByAndLastUpdate(element); MetadataUtility.updateModifiedByAndLastUpdate(element);
element.save(); element.save();
affectedInstances.put(uuid, serializeAsAffectedInstance()); affectedInstances.put(uuid, serializeAsAffectedInstance());
sanityCheck(); sanityCheck();
@ -402,7 +399,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
try { try {
setOperation(Operation.REMOVE_FROM_CONTEXT); setOperation(Operation.REMOVE_FROM_CONTEXT);
reallyRemoveFromContext(); reallyRemoveFromContext();
HeaderUtility.updateModifiedByAndLastUpdate(element); MetadataUtility.updateModifiedByAndLastUpdate(element);
element.save(); element.save();
affectedInstances.put(uuid, serializeAsAffectedInstance()); affectedInstances.put(uuid, serializeAsAffectedInstance());
sanityCheck(); sanityCheck();
@ -716,7 +713,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
ArrayNode arrayNode = objectMapper.createArrayNode(); ArrayNode arrayNode = objectMapper.createArrayNode();
if(referenceUUID != null) { if(referenceUUID != null) {
constraint.put(Entity.HEADER_PROPERTY + "." + Header.UUID_PROPERTY, referenceUUID.toString()); constraint.put(Entity.UUID_PROPERTY, referenceUUID.toString());
} }
// TODO check types // TODO check types
@ -795,8 +792,8 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
EntityManagement<?,?> entityManagement = ElementManagementUtility.getEntityManagement(getWorkingContext(), EntityManagement<?,?> entityManagement = ElementManagementUtility.getEntityManagement(getWorkingContext(),
oDatabaseDocument, vertex); oDatabaseDocument, vertex);
try { try {
if(constraint.containsKey(Entity.HEADER_PROPERTY + "." + Header.UUID_PROPERTY)) { if(constraint.containsKey(Entity.UUID_PROPERTY)) {
String uuid = constraint.get(Entity.HEADER_PROPERTY + "." + Header.UUID_PROPERTY); String uuid = constraint.get(Entity.UUID_PROPERTY);
if(entityManagement.getUUID().compareTo(UUID.fromString(uuid)) == 0) { if(entityManagement.getUUID().compareTo(UUID.fromString(uuid)) == 0) {
continue; continue;
} }

View File

@ -40,7 +40,7 @@ import org.gcube.informationsystem.resourceregistry.instances.model.entities.Ent
import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.types.TypesCache;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility;
import org.gcube.informationsystem.resourceregistry.utils.PropagationConstraintOrient; import org.gcube.informationsystem.resourceregistry.utils.PropagationConstraintOrient;
import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.gcube.informationsystem.serialization.ElementMapper; import org.gcube.informationsystem.serialization.ElementMapper;
@ -473,7 +473,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
operation = Operation.ADD_TO_CONTEXT; operation = Operation.ADD_TO_CONTEXT;
reallyAddToContext(); reallyAddToContext();
if(!skipped && propagationConstraint.getAddConstraint()==PropagationConstraint.AddConstraint.propagate) { if(!skipped && propagationConstraint.getAddConstraint()==PropagationConstraint.AddConstraint.propagate) {
HeaderUtility.updateModifiedByAndLastUpdate(element); MetadataUtility.updateModifiedByAndLastUpdate(element);
element.save(); element.save();
affectedInstances.put(uuid, serializeAsAffectedInstance()); affectedInstances.put(uuid, serializeAsAffectedInstance());
} }
@ -650,7 +650,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
try { try {
setOperation(Operation.REMOVE_FROM_CONTEXT); setOperation(Operation.REMOVE_FROM_CONTEXT);
reallyRemoveFromContext(); reallyRemoveFromContext();
HeaderUtility.updateModifiedByAndLastUpdate(element); MetadataUtility.updateModifiedByAndLastUpdate(element);
element.save(); element.save();
affectedInstances.put(uuid, serializeAsAffectedInstance()); affectedInstances.put(uuid, serializeAsAffectedInstance());
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {

View File

@ -73,7 +73,7 @@ public class JsonQueryResource extends JsonQueryEntity {
} }
} }
if(jsonNode.has(IdentifiableElement.HEADER_PROPERTY)) { if(jsonNode.has(IdentifiableElement.METADATA_PROPERTY)) {
StringBuffer newBuffer = new StringBuffer(); StringBuffer newBuffer = new StringBuffer();
newBuffer.append("SELECT FROM ( "); newBuffer.append("SELECT FROM ( ");
newBuffer.append(stringBuffer); newBuffer.append(stringBuffer);

View File

@ -17,9 +17,7 @@ import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.common.authorization.library.provider.CalledMethodProvider; import org.gcube.common.authorization.library.provider.CalledMethodProvider;
import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.resourceregistry.ResourceInitializer; import org.gcube.informationsystem.resourceregistry.ResourceInitializer;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException;
@ -31,6 +29,7 @@ import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
import org.gcube.informationsystem.resourceregistry.instances.model.ERManagement; import org.gcube.informationsystem.resourceregistry.instances.model.ERManagement;
import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -123,8 +122,7 @@ public class SharingManager {
for(JsonNode node : arrayNode) { for(JsonNode node : arrayNode) {
@SuppressWarnings("unused") @SuppressWarnings("unused")
String type = node.get(Element.CLASS_PROPERTY).asText(); String type = node.get(Element.CLASS_PROPERTY).asText();
String instanceId = node.get(IdentifiableElement.HEADER_PROPERTY).get(Header.UUID_PROPERTY).asText(); UUID uuid = Utility.getUUID(node);
UUID uuid = UUID.fromString(instanceId);
expectedInstances.put(uuid, node); expectedInstances.put(uuid, node);
} }

View File

@ -537,7 +537,7 @@ public class TypeManagement {
String propertyName = newPropertyDefinition.getName(); String propertyName = newPropertyDefinition.getName();
if(propertyName.compareTo(IdentifiableElement.HEADER_PROPERTY)==0 || propertyName.compareTo(Relation.PROPAGATION_CONSTRAINT_PROPERTY)==0) { if(propertyName.compareTo(IdentifiableElement.UUID_PROPERTY)==0 || propertyName.compareTo(IdentifiableElement.METADATA_PROPERTY)==0 || propertyName.compareTo(Relation.PROPAGATION_CONSTRAINT_PROPERTY)==0) {
continue; continue;
} }

View File

@ -1,137 +0,0 @@
package org.gcube.informationsystem.resourceregistry.utils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.core.JsonParseException;
import org.gcube.com.fasterxml.jackson.databind.JsonMappingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.utils.UUIDManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.record.OElement;
import com.orientechnologies.orient.core.record.impl.ODocument;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class HeaderUtility {
private static final Logger logger = LoggerFactory.getLogger(HeaderUtility.class);
public static String getUser() {
String user = Header.UNKNOWN_USER;
try {
user = SecretManagerProvider.instance.get().getUser().getUsername();
} catch(Exception e) {
logger.error("Unable to retrieve user. {} will be used", user);
}
return user;
}
public static Header createHeader(UUID uuid) {
HeaderOrient header = new HeaderOrient();
if(uuid == null) {
uuid = UUIDManager.generateValidRandomUUID();
}
header.setUUID(uuid);
String creator = getUser();
header.setCreatedBy(creator);
header.setLastUpdateBy(creator);
Date date = Calendar.getInstance().getTime();
SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
logger.trace("Setting Last Update and Creation Time to " + ft.format(date));
header.setCreationTime(date);
header.setLastUpdateTime(date);
return header;
}
public static Header getHeader(JsonNode jsonNode, boolean creation)
throws JsonParseException, JsonMappingException, IOException, ResourceRegistryException {
if(jsonNode.has(IdentifiableElement.HEADER_PROPERTY)) {
JsonNode headerNode = jsonNode.get(IdentifiableElement.HEADER_PROPERTY);
if(headerNode.isNull()) {
return null;
}
HeaderOrient header = null;
if(creation) {
// If an header is provided, it MUST contains an UUID otherwise is
// an invalid request so that let that an exception is raised
UUID uuid = UUID.fromString(headerNode.get(Header.UUID_PROPERTY).asText());
if(UUIDManager.isReservedUUID(uuid)) {
throw new ResourceRegistryException("The provided UUID " + uuid.toString() + "is reserved. The reserved UUID are : " + UUIDManager.getAllReservedUUIDAsStrings());
}
header = (HeaderOrient) createHeader(uuid);
} else {
header = new HeaderOrient();
header.fromJSON(headerNode.toString());
}
return header;
}
return null;
}
public static HeaderOrient getHeaderOrient(ODocument oDocument) throws ResourceRegistryException {
if(oDocument instanceof HeaderOrient) {
return (HeaderOrient) oDocument;
} else {
try {
HeaderOrient headerOrient = new HeaderOrient();
String json = oDocument.toJSON();
Header header = ElementMapper.unmarshal(Header.class, json);
headerOrient.setUUID(header.getUUID());
headerOrient.setCreatedBy(header.getCreatedBy());
headerOrient.setCreationTime(header.getCreationTime());
headerOrient.setLastUpdateBy(header.getLastUpdateBy());
headerOrient.setLastUpdateTime(header.getLastUpdateTime());
return headerOrient;
} catch(Exception e) {
throw new ResourceRegistryException(
"Unable to recreate Header. " + Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
}
}
}
public static Header addHeader(OElement element, UUID uuid) {
Header header = createHeader(uuid);
element.setProperty(IdentifiableElement.HEADER_PROPERTY, header);
return header;
}
/*
public static Header addHeader(Edge edge, UUID uuid) {
Header header = createHeader(uuid);
edge.setProperty(IdentifiableElement.HEADER_PROPERTY, header);
return header;
}
*/
public static Header getHeader(OElement element) throws ResourceRegistryException {
return Utility.getPropertyDocument(Header.class, element, IdentifiableElement.HEADER_PROPERTY);
}
public static void updateModifiedByAndLastUpdate(OElement element) throws ResourceRegistryException {
ODocument oDocument = element.getProperty(IdentifiableElement.HEADER_PROPERTY);
String lastUpdateBy = getUser();
oDocument.field(Header.LAST_UPDATE_BY_PROPERTY, lastUpdateBy);
Date lastUpdateTime = Calendar.getInstance().getTime();
oDocument.field(Header.LAST_UPDATE_TIME_PROPERTY, lastUpdateTime);
element.setProperty(IdentifiableElement.HEADER_PROPERTY, oDocument);
}
}

View File

@ -2,69 +2,58 @@ package org.gcube.informationsystem.resourceregistry.utils;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.properties.Metadata;
import com.orientechnologies.orient.core.record.impl.ODocument; import com.orientechnologies.orient.core.record.impl.ODocument;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
public class HeaderOrient extends ODocument implements Header { public class MetadataOrient extends ODocument implements Metadata {
public HeaderOrient() { public MetadataOrient() {
super(Header.NAME); super(Metadata.NAME);
} }
protected HeaderOrient(String iClassName) { protected MetadataOrient(String iClassName) {
super(iClassName); super(iClassName);
} }
@Override
public UUID getUUID() {
return UUID.fromString((String) this.field(Header.UUID_PROPERTY));
}
@Override
public void setUUID(UUID uuid) {
this.field(Header.UUID_PROPERTY, uuid.toString());
}
@Override @Override
public String getCreatedBy() { public String getCreatedBy() {
return this.field(Header.CREATED_BY_PROPERTY); return this.field(Metadata.CREATED_BY_PROPERTY);
} }
public void setCreatedBy(String createdBy) { public void setCreatedBy(String createdBy) {
this.field(Header.CREATED_BY_PROPERTY, createdBy); this.field(Metadata.CREATED_BY_PROPERTY, createdBy);
} }
@Override @Override
public Date getCreationTime() { public Date getCreationTime() {
return this.field(Header.CREATION_TIME_PROPERTY); return this.field(Metadata.CREATION_TIME_PROPERTY);
} }
public void setCreationTime(Date creationTime) { public void setCreationTime(Date creationTime) {
this.field(Header.CREATION_TIME_PROPERTY, creationTime); this.field(Metadata.CREATION_TIME_PROPERTY, creationTime);
} }
@Override @Override
public String getLastUpdateBy() { public String getLastUpdateBy() {
return this.field(Header.LAST_UPDATE_BY_PROPERTY); return this.field(Metadata.LAST_UPDATE_BY_PROPERTY);
} }
public void setLastUpdateBy(String lastUpdateBy) { public void setLastUpdateBy(String lastUpdateBy) {
this.field(Header.LAST_UPDATE_BY_PROPERTY, lastUpdateBy); this.field(Metadata.LAST_UPDATE_BY_PROPERTY, lastUpdateBy);
} }
@Override @Override
public Date getLastUpdateTime() { public Date getLastUpdateTime() {
return this.field(Header.LAST_UPDATE_TIME_PROPERTY); return this.field(Metadata.LAST_UPDATE_TIME_PROPERTY);
} }
public void setLastUpdateTime(Date lastUpdateTime) { public void setLastUpdateTime(Date lastUpdateTime) {
this.field(Header.LAST_UPDATE_TIME_PROPERTY, lastUpdateTime); this.field(Metadata.LAST_UPDATE_TIME_PROPERTY, lastUpdateTime);
} }
@Override @Override

View File

@ -0,0 +1,109 @@
package org.gcube.informationsystem.resourceregistry.utils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.gcube.com.fasterxml.jackson.core.JsonParseException;
import org.gcube.com.fasterxml.jackson.databind.JsonMappingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.record.OElement;
import com.orientechnologies.orient.core.record.impl.ODocument;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class MetadataUtility {
private static final Logger logger = LoggerFactory.getLogger(MetadataUtility.class);
public static String getUser() {
String user = Metadata.UNKNOWN_USER;
try {
user = SecretManagerProvider.instance.get().getUser().getUsername();
} catch(Exception e) {
logger.error("Unable to retrieve user. {} will be used", user);
}
return user;
}
public static Metadata createMetadata() {
MetadataOrient header = new MetadataOrient();
String creator = getUser();
header.setCreatedBy(creator);
header.setLastUpdateBy(creator);
Date date = Calendar.getInstance().getTime();
SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
logger.trace("Setting Last Update and Creation Time to " + ft.format(date));
header.setCreationTime(date);
header.setLastUpdateTime(date);
return header;
}
public static Metadata getMetadata(JsonNode jsonNode)
throws JsonParseException, JsonMappingException, IOException, ResourceRegistryException {
if(jsonNode.has(IdentifiableElement.METADATA_PROPERTY)) {
JsonNode metadataNode = jsonNode.get(IdentifiableElement.METADATA_PROPERTY);
if(metadataNode.isNull()) {
return null;
}
MetadataOrient metadata = new MetadataOrient();
metadata.fromJSON(metadataNode.toString());
return metadata;
}
return null;
}
public static MetadataOrient getMetadataOrient(ODocument oDocument) throws ResourceRegistryException {
if(oDocument instanceof MetadataOrient) {
return (MetadataOrient) oDocument;
} else {
try {
MetadataOrient metadataOrient = new MetadataOrient();
String json = oDocument.toJSON();
Metadata metadata = ElementMapper.unmarshal(Metadata.class, json);
metadataOrient.setCreatedBy(metadata.getCreatedBy());
metadataOrient.setCreationTime(metadata.getCreationTime());
metadataOrient.setLastUpdateBy(metadata.getLastUpdateBy());
metadataOrient.setLastUpdateTime(metadata.getLastUpdateTime());
return metadataOrient;
} catch(Exception e) {
throw new ResourceRegistryException(
"Unable to recreate Header. " + Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
}
}
}
public static Metadata addMetadata(OElement element) {
Metadata metadata = createMetadata();
element.setProperty(IdentifiableElement.METADATA_PROPERTY, metadata);
return metadata;
}
public static Metadata getHeader(OElement element) throws ResourceRegistryException {
return Utility.getPropertyDocument(Metadata.class, element, IdentifiableElement.METADATA_PROPERTY);
}
public static void updateModifiedByAndLastUpdate(OElement element) throws ResourceRegistryException {
ODocument oDocument = element.getProperty(IdentifiableElement.METADATA_PROPERTY);
String lastUpdateBy = getUser();
oDocument.field(Metadata.LAST_UPDATE_BY_PROPERTY, lastUpdateBy);
Date lastUpdateTime = Calendar.getInstance().getTime();
oDocument.field(Metadata.LAST_UPDATE_TIME_PROPERTY, lastUpdateTime);
element.setProperty(IdentifiableElement.METADATA_PROPERTY, oDocument);
}
}

View File

@ -8,7 +8,6 @@ import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.base.reference.properties.PropertyElement; import org.gcube.informationsystem.base.reference.properties.PropertyElement;
import org.gcube.informationsystem.model.reference.entities.Entity; import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
@ -17,6 +16,7 @@ import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecur
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
import org.gcube.informationsystem.serialization.ElementMapper; import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.utils.UUIDManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -97,7 +97,7 @@ public class Utility {
} }
// TODO Rewrite using Gremlin // TODO Rewrite using Gremlin
String select = "SELECT FROM " + elementType + " WHERE " + IdentifiableElement.HEADER_PROPERTY + "." + Header.UUID_PROPERTY String select = "SELECT FROM " + elementType + " WHERE " + IdentifiableElement.UUID_PROPERTY
+ " = \"" + uuid.toString() + "\""; + " = \"" + uuid.toString() + "\"";
OResultSet resultSet = oDatabaseDocument.query(select, new HashMap<>()); OResultSet resultSet = oDatabaseDocument.query(select, new HashMap<>());
@ -135,14 +135,37 @@ public class Utility {
} }
} }
public static UUID getUUID(OElement element) throws ResourceRegistryException { public static UUID getUUID(JsonNode jsonNode) throws ResourceRegistryException {
/* if(jsonNode.has(IdentifiableElement.UUID_PROPERTY)) {
* ODocument header = element.getProperty(Entity.HEADER_PROPERTY); String String uuidString = jsonNode.get(IdentifiableElement.UUID_PROPERTY).asText();
* contextID = header.field(Header.UUID_PROPERTY); return return getUUID(uuidString, false);
* UUID.fromString(contextID); }
*/ return null;
Header header = HeaderUtility.getHeader(element); }
return header.getUUID();
public static UUID getUUID(OElement element) throws ResourceRegistryException {
return getUUID(element, false);
}
public static UUID getUUID(OElement element, boolean create) throws ResourceRegistryException {
String uuidString = element.getProperty(IdentifiableElement.UUID_PROPERTY);
return getUUID(uuidString, create);
}
public static UUID getUUID(String uuidString, boolean create) throws ResourceRegistryException {
UUIDManager uuidManager = UUIDManager.getInstance();
UUID uuid = null;
if(uuidString == null) {
if(create) {
uuid = uuidManager.generateValidRandomUUID();
}
}else {
if(!uuidManager.isReservedUUID(uuidString)) {
uuid = UUID.fromString(uuidString);
}else {
uuid = uuidManager.generateValidRandomUUID();
}
}
return uuid;
} }
} }

View File

@ -1,96 +0,0 @@
package org.gcube.informationsystem.resourceregistry.utils;
import java.security.Key;
import java.util.Map;
import org.gcube.common.encryption.encrypter.StringEncrypter;
import org.gcube.informationsystem.model.reference.properties.Vault;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import com.orientechnologies.orient.core.record.impl.ODocument;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class VaultOrient extends ODocument implements org.gcube.informationsystem.model.reference.properties.Vault {
protected String decryptedValue;
protected String dbEncryptedValue;
protected String contextEncryptedValue;
public VaultOrient() {
super(Vault.NAME);
}
protected VaultOrient(String iClassName) {
super(iClassName);
}
@Override
public String getValue() {
return this.field(Vault.VALUE);
}
@Override
public void setValue(String value) {
this.field(Vault.VALUE, value);
}
@Override
public String toJSON(String iFormat) {
return super.toJSON(iFormat);
}
public String getDecryptedValue() {
return decryptedValue;
}
public String getDbEncryptedValue() {
return dbEncryptedValue;
}
public String getContextEncryptedValue() {
return contextEncryptedValue;
}
public void setDecryptedValue(String decryptedValue, boolean setEncryptedForContext) throws Exception {
this.decryptedValue = decryptedValue;
// Encrypting with DB Key
Key databaseKey = DatabaseEnvironment.getDatabaseKey();
this.dbEncryptedValue = StringEncrypter.getEncrypter().encrypt(decryptedValue, databaseKey);
// Encrypting with Context Key (default key)
this.contextEncryptedValue = StringEncrypter.getEncrypter().encrypt(decryptedValue);
if(setEncryptedForContext) {
setValue(contextEncryptedValue);
}else {
setValue(dbEncryptedValue);
}
}
@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) {
}
}

View File

@ -23,7 +23,7 @@ import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityCo
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.SecurityType; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.SecurityType;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility;
import org.gcube.informationsystem.serialization.ElementMapper; import org.gcube.informationsystem.serialization.ElementMapper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -54,7 +54,7 @@ public class ContextManagementTest extends ContextTest {
Assert.assertTrue(er.getHeader().getUUID().compareTo(uuid) == 0); Assert.assertTrue(er.getHeader().getUUID().compareTo(uuid) == 0);
} }
String user = HeaderUtility.getUser(); String user = MetadataUtility.getUser();
Assert.assertTrue(er.getHeader().getLastUpdateBy().compareTo(user) == 0); Assert.assertTrue(er.getHeader().getLastUpdateBy().compareTo(user) == 0);
if(create) { if(create) {

View File

@ -35,7 +35,7 @@ import org.gcube.informationsystem.resourceregistry.instances.model.entities.Fac
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility;
import org.gcube.informationsystem.serialization.ElementMapper; import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.CPUFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.CPUFacetImpl;
@ -247,13 +247,13 @@ public class ERManagementTest extends ContextTest {
if(header.getCreatedBy()!=null) { if(header.getCreatedBy()!=null) {
Assert.assertTrue(createdBy.compareTo(header.getCreatedBy())==0); Assert.assertTrue(createdBy.compareTo(header.getCreatedBy())==0);
}else { }else {
Assert.assertTrue(createdBy.compareTo(HeaderUtility.getUser())==0); Assert.assertTrue(createdBy.compareTo(MetadataUtility.getUser())==0);
} }
if(header.getLastUpdateBy()!=null) { if(header.getLastUpdateBy()!=null) {
Assert.assertTrue(lastUpdateBy.compareTo(header.getLastUpdateBy())==0); Assert.assertTrue(lastUpdateBy.compareTo(header.getLastUpdateBy())==0);
}else { }else {
Assert.assertTrue(lastUpdateBy.compareTo(HeaderUtility.getUser())==0); Assert.assertTrue(lastUpdateBy.compareTo(MetadataUtility.getUser())==0);
} }
if(header.getLastUpdateTime()!=null) { if(header.getLastUpdateTime()!=null) {

View File

@ -5,7 +5,7 @@ import java.util.UUID;
import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.resourceregistry.instances.multicontext.MultiContextTest; import org.gcube.informationsystem.resourceregistry.instances.multicontext.MultiContextTest;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility;
import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
import org.junit.Assert; import org.junit.Assert;
@ -48,7 +48,7 @@ public class FacetManagementTest extends MultiContextTest {
Assert.assertTrue(er.getHeader().getUUID().compareTo(uuid) == 0); Assert.assertTrue(er.getHeader().getUUID().compareTo(uuid) == 0);
} }
String user = HeaderUtility.getUser(); String user = MetadataUtility.getUser();
Assert.assertTrue(er.getHeader().getLastUpdateBy().compareTo(user) == 0); Assert.assertTrue(er.getHeader().getLastUpdateBy().compareTo(user) == 0);
if(create) { if(create) {