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 http://www.apache.org/licenses/LICENSE-2.0 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.
 
 
 package org.springframework.data.neo4j.fieldaccess;
 
 
 import java.util.Map;
 
 import static org.springframework.data.neo4j.support.DoReturn.doReturn;
 
 
 	
 		this. = template;
 	}
 
 	public boolean accept(final Neo4jPersistentProperty property) {
         if (!property.isRelationship()) return false;
         return property.getRelationshipInfo().isRelatedToVia() && property.getRelationshipInfo().isSingle();
     }
 
 	public FieldAccessor forField(final Neo4jPersistentProperty property) {
         final RelationshipInfo relationshipInfo = property.getRelationshipInfo();
 		return new RelatedToViaSingleFieldAccessor(relationshipInfo.getRelationshipType(), relationshipInfo.getDirection(), (Class<?>) relationshipInfo.getTargetType().getType(), ,property);
 	}
 
 	public static class RelatedToViaSingleFieldAccessor implements FieldAccessor {
 
         private final Class<?> relatedType;
         private final Neo4jTemplate template;
         private final Neo4jPersistentProperty property;
         private final RelationshipHelper relationshipHelper;
         private final RelationshipEntities relationshipEntities;
 
         public RelatedToViaSingleFieldAccessor(final RelationshipType typefinal Direction directionfinal Class<?> relatedTypefinal Neo4jTemplate templateNeo4jPersistentProperty property) {
              = new RelationshipHelper(templatedirectiontype);
              = new RelationshipEntities(property);
             this. = relatedType;
             this. = template;
             this. = property;
 
         }
 
         @Override
         public Object getDefaultValue() {
             return null;
         }
 
         @Override
 	    public Object setValue(final Object entityfinal Object newValMappingPolicy mappingPolicy) {
             final Node startNode = .checkAndGetNode(entity);
             final Map<Node,ObjectendNodeToEntityMapping = .loadEndNodeToRelationshipEntityMapping(startNodetoSet(newVal), );
             .removeMissingRelationshipsInStoreAndKeepOnlyNewRelationShipsInSet(startNodeendNodeToEntityMapping.keySet(), null);
             persistEntities(endNodeToEntityMapping.values(), .getRelationshipType() );
             return newVal;
 	    }
 
         private Iterable<ObjecttoSet(Object newVal) {
             if (newVal==nullreturn Collections.emptySet();
             return Collections.singleton(newVal);
         }
 
         private void persistEntitiesfinal Collection<ObjectrelationshipEntitiesRelationshipType relationshipType ) {
             for (Object entity : relationshipEntities) {
                 .save(entityrelationshipType);
             }
         }
 
 	    @Override
 	    public boolean isWriteable(Object entity) {
 	        return true;
 	    }
	    @Override
	    public Object getValue(final Object entityMappingPolicy mappingPolicy) {
            final Node node = .checkAndGetNode(entity);
            Relationship rel = .getSingleRelationship(node);
            return doReturn(rel==null ? null :  .createEntityFromState(rel,,mappingPolicy)); // template.load(rel,relatedType));
        }
    }
New to GrepCode? Check out our FAQ X