Fixing prepared query
This commit is contained in:
parent
f18d0ad3cf
commit
daa7959a7e
2
pom.xml
2
pom.xml
|
@ -10,7 +10,7 @@
|
|||
|
||||
<groupId>org.gcube.information-system</groupId>
|
||||
<artifactId>resource-registry</artifactId>
|
||||
<version>4.0.0</version>
|
||||
<version>4.0.1-SNAPSHOT</version>
|
||||
<name>Resource Registry Service</name>
|
||||
<description>The Resource Registry is a web-service which represent the core component of the gCube Information System</description>
|
||||
<packaging>war</packaging>
|
||||
|
|
|
@ -2,8 +2,10 @@ package org.gcube.informationsystem.resourceregistry.instances.model.entities;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
@ -25,6 +27,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
|
|||
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityAlreadyPresentException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
|
||||
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
|
||||
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
|
||||
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
|
||||
|
@ -34,6 +37,7 @@ import org.gcube.informationsystem.resourceregistry.instances.model.relations.Re
|
|||
import org.gcube.informationsystem.resourceregistry.utils.Utility;
|
||||
|
||||
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
|
||||
import com.orientechnologies.orient.core.id.ORID;
|
||||
import com.orientechnologies.orient.core.metadata.schema.OClass;
|
||||
import com.orientechnologies.orient.core.record.ODirection;
|
||||
import com.orientechnologies.orient.core.record.OEdge;
|
||||
|
@ -244,6 +248,32 @@ public abstract class EntityManagement<E extends EntityElement> extends EntityEl
|
|||
}
|
||||
}
|
||||
|
||||
public boolean propertyMatchRequestedVlaue(OVertex v, String key, String requestedValue, Object instanceValue) throws SchemaException, ResourceRegistryException {
|
||||
return requestedValue.compareTo(instanceValue.toString())==0;
|
||||
|
||||
|
||||
/*
|
||||
OClass oClass = ElementManagement.getOClass(v);
|
||||
OProperty oProperty = oClass.getProperty(key);
|
||||
if(oProperty==null){
|
||||
// It is an additional property
|
||||
return requestedValue.compareTo(instanceValue.toString())==0;
|
||||
}
|
||||
OType oType = oProperty.getType();
|
||||
switch (oType) {
|
||||
case BOOLEAN:
|
||||
Boolean requested = Boolean.valueOf(requestedValue.toLowerCase());
|
||||
return requested == (Boolean) instanceValue;
|
||||
|
||||
case STRING:
|
||||
return requestedValue.compareTo((String) instanceValue)==0;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
public String reallyQuery(String relationType, String referenceType, UUID referenceUUID, ODirection direction,
|
||||
boolean polymorphic, Map<String,String> constraint) throws ResourceRegistryException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
@ -282,8 +312,43 @@ public abstract class EntityManagement<E extends EntityElement> extends EntityEl
|
|||
references = oDatabaseDocument.browseClass(referenceType, polymorphic);
|
||||
}
|
||||
|
||||
Set<ORID> analysed = new HashSet<>();
|
||||
|
||||
for(Object r : references) {
|
||||
OVertex v = (OVertex) r;
|
||||
|
||||
boolean skip = false;
|
||||
// checking if the constraints are satisfied
|
||||
for(String key : constraint.keySet()) {
|
||||
String value = constraint.get(key);
|
||||
Object o = v.getProperty(key);
|
||||
if(value==null) {
|
||||
if(o==null) {
|
||||
//ok
|
||||
}else {
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
}else {
|
||||
if(o==null) {
|
||||
// The vertex has not a required property to be tested
|
||||
// or the property is null
|
||||
skip = true;
|
||||
break;
|
||||
}else {
|
||||
skip = !propertyMatchRequestedVlaue(v, key, value, o);
|
||||
if(skip) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(skip) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
List<ODirection> directions = new ArrayList<>();
|
||||
if(direction==ODirection.BOTH) {
|
||||
directions.add(ODirection.IN);
|
||||
|
@ -298,16 +363,33 @@ public abstract class EntityManagement<E extends EntityElement> extends EntityEl
|
|||
for(OEdge edge : edges) {
|
||||
OVertex vertex = edge.getVertex(d);
|
||||
|
||||
if(v.getIdentity().compareTo(vertex.getIdentity()) == 0) {
|
||||
ORID vertexORID = vertex.getIdentity();
|
||||
|
||||
if(analysed.contains(vertexORID)) {
|
||||
continue;
|
||||
}
|
||||
analysed.add(vertexORID);
|
||||
|
||||
if(v.getIdentity().compareTo(vertexORID) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
OClass oClass = ElementManagement.getOClass(vertex);
|
||||
if(polymorphic && oClass.isSubClassOf(elementType)) {
|
||||
// OK
|
||||
} else {
|
||||
// excluding from results
|
||||
continue;
|
||||
|
||||
/*
|
||||
* If the requested type (i.e. elementType)
|
||||
* differs form the resulting type (i.e. oClass.getName())
|
||||
* we need to evaluate if polymorphism is requested and
|
||||
* if the resulting type is a subclass of the requested type
|
||||
*
|
||||
*/
|
||||
if(oClass.getName().compareTo(elementType)!=0) {
|
||||
if(polymorphic && oClass.isSubClassOf(elementType)) {
|
||||
// OK
|
||||
} else {
|
||||
// excluding from results
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package org.gcube.informationsystem.resourceregistry.rest;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||
import org.gcube.informationsystem.resourceregistry.ContextTest;
|
||||
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
|
||||
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
|
||||
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
|
||||
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
|
||||
import org.gcube.resourcemanagement.model.reference.entities.facets.IdentifierFacet;
|
||||
import org.gcube.resourcemanagement.model.reference.entities.resources.Configuration;
|
||||
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.orientechnologies.orient.core.record.ODirection;
|
||||
|
||||
public class AccessTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AccessTest.class);
|
||||
|
||||
// @Test
|
||||
public void simulatGetAllResourcesHavingFacet() throws ObjectNotFound, Exception {
|
||||
ContextTest.setContextByName("/gcube/devNext");
|
||||
|
||||
UUID refereceUUID = null;
|
||||
ODirection directionEnum = ODirection.OUT;
|
||||
Boolean polymorphic = false;
|
||||
|
||||
String resourceType = Utility.getTypeName(Configuration.class);
|
||||
String relationType = Utility.getTypeName(IsIdentifiedBy.class);
|
||||
String referenceType = Utility.getTypeName(IdentifierFacet.class);
|
||||
|
||||
Map<String,String> constraint = new HashMap<>();
|
||||
constraint.put("value", "Aux");
|
||||
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
ElementManagement erManagement = ElementManagementUtility.getERManagement(resourceType);
|
||||
|
||||
String ret = ((ResourceManagement) erManagement).query(relationType, referenceType, refereceUUID, directionEnum,
|
||||
polymorphic, constraint);
|
||||
|
||||
logger.debug(ret);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue