Search in sources :

Example 1 with MappingException

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

the class IndexHelperTest method calculateBadKeys.

@Test
public void calculateBadKeys() {
    EntityModel model = getMapper().getEntityModel(IndexedClass.class);
    Index index = indexBuilder().fields(fieldBuilder().value("texting").type(IndexType.TEXT).weight(1).build(), fieldBuilder().value("nest").type(IndexType.DESC).build()).build();
    try {
        getIndexHelper().calculateKeys(model, index);
        fail("Validation should have failed on the bad key");
    } catch (MappingException e) {
    // all good
    }
    index = indexBuilder().fields(fieldBuilder().value("texting").type(IndexType.TEXT).weight(1).build(), fieldBuilder().value("nest").type(IndexType.DESC).build()).options(indexOptionsBuilder().disableValidation(true).build()).build();
    getIndexHelper().calculateKeys(model, index);
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Index(dev.morphia.annotations.Index) MappingException(dev.morphia.mapping.MappingException) Test(org.testng.annotations.Test)

Example 2 with MappingException

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

the class IndexHelperTest method createIndex.

@Test
public void createIndex() {
    String collectionName = getDs().getCollection(IndexedClass.class).getNamespace().getCollectionName();
    MongoCollection<Document> collection = getDatabase().getCollection(collectionName);
    Mapper mapper = getMapper();
    getIndexHelper().createIndex(collection, mapper.getEntityModel(IndexedClass.class));
    List<Document> indexInfo = getIndexInfo(IndexedClass.class);
    List<String> names = new ArrayList<>(asList("latitude_1", "searchme", "indexName_1"));
    for (Document document : indexInfo) {
        String name = document.get("name").toString();
        if (name.equals("latitude_1")) {
            names.remove(name);
            assertEquals(document.get("key"), parse("{ 'latitude' : 1 }"));
        } else if (name.equals("searchme")) {
            names.remove(name);
            assertEquals(document.get("weights"), parse("{ 'text' : 10 }"));
        } else if (name.equals("indexName_1")) {
            names.remove(name);
            assertEquals(document.get("key"), parse("{'indexName': 1 }"));
        } else {
            if (!"_id_".equals(document.get("name"))) {
                throw new MappingException("Found an index I wasn't expecting:  " + document);
            }
        }
    }
    assertTrue(names.isEmpty(), "Should be empty: " + names);
    collection = getDatabase().getCollection(getDs().getCollection(AbstractParent.class).getNamespace().getCollectionName());
    getIndexHelper().createIndex(collection, mapper.getEntityModel(AbstractParent.class));
    indexInfo = getIndexInfo(AbstractParent.class);
    assertTrue(indexInfo.isEmpty(), "Shouldn't find any indexes: " + indexInfo);
}
Also used : Mapper(dev.morphia.mapping.Mapper) ArrayList(java.util.ArrayList) Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException) Test(org.testng.annotations.Test)

Example 3 with MappingException

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

the class ConstructorCreator method getInstance.

@Override
public Object getInstance() {
    if (instance == null) {
        try {
            instance = constructor.newInstance(parameters);
            setFunctions.forEach(function -> function.accept(instance));
        } catch (Exception e) {
            throw new MappingException(Sofia.cannotInstantiate(model.getType().getName(), e.getMessage()), e);
        }
    }
    return instance;
}
Also used : MappingException(dev.morphia.mapping.MappingException) MappingException(dev.morphia.mapping.MappingException)

Example 4 with MappingException

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

the class IndexHelper method calculateKeys.

public Document calculateKeys(EntityModel entityModel, Index index) {
    Document keys = new Document();
    for (Field field : index.fields()) {
        String path;
        try {
            path = findField(entityModel, index.options(), field.value());
        } catch (Exception e) {
            path = field.value();
            if (!index.options().disableValidation()) {
                throw new MappingException(Sofia.invalidIndexPath(path, entityModel.getType().getName()));
            }
            LOG.warn(Sofia.invalidIndexPath(path, entityModel.getType().getName()));
        }
        keys.putAll(new Document(path, field.type().toIndexValue()));
    }
    return keys;
}
Also used : Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException) MappingException(dev.morphia.mapping.MappingException)

Example 5 with MappingException

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

the class ReferenceCodec method encodeId.

/**
 * Encodes a value
 *
 * @param mapper
 * @param value  the value to encode
 * @param model  the mapped class of the field type
 * @return the encoded value
 * @morphia.internal
 */
@Nullable
public static Object encodeId(Mapper mapper, Object value, EntityModel model) {
    Object idValue;
    Class<?> type;
    if (value instanceof Key) {
        idValue = ((Key) value).getId();
        String collectionName = ((Key<?>) value).getCollection();
        type = collectionName != null ? mapper.getClassFromCollection(collectionName) : ((Key<?>) value).getType();
        if (type == null) {
            throw new MappingException("The type for the reference could not be determined for the key " + value);
        }
    } else {
        idValue = mapper.getId(value);
        if (idValue == null) {
            return !mapper.isMappable(value.getClass()) ? value : null;
        }
        type = value.getClass();
    }
    String valueCollectionName = mapper.getEntityModel(type).getCollectionName();
    String fieldCollectionName = model.getCollectionName();
    Reference annotation = model.getAnnotation(Reference.class);
    if (annotation != null && !annotation.idOnly() || valueCollectionName != null && !valueCollectionName.equals(fieldCollectionName)) {
        idValue = new DBRef(valueCollectionName, idValue);
    }
    return idValue;
}
Also used : SetReference(dev.morphia.mapping.experimental.SetReference) ListReference(dev.morphia.mapping.experimental.ListReference) MapReference(dev.morphia.mapping.experimental.MapReference) SingleReference(dev.morphia.mapping.experimental.SingleReference) MorphiaReference(dev.morphia.mapping.experimental.MorphiaReference) Reference(dev.morphia.annotations.Reference) DBRef(com.mongodb.DBRef) Key(dev.morphia.Key) MappingException(dev.morphia.mapping.MappingException) Nullable(com.mongodb.lang.Nullable)

Aggregations

MappingException (dev.morphia.mapping.MappingException)12 Document (org.bson.Document)4 ArrayList (java.util.ArrayList)3 DBRef (com.mongodb.DBRef)2 Key (dev.morphia.Key)2 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)2 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)2 Field (java.lang.reflect.Field)2 BsonReaderMark (org.bson.BsonReaderMark)2 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)2 Test (org.testng.annotations.Test)2 NonNull (com.mongodb.lang.NonNull)1 Nullable (com.mongodb.lang.Nullable)1 Index (dev.morphia.annotations.Index)1 Reference (dev.morphia.annotations.Reference)1 Mapper (dev.morphia.mapping.Mapper)1 DocumentReader (dev.morphia.mapping.codec.reader.DocumentReader)1 ListReference (dev.morphia.mapping.experimental.ListReference)1 MapReference (dev.morphia.mapping.experimental.MapReference)1 MorphiaReference (dev.morphia.mapping.experimental.MorphiaReference)1