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.support.mapping;
 
 
 import java.util.*;

Neo4J specific MappingContext implementation. Simply creates Neo4jPersistentEntityImpl and org.springframework.data.neo4j.mapping.Neo4jPersistentProperty instances.

Author(s):
Oliver Gierke
 
 
     private final static Logger log = LoggerFactory.getLogger(Neo4jMappingContext.class);
 
     private final Map<AnnotationBooleanreferenceAnnotations = new IdentityHashMap<>();
 
     protected <T> Neo4jPersistentEntityImpl<?> createPersistentEntity(TypeInformation<T> typeInformation) {
         final Class<T> type = typeInformation.getType();
         if (type.isAnnotationPresent(NodeEntity.class)) {
             return new Neo4jPersistentEntityImpl<T>(typeInformation,);
         }
         if (type.isAnnotationPresent(RelationshipEntity.class)) {
             return new Neo4jPersistentEntityImpl<T>(typeInformation,);
         }
         throw new InvalidEntityTypeException("Type " + type + " is neither a @NodeEntity nor a @RelationshipEntity");
     }
 
     @Override
     protected Neo4jPersistentEntityImpl<?> addPersistentEntity(TypeInformation<?> typeInformation) {
         final Neo4jPersistentEntityImpl<?> entity = super.addPersistentEntity(typeInformation);
         Collection<Neo4jPersistentEntity<?>> superTypeEntities = addSuperTypes(entity);
         updateStoredEntityType(entitysuperTypeEntities);
         return entity;
     }
 
     private void updateStoredEntityType(Neo4jPersistentEntityImpl<?> entityCollection<Neo4jPersistentEntity<?>> superTypeEntities) {
         entity.updateStoredType(superTypeEntities);
         if (!=null) {
             .ensureEntityIndexes(entity);
 //            if (superTypeEntities!=null) {
 //                for (Neo4jPersistentEntity<?> superTypeEntity : superTypeEntities) {
 //                    entityIndexCreator.ensureEntityIndexes(superTypeEntity);
 //                }
 //            }
         }
     }
 
     private List<Neo4jPersistentEntity<?>> addSuperTypes(Neo4jPersistentEntity<?> entity) {
         List<Neo4jPersistentEntity<?>> entities=new ArrayList<>();
         Class<?> type = entity.getType();
         Collection<ClasstypesToAdd = new LinkedHashSet<>();
         while (type != null) {
             typesToAdd.add(type.getSuperclass());
             typesToAdd.addAll(Arrays.asList(type.getInterfaces()));
             type = type.getSuperclass();
         }
         for (Class<?> superType : typesToAdd) {
             entities.addAll(addPersistentEntityWithCheck(superType));
         }
         return entities;
    }
        if (type != null && (isNodeEntityType(type) || isRelationshipEntityType(type))) {
            return Collections.<Neo4jPersistentEntity<?>>singletonList(addPersistentEntity(type));
        }
        return Collections.emptyList();
    }
    @Override
    public void initialize() {
        super.initialize();
        setStrict(true);
    }
    private boolean isRelationshipEntityType(Class<?> type) {
        return type.isAnnotationPresent(RelationshipEntity.class);
    }
    private boolean isNodeEntityType(Class<?> type) {
        return type.isAnnotationPresent(NodeEntity.class);
    }
    // todo performance, change to direct lookups if too slow
    public Neo4jPersistentEntity<?> getPersistentEntity(Object alias) {
        for (Neo4jPersistentEntityImpl<?> entity : getPersistentEntities()) {
            if (entity.matchesAlias(alias)) return entity;
        }
        return tryToResolveAliasAsEntityClassName(alias);
    }
        if (alias instanceof Class) {
            try {
                return getPersistentEntity((Class)alias);
            } catch(MappingException me) {
                // ignores
            }
        }
        if (alias instanceof String && alias.toString().contains(".")) {
            try {
                return tryToResolveAliasAsEntityClassName(Class.forName(alias.toString()));
            } catch (ClassNotFoundException cnfe) {
                // ignore
            }
        }
        if (alias instanceof StoredEntityType) {
            return ((StoredEntityType)alias).getEntity();
        }
        return null;
    }
    public StoredEntityType getStoredEntityType(Class type) {
        if (type==nullreturn null;
        Neo4jPersistentEntityImpl<?> persistentEntity = getPersistentEntity(type);
        return persistentEntity==null ? null : persistentEntity.getEntityType();
    }
    @Override
                                                               Neo4jPersistentEntityImpl<?> ownerSimpleTypeHolder simpleTypeHolder) {
        return new Neo4jPersistentPropertyImpl(fielddescriptorownersimpleTypeHolder,this);
    }
    private final Map<Class<?>,Class<?>> annotationCheckCache = new IdentityHashMap<Class<?>, Class<?>>();
    public boolean isNodeEntity(Class<?> type) {
        if (Node.class.isAssignableFrom(type)) return true;
        if (!.containsKey(type)) cacheType(type);
        return checkAnnotationType(type,NodeEntity.class);
    }
    private void cacheType(Class<?> type) {
        try {
            final Neo4jPersistentEntityImpl<?> entity = getPersistentEntity(type);
            if (entity == null.put(typetype);
            else if (!shouldCreatePersistentEntityFor(ClassTypeInformation.from(type))) .put(typetype);
            else if (entity.isNodeEntity()) .put(typeNodeEntity.class);
            else if (entity.isRelationshipEntity()) .put(typeRelationshipEntity.class);
            else .put(typetype);
        } catch (InvalidEntityTypeException me) {
            .put(typetype);
        }
    }
    @Override
    protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> typeInformation) {
        boolean result = super.shouldCreatePersistentEntityFor(typeInformation);
        if (!resultreturn result;
        if (typeInformation.isCollectionLike()) return false;
        Class<?> type = typeInformation.getType();
        if (type.isEnum() || type.isArray() || type.equals(Node.class) || type.equals(Relationship.class) || type.equals(ValueContext.class)) {
            return false;
        }
        String packageName = type.getPackage().getName();
        if (packageName.startsWith("java")) {
            return false;
        }
        return true;
    }
    private boolean checkAnnotationType(Class<?> typeClass<?> annotation) {
        final Class<?> marker = .get(type);
        return marker==annotation;
    }
    public boolean isRelationshipEntity(Class<?> type) {
        if (!.containsKey(type)) cacheType(type);
        return checkAnnotationType(type,RelationshipEntity.class);
    }
    public void setPersistentState(Object entityPropertyContainer pc) {
        final Neo4jPersistentEntityImpl<?> persistentEntity = getPersistentEntity(entity.getClass());
        persistentEntity.setPersistentState(entitypc);
    }
    private EntityAlias entityAlias=new EntityAlias();
    public EntityAlias getEntityAlias() {
        return ;
    }
    public void setEntityAlias(EntityAlias entityAlias) {
        this. = entityAlias;
    }
    
    public boolean isReference(Neo4jPersistentProperty property) {
        for (Annotation annotation : property.getAnnotations()) {
            Boolean isReference = .get(annotation);
            if (isReference == null) {
                isReference = Reference.class.isInstance(annotation) || annotation.annotationType().isAnnotationPresent(Reference.class);
                .put(annotationisReference);
            } 
            if (isReferencereturn true;
        }
        return false;
    }
    public void setEntityIndexCreator(EntityIndexCreator entityIndexCreator) {
        this. = entityIndexCreator;
    }
New to GrepCode? Check out our FAQ X