Search in sources :

Example 11 with MappingException

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

the class IndexHelperTest method findField.

@Test
public void findField() {
    MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
    assertEquals("indexName", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), singletonList("indexName")));
    assertEquals("nest.name", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), asList("nested", "name")));
    assertEquals("nest.name", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), asList("nest", "name")));
    try {
        assertEquals("nest.whatsit", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), asList("nest", "whatsit")));
        fail("Should have failed on the bad index path");
    } catch (MappingException e) {
    // alles ist gut
    }
    assertEquals("nest.whatsit.nested.more.deeply.than.the.object.model", indexHelper.findField(mappedClass, new IndexOptionsBuilder().disableValidation(true), asList("nest", "whatsit", "nested", "more", "deeply", "than", "the", "object", "model")));
}
Also used : MappedClass(org.mongodb.morphia.mapping.MappedClass) MappingException(org.mongodb.morphia.mapping.MappingException) Test(org.junit.Test)

Example 12 with MappingException

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

the class ReflectionUtils method getParameterizedType.

/**
     * Returns the parameterized type for a field
     *
     * @param field the field to examine
     * @param index the location of the parameter to return
     * @return the type
     */
public static Type getParameterizedType(final Field field, final int index) {
    if (field != null) {
        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) {
                //((GenericArrayType) paramType).getGenericComponentType();
                return paramType;
            } else {
                if (paramType instanceof ParameterizedType) {
                    return paramType;
                } else {
                    if (paramType instanceof TypeVariable) {
                        // paramType).getName() + "> = " + ((TypeVariable) paramType).getBounds()[0]);
                        return paramType;
                    } else if (paramType instanceof WildcardType) {
                        return paramType;
                    } else if (paramType instanceof Class) {
                        return paramType;
                    } else {
                        throw new MappingException("Unknown type... pretty bad... call for help, wave your hands... yeah!");
                    }
                }
            }
        }
        // Not defined on field, but may be on class or super class...
        return getParameterizedClass(field.getType());
    }
    return null;
}
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) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 13 with MappingException

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

the class IndexHelperTest method createIndex.

@Test
public void createIndex() {
    checkMinServerVersion(3.4);
    String collectionName = getDs().getCollection(IndexedClass.class).getName();
    MongoCollection<Document> collection = getDatabase().getCollection(collectionName);
    Mapper mapper = getMorphia().getMapper();
    indexHelper.createIndex(collection, mapper.getMappedClass(IndexedClass.class), false);
    List<DBObject> indexInfo = getDs().getCollection(IndexedClass.class).getIndexInfo();
    assertEquals("Should have 6 indexes", 6, indexInfo.size());
    for (DBObject dbObject : indexInfo) {
        String name = dbObject.get("name").toString();
        if (name.equals("latitude_1")) {
            assertEquals(parse("{ 'latitude' : 1 }"), dbObject.get("key"));
        } else if (name.equals("behind_interface")) {
            assertEquals(parse("{ 'nest.name' : -1} "), dbObject.get("key"));
            assertEquals(parse("{ 'locale' : 'en' , 'caseLevel' : false , 'caseFirst' : 'off' , 'strength' : 2 , 'numericOrdering' :" + " false , 'alternate' : 'non-ignorable' , 'maxVariable' : 'punct' , 'normalization' : false , " + "'backwards' : false , 'version' : '57.1'}"), dbObject.get("collation"));
        } else if (name.equals("nest.name_1")) {
            assertEquals(parse("{ 'nest.name' : 1} "), dbObject.get("key"));
        } else if (name.equals("searchme")) {
            assertEquals(parse("{ 'text' : 10 }"), dbObject.get("weights"));
        } else if (name.equals("indexName_1")) {
            assertEquals(parse("{'indexName': 1 }"), dbObject.get("key"));
        } else {
            if (!"_id_".equals(dbObject.get("name"))) {
                throw new MappingException("Found an index I wasn't expecting:  " + dbObject);
            }
        }
    }
    collection = getDatabase().getCollection(getDs().getCollection(AbstractParent.class).getName());
    indexHelper.createIndex(collection, mapper.getMappedClass(AbstractParent.class), false);
    indexInfo = getDs().getCollection(AbstractParent.class).getIndexInfo();
    assertTrue("Shouldn't find any indexes: " + indexInfo, indexInfo.isEmpty());
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.mongodb.morphia.mapping.MappingException) Test(org.junit.Test)

Example 14 with MappingException

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

the class IndexHelperTest method calculateBadKeys.

@Test
public void calculateBadKeys() {
    MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
    IndexBuilder index = new IndexBuilder().fields(new FieldBuilder().value("texting").type(IndexType.TEXT).weight(1), new FieldBuilder().value("nest").type(IndexType.DESC));
    try {
        indexHelper.calculateKeys(mappedClass, index);
        fail("Validation should have failed on the bad key");
    } catch (MappingException e) {
    // all good
    }
    index.options(new IndexOptionsBuilder().disableValidation(true));
    indexHelper.calculateKeys(mappedClass, index);
}
Also used : MappedClass(org.mongodb.morphia.mapping.MappedClass) MappingException(org.mongodb.morphia.mapping.MappingException) Test(org.junit.Test)

Example 15 with MappingException

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

the class DatastoreImpl method toDbObject.

private <T> DBObject toDbObject(final T ent, final Map<Object, DBObject> involvedObjects) {
    final MappedClass mc = mapper.getMappedClass(ent);
    if (mc.getAnnotation(NotSaved.class) != null) {
        throw new MappingException(format("Entity type: %s is marked as NotSaved which means you should not try to save it!", mc.getClazz().getName()));
    }
    DBObject dbObject = entityToDBObj(ent, involvedObjects);
    List<MappedField> versionFields = mc.getFieldsAnnotatedWith(Version.class);
    for (MappedField mappedField : versionFields) {
        String name = mappedField.getNameToStore();
        if (dbObject.get(name) == null) {
            dbObject.put(name, 1);
            mappedField.setFieldValue(ent, 1L);
        }
    }
    return dbObject;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) NotSaved(org.mongodb.morphia.annotations.NotSaved) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) 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