Search in sources :

Example 1 with MetadataException

use of org.neo4j.ogm.exception.core.MetadataException in project neo4j-ogm by neo4j.

the class ClassInfo method getCompositeIndexes.

public Collection<CompositeIndex> getCompositeIndexes() {
    Collection<CompositeIndex> result = this.compositeIndexes;
    if (result == null) {
        synchronized (this) {
            result = this.compositeIndexes;
            if (result == null) {
                CompositeIndex[] annotations = cls.getDeclaredAnnotationsByType(CompositeIndex.class);
                List<CompositeIndex> intermediateResult = new ArrayList<>(annotations.length);
                for (CompositeIndex annotation : annotations) {
                    String[] properties = annotation.value().length > 0 ? annotation.value() : annotation.properties();
                    if (properties.length < 1) {
                        throw new MetadataException("Incorrect CompositeIndex definition on " + className + ". Provide at least 1 property");
                    }
                    for (String property : properties) {
                        // Determine the original field in case the user uses a MapCompositeConverter.
                        Matcher m = AutoIndexManager.COMPOSITE_KEY_MAP_COMPOSITE_PATTERN.matcher(property);
                        if (m.matches()) {
                            property = m.group(1);
                        }
                        FieldInfo fieldInfo = propertyField(property);
                        if (fieldInfo == null) {
                            throw new MetadataException("Incorrect CompositeIndex definition on " + className + ". Property " + property + " does not exists.");
                        }
                    }
                    intermediateResult.add(annotation);
                }
                this.compositeIndexes = Collections.unmodifiableList(intermediateResult);
                result = this.compositeIndexes;
            }
        }
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) MetadataException(org.neo4j.ogm.exception.core.MetadataException)

Example 2 with MetadataException

use of org.neo4j.ogm.exception.core.MetadataException in project neo4j-ogm by neo4j.

the class ClassInfo method getOrComputeIdentityField.

private Optional<FieldInfo> getOrComputeIdentityField() {
    Optional<FieldInfo> result = this.identityField;
    if (result == null) {
        synchronized (this) {
            result = this.identityField;
            if (result == null) {
                // Didn't want to add yet another method related to determining the identy field
                // so the actual resolving of the field inside the Double-checked locking here
                // has been inlined.
                Collection<FieldInfo> identityFields = getFieldInfos(FieldInfo::isInternalIdentity);
                if (identityFields.size() == 1) {
                    this.identityField = Optional.of(identityFields.iterator().next());
                } else if (identityFields.size() > 1) {
                    throw new MetadataException("Expected exactly one internal identity field (@Id with " + "InternalIdStrategy), found " + identityFields.size() + " " + identityFields);
                } else {
                    this.identityField = fieldsInfo.fields().stream().filter(f -> "id".equals(f.getName())).filter(f -> "java.lang.Long".equals(f.getTypeDescriptor())).findFirst();
                }
                result = this.identityField;
            }
        }
    }
    return result;
}
Also used : org.neo4j.ogm.annotation(org.neo4j.ogm.annotation) MetadataException(org.neo4j.ogm.exception.core.MetadataException) java.util(java.util) Logger(org.slf4j.Logger) IdStrategy(org.neo4j.ogm.id.IdStrategy) Predicate(java.util.function.Predicate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AutoIndexManager(org.neo4j.ogm.autoindex.AutoIndexManager) LoggerFactory(org.slf4j.LoggerFactory) InternalIdStrategy(org.neo4j.ogm.id.InternalIdStrategy) InvalidPropertyFieldException(org.neo4j.ogm.exception.core.InvalidPropertyFieldException) Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Direction(org.neo4j.ogm.annotation.Relationship.Direction) Collectors(java.util.stream.Collectors) Function(java.util.function.Function) InvocationTargetException(java.lang.reflect.InvocationTargetException) Matcher(java.util.regex.Matcher) TypeSystem(org.neo4j.ogm.driver.TypeSystem) Modifier(java.lang.reflect.Modifier) UuidStrategy(org.neo4j.ogm.id.UuidStrategy) MappingException(org.neo4j.ogm.exception.core.MappingException) AccessController(java.security.AccessController) MetadataException(org.neo4j.ogm.exception.core.MetadataException)

Aggregations

Matcher (java.util.regex.Matcher)2 MetadataException (org.neo4j.ogm.exception.core.MetadataException)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Modifier (java.lang.reflect.Modifier)1 AccessController (java.security.AccessController)1 PrivilegedAction (java.security.PrivilegedAction)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 org.neo4j.ogm.annotation (org.neo4j.ogm.annotation)1 Direction (org.neo4j.ogm.annotation.Relationship.Direction)1 AutoIndexManager (org.neo4j.ogm.autoindex.AutoIndexManager)1 TypeSystem (org.neo4j.ogm.driver.TypeSystem)1 InvalidPropertyFieldException (org.neo4j.ogm.exception.core.InvalidPropertyFieldException)1 MappingException (org.neo4j.ogm.exception.core.MappingException)1 IdStrategy (org.neo4j.ogm.id.IdStrategy)1 InternalIdStrategy (org.neo4j.ogm.id.InternalIdStrategy)1