Search in sources :

Example 6 with MappingException

use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.

the class IndexHelper method findField.

String findField(final MappedClass mc, final IndexOptions options, final List<String> path) {
    String segment = path.get(0);
    if (segment.equals("$**")) {
        return segment;
    }
    MappedField mf = mc.getMappedField(segment);
    if (mf == null) {
        mf = mc.getMappedFieldByJavaField(segment);
    }
    if (mf == null && mc.isInterface()) {
        for (final MappedClass mappedClass : mapper.getSubTypes(mc)) {
            try {
                return findField(mappedClass, options, new ArrayList<String>(path));
            } catch (MappingException e) {
            // try the next one
            }
        }
    }
    String namePath;
    if (mf != null) {
        namePath = mf.getNameToStore();
    } else {
        if (!options.disableValidation()) {
            throw pathFail(mc, path);
        } else {
            return join(path, '.');
        }
    }
    if (path.size() > 1) {
        try {
            Class concreteType = !mf.isSingleValue() ? mf.getSubClass() : mf.getConcreteType();
            namePath += "." + findField(mapper.getMappedClass(concreteType), options, path.subList(1, path.size()));
        } catch (MappingException e) {
            if (!options.disableValidation()) {
                throw pathFail(mc, path);
            } else {
                return join(path, '.');
            }
        }
    }
    return namePath;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) MappedClass(org.mongodb.morphia.mapping.MappedClass) MappedClass(org.mongodb.morphia.mapping.MappedClass) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 7 with MappingException

use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.

the class IndexHelper method calculateKeys.

BsonDocument calculateKeys(final MappedClass mc, final Index index) {
    BsonDocument keys = new BsonDocument();
    for (Field field : index.fields()) {
        String path;
        try {
            path = findField(mc, index.options(), new ArrayList<String>(asList(field.value().split("\\."))));
        } catch (Exception e) {
            path = field.value();
            String message = format("The path '%s' can not be validated against '%s' and may represent an invalid index", path, mc.getClazz().getName());
            if (!index.options().disableValidation()) {
                throw new MappingException(message);
            }
            LOG.warning(message);
        }
        keys.putAll(toBsonDocument(path, field.type().toIndexValue()));
    }
    return keys;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Field(org.mongodb.morphia.annotations.Field) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) MappingException(org.mongodb.morphia.mapping.MappingException) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 8 with MappingException

use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.

the class Morphia method mapPackage.

/**
     * Tries to map all classes in the package specified.
     *
     * @param packageName          the name of the package to process
     * @param ignoreInvalidClasses specifies whether to ignore classes in the package that cannot be mapped
     * @return the Morphia instance
     */
public synchronized Morphia mapPackage(final String packageName, final boolean ignoreInvalidClasses) {
    try {
        for (final Class clazz : ReflectionUtils.getClasses(packageName, mapper.getOptions().isMapSubPackages())) {
            try {
                final Embedded embeddedAnn = ReflectionUtils.getClassEmbeddedAnnotation(clazz);
                final Entity entityAnn = ReflectionUtils.getClassEntityAnnotation(clazz);
                final boolean isAbstract = Modifier.isAbstract(clazz.getModifiers());
                if ((entityAnn != null || embeddedAnn != null) && !isAbstract) {
                    map(clazz);
                }
            } catch (final MappingException ex) {
                if (!ignoreInvalidClasses) {
                    throw ex;
                }
            }
        }
        return this;
    } catch (IOException e) {
        throw new MappingException("Could not get map classes from package " + packageName, e);
    } catch (ClassNotFoundException e) {
        throw new MappingException("Could not get map classes from package " + packageName, e);
    }
}
Also used : Entity(org.mongodb.morphia.annotations.Entity) Embedded(org.mongodb.morphia.annotations.Embedded) IOException(java.io.IOException) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 9 with MappingException

use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.

the class Converters method fromDBObject.

/**
     * Creates an entity and populates its state based on the dbObject given.  This method is primarily an internal method.  Reliance on
     * this method may break your application in future releases.
     *
     * @param dbObj        the object state to use
     * @param mf           the MappedField containing the metadata to use when decoding in to a field
     * @param targetEntity then entity to hold the state from the database
     */
public void fromDBObject(final DBObject dbObj, final MappedField mf, final Object targetEntity) {
    final Object object = mf.getDbObjectValue(dbObj);
    if (object != null) {
        final TypeConverter enc = getEncoder(mf);
        final Object decodedValue = enc.decode(mf.getType(), object, mf);
        try {
            mf.setFieldValue(targetEntity, decodedValue);
        } catch (IllegalArgumentException e) {
            throw new MappingException(format("Error setting value from converter (%s) for %s to %s", enc.getClass().getSimpleName(), mf.getFullName(), decodedValue), e);
        }
    }
}
Also used : DBObject(com.mongodb.DBObject) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 10 with MappingException

use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.

the class ReflectionUtils method getParameterizedClass.

/**
     * Get the class that parameterizes the Field supplied, at the index supplied (field can be parameterized with multiple param classes).
     *
     * @param field the field
     * @param index the index of the parameterizing class
     * @return the class that parameterizes the field, or null if field is not parameterized
     */
public static Class getParameterizedClass(final Field field, final int index) {
    if (field.getGenericType() instanceof ParameterizedType) {
        final ParameterizedType type = (ParameterizedType) field.getGenericType();
        if ((type.getActualTypeArguments() != null) && (type.getActualTypeArguments().length <= index)) {
            return null;
        }
        final Type paramType = type.getActualTypeArguments()[index];
        if (paramType instanceof GenericArrayType) {
            final Class arrayType = (Class) ((GenericArrayType) paramType).getGenericComponentType();
            return Array.newInstance(arrayType, 0).getClass();
        } else {
            if (paramType instanceof ParameterizedType) {
                final ParameterizedType paramPType = (ParameterizedType) paramType;
                return (Class) paramPType.getRawType();
            } else {
                if (paramType instanceof TypeVariable) {
                    // from the T/V/X
                    throw new MappingException("Generic Typed Class not supported:  <" + ((TypeVariable) paramType).getName() + "> = " + ((TypeVariable) paramType).getBounds()[0]);
                } else if (paramType instanceof Class) {
                    return (Class) paramType;
                } else {
                    throw new MappingException("Unknown type... pretty bad... call for help, wave your hands... yeah!");
                }
            }
        }
    }
    return getParameterizedClass(field.getType());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType) MappingException(org.mongodb.morphia.mapping.MappingException)

Aggregations

MappingException (org.mongodb.morphia.mapping.MappingException)16 DBObject (com.mongodb.DBObject)9 BasicDBObject (com.mongodb.BasicDBObject)8 MappedClass (org.mongodb.morphia.mapping.MappedClass)7 Test (org.junit.Test)3 MappedField (org.mongodb.morphia.mapping.MappedField)3 WriteResult (com.mongodb.WriteResult)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 TypeVariable (java.lang.reflect.TypeVariable)2 WildcardType (java.lang.reflect.WildcardType)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 BsonDocument (org.bson.BsonDocument)2 NotSaved (org.mongodb.morphia.annotations.NotSaved)2 UpdateException (org.mongodb.morphia.query.UpdateException)2 DBCollection (com.mongodb.DBCollection)1 IOException (java.io.IOException)1 BsonString (org.bson.BsonString)1