use of dev.morphia.Key in project morphia by mongodb.
the class ReferenceCodec method encodeId.
/**
* Encodes a value
*
* @param value the value to encode
* @param model the mapped class of the field type
* @return the encoded value
* @morphia.internal
*/
private Object encodeId(Object value, PropertyModel model) {
Object idValue;
final String valueCollectionName;
if (value instanceof Key) {
idValue = ((Key<?>) value).getId();
String collectionName = ((Key<?>) value).getCollection();
Class<?> type = ((Key<?>) value).getType();
if (collectionName == null || type == null) {
throw new QueryException("Missing type or collection information in key");
}
valueCollectionName = collectionName;
} else {
idValue = mapper.getId(value);
if (idValue == null) {
if (!mapper.isMappable(value.getClass())) {
return value;
}
throw new QueryException("No ID value found on referenced entity. Save referenced entities before defining references to" + " them.");
}
valueCollectionName = mapper.getEntityModel(value.getClass()).getCollectionName();
}
Reference annotation = model.getAnnotation(Reference.class);
if (annotation != null && !annotation.idOnly()) {
idValue = new DBRef(valueCollectionName, idValue);
}
return idValue;
}
use of dev.morphia.Key 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;
}
use of dev.morphia.Key in project morphia by mongodb.
the class TestLegacyQuery method testKeyList.
@Test
public void testKeyList() {
final Rectangle rect = new Rectangle(1000, 1);
Rectangle rectangle = getDs().save(rect);
assertEquals(rectangle.getId(), rect.getId());
final FacebookUser fbUser1 = new FacebookUser(1, "scott");
final FacebookUser fbUser2 = new FacebookUser(2, "tom");
final FacebookUser fbUser3 = new FacebookUser(3, "oli");
final FacebookUser fbUser4 = new FacebookUser(4, "frank");
final List<FacebookUser> users = getDs().save(asList(fbUser1, fbUser2, fbUser3, fbUser4));
assertEquals(fbUser1.getId(), 1);
final List<Key<FacebookUser>> fbUserKeys = new ArrayList<>();
for (FacebookUser user : users) {
fbUserKeys.add(getMapper().getKey(user));
}
assertEquals(fbUser1.getId(), fbUserKeys.get(0).getId());
assertEquals(fbUser2.getId(), fbUserKeys.get(1).getId());
assertEquals(fbUser3.getId(), fbUserKeys.get(2).getId());
assertEquals(fbUser4.getId(), fbUserKeys.get(3).getId());
final Keys k1 = new Keys(getMapper().getKey(rectangle), fbUserKeys);
final Keys keys = getDs().save(k1);
assertEquals(k1.getId(), keys.getId());
final Datastore datastore = getDs();
final Keys k1Loaded = datastore.find(Keys.class).filter("_id", k1.getId()).first();
for (Key<FacebookUser> key : k1Loaded.getUsers()) {
assertNotNull(key.getId());
}
assertNotNull(k1Loaded.getRect().getId());
}
use of dev.morphia.Key in project morphia by mongodb.
the class KeyCodec method decode.
@Override
public Key decode(BsonReader reader, DecoderContext decoderContext) {
reader.readStartDocument();
final String ref = reader.readString("$ref");
reader.readName();
final BsonReaderMark mark = reader.getMark();
Object idValue = null;
EntityModel model = null;
final Iterator<EntityModel> iterator = datastore.getMapper().getClassesMappedToCollection(ref).iterator();
while (idValue == null && iterator.hasNext()) {
model = iterator.next();
try {
final PropertyModel idField = model.getIdProperty();
if (idField != null) {
final Class<?> idType = idField.getType();
idValue = datastore.getCodecRegistry().get(idType).decode(reader, decoderContext);
}
} catch (Exception e) {
mark.reset();
}
}
if (idValue == null) {
throw new MappingException("Could not map the Key to a type.");
}
reader.readEndDocument();
return new Key<>(model.getType(), ref, idValue);
}
use of dev.morphia.Key in project morphia by mongodb.
the class TestLegacyQuery method testKeys.
@Test
public void testKeys() {
PhotoWithKeywords pwk1 = new PhotoWithKeywords("california", "nevada", "arizona");
PhotoWithKeywords pwk2 = new PhotoWithKeywords("Joe", "Sarah");
PhotoWithKeywords pwk3 = new PhotoWithKeywords("MongoDB", "World");
getDs().save(asList(pwk1, pwk2, pwk3));
MongoCursor<Key<PhotoWithKeywords>> keys = getDs().find(PhotoWithKeywords.class).keys();
assertTrue(keys.hasNext());
assertEquals(pwk1.id, keys.next().getId());
assertEquals(pwk2.id, keys.next().getId());
assertEquals(pwk3.id, keys.next().getId());
List<Complex> list = asList(new Complex(new ChildId("Turk", 27), "Turk"), new Complex(new ChildId("JD", 26), "Dorian"), new Complex(new ChildId("Carla", 29), "Espinosa"));
getDs().save(list);
Iterator<Key<Complex>> complexKeys = getDs().find(Complex.class).keys();
assertTrue(complexKeys.hasNext());
assertEquals(list.get(0).getId(), complexKeys.next().getId());
assertEquals(list.get(1).getId(), complexKeys.next().getId());
assertEquals(list.get(2).getId(), complexKeys.next().getId());
assertFalse(complexKeys.hasNext());
}
Aggregations