use of org.mongodb.morphia.Key in project morphia by mongodb.
the class KeyConverter method decode.
@Override
public Object decode(final Class targetClass, final Object o, final MappedField optionalExtraInfo) {
if (o == null) {
return null;
}
if (!(o instanceof DBRef)) {
throw new ConverterException(String.format("cannot convert %s to Key because it isn't a DBRef", o.toString()));
}
DBRef ref = (DBRef) o;
MappedField actualType = getActualType(optionalExtraInfo);
final Class<?> keyType = actualType != null ? actualType.getConcreteType() : getMapper().getClassFromCollection(ref.getCollectionName());
final Key<?> key = new Key<Object>(keyType, ref.getCollectionName(), ref.getId());
return key;
}
use of org.mongodb.morphia.Key in project morphia by mongodb.
the class MorphiaKeyIterator method convertItem.
@Override
@SuppressWarnings("unchecked")
protected Key<T> convertItem(final DBObject dbObj) {
Object id = dbObj.get(Mapper.ID_KEY);
if (id instanceof DBObject) {
Class type = getMapper().getMappedClass(getClazz()).getMappedIdField().getType();
id = getMapper().fromDBObject(getDatastore(), type, (DBObject) id, getMapper().createEntityCache());
}
return new Key<T>(getClazz(), getCollection(), id);
}
use of org.mongodb.morphia.Key in project morphia by mongodb.
the class QueryValidatorTest method shouldNotAllowTypeThatDoesNotMatchKeyTypeValue.
@Test
public void shouldNotAllowTypeThatDoesNotMatchKeyTypeValue() {
// expect
MappedClass mappedClass = new MappedClass(SimpleEntity.class, new Mapper());
MappedField mappedField = mappedClass.getMappedField("name");
assertThat(QueryValidator.isCompatibleForOperator(mappedClass, mappedField, String.class, EQUAL, new Key<Number>(Integer.class, "Integer", new ObjectId()), new ArrayList<ValidationFailure>()), is(false));
}
use of org.mongodb.morphia.Key in project morphia by mongodb.
the class TestQuery method testGetByKeysHetero.
@Test
public void testGetByKeysHetero() {
final Iterable<Key<Object>> keys = getDs().save(asList(new FacebookUser(1, "scott"), new Rectangle(1, 1)));
final List<Object> entities = getDs().getByKeys(keys);
assertNotNull(entities);
assertEquals(2, entities.size());
int userCount = 0;
int rectCount = 0;
for (final Object o : entities) {
if (o instanceof Rectangle) {
rectCount++;
} else if (o instanceof FacebookUser) {
userCount++;
}
}
assertEquals(1, rectCount);
assertEquals(1, userCount);
}
use of org.mongodb.morphia.Key in project morphia by mongodb.
the class QueryForSubtypeTest method testSubclassOfKeyShouldBeCompatibleWithFieldUser.
@Test
public void testSubclassOfKeyShouldBeCompatibleWithFieldUser() {
MappedField user = jobMappedClass.getMappedField("owner");
Key<User> anonymousKeySubclass = new Key<User>(User.class, "User", 212L) {
};
boolean compatible = isCompatibleForOperator(jobMappedClass, user, User.class, EQUAL, anonymousKeySubclass, new ArrayList<ValidationFailure>());
assertThat(compatible, is(true));
}
Aggregations