Creating resource in the proper way

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@131287 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-09-12 12:45:40 +00:00
parent 2440e59082
commit 9343645236
8 changed files with 494 additions and 112 deletions

View File

@ -4,13 +4,13 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="resource-registry-api-1.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/resource-registry-api/resource-registry-api">
<dependent-module archiveName="resource-registry-api-1.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/resource-registry-api/resource-registry-api">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="information-system-model-1.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/information-system-model/information-system-model">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="information-system-model-orientdb-binding-1.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/information-system-model-orientdb-binding/information-system-model-orientdb-binding">
<dependent-module archiveName="information-system-model-orientdb-binding-1.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/information-system-model-orientdb-binding/information-system-model-orientdb-binding">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="resource-registry"/>

View File

@ -8,7 +8,7 @@
</parent>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<name>Resource Registry</name>
<description>Resource Registry</description>
<packaging>war</packaging>

View File

@ -4,13 +4,21 @@
package org.gcube.informationsystem.resourceregistry.resources.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.gcube.informationsystem.impl.utils.Entities;
import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.orientdb.impl.embedded.Header;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.model.relation.Relation;
@ -26,9 +34,12 @@ import org.gcube.informationsystem.resourceregistry.resources.utils.Utility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
@ -94,14 +105,260 @@ public class EntityManagementImpl implements EntityManagement {
}
public String createVertexEntity(String entityType,
Class<? extends Entity> entity, String jsonRepresentation)
throws ResourceRegistryException {
OrientGraph orientGraph = null;
private static String lowerCaseFirstCharacter(String string) {
return string.substring(0, 1).toLowerCase() + string.substring(1);
}
private static String getClassProperty(JsonNode jsonNode) {
if (jsonNode.has(Entities.CLASS_PROPERTY)) {
return jsonNode.get(Entities.CLASS_PROPERTY).asText();
}
return null;
}
private static Header getHeader(JsonNode jsonNode)
throws JsonParseException, JsonMappingException, IOException {
if (jsonNode.has(Resource.HEADER_PROPERTY)) {
JsonNode header = jsonNode.get(Resource.HEADER_PROPERTY);
if (header.isNull()) {
return null;
}
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(header.toString(), Header.class);
}
return null;
}
private static Entity checkTypeAndGetHeader(JsonNode jsonNode) throws ResourceRegistryException {
if(jsonNode.has(Entities.CLASS_PROPERTY)){
// Complex type
String type = getClassProperty(jsonNode);
SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
try {
schemaManagement.getTypeSchema(type, Embedded.NAME);
} catch (SchemaNotFoundException e) {
throw e;
}
boolean validType = false;
boolean notEmbedded = false;
// Header can be ignored or managed. Actually is managed.
if(validType && notEmbedded){
Header header = null;
try {
header = getHeader(jsonNode);
} catch (Exception e){
logger.warn("An invalid Header has been provided. An embedded object cannot have an Header. It will be ignored.");
//throw new ResourceRegistryException("An embedded object cannot have an Header");
}
if(header!=null){
logger.warn("An embedded object cannot have an Header. It will be ignored.");
//throw new ResourceRegistryException("An embedded object cannot have an Header");
}
}
}
return null;
}
public static Object getObejctFromElement(JsonNode value) throws ResourceRegistryException {
JsonNodeType jsonNodeType = value.getNodeType();
switch (jsonNodeType) {
case OBJECT:
// TODO
checkTypeAndGetHeader(value);
return null;
case ARRAY:
List<Object> array = new ArrayList<>();
Iterator<JsonNode> arrayElement = value.elements();
while(arrayElement.hasNext()){
JsonNode arrayNode = arrayElement.next();
Object objectNode = getObejctFromElement(arrayNode);
if(objectNode!=null){
array.add(objectNode);
}
}
return array;
case BINARY:
break;
case BOOLEAN:
return value.asBoolean();
case NULL:
break;
case NUMBER:
if(value.isDouble() || value.isFloat()){
return value.asDouble();
}
if(value.isBigInteger() || value.isShort() || value.isInt()){
return value.asInt();
}
if(value.isLong()){
return value.asLong();
}
break;
case STRING:
return value.asText();
case MISSING:
break;
case POJO:
break;
default:
break;
}
return null;
}
public static Map<String, Object> getPropertyMap(JsonNode jsonNode,
Set<String> ignoreKeys) throws JsonProcessingException, IOException{
Map<String, Object> map = new HashMap<>();
Iterator<Entry<String, JsonNode>> fields = jsonNode.fields();
while (fields.hasNext()) {
Entry<String, JsonNode> entry = fields.next();
String key = entry.getKey();
if (key.compareTo(Relation.HEADER_PROPERTY) == 0) {
continue;
}
if (key.startsWith("@") || key.startsWith("_")) {
continue;
}
if (ignoreKeys.contains(key)) {
continue;
}
JsonNode value = entry.getValue();
Object object = null;
try {
object = getObejctFromElement(value);
if(object!=null){
map.put(key, object);
}
} catch (ResourceRegistryException e) {
logger.warn("An invalidy property has been provided. It will be ignored.");
}
}
return map;
}
private void createRelation(OrientGraph orientGraph, Vertex resource, JsonNode relationArray,
@SuppressWarnings("rawtypes") Class<? extends Relation> relation) throws FacetNotFoundException,
ResourceNotFoundException, ResourceRegistryException {
Iterator<JsonNode> iterator = relationArray.elements();
while (iterator.hasNext()) {
JsonNode node = iterator.next();
/* Managing Target */
JsonNode target = node.get(Relation.TARGET_PROPERTY);
Header targetHeader = null;
try {
targetHeader = getHeader(target);
} catch (IOException e) {
new ResourceRegistryException(e);
}
Class<? extends Entity> targetClass = null;
Vertex targetVertex = null;
if (targetHeader == null) {
if (ConsistsOf.class.isAssignableFrom(relation)) {
targetVertex = createVertexEntity(
orientGraph, getClassProperty(target),
Facet.class,
target.toString(), true);
targetClass = Facet.class;
targetHeader = targetVertex.getProperty(Facet.HEADER_PROPERTY);
} else {
String error = String.format(
"%s %s must already exist. The UUID must be provided in the %s of %s json respresentation",
Relation.TARGET_PROPERTY, Resource.NAME,
Header.NAME, IsRelatedTo.NAME);
logger.error(error);
throw new ResourceRegistryException(error);
}
}else {
// The target Entity was already created we just need to create
// the right relation
if(ConsistsOf.class.isAssignableFrom(relation)) {
targetClass = Facet.class;
}else if(IsRelatedTo.class.isAssignableFrom(relation)){
targetClass = Resource.class;
}else{
String error = String.format(
"%s Unsupported %s creation", relation.toString(),
Relation.NAME);
logger.error(error);
throw new ResourceRegistryException(error);
}
String targetUUID = targetHeader.getUUID().toString();
String entityType = getClassProperty(target);
targetVertex = getEntity(orientGraph, targetUUID, entityType, targetClass);
}
String relationType = getClassProperty(node);
Set<String> ignoreKeys = new HashSet<>();
ignoreKeys.add(Relation.TARGET_PROPERTY);
ignoreKeys.add(Relation.SOURCE_PROPERTY);
Map<String, Object> edgeProperties = null;
try {
edgeProperties = getPropertyMap(node, ignoreKeys);
}catch(Exception e){
String error = "Error while parsing json to get Relation properties";
logger.error(error, e);
throw new ResourceRegistryException(error, e);
}
createEdgeRelation(orientGraph, resource, targetVertex,
relationType, relation, edgeProperties, true);
}
}
public Vertex createVertexEntity(String entityType,
Class<? extends Entity> entity, String jsonRepresentation,
boolean deferredCommit) throws ResourceRegistryException {
OrientGraph orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER);
return createVertexEntity(orientGraph, entityType, entity, jsonRepresentation,
deferredCommit);
}
public Vertex createVertexEntity(OrientGraph orientGraph, String entityType,
Class<? extends Entity> entity, String jsonRepresentation,
boolean deferredCommit) throws ResourceRegistryException {
try {
orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER);
SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
try {
@ -114,32 +371,57 @@ public class EntityManagementImpl implements EntityManagement {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(jsonRepresentation);
OrientVertex entityVertex = orientGraph.addVertex("class:"
String type = getClassProperty(jsonNode);
if (type != null && type.compareTo(entityType) != 0) {
String error = String
.format("Declared resourceType does not match with json representation %s!=%s",
entityType, type);
logger.trace(error);
throw new ResourceRegistryException(error);
}
OrientVertex vertex = orientGraph.addVertex("class:"
+ entityType);
Header entityHeader = getHeader(jsonNode);
if (entityHeader != null) {
vertex.setProperty(Entity.HEADER_PROPERTY, entityHeader);
} else {
entityHeader = HeaderUtility.addHeader(vertex, null);
}
if (Resource.class.isAssignableFrom(entity)) {
// TODO
// Facet and relation are created in calling method
} else {
Iterator<Entry<String, JsonNode>> iterator = jsonNode.fields();
while (iterator.hasNext()) {
Entry<String, JsonNode> entry = iterator.next();
if (entry.getKey().compareTo(Facet.HEADER_PROPERTY) == 0) {
continue;
}
if (entry.getKey().startsWith("@")
|| entry.getKey().startsWith("_")) {
continue;
}
JsonNode value = entry.getValue();
entityVertex.setProperty(entry.getKey(), value.asText());
vertex.setProperty(entry.getKey(), value.asText());
}
}
HeaderUtility.addHeader(entityVertex, null);
ContextUtility.addToActualContext(orientGraph, entityVertex);
ContextUtility.addToActualContext(orientGraph, vertex);
entityVertex.save();
vertex.save();
if (!deferredCommit) {
orientGraph.commit();
logger.trace("Created {} is {} orientVertexToJsonString", Vertex.class.getSimpleName(),
Utility.orientVertexToJsonString((OrientVertex) entityVertex, true));
return Utility.orientVertexToJsonString((OrientVertex) entityVertex, false);
//return Utility.orientVertexToJsonString((OrientVertex) entityVertex);
logger.trace("Created {} is {} orientVertexToJsonString",
Vertex.class.getSimpleName(), Utility
.orientVertexToJsonString(
(OrientVertex) vertex, true));
}
return vertex;
} catch (Exception e) {
if (orientGraph != null) {
@ -148,17 +430,19 @@ public class EntityManagementImpl implements EntityManagement {
throw new ResourceRegistryException("Error Creating " + entityType
+ " with " + jsonRepresentation, e.getCause());
} finally {
if (orientGraph != null) {
if (orientGraph != null && !deferredCommit) {
orientGraph.shutdown();
}
}
}
public String createEdgeRelation(String sourceUUID,
Class<? extends Entity> sourceClass, String targetUUID,
Class<? extends Entity> targetClass, String relationType,
String jsonProperties) throws FacetNotFoundException,
ResourceNotFoundException, ResourceRegistryException {
public Edge createEdgeRelation(
String sourceUUID, Class<? extends Entity> sourceClass,
String targetUUID, Class<? extends Entity> targetClass,
String relationType, @SuppressWarnings("rawtypes") Class<? extends Relation> relationBaseClass,
String jsonProperties)
throws FacetNotFoundException, ResourceNotFoundException,
ResourceRegistryException {
OrientGraph orientGraph = null;
if (relationType == null || relationType.compareTo("") == 0) {
@ -170,20 +454,63 @@ public class EntityManagementImpl implements EntityManagement {
orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER);
SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
try {
schemaManagement.getTypeSchema(relationType, Relation.NAME);
} catch (SchemaNotFoundException e) {
throw e;
}
Vertex source = getEntity(orientGraph, sourceUUID, null,
sourceClass);
Vertex target = getEntity(orientGraph, targetUUID, null,
targetClass);
// TODO Check if in and out types are compatible with the relation
// type as defined in relation type
Map<String, Object> edgeProperties = new HashMap<>();
if (jsonProperties != null && jsonProperties.compareTo("") != 0) {
try {
Set<String> ignoreKeys = new HashSet<>();
ignoreKeys.add(Relation.SOURCE_PROPERTY);
ignoreKeys.add(Relation.TARGET_PROPERTY);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(jsonProperties);
edgeProperties = getPropertyMap(jsonNode, ignoreKeys);
} catch (Exception e) {
new ResourceRegistryException(
"Error while setting Relation Properties", e);
}
}
return createEdgeRelation(orientGraph, source, target,
relationType, relationBaseClass, edgeProperties, false);
} catch(ResourceNotFoundException rnfe){
throw rnfe;
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
public Edge createEdgeRelation(OrientGraph orientGraph,
Vertex source, Vertex target,
String relationType, @SuppressWarnings("rawtypes") Class<? extends Relation> relationBaseClass,
Map<String, Object> edgeProperties,
boolean deferredCommit) throws ResourceRegistryException {
try {
SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
try {
schemaManagement.getTypeSchema(relationType, relationBaseClass.getClass().getSimpleName());
} catch (SchemaNotFoundException e) {
throw e;
}
if (relationType == null || relationType.compareTo("") == 0) {
throw new ResourceRegistryException(Relation.class.getSimpleName()
+ " Type cannot be empty or null");
}
// TODO Check the relation compatibility beetween source and target
logger.trace("Creating {} ({}) beetween {} -> {}",
Relation.class.getSimpleName(), relationType,
@ -192,43 +519,39 @@ public class EntityManagementImpl implements EntityManagement {
Edge edge = orientGraph.addEdge(null, source, target, relationType);
if (jsonProperties != null && jsonProperties.compareTo("") != 0) {
for(String key : edgeProperties.keySet()){
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(jsonProperties);
edge.setProperty(key, edgeProperties.get(key));
} catch (Exception e) {
String error = String.format("Error while setting property %s : %s", key, edgeProperties.get(key).toString());
logger.error(error);
throw new ResourceRegistryException(error, e);
}
}
Iterator<Entry<String, JsonNode>> iterator = jsonNode
.fields();
while (iterator.hasNext()) {
Entry<String, JsonNode> entry = iterator.next();
try {
JsonNode value = entry.getValue();
edge.setProperty(entry.getKey(), value.asText());
} catch (Exception e) {
throw new ResourceRegistryException(
"Error while setting property"
+ String.valueOf(entry), e);
}
}
} catch (Exception e) {
new ResourceRegistryException(
"Error while setting Relation Properties", e);
}
}
HeaderUtility.addHeader(edge, null);
ContextUtility.addToActualContext(orientGraph, edge);
((OrientEdge) edge).save();
orientGraph.commit();
return Utility.orientEdgeToJsonString((OrientEdge) edge);
} catch (ResourceNotFoundException rnfe) {
throw rnfe;
if (!deferredCommit) {
orientGraph.commit();
}
return edge;
} catch (ResourceRegistryException rre) {
if (orientGraph!=null) {
orientGraph.rollback();
}
throw rre;
} catch (Exception e) {
throw new ResourceNotFoundException(e.getMessage());
if (orientGraph!=null) {
orientGraph.rollback();
}
throw new ResourceRegistryException(e);
} finally {
if (orientGraph != null) {
if (orientGraph!= null && !deferredCommit) {
orientGraph.shutdown();
}
}
@ -237,7 +560,8 @@ public class EntityManagementImpl implements EntityManagement {
@Override
public String createFacet(String facetType, String jsonRepresentation)
throws ResourceRegistryException {
return createVertexEntity(facetType, Facet.class, jsonRepresentation);
Vertex vertex = createVertexEntity(facetType, Facet.class, jsonRepresentation, false);
return Utility.orientVertexToJsonString((OrientVertex) vertex, false);
}
@Override
@ -257,7 +581,9 @@ public class EntityManagementImpl implements EntityManagement {
Vertex facet = getEntity(orientGraph, uuid, facetType, Facet.class);
logger.trace("{} of type {} with UUID {} is {}", Facet.NAME, facetType, uuid, Utility.orientVertexToJsonString((OrientVertex) facet, true));
logger.trace("{} of type {} with UUID {} is {}", Facet.NAME,
facetType, uuid, Utility.orientVertexToJsonString(
(OrientVertex) facet, true));
return Utility.orientVertexToJsonString((OrientVertex) facet, true);
} catch (FacetNotFoundException fnfe) {
@ -322,11 +648,12 @@ public class EntityManagementImpl implements EntityManagement {
((OrientVertex) facet).save();
orientGraph.commit();
logger.trace("{} with UUID {} has been updated {}", Facet.NAME,
uuid, Utility.orientVertexToJsonString(
(OrientVertex) facet, true));
logger.trace("{} with UUID {} has been updated {}", Facet.NAME, uuid, Utility.orientVertexToJsonString((OrientVertex) facet, true));
return Utility.orientVertexToJsonString((OrientVertex) facet, false);
return Utility
.orientVertexToJsonString((OrientVertex) facet, false);
} catch (FacetNotFoundException fnfe) {
if (orientGraph != null) {
@ -383,8 +710,11 @@ public class EntityManagementImpl implements EntityManagement {
String consistOfType, String jsonProperties)
throws FacetNotFoundException, ResourceNotFoundException,
ResourceRegistryException {
return createEdgeRelation(resourceUUID, Resource.class, facetUUID,
Facet.class, consistOfType, jsonProperties);
Edge edge = createEdgeRelation(resourceUUID, Resource.class,
facetUUID, Facet.class,
consistOfType, ConsistsOf.class,
jsonProperties);
return Utility.orientEdgeToJsonString((OrientEdge) edge, false);
}
@Override
@ -396,8 +726,8 @@ public class EntityManagementImpl implements EntityManagement {
orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER);
Edge edge = getRelation(orientGraph, consistOfUUID, ConsistsOf.NAME,
ConsistsOf.class);
Edge edge = getRelation(orientGraph, consistOfUUID,
ConsistsOf.NAME, ConsistsOf.class);
edge.remove();
orientGraph.commit();
@ -426,9 +756,11 @@ public class EntityManagementImpl implements EntityManagement {
String targetResourceUuid, String relatedToType,
String jsonProperties) throws ResourceNotFoundException,
ResourceRegistryException {
return createEdgeRelation(sourceResourceUuid, Resource.class,
targetResourceUuid, Resource.class, relatedToType,
Edge edge = createEdgeRelation(sourceResourceUuid, Resource.class,
targetResourceUuid, Resource.class,
relatedToType, IsRelatedTo.class,
jsonProperties);
return Utility.orientEdgeToJsonString((OrientEdge) edge, false);
}
@Override
@ -440,8 +772,8 @@ public class EntityManagementImpl implements EntityManagement {
orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER);
Edge edge = getRelation(orientGraph, relatedToUUID, IsRelatedTo.NAME,
IsRelatedTo.class);
Edge edge = getRelation(orientGraph, relatedToUUID,
IsRelatedTo.NAME, IsRelatedTo.class);
edge.remove();
orientGraph.commit();
@ -464,18 +796,49 @@ public class EntityManagementImpl implements EntityManagement {
return true;
}
@Override
public String createResource(String resourceType, String jsonRepresentation)
throws ResourceRegistryException {
try {
JsonNode jsonNode = org.gcube.informationsystem.impl.utils.Utility.getJSONNode(jsonRepresentation);
} catch (IOException e) {
throw new ResourceRegistryException(e);
private static String marshallResource(Vertex vertex){
// TODO serialize all facets as weel
return Utility.orientVertexToJsonString((OrientVertex) vertex, false);
}
return createVertexEntity(resourceType, Resource.class,
@Override
public String createResource(String resourceType, String jsonRepresentation)
throws ResourceRegistryException {
logger.trace("Trying to create {} : {}", resourceType,
jsonRepresentation);
OrientGraph orientGraph;
try {
orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER);
Vertex resource = createVertexEntity(orientGraph, resourceType, Resource.class, jsonRepresentation, true);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(jsonRepresentation);
String property = lowerCaseFirstCharacter(ConsistsOf.NAME);
if (jsonNode.has(property)) {
JsonNode jsonNodeArray = jsonNode.get(property);
createRelation(orientGraph, resource, jsonNodeArray, ConsistsOf.class);
}
property = lowerCaseFirstCharacter(IsRelatedTo.NAME);
if (jsonNode.has(property)) {
JsonNode jsonNodeArray = jsonNode.get(property);
createRelation(orientGraph, resource, jsonNodeArray, IsRelatedTo.class);
}
return marshallResource(resource);
} catch(ResourceRegistryException rre) {
throw rre;
} catch(Exception e){
throw new ResourceRegistryException(e);
}
}
@Override
@ -495,11 +858,11 @@ public class EntityManagementImpl implements EntityManagement {
Vertex resource = getEntity(orientGraph, uuid, resourceType,
Resource.class);
// TODO get all attached facets
logger.trace("{} of type {} with UUID {} is {}", Resource.NAME, resourceType, uuid, Utility.orientVertexToJsonString((OrientVertex) resource, true));
logger.trace("{} of type {} with UUID {} is {}", Resource.NAME,
resourceType, uuid, Utility.orientVertexToJsonString(
(OrientVertex) resource, true));
return Utility.orientVertexToJsonString((OrientVertex) resource, false);
return marshallResource(resource);
} catch (ResourceNotFoundException rnfe) {
throw rnfe;
} catch (Exception e) {
@ -523,6 +886,8 @@ public class EntityManagementImpl implements EntityManagement {
Vertex resource = getEntity(orientGraph, uuid, null, Resource.class);
// TODO remove attached facets if not managed from hooks
resource.remove();
orientGraph.commit();

View File

@ -9,6 +9,7 @@ import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.informationsystem.model.orientdb.impl.entity.Context;
import org.gcube.informationsystem.model.orientdb.impl.entity.Entity;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.dbinitialization.SecurityContext;
@ -77,15 +78,18 @@ public class ContextUtility {
}
public static OrientGraph getActualSecurityContextGraph(
PermissionMode permissionMode) throws Exception {
PermissionMode permissionMode) throws ResourceRegistryException {
try {
String contextID = getActualContextUUID();
OrientGraphFactory factory = SecurityContextMapper
.getSecurityContextFactory(contextID, permissionMode);
return factory.getTx();
} catch (ContextException ce) {
logger.error("Unable to retrieve context.", ce);
throw ce;
} catch (Exception e) {
logger.error("Unable to retrieve context.", e);
throw e;
throw new ResourceRegistryException(e);
}
}

View File

@ -9,7 +9,8 @@ import java.util.Date;
import java.util.UUID;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.provider.UserInfo;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.informationsystem.model.orientdb.impl.embedded.Header;
import org.gcube.informationsystem.model.orientdb.impl.entity.Entity;
import org.gcube.informationsystem.model.orientdb.impl.relation.Relation;
@ -39,10 +40,11 @@ public class HeaderUtility {
String creator = org.gcube.informationsystem.model.embedded.Header.UNKNOWN_USER;
try {
UserInfo userInfo = AuthorizationProvider.instance.get();
String username = userInfo.getUserName();
if (username != null && username.compareTo("") != 0) {
creator = username;
Caller caller = AuthorizationProvider.instance.get();
ClientInfo clientInfo = caller.getClient();
String clientId = clientInfo.getId();
if (clientId != null && clientId.compareTo("") != 0) {
creator = clientId;
} else {
throw new Exception("Username null or empty");
}

View File

@ -35,9 +35,19 @@ public class Utility {
private static final Logger logger = LoggerFactory.getLogger(Utility.class);
public static String orientVertexToJsonString(OrientVertex orientVertex, boolean raw) {
public static String orientVertexToJsonString(OrientVertex orientVertex,
boolean raw) {
ORecord oRecord = orientVertex.getRecord();
if(raw){
if (raw) {
return oRecord.toJSON();
}
return oRecord.toJSON("class");
}
public static String orientEdgeToJsonString(OrientEdge orientEdge,
boolean raw) {
ORecord oRecord = orientEdge.getRecord();
if (raw) {
return oRecord.toJSON();
}
return oRecord.toJSON("class");
@ -68,11 +78,6 @@ public class Utility {
}
}
public static String orientEdgeToJsonString(OrientEdge orientEdge) {
ORecord oRecord = orientEdge.getRecord();
return oRecord.toJSON();
}
public static String edgeToJsonString(Edge edge, boolean raw)
throws JSONException {
if (raw) {

View File

@ -155,8 +155,8 @@ public class ContextManagementImplTest {
}
@Test
public void devContextTest() throws ContextNotFoundException, ContextException, InternalException {
//@Test
public void createDevContext() throws ContextNotFoundException, ContextException, InternalException {
String gcube = contextManagementImpl.create(null, "gcube");
logger.trace("/gcube : {}", gcube);
String devsec = contextManagementImpl.create(gcube, "devsec");
@ -179,6 +179,15 @@ public class ContextManagementImplTest {
logger.debug("The DB should be now clean");
}
//@Test
public void removeDevContext() throws ContextNotFoundException, ContextException, InternalException {
contextManagementImpl.delete("508da10a-b8e7-414f-b176-65e227addcae");
contextManagementImpl.delete("d729c993-32f9-4bcd-9311-d92f791e5e12");
contextManagementImpl.delete("8bf286f8-a93c-4936-9826-91f1b1c2af34");
contextManagementImpl.delete("7273c1c7-bc06-4d49-bade-19dc3ab322f7");
contextManagementImpl.delete("132691d2-e22d-4e3b-b605-f8b2c94f2c77");
}
@Test
public void readTest() throws ContextNotFoundException, ContextException, InternalException {
String A_1 = contextManagementImpl.create(null, "LLL");

View File

@ -233,10 +233,7 @@ public class EntityManagementImplTest {
hostingNode.addFacet(cpuFacetImpl);
StringWriter stringWriter = new StringWriter();
Entities.marshal(hostingNode, stringWriter);
String json = entityManagementImpl.createResource(HostingNode.NAME, stringWriter.toString());
String json = entityManagementImpl.createResource(HostingNode.NAME, Entities.marshal(hostingNode));
logger.debug("Created : {}", json);
}