From 9a5df4a24920f754c9fbf891e2473b8896ceec52 Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Sat, 11 Sep 2010 14:57:38 +0200 Subject: [PATCH] fixed entity-manger bug --- .../datastore/graph/neo4j/jpa/Neo4JQuery.java | 121 ++++++++++-------- .../graph/neo4j/jpa/Neo4jEntityManager.java | 4 +- 2 files changed, 68 insertions(+), 57 deletions(-) diff --git a/src/main/java/org/springframework/datastore/graph/neo4j/jpa/Neo4JQuery.java b/src/main/java/org/springframework/datastore/graph/neo4j/jpa/Neo4JQuery.java index f986b3715..cfbf36db7 100644 --- a/src/main/java/org/springframework/datastore/graph/neo4j/jpa/Neo4JQuery.java +++ b/src/main/java/org/springframework/datastore/graph/neo4j/jpa/Neo4JQuery.java @@ -16,62 +16,71 @@ import static org.springframework.datastore.graph.neo4j.support.Tuple2._; import static org.springframework.util.ObjectUtils.nullSafeEquals; /** -* @author Michael Hunger -* @since 29.08.2010 -*/ -public class Neo4JQuery implements TypedQuery { - protected final Finder finder; - protected final Class entityClass; + * @author Michael Hunger + * @since 29.08.2010 + */ +public class Neo4JQuery implements TypedQuery { + protected final Class resultClass; protected final String qlString; + private final FinderFactory finderFactory; private final PersistenceUnitInfo info; private final Pattern fromPattern = Pattern.compile("^.*\\bfrom\\s+([A-Z][A-Za-z0-9]+)\\b.*"); - private int startPosition=0; - private int maxResult=-1; - private QueryExectuor queryExectuor; - private Map, Tuple2> parameters=new HashMap, Tuple2>(); + private int startPosition = 0; + private int maxResult = -1; + private QueryExecutor queryExecutor; + private Map, Tuple2> parameters = new HashMap, Tuple2>(); - public Neo4JQuery(final String qlString, final FinderFactory finderFactory, final PersistenceUnitInfo info, final Class entityClass) { + public Neo4JQuery(final String qlString, final FinderFactory finderFactory, final PersistenceUnitInfo info, Class resultClass) { this.qlString = qlString; + this.finderFactory = finderFactory; this.info = info; - final Matcher matcher = fromPattern.matcher(qlString); - if (matcher.matches()) { - final String shortName = matcher.group(1); - if (entityClass!=null) this.entityClass= entityClass; - else this.entityClass=getEntityClass(shortName); - finder = finderFactory.getFinderForClass(this.entityClass); - queryExectuor = createExecutor(qlString); - } else { - throw new IllegalAccessError("Unable to parse query "+qlString); - } + this.resultClass = resultClass; + queryExecutor = createExecutor(qlString); } - private QueryExectuor createExecutor(final String qlString) { - if (qlString.contains(" count(")) return new QueryExectuor() { - @Override - protected Long findObject() { - return finder.count(); - } - }; - return new QueryExectuor() { + private QueryExecutor createExecutor(final String qlString) { + final Finder finder = getFinderFromQuery(qlString); + if (qlString.contains(" count(")) { + return new QueryExecutor() { + @Override + protected T findObject() { + return (T)Long.valueOf(finder.count()); + } + }; + } + return new QueryExecutor() { @Override protected Iterable findList() { - return finder.findAll(); + return (Iterable) finder.findAll(); } }; } - abstract static class QueryExectuor { - protected Iterable findList() { return Collections.singleton(findObject()); } - protected T findObject() { return null; } + private Finder getFinderFromQuery(String qlString) { + final Matcher matcher = fromPattern.matcher(qlString); + if (!matcher.matches()) throw new IllegalAccessError("Unable to parse query " + qlString); + final String shortName = matcher.group(1); + final Class entityClass = getEntityClass(shortName); + return finderFactory.getFinderForClass(entityClass); + } + + abstract static class QueryExecutor { + protected Iterable findList() { + return Collections.singleton(findObject()); + } + + protected T findObject() { + return null; + } } - private Class getEntityClass(final String shortName) { + private Class getEntityClass(final String shortName) { try { final String className = getFQN(shortName); - return (Class) Class.forName(className); + return (Class) Class.forName(className); } catch (ClassNotFoundException e) { - throw new IllegalStateException("Error resolving class "+shortName,e); + throw new IllegalStateException("Error resolving class " + shortName, e); } } @@ -79,16 +88,16 @@ public class Neo4JQuery implements TypedQuery { for (final String className : info.getManagedClassNames()) { if (className.endsWith(shortName)) return className; } - throw new ClassNotFoundException("No mapped class found for "+shortName); + throw new ClassNotFoundException("No mapped class found for " + shortName); } @Override public List getResultList() { final List result = new ArrayList(); - int count=0; - for (final T nodeBacked : (Iterable)queryExectuor.findList()) { - if (maxResult>=0 && count==startPosition+maxResult) break; - if (count>=startPosition) { + int count = 0; + for (final T nodeBacked : (Iterable) queryExecutor.findList()) { + if (maxResult >= 0 && count == startPosition + maxResult) break; + if (count >= startPosition) { result.add(nodeBacked); } count++; @@ -98,7 +107,7 @@ public class Neo4JQuery implements TypedQuery { @Override public T getSingleResult() { - final Iterator found = queryExectuor.findList().iterator(); + final Iterator found = queryExecutor.findList().iterator(); return found.hasNext() ? (T) found.next() : null; // todo errors when none or too many ? } @@ -146,48 +155,48 @@ public class Neo4JQuery implements TypedQuery { } private static

Tuple2 value(final P value) { - return _(value,(TemporalType)null); + return _(value, (TemporalType) null); } @Override public TypedQuery setParameter(final Parameter parameter, final Calendar calendar, final TemporalType temporalType) { - this.parameters.put(parameter,_(calendar,temporalType)); + this.parameters.put(parameter, _(calendar, temporalType)); return this; } @Override public TypedQuery setParameter(final Parameter parameter, final Date date, final TemporalType temporalType) { - this.parameters.put(parameter,_(date,temporalType)); + this.parameters.put(parameter, _(date, temporalType)); return this; } @Override public TypedQuery setParameter(final String name, final Object value) { - this.parameters.put(param(name),value(value)); + this.parameters.put(param(name), value(value)); return this; } @Override public TypedQuery setParameter(final String name, final Date value, final TemporalType temporalType) { - this.parameters.put(param(name),_(value,temporalType)); + this.parameters.put(param(name), _(value, temporalType)); return this; } @Override public TypedQuery setParameter(final String name, final Calendar value, final TemporalType temporalType) { - this.parameters.put(param(name),_(value,temporalType)); + this.parameters.put(param(name), _(value, temporalType)); return this; } @Override public TypedQuery setParameter(final int position, final Object value) { - this.parameters.put(param(position),value(value)); + this.parameters.put(param(position), value(value)); return this; } @Override public TypedQuery setParameter(final int position, final Date value, final TemporalType temporalType) { - this.parameters.put(param(position),value(value)); + this.parameters.put(param(position), value(value)); return this; } @@ -199,7 +208,7 @@ public class Neo4JQuery implements TypedQuery { @Override public Parameter getParameter(final String name) { for (final Parameter parameter : parameters.keySet()) { - if (nullSafeEquals(parameter.getName(),name)) return parameter; + if (nullSafeEquals(parameter.getName(), name)) return parameter; } return null; } @@ -207,7 +216,8 @@ public class Neo4JQuery implements TypedQuery { @Override public Parameter getParameter(final String name, final Class type) { for (final Parameter parameter : parameters.keySet()) { - if (nullSafeEquals(parameter.getName(),name) && nullSafeEquals(type,parameter.getParameterType())) return (Parameter) parameter; + if (nullSafeEquals(parameter.getName(), name) && nullSafeEquals(type, parameter.getParameterType())) + return (Parameter) parameter; } return null; } @@ -215,7 +225,7 @@ public class Neo4JQuery implements TypedQuery { @Override public Parameter getParameter(final int index) { for (final Parameter parameter : parameters.keySet()) { - if (nullSafeEquals(parameter.getPosition(),index)) return parameter; + if (nullSafeEquals(parameter.getPosition(), index)) return parameter; } return null; } @@ -223,7 +233,8 @@ public class Neo4JQuery implements TypedQuery { @Override public Parameter getParameter(final int index, final Class type) { for (final Parameter parameter : parameters.keySet()) { - if (nullSafeEquals(parameter.getPosition(),index) && nullSafeEquals(type,parameter.getParameterType())) return (Parameter) parameter; + if (nullSafeEquals(parameter.getPosition(), index) && nullSafeEquals(type, parameter.getParameterType())) + return (Parameter) parameter; } return null; } @@ -250,7 +261,7 @@ public class Neo4JQuery implements TypedQuery { @Override public TypedQuery setParameter(final int position, final Calendar value, final TemporalType temporalType) { - parameters.put(param(position),_(value,temporalType)); + parameters.put(param(position), _(value, temporalType)); return this; } diff --git a/src/main/java/org/springframework/datastore/graph/neo4j/jpa/Neo4jEntityManager.java b/src/main/java/org/springframework/datastore/graph/neo4j/jpa/Neo4jEntityManager.java index 32925cbe5..81862c02d 100644 --- a/src/main/java/org/springframework/datastore/graph/neo4j/jpa/Neo4jEntityManager.java +++ b/src/main/java/org/springframework/datastore/graph/neo4j/jpa/Neo4jEntityManager.java @@ -223,10 +223,10 @@ public class Neo4jEntityManager implements EntityManager { @Override public TypedQuery createQuery(final String qlString, final Class entityClass) { checkClosed(); - return (TypedQuery)createNeo4jQuery(qlString,(Class)entityClass); + return (TypedQuery)createNeo4jQuery(qlString,entityClass); } - public TypedQuery createNeo4jQuery(final String qlString, final Class entityClass) { + public TypedQuery createNeo4jQuery(final String qlString, final Class entityClass) { return new Neo4JQuery(qlString, finderFactory,info,entityClass); }