2021-10-25 11:00:54 +02:00
package org.gcube.informationsystem.resourceregistry.queries.json.base.relations ;
2021-10-14 15:01:22 +02:00
2023-11-08 17:16:18 +01:00
import javax.ws.rs.InternalServerErrorException ;
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 ;
2022-12-07 12:19:19 +01:00
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo ;
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.entities.JsonQueryResource ;
2023-11-08 17:16:18 +01:00
import org.gcube.informationsystem.resourceregistry.utils.OrientDBUtility ;
2021-10-14 15:01:22 +02:00
/ * *
* @author Luca Frosini ( ISTI - CNR )
* /
2021-10-19 16:46:00 +02:00
public class JsonQueryIsRelatedTo extends JsonQueryRelation {
2021-10-14 15:01:22 +02:00
protected String requestedResourceType ;
2021-10-18 16:23:41 +02:00
public JsonQueryIsRelatedTo ( JsonNode jsonQuery ) throws SchemaException , ResourceRegistryException {
2021-10-14 15:01:22 +02:00
super ( jsonQuery , AccessType . IS_RELATED_TO ) ;
2023-11-08 17:16:18 +01:00
direction = null ;
2021-10-14 15:01:22 +02:00
}
public String getRequestedResourceType ( ) {
return requestedResourceType ;
}
public void setRequestedResourceType ( String requestedResourceType ) {
this . requestedResourceType = requestedResourceType ;
}
2023-11-08 17:16:18 +01:00
protected StringBuffer traverseBackToCallerResource ( StringBuffer stringBuffer ) {
StringBuffer buffer = new StringBuffer ( ) ;
buffer . append ( " TRAVERSE " ) ;
buffer . append ( direction . opposite ( ) . name ( ) . toLowerCase ( ) ) ;
buffer . append ( " V( \" " ) ;
buffer . append ( requestedResourceType ) ;
buffer . append ( " \" ) FROM ( " ) ; // Open (
buffer . append ( stringBuffer ) ;
buffer . append ( " ) " ) ; // Close )
return buffer ;
}
2021-10-14 15:01:22 +02:00
2023-11-08 17:16:18 +01:00
public StringBuffer createSelect ( StringBuffer stringBuffer , boolean wrapInnerQuery ) throws SchemaException , ResourceRegistryException {
2021-10-15 11:33:07 +02:00
StringBuffer buffer = new StringBuffer ( ) ;
2023-11-08 17:16:18 +01:00
buffer . append ( " SELECT FROM " ) ;
2021-10-15 11:33:07 +02:00
2023-11-08 17:16:18 +01:00
if ( wrapInnerQuery ) {
buffer . append ( " ( " ) ;
buffer . append ( stringBuffer ) ;
buffer . append ( " ) " ) ;
} else {
buffer . append ( type ) ;
2022-12-07 12:19:19 +01:00
}
2023-11-08 17:16:18 +01:00
if ( entryPoint | | size > 1 ) {
buffer . append ( " WHERE " ) ;
2022-12-07 12:19:19 +01:00
}
2023-11-08 17:16:18 +01:00
if ( size > 1 ) {
buffer . append ( addConstraints ( jsonNode , null , null ) ) ;
if ( entryPoint ) {
buffer . append ( " AND " ) ;
}
2021-10-15 11:33:07 +02:00
}
2023-11-08 17:16:18 +01:00
if ( entryPoint ) {
buffer . append ( OrientDBUtility . ORIENTDB_CLASS_PROPERTY ) ;
buffer . append ( " INSTANCEOF \" " ) ;
2021-10-15 15:43:56 +02:00
buffer . append ( type ) ;
2023-11-08 17:16:18 +01:00
buffer . append ( " \" " ) ;
2021-10-15 15:43:56 +02:00
}
2021-10-15 11:33:07 +02:00
2023-11-08 17:16:18 +01:00
return buffer ;
}
public void setDirectionByJson ( ) throws InvalidQueryException {
if ( entryPoint ) {
String error = " The function JsonQueryIsRelatedTo#setDirectionByJson() cannot be called for an entry point " ;
logger . error ( error ) ;
throw new InternalServerErrorException ( error ) ;
}
2021-10-15 11:33:07 +02:00
2023-11-08 17:16:18 +01:00
boolean found = false ;
2021-10-15 11:33:07 +02:00
2023-11-08 17:16:18 +01:00
if ( jsonNode . has ( IsRelatedTo . SOURCE_PROPERTY ) ) {
logger . trace ( " {} for type {} has {} property " , IsRelatedTo . NAME , type , IsRelatedTo . SOURCE_PROPERTY ) ;
direction = Direction . OUT ;
found = true ;
2021-10-15 11:33:07 +02:00
}
2023-11-08 17:16:18 +01:00
if ( jsonNode . has ( IsRelatedTo . TARGET_PROPERTY ) ) {
if ( found ) {
StringBuffer buffer = new StringBuffer ( ) ;
buffer . append ( IsRelatedTo . NAME ) ;
buffer . append ( " for type " ) ;
buffer . append ( type ) ;
buffer . append ( " has both " ) ;
buffer . append ( IsRelatedTo . SOURCE_PROPERTY ) ;
buffer . append ( " and " ) ;
buffer . append ( IsRelatedTo . TARGET_PROPERTY ) ;
buffer . append ( " property. Only entry points can have both because one is implicit from the resource containg the " ) ;
buffer . append ( IsRelatedTo . NAME ) ;
buffer . append ( " relation. " ) ;
logger . error ( " This part of the json query is not valid {} \ n{} " , jsonNode . toString ( ) , buffer . toString ( ) ) ;
throw new InvalidQueryException ( buffer . toString ( ) ) ;
}
direction = Direction . IN ;
}
2021-10-15 11:33:07 +02:00
}
2021-10-14 15:01:22 +02:00
@Override
2021-10-19 16:46:00 +02:00
public StringBuffer analize ( StringBuffer stringBuffer ) throws SchemaException , ResourceRegistryException {
2021-10-14 15:01:22 +02:00
2023-11-08 17:16:18 +01:00
if ( ! entryPoint & & direction = = null ) {
throw new InternalServerErrorException ( " Caller Resource must invoke setDirectionByJson() first. This is a server bug. Please contact the administator. " ) ;
2021-10-14 15:01:22 +02:00
}
2023-11-08 17:16:18 +01:00
boolean wrapInnerQuery = false ;
2021-10-14 18:03:55 +02:00
2023-11-08 17:16:18 +01:00
if ( traverseBack ) {
StringBuffer buffer = new StringBuffer ( ) ;
2022-12-07 12:19:19 +01:00
buffer . append ( " TRAVERSE " ) ;
buffer . append ( direction . opposite ( ) . name ( ) . toLowerCase ( ) ) ;
2023-11-08 17:16:18 +01:00
buffer . append ( " E( \" " ) ;
buffer . append ( type ) ;
2022-12-07 12:19:19 +01:00
buffer . append ( " \" ) FROM ( " ) ;
2023-11-08 17:16:18 +01:00
buffer . append ( stringBuffer ) ;
2022-12-07 12:19:19 +01:00
buffer . append ( " ) " ) ;
2023-11-08 17:16:18 +01:00
stringBuffer = buffer ;
wrapInnerQuery = true ;
}
Direction wrapDirection = direction ;
if ( jsonNode . has ( IsRelatedTo . SOURCE_PROPERTY ) ) {
- - size ;
JsonNode sourceJsonNode = jsonNode . get ( IsRelatedTo . SOURCE_PROPERTY ) ;
JsonQueryResource jsonQueryResource = new JsonQueryResource ( sourceJsonNode ) ;
wrapDirection = Direction . OUT ;
jsonQueryResource . setDirection ( Direction . OUT ) ;
jsonQueryResource . setTraverseBack ( ! ( ( ! traverseBack ) & & ! wrapInnerQuery ) ) ;
stringBuffer = jsonQueryResource . analize ( stringBuffer ) ;
wrapInnerQuery = true ;
2022-12-07 12:19:19 +01:00
}
2021-10-14 18:03:55 +02:00
2023-11-08 17:16:18 +01:00
if ( jsonNode . has ( IsRelatedTo . TARGET_PROPERTY ) ) {
if ( jsonNode . has ( IsRelatedTo . SOURCE_PROPERTY ) ) {
StringBuffer buffer = new StringBuffer ( ) ;
buffer . append ( " TRAVERSE " ) ;
buffer . append ( wrapDirection . name ( ) . toLowerCase ( ) ) ;
buffer . append ( " E( \" " ) ;
buffer . append ( type ) ;
buffer . append ( " \" ) FROM ( " ) ;
buffer . append ( stringBuffer ) ;
buffer . append ( " ) " ) ;
stringBuffer = buffer ;
}
2022-12-07 12:19:19 +01:00
2023-11-08 17:16:18 +01:00
- - size ;
JsonNode targetJsonNode = jsonNode . get ( IsRelatedTo . TARGET_PROPERTY ) ;
JsonQueryResource jsonQueryResource = new JsonQueryResource ( targetJsonNode ) ;
wrapDirection = Direction . IN ;
2022-12-07 12:19:19 +01:00
jsonQueryResource . setDirection ( Direction . IN ) ;
2023-11-08 17:16:18 +01:00
jsonQueryResource . setTraverseBack ( ! ( ( ! traverseBack ) & & ! wrapInnerQuery ) ) ;
2022-12-07 12:19:19 +01:00
stringBuffer = jsonQueryResource . analize ( stringBuffer ) ;
2023-11-08 17:16:18 +01:00
wrapInnerQuery = true ;
}
if ( wrapInnerQuery ) {
StringBuffer buffer = new StringBuffer ( ) ;
buffer . append ( " TRAVERSE " ) ;
buffer . append ( wrapDirection . name ( ) . toLowerCase ( ) ) ;
buffer . append ( " E( \" " ) ;
buffer . append ( type ) ;
buffer . append ( " \" ) FROM ( " ) ;
buffer . append ( stringBuffer ) ;
buffer . append ( " ) " ) ;
stringBuffer = buffer ;
}
if ( entryPoint | | size > 1 ) {
stringBuffer = createSelect ( stringBuffer , wrapInnerQuery ) ;
}
if ( ! entryPoint ) {
stringBuffer = traverseBackToCallerResource ( stringBuffer ) ;
2022-12-07 12:19:19 +01:00
}
2021-10-15 11:33:07 +02:00
return stringBuffer ;
2021-10-14 15:01:22 +02:00
}
}