Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import groovy.transform.CompileStatic
*/
@CompileStatic
enum Direction {

INCOMING, OUTGOING, BOTH

@Override
Expand All @@ -53,4 +54,4 @@ enum Direction {
boolean isOutgoing() {
return this == OUTGOING || this == BOTH
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
*/
package grails.neo4j

import groovy.transform.CompileStatic

import org.neo4j.driver.QueryRunner
import org.neo4j.driver.Result

import grails.gorm.MultiTenant
import grails.gorm.api.GormAllOperations
import grails.gorm.multitenancy.Tenants
import groovy.transform.CompileStatic
import org.grails.datastore.gorm.GormEnhancer
import org.grails.datastore.gorm.GormEntity
import org.grails.datastore.gorm.GormStaticApi
Expand All @@ -34,8 +38,6 @@ import org.grails.datastore.gorm.schemaless.DynamicAttributes
import org.grails.datastore.mapping.model.config.GormProperties
import org.grails.datastore.mapping.multitenancy.MultiTenancySettings
import org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException
import org.neo4j.driver.Result
import org.neo4j.driver.QueryRunner

/**
* Extends the default {@org.grails.datastore.gorm.GormEntity} trait, adding new methods specific to Neo4j
Expand Down Expand Up @@ -67,7 +69,6 @@ trait Neo4jEntity<D> implements GormEntity<D>, DynamicAttributes {
DynamicAttributes.super.putAt(name, val)
}


/**
* Obtains a dynamic attribute
*
Expand All @@ -77,12 +78,12 @@ trait Neo4jEntity<D> implements GormEntity<D>, DynamicAttributes {
@Override
def getAt(String name) {
def val = DynamicAttributes.super.getAt(name)
if(val == null) {
if (val == null) {
GormStaticApi staticApi = GormEnhancer.findStaticApi(getClass())
GraphPersistentEntity entity = (GraphPersistentEntity) staticApi.gormPersistentEntity
if(entity.hasDynamicAssociations()) {
if (entity.hasDynamicAssociations()) {
def id = ident()
if(id != null) {
if (id != null) {
staticApi.withSession { Neo4jSession session ->
DynamicAssociationSupport.loadDynamicAssociations(session, entity, this, id)
}
Expand All @@ -105,10 +106,9 @@ trait Neo4jEntity<D> implements GormEntity<D>, DynamicAttributes {
QueryRunner boltSession = getStatementRunner(session)

String queryString
if(cypher instanceof GString) {
if (cypher instanceof GString) {
queryString = Neo4jGormStaticApi.buildNamedParameterQueryFromGString((GString) cypher, params)
}
else {
} else {
queryString = cypher.toString()
}
params['this'] = session.getObjectIdentifier(this)
Expand All @@ -129,7 +129,7 @@ trait Neo4jEntity<D> implements GormEntity<D>, DynamicAttributes {
QueryRunner boltSession = getStatementRunner(session)

Map<String, Object> paramsMap = new LinkedHashMap()
paramsMap.put("this", session.getObjectIdentifier(this))
paramsMap.put('this', session.getObjectIdentifier(this))

includeTenantIdIfNecessary(session, cypher, paramsMap)

Expand All @@ -141,7 +141,6 @@ trait Neo4jEntity<D> implements GormEntity<D>, DynamicAttributes {
}
}


/**
* perform a cypher query
* @param queryString
Expand All @@ -152,14 +151,14 @@ trait Neo4jEntity<D> implements GormEntity<D>, DynamicAttributes {
Map<String, Object> arguments
if (session.getDatastore().multiTenancyMode == MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR) {
if (!queryString.contains("\$tenantId")) {
throw new TenantNotFoundException("Query does not specify a tenant id, but multi tenant mode is DISCRIMINATOR!")
throw new TenantNotFoundException('Query does not specify a tenant id, but multi tenant mode is DISCRIMINATOR!')
} else {
arguments = new LinkedHashMap<String, Object>()
arguments.put(GormProperties.TENANT_IDENTITY, Tenants.currentId(Neo4jDatastore))
arguments.put("this", session.getObjectIdentifier(this))
arguments.put('this', session.getObjectIdentifier(this))
}
} else {
arguments = (Map<String, Object>) Collections.singletonMap("this", session.getObjectIdentifier(this))
arguments = (Map<String, Object>) Collections.singletonMap('this', session.getObjectIdentifier(this))
}
QueryRunner boltSession = getStatementRunner(session)
boltSession.run(queryString, arguments)
Expand Down Expand Up @@ -269,10 +268,10 @@ trait Neo4jEntity<D> implements GormEntity<D>, DynamicAttributes {
private void includeTenantIdIfNecessary(Neo4jSession session, String queryString, Map<String, Object> paramsMap) {
if ((this instanceof MultiTenant) && session.getDatastore().multiTenancyMode == MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR) {
if (!queryString.contains("\$tenantId")) {
throw new TenantNotFoundException("Query does not specify a tenant id, but multi tenant mode is DISCRIMINATOR!")
throw new TenantNotFoundException('Query does not specify a tenant id, but multi tenant mode is DISCRIMINATOR!')
} else {
paramsMap.put(GormProperties.TENANT_IDENTITY, Tenants.currentId(Neo4jDatastore))
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package grails.neo4j

import groovy.transform.CompileStatic

import org.grails.datastore.gorm.GormEnhancer
import org.grails.datastore.gorm.GormEntity
import org.grails.datastore.gorm.neo4j.api.Neo4jGormStaticApi
Expand All @@ -33,6 +34,7 @@ import org.grails.datastore.gorm.schemaless.DynamicAttributes
*/
@CompileStatic
trait Node<D> implements Neo4jEntity<D>, GormEntity<D>, DynamicAttributes {

/**
* Allows accessing to dynamic properties with the dot operator
*
Expand Down Expand Up @@ -139,4 +141,4 @@ trait Node<D> implements Neo4jEntity<D>, GormEntity<D>, DynamicAttributes {
static <T> Path<D, T> findPathTo(Class<T> type, CharSequence cypher, Map params) {
((Neo4jGormStaticApi) GormEnhancer.findStaticApi(this)).findPathTo(type, cypher, params)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ package grails.neo4j
* @see org.neo4j.driver.types.Path
* @since 6.1
*/
interface Path<S, E> extends Iterable<Segment<S,E>> {
interface Path<S, E> extends Iterable<Segment<S, E>> {

/**
* A segment
*
* @see org.neo4j.driver.types.Path.Segment
*/
interface Segment<S, E> {

Relationship<S, E> relationship()

S start()

E end()
}


/** @return the start node of this path */
S start()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package grails.neo4j

import groovy.transform.CompileStatic

import org.grails.datastore.gorm.GormEnhancer
import org.grails.datastore.gorm.GormEntity
import org.grails.datastore.gorm.neo4j.RelationshipPersistentEntity
Expand All @@ -32,7 +33,7 @@ import org.grails.datastore.gorm.schemaless.DynamicAttributes
* @since 6.1
*/
@CompileStatic
trait Relationship<F,T> implements DynamicAttributes, Serializable {
trait Relationship<F, T> implements DynamicAttributes, Serializable {

/**
* The relationship type
Expand All @@ -54,13 +55,12 @@ trait Relationship<F,T> implements DynamicAttributes, Serializable {
*/
T to


/**
* @return The type of the relationship
*/
String type() {
if(this.theType == null) {
theType = ((RelationshipPersistentEntity)GormEnhancer.findEntity(getClass())).type()
if (this.theType == null) {
theType = ((RelationshipPersistentEntity) GormEnhancer.findEntity(getClass())).type()
}
return theType
}
Expand All @@ -84,11 +84,11 @@ trait Relationship<F,T> implements DynamicAttributes, Serializable {
* @return True if they are equal
*/
boolean equals(Object object) {
if(object instanceof Relationship) {
Relationship other = (Relationship)object
GormEntity fromEntity = (GormEntity)this.from
GormEntity toEntity = (GormEntity)this.to
return fromEntity?.ident() == ((GormEntity)other.from)?.ident() && toEntity?.ident() == ((GormEntity)other.to)?.ident()
if (object instanceof Relationship) {
Relationship other = (Relationship) object
GormEntity fromEntity = (GormEntity) this.from
GormEntity toEntity = (GormEntity) this.to
return fromEntity?.ident() == ((GormEntity) other.from)?.ident() && toEntity?.ident() == ((GormEntity) other.to)?.ident()
}
return false
}
Expand All @@ -97,16 +97,16 @@ trait Relationship<F,T> implements DynamicAttributes, Serializable {
* @return hashCode
*/
int hashCode() {
GormEntity fromEntity = (GormEntity)this.from
GormEntity toEntity = (GormEntity)this.to
GormEntity fromEntity = (GormEntity) this.from
GormEntity toEntity = (GormEntity) this.to

int result = type != null ? type.hashCode() : 0
def fromId = fromEntity?.ident()
if(fromId != null) {
if (fromId != null) {
result = 31 * result + fromId.hashCode()
}
def toId = toEntity?.ident()
if(toId != null) {
if (toId != null) {
result = 31 * result + toId.hashCode()
}
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package grails.neo4j.mapping

import groovy.transform.CompileStatic

import org.grails.datastore.gorm.neo4j.mapping.config.Attribute
import org.grails.datastore.gorm.neo4j.mapping.config.NodeConfig
import org.grails.datastore.gorm.neo4j.mapping.config.RelationshipConfig
Expand Down Expand Up @@ -54,9 +55,9 @@ class MappingBuilder {
new ClosureRelMappingDefinition(mappingDefinition)
}


@CompileStatic
private static class ClosureRelMappingDefinition implements MappingDefinition<RelationshipConfig, Attribute> {

final Closure definition
private RelationshipConfig mapping

Expand All @@ -71,16 +72,18 @@ class MappingBuilder {

@Override
RelationshipConfig build() {
if(mapping == null) {
if (mapping == null) {
RelationshipConfig nc = new RelationshipConfig()
mapping = RelationshipConfig.configureExisting(nc, definition)
}
return mapping
}

}

@CompileStatic
private static class ClosureNodeMappingDefinition implements MappingDefinition<NodeConfig, Attribute> {

final Closure definition
private NodeConfig mapping

Expand All @@ -95,7 +98,7 @@ class MappingBuilder {

@Override
NodeConfig build() {
if(mapping == null) {
if (mapping == null) {
NodeConfig nc = new NodeConfig()
mapping = NodeConfig.configureExisting(nc, definition)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ import java.lang.annotation.Target
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
@interface Cypher {

String value()
}
}
Loading
Loading