Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2011 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 import  org.springframework.dao.InvalidDataAccessApiUsageException;
 import static;
 public class TraversalFieldAccessorFactory implements FieldAccessorFactory {
     private final Neo4jTemplate template;
     public TraversalFieldAccessorFactory(Neo4jTemplate template) {
         this. = template;
     public boolean accept(final Neo4jPersistentProperty f) {
         final GraphTraversal graphEntityTraversal = f.getAnnotation(GraphTraversal.class);
         return graphEntityTraversal != null
                 && graphEntityTraversal.traversal() != FieldTraversalDescriptionBuilder.class
                 && f.getType().equals(Iterable.class);
     public FieldAccessor forField(final Neo4jPersistentProperty property) {
         return new TraversalFieldAccessor(property);

Michael Hunger
 	public static class TraversalFieldAccessor implements FieldAccessor {
 	    protected final Neo4jPersistentProperty property;
         private final Neo4jTemplate template;
 	    private Class<?> target;
         protected String[] params;
         public TraversalFieldAccessor(final Neo4jPersistentProperty propertyNeo4jTemplate template) {
 	        this. = property;
             this. = template;
             final GraphTraversal graphEntityTraversal = property.getAnnotation(GraphTraversal.class);
 	        this. = resolveTarget(graphEntityTraversal,property);
             this. = graphEntityTraversal.params();
             this. = createTraversalDescription(graphEntityTraversal);
         private Class<?> resolveTarget(GraphTraversal graphTraversalNeo4jPersistentProperty property) {
             if (!graphTraversal.elementClass().equals(Object.class)) return graphTraversal.elementClass();
             final Class<?> result = property.getTypeInformation().getActualType().getType();
             Class<?>[] allowedTypes={Node.class,Relationship.classPath.class};
             if (checkTypes(result,allowedTypes)) {
                 return result;
             // Strict mode causes problem if these checks are done before the above
             if (.isNodeEntity(result)) return result;
             if (.isRelationshipEntity(result)) return result;
             throw new IllegalArgumentException("The target result type "+result+" of the traversal is no subclass of the allowed types: "+property+" "+allowedTypes);
         private boolean checkTypes(Class<?> targetClass<?>...allowedTypes) {
             for (Class<?> type : allowedTypes) {
                 if (type.isAssignableFrom(target)) return true;
             return false;
	    public boolean isWriteable(Object entity) {
	        return false;
	    public Object setValue(final Object entityfinal Object newValMappingPolicy mappingPolicy) {
	        throw new InvalidDataAccessApiUsageException("Cannot set readonly traversal description field " + );
	    public Object getValue(final Object entityMappingPolicy mappingPolicy) {
	        final TraversalDescription traversalDescription = .build(entity,);
            return doReturn(.traverse(entitytraversalDescription));
	    private FieldTraversalDescriptionBuilder createTraversalDescription(final GraphTraversal graphEntityTraversal) {
	        try {
	            final Class<? extends FieldTraversalDescriptionBuildertraversalDescriptionClass = graphEntityTraversal.traversal();
	            final Constructor<? extends FieldTraversalDescriptionBuilderconstructor = traversalDescriptionClass.getDeclaredConstructor();
	            return constructor.newInstance();
	        } catch (Exception e) {
	            throw new RuntimeException("Error creating TraversalDescription from " + ,e);
		public Object getDefaultValue() {
			return null;
New to GrepCode? Check out our FAQ X