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);
}
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);
}
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;
}
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;
}
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;
}
Aggregations