Skip to content

Commit

Permalink
refactored repository queries
Browse files Browse the repository at this point in the history
added support for like, not null, property-exists and index-query lookups for derived queries
fixed delete* methods in repositories
added singleOrNull() method to fluent result API
  • Loading branch information
jexp committed Nov 1, 2011
1 parent a6f3a9a commit bf8e0f3
Show file tree
Hide file tree
Showing 33 changed files with 798 additions and 459 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ src/ant/.ant-targets-upload-dist.xml
*.ipr
*.iws
spring-data-neo4j-examples/imdb/log.roo

src/docbkx/snippets
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Movie {
@RelatedTo(type="DIRECTED", direction = INCOMING)
Person director;

@RelatedTo(elementClass = Person.class, type = "ACTS_IN", direction = INCOMING)
@RelatedTo(type = "ACTS_IN", direction = INCOMING)
Set<Person> actors;

@RelatedToVia(elementClass = Role.class, type = "ACTS_IN", direction = INCOMING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.neo4j.rest;

import org.neo4j.helpers.collection.IteratorUtil;
import org.neo4j.rest.graphdb.util.ConvertedResult;
import org.springframework.data.neo4j.conversion.EndResult;

Expand All @@ -32,6 +33,11 @@ public R single() {
return result.single();
}

@Override
public R singleOrNull() {
return IteratorUtil.singleOrNull(result);
}

@Override
public void handle(final org.springframework.data.neo4j.conversion.Handler<R> rHandler) {
result.handle(new SpringHandler<R>(rHandler));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public Iterator<T> iterator() {
}


@SuppressWarnings({"unchecked"})
@Override
public T singleOrNull() {
return (T) to(Object.class).singleOrNull();
}

@SuppressWarnings("unchecked")
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
*/
public interface EndResult<R> extends Iterable<R> {
R single();
R singleOrNull();
void handle(Handler<R> handler);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.neo4j.graphdb.index.IndexHits;
import org.neo4j.helpers.collection.ClosableIterable;
import org.neo4j.helpers.collection.IteratorUtil;
import org.neo4j.helpers.collection.IteratorWrapper;

import java.util.Iterator;
Expand Down Expand Up @@ -52,12 +53,15 @@ public <R> EndResult<R> to(Class<R> type) {
@Override
public T single() {
try {
final Iterator<T> it = result.iterator();
if (!it.hasNext()) throw new IllegalStateException("Expected at least one result, got none.");
final T value = it.next();
if (it.hasNext())
throw new IllegalStateException("Expected at least one result, got more than one.");
return value;
return IteratorUtil.single(result);
} finally {
closeIfNeeded();
}
}
@Override
public T singleOrNull() {
try {
return IteratorUtil.singleOrNull(result);
} finally {
closeIfNeeded();
}
Expand All @@ -69,11 +73,16 @@ public <R> EndResult<R> to(final Class<R> type, final ResultConverter<T, R> resu
@Override
public R single() {
try {
final Iterator<T> it = result.iterator();
if (!it.hasNext()) throw new IllegalStateException("Expected at least one result, got none.");
final T value = it.next();
if (it.hasNext())
throw new IllegalStateException("Expected at least one result, got more than one.");
final T value = IteratorUtil.single(result);
return resultConverter.convert(value, type);
} finally {
closeIfNeeded();
}
}
@Override
public R singleOrNull() {
try {
final T value = IteratorUtil.singleOrNull(result);
return resultConverter.convert(value, type);
} finally {
closeIfNeeded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public class IndexInfo {
private IndexType indexType;
private final String fieldName;
private final Indexed.Level level;
private String indexKey;

public IndexInfo(Indexed annotation, Neo4jPersistentProperty property) {
this.indexName = determineIndexName(annotation,property);
this.indexType = annotation.indexType();
fieldName = annotation.fieldName();
this.indexKey = fieldName.isEmpty() ? property.getNeo4jPropertyName() : fieldName;
level = annotation.level();
}

Expand All @@ -50,4 +52,12 @@ public String getIndexName() {
public IndexType getIndexType() {
return indexType;
}

public boolean isFullText() {
return indexType == IndexType.FULLTEXT;
}

public String getIndexKey() {
return indexKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ public interface Neo4jPersistentProperty extends PersistentProperty<Neo4jPersist
Object getValue(final Object entity);

Neo4jPersistentEntity<?> getOwner();

String getIndexKey();
}
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,7 @@ public boolean exists(Long id) {

@Override
public void delete(T entity) {
final PropertyContainer state = template.getPersistentState(entity);
if (state instanceof Node) {
Node node = (Node) state;
node.delete();
}
if (state instanceof Relationship) {
Relationship relationship = (Relationship) state;
relationship.delete();
}

template.delete(entity);
}

@Override
Expand Down
Loading

0 comments on commit bf8e0f3

Please sign in to comment.