Skip to content

Commit

Permalink
Make SortOrder reusable.
Browse files Browse the repository at this point in the history
The properties used in a SortOrder object are now processed before every
usage. This makes the SortOrder class reusable in multiple calls and
even allows usage for other domain classes that also have the same
properties to get ordered by.

Fixes #486
Backport to 3.0.

(cherry picked from commit 1a0924d)
  • Loading branch information
meistermeier committed Jun 20, 2018
1 parent 86c1e2e commit adc4fa4
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public PagingAndSortingQuery(
}

public String getStatement() {
String sorting = sortOrder().toString();
String sorting = sortOrder().asString();

StringBuilder sb = new StringBuilder();
sb.append(matchClause);
Expand Down
14 changes: 12 additions & 2 deletions core/src/main/java/org/neo4j/ogm/cypher/query/SortClause.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package org.neo4j.ogm.cypher.query;

import java.util.List;

/**
* @author Luanne Misquitta
*/
Expand All @@ -21,7 +23,7 @@ public class SortClause {
private final SortOrder.Direction direction;
private final String[] properties;

public SortClause(SortOrder.Direction direction, String... properties) {
SortClause(SortOrder.Direction direction, String... properties) {
this.direction = direction;
this.properties = properties;
}
Expand All @@ -30,8 +32,16 @@ public String[] getProperties() {
return properties;
}

public String toString() {
public SortClause fromResolvedProperties(String... resolvedProperties) {
if (resolvedProperties.length != properties.length) {
throw new IllegalArgumentException("Resolved properties count must match existing properties count.");
}

return new SortClause(this.direction, resolvedProperties);

}

String asString() {
StringBuilder sb = new StringBuilder();

if (properties.length > 0) {
Expand Down
12 changes: 9 additions & 3 deletions core/src/main/java/org/neo4j/ogm/cypher/query/SortOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public class SortOrder {

private List<SortClause> sortClauses;
final List<SortClause> sortClauses;

/**
* Creates SortOrder with a empty list of {@link SortClause}.
Expand Down Expand Up @@ -52,6 +52,12 @@ public SortOrder(Direction direction, String... properties) {
add(direction, properties);
}

public static SortOrder fromSortClauses(List<SortClause> sortClauses) {
SortOrder sortOrder = new SortOrder();
sortOrder.sortClauses.addAll(sortClauses);
return sortOrder;
}

/**
* Adds a new {@link SortClause} containing properties ordered by {@link Direction#ASC}.
*
Expand Down Expand Up @@ -101,12 +107,12 @@ public boolean hasSortClauses() {
return !sortClauses.isEmpty();
}

public String toString() {
public String asString() {
StringBuilder sb = new StringBuilder();
if (!sortClauses.isEmpty()) {
sb.append(" ORDER BY ");
for (SortClause ordering : sortClauses) {
sb.append(ordering);
sb.append(ordering.asString());
sb.append(",");
}
sb.deleteCharAt(sb.length() - 1);
Expand Down
74 changes: 0 additions & 74 deletions core/src/main/java/org/neo4j/ogm/session/Neo4jSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -627,80 +627,6 @@ public void debug(String msg) {
logger.debug("Thread {}: {}", Thread.currentThread().getId(), msg);
}

public void resolvePropertyAnnotations(Class entityType, Iterable<Filter> filters) {
for (Filter filter : filters) {
if (filter.getOwnerEntityType() == null) {
filter.setOwnerEntityType(entityType);
}
String propertyName = resolvePropertyName(filter.getOwnerEntityType(), filter.getPropertyName());
Filter.setNameFromProperty(filter, propertyName);

ClassInfo classInfo = metaData().classInfo(entityType.getName());
FieldInfo fieldInfo = classInfo.fieldsInfo().get(filter.getPropertyName());
if (fieldInfo != null) {
filter.setPropertyConverter(fieldInfo.getPropertyConverter());
filter.setCompositeConverter(fieldInfo.getCompositeConverter());
}

if (filter.isNested()) {
resolveRelationshipType(filter);
ClassInfo nestedClassInfo = metaData().classInfo(filter.getNestedPropertyType().getName());
filter.setNestedEntityTypeLabel(entityType(nestedClassInfo.name()));
if (metaData().isRelationshipEntity(nestedClassInfo.name())) {
filter.setNestedRelationshipEntity(true);
}
}
}
}

public void resolvePropertyAnnotations(Class entityType, SortOrder sortOrder) {
final String escapedProperty = "`%s`";

if (sortOrder != null) {
for (SortClause sortClause : sortOrder.sortClauses()) {
for (int i = 0; i < sortClause.getProperties().length; i++) {
sortClause.getProperties()[i] = String
.format(escapedProperty, resolvePropertyName(entityType, sortClause.getProperties()[i]));
}
}
}
}

private String resolvePropertyName(Class entityType, String propertyName) {
ClassInfo classInfo = metaData().classInfo(entityType.getName());
FieldInfo fieldInfo = classInfo.propertyFieldByName(propertyName);
if (fieldInfo != null && fieldInfo.getAnnotations() != null) {
AnnotationInfo annotation = fieldInfo.getAnnotations().get(Property.class);
if (annotation != null) {
return annotation.get(Property.NAME, propertyName);
}
}
//session.metaData().classInfo(entityType.getName()).propertyFieldByName(propertyName).property();
return propertyName;
}

private void resolveRelationshipType(Filter parameter) {
ClassInfo classInfo = metaData().classInfo(parameter.getOwnerEntityType().getName());
FieldInfo fieldInfo = classInfo.relationshipFieldByName(parameter.getNestedPropertyName());

String defaultRelationshipType = RelationshipUtils.inferRelationshipType(parameter.getNestedPropertyName());
parameter.setRelationshipType(defaultRelationshipType);
parameter.setRelationshipDirection(Relationship.UNDIRECTED);
if (fieldInfo.getAnnotations() != null) {
AnnotationInfo annotation = fieldInfo.getAnnotations().get(Relationship.class);
if (annotation != null) {
parameter.setRelationshipType(annotation.get(Relationship.TYPE, defaultRelationshipType));
parameter.setRelationshipDirection(annotation.get(Relationship.DIRECTION, Relationship.UNDIRECTED));
}
if (fieldInfo.getAnnotations().get(StartNode.class) != null) {
parameter.setRelationshipDirection(Relationship.OUTGOING);
}
if (fieldInfo.getAnnotations().get(EndNode.class) != null) {
parameter.setRelationshipDirection(Relationship.INCOMING);
}
}
}

@Override
public String getLastBookmark() {
return bookmark;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@
/**
* @author Vince Bickers
*/
public class DeleteDelegate {
public class DeleteDelegate extends SessionDelegate {

private static final Logger LOG = LoggerFactory.getLogger(DeleteDelegate.class);
private final Neo4jSession session;

public DeleteDelegate(Neo4jSession neo4jSession) {
this.session = neo4jSession;
public DeleteDelegate(Neo4jSession session) {
super(session);
}

private DeleteStatements getDeleteStatementsBasedOnType(Class type) {
Expand Down Expand Up @@ -173,7 +172,7 @@ public <T> Object delete(Class<T> clazz, Iterable<Filter> filters, boolean listR

if (classInfo != null) {

session.resolvePropertyAnnotations(clazz, filters);
resolvePropertyAnnotations(clazz, filters);

CypherQuery query;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,12 @@
* @author Luanne Misquitta
* @author Jasper Blues
*/
public class ExecuteQueriesDelegate {
public class ExecuteQueriesDelegate extends SessionDelegate {

private static final Pattern WRITE_CYPHER_KEYWORDS = Pattern.compile("\\b(CREATE|MERGE|SET|DELETE|REMOVE|DROP)\\b");

private final Neo4jSession session;

public ExecuteQueriesDelegate(Neo4jSession neo4jSession) {
this.session = neo4jSession;
public ExecuteQueriesDelegate(Neo4jSession session) {
super(session);
}

public <T> T queryForObject(Class<T> type, String cypher, Map<String, ?> parameters) {
Expand Down Expand Up @@ -182,7 +180,7 @@ public long count(Class<?> clazz, Iterable<Filter> filters) {

if (classInfo != null) {

session.resolvePropertyAnnotations(clazz, filters);
resolvePropertyAnnotations(clazz, filters);

CypherQuery query;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
/**
* @author Luanne Misquitta
*/
public class GraphIdDelegate {

private final Neo4jSession session;
public class GraphIdDelegate extends SessionDelegate {

public GraphIdDelegate(Neo4jSession session) {
this.session = session;
super(session);
}

public Long resolveGraphIdFor(Object possibleEntity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@
* @author Vince Bickers
* @author Luanne Misquitta
*/
public class LoadByIdsDelegate {
public class LoadByIdsDelegate extends SessionDelegate {

private static final Logger LOG = LoggerFactory.getLogger(LoadByIdsDelegate.class);
private final Neo4jSession session;

public LoadByIdsDelegate(Neo4jSession session) {
this.session = session;
super(session);
}

public <T, ID extends Serializable> Collection<T> loadAll(Class<T> type, Collection<ID> ids, SortOrder sortOrder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
/**
* @author Vince Bickers
*/
public class LoadByInstancesDelegate {

private final Neo4jSession session;
public class LoadByInstancesDelegate extends SessionDelegate {

public LoadByInstancesDelegate(Neo4jSession session) {
this.session = session;
super(session);
}

public <T> Collection<T> loadAll(Collection<T> objects, SortOrder sortOrder, Pagination pagination, int depth) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@
* @author Vince Bickers
* @author Luanne Misquitta
*/
public class LoadByTypeDelegate {
public class LoadByTypeDelegate extends SessionDelegate {

private static final Logger LOG = LoggerFactory.getLogger(LoadByTypeDelegate.class);
private final Neo4jSession session;

public LoadByTypeDelegate(Neo4jSession session) {
this.session = session;
super(session);
}

public <T> Collection<T> loadAll(Class<T> type, Filters filters, SortOrder sortOrder, Pagination pagination,
Expand All @@ -58,17 +57,17 @@ public <T> Collection<T> loadAll(Class<T> type, Filters filters, SortOrder sortO
}
QueryStatements queryStatements = session.queryStatementsFor(type, depth);

session.resolvePropertyAnnotations(type, sortOrder);
SortOrder sortOrderWithResolvedProperties = sortOrderWithResolvedProperties(type, sortOrder);

PagingAndSortingQuery query;
if (filters.isEmpty()) {
query = queryStatements.findByType(entityLabel, depth);
} else {
session.resolvePropertyAnnotations(type, filters);
resolvePropertyAnnotations(type, filters);
query = queryStatements.findByType(entityLabel, filters, depth);
}

query.setSortOrder(sortOrder)
query.setSortOrder(sortOrderWithResolvedProperties)
.setPagination(pagination);

return session.doInTransaction( () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@
* @author Vince Bickers
* @author Mark Angrish
*/
public class LoadOneDelegate {
public class LoadOneDelegate extends SessionDelegate {

private static final Logger logger = LoggerFactory.getLogger(LoadOneDelegate.class);

private Neo4jSession session;

public LoadOneDelegate(Neo4jSession session) {
this.session = session;
super(session);
}

public <T, ID extends Serializable> T load(Class<T> type, ID id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
* @author Vince Bickers
* @author Luanne Misquitta
*/
public class SaveDelegate {
public class SaveDelegate extends SessionDelegate {

private final Neo4jSession session;
private final RequestExecutor requestExecutor;

public SaveDelegate(Neo4jSession neo4jSession) {
this.session = neo4jSession;
requestExecutor = new RequestExecutor(neo4jSession);
public SaveDelegate(Neo4jSession session) {
super(session);
requestExecutor = new RequestExecutor(session);
}

public <T> void save(T object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,18 @@
/**
* @author vince
*/
final class SaveEventDelegate {
final class SaveEventDelegate extends SessionDelegate {

private static final Logger logger = LoggerFactory.getLogger(SaveEventDelegate.class);

private Neo4jSession session;
private Set<Object> visited;
private Set<Object> preSaved;
private Set<MappedRelationship> registeredRelationships = new HashSet<>();
private Set<MappedRelationship> addedRelationships = new HashSet<>();
private Set<MappedRelationship> deletedRelationships = new HashSet<>();

SaveEventDelegate(Neo4jSession session) {
this.session = session;
super(session);
this.preSaved = new HashSet<>();
this.visited = new HashSet<>();

Expand Down
Loading

0 comments on commit adc4fa4

Please sign in to comment.