Search in sources :

Example 1 with Key

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;
}
Also used : QueryException(dev.morphia.query.QueryException) 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)

Example 2 with Key

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

Example 3 with Key

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());
}
Also used : Datastore(dev.morphia.Datastore) Rectangle(dev.morphia.test.models.Rectangle) ArrayList(java.util.ArrayList) Key(dev.morphia.Key) Test(org.testng.annotations.Test)

Example 4 with Key

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);
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) MappingException(dev.morphia.mapping.MappingException) Key(dev.morphia.Key) MappingException(dev.morphia.mapping.MappingException)

Example 5 with Key

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());
}
Also used : ChildId(dev.morphia.test.mapping.experimental.TestReferences.ChildId) Key(dev.morphia.Key) Complex(dev.morphia.test.mapping.experimental.TestReferences.Complex) Test(org.testng.annotations.Test)

Aggregations

Key (dev.morphia.Key)8 Test (org.testng.annotations.Test)5 DBRef (com.mongodb.DBRef)2 Datastore (dev.morphia.Datastore)2 Reference (dev.morphia.annotations.Reference)2 MappingException (dev.morphia.mapping.MappingException)2 ListReference (dev.morphia.mapping.experimental.ListReference)2 MapReference (dev.morphia.mapping.experimental.MapReference)2 MorphiaReference (dev.morphia.mapping.experimental.MorphiaReference)2 SetReference (dev.morphia.mapping.experimental.SetReference)2 SingleReference (dev.morphia.mapping.experimental.SingleReference)2 Rectangle (dev.morphia.test.models.Rectangle)2 ArrayList (java.util.ArrayList)2 Nullable (com.mongodb.lang.Nullable)1 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)1 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)1 FindOptions (dev.morphia.query.FindOptions)1 QueryException (dev.morphia.query.QueryException)1 ChildId (dev.morphia.test.mapping.experimental.TestReferences.ChildId)1 Complex (dev.morphia.test.mapping.experimental.TestReferences.Complex)1