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 static org.springframework.data.neo4j.support.DoReturn.doReturn;

Author(s):
Michael Hunger
Since:
12.09.2010
public class IdFieldAccessorFactory implements FieldAccessorFactory {
    private final Neo4jTemplate template;
    public IdFieldAccessorFactory(Neo4jTemplate template) {
        this. = template;
    }
    @Override
	public boolean accept(final Neo4jPersistentProperty property) {
	    return property.isIdProperty();
	}
	public FieldAccessor forField(final Neo4jPersistentProperty property) {
	    return new IdFieldAccessor(property);
	}
	public static class IdFieldAccessor implements FieldAccessor {
	    protected final Neo4jPersistentProperty property;
        private final Neo4jTemplate template;
        public IdFieldAccessor(final Neo4jPersistentProperty propertyNeo4jTemplate template) {
	        this. = property;
            this. = template;
        }
	    @Override
	    public boolean isWriteable(Object entity) {
	        return false;
	    }
	    @Override
	    public Object setValue(final Object entityfinal Object newValMappingPolicy mappingPolicy) {
	        return newVal;
	    }
	    @Override
	    public Object getValue(final Object entityMappingPolicy mappingPolicy) {
            final PropertyContainer state = .getPersistentState(entity);
            if (state instanceof Node) {
                return doReturn(((Node)state).getId());
            }
            if (state instanceof Relationship) {
                return doReturn(((Relationship)state).getId());
            }
            throw new MappingException("Error retrieving id value from "+entity);
	    }
		public Object getDefaultValue() {
			return null;
		}
	}
New to GrepCode? Check out our FAQ X