resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/base/entities/JsonQueryFacet.java

75 lines
2.3 KiB
Java
Raw Normal View History

2021-10-25 11:00:54 +02:00
package org.gcube.informationsystem.resourceregistry.queries.json.base.entities;
2021-10-14 15:01:22 +02:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Direction;
2021-10-18 16:23:41 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
2021-10-25 11:00:54 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException;
import org.gcube.informationsystem.resourceregistry.queries.json.base.relations.JsonQueryConsistsOf;
2021-10-14 15:01:22 +02:00
/**
* @author Luca Frosini (ISTI - CNR)
*/
2021-10-19 22:18:41 +02:00
public class JsonQueryFacet extends JsonQueryEntity {
2021-10-14 15:01:22 +02:00
public final static String _IN = "_in";
2021-10-18 16:23:41 +02:00
public JsonQueryFacet(JsonNode jsonQuery) throws SchemaException, ResourceRegistryException {
2021-10-14 15:01:22 +02:00
super(jsonQuery, AccessType.FACET);
2021-10-19 22:18:41 +02:00
fieldNamesToRemove.add(JsonQueryFacet._IN);
2021-10-14 15:01:22 +02:00
}
@Override
public StringBuffer analize(StringBuffer stringBuffer) throws SchemaException, ResourceRegistryException {
2021-10-14 15:01:22 +02:00
StringBuffer newBuffer = new StringBuffer();
2022-12-02 15:48:10 +01:00
int size = jsonNode.size();
boolean entry = entryPoint;
if(jsonNode.has(_IN)) {
if(!entryPoint) {
throw new InvalidQueryException(_IN + " property cannot be used in a facet if it is not the entry object");
}
JsonNode consistsOfNode = jsonNode.get(_IN);
JsonQueryConsistsOf jsonQueryConsistsOf = new JsonQueryConsistsOf(consistsOfNode);
jsonQueryConsistsOf.setEntryPoint(entryPoint);
jsonQueryConsistsOf.setDirection(Direction.OUT);
stringBuffer = jsonQueryConsistsOf.analize(stringBuffer);
entry = false;
2022-12-02 15:48:10 +01:00
/* Need to substract 1 from size otherwise
* it add WHERE at the end because _in
* is not a property to be used for a WHERE compare
*/
--size;
}
2021-10-14 15:01:22 +02:00
newBuffer.append("SELECT FROM ");
if(!entry) {
2021-10-15 15:43:56 +02:00
newBuffer.append("( ");
2021-10-14 15:01:22 +02:00
newBuffer.append("TRAVERSE inV(\"");
}
newBuffer.append(type);
if(!entry) {
2021-10-15 15:43:56 +02:00
newBuffer.append("\") FROM ( ");
2021-10-14 15:01:22 +02:00
newBuffer.append(stringBuffer);
newBuffer.append(")");
newBuffer.append(")");
}
// Size 1 means that only '@class' property is present
if(size > 1) {
newBuffer.append(" WHERE ");
newBuffer.append(addConstraints(jsonNode, null, null));
2021-10-14 15:01:22 +02:00
}
return newBuffer;
}
}