Search in sources :

Example 11 with DBRef

use of com.mongodb.DBRef in project morphia by mongodb.

the class ReferenceMapper method readMap.

private void readMap(final Datastore datastore, final Mapper mapper, final Object entity, final Reference refAnn, final EntityCache cache, final MappedField mf, final DBObject dbObject) {
    final Class referenceObjClass = mf.getSubClass();
    Map m = mapper.getOptions().getObjectFactory().createMap(mf);
    final DBObject dbVal = (DBObject) mf.getDbObjectValue(dbObject);
    if (dbVal != null) {
        if (refAnn.lazy() && LazyFeatureDependencies.assertDependencyFullFilled()) {
            // replace map by proxy to it.
            m = mapper.getProxyFactory().createMapProxy(datastore, m, referenceObjClass, refAnn.ignoreMissing());
        }
        final Map map = m;
        new IterHelper<Object, Object>().loopMap(dbVal, new MapIterCallback<Object, Object>() {

            @Override
            public void eval(final Object k, final Object val) {
                final Object objKey = mapper.getConverters().decode(mf.getMapKeyClass(), k, mf);
                if (refAnn.lazy() && LazyFeatureDependencies.assertDependencyFullFilled()) {
                    final ProxiedEntityReferenceMap proxiedMap = (ProxiedEntityReferenceMap) map;
                    proxiedMap.__put(objKey, refAnn.idOnly() ? mapper.manualRefToKey(referenceObjClass, val) : mapper.refToKey((DBRef) val));
                } else {
                    map.put(objKey, resolveObject(datastore, mapper, cache, mf, refAnn.idOnly(), val));
                }
            }
        });
    }
    mf.setFieldValue(entity, m);
}
Also used : ProxiedEntityReferenceMap(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceMap) DBRef(com.mongodb.DBRef) DBObject(com.mongodb.DBObject) ProxiedEntityReferenceMap(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceMap) Map(java.util.Map) DBObject(com.mongodb.DBObject)

Example 12 with DBRef

use of com.mongodb.DBRef in project morphia by mongodb.

the class Mapper method getKeysByRefs.

/**
     * Gets the Keys for a list of objects
     *
     * @param refs the objects to process
     * @param <T>  the type of the objects
     * @return the list of Keys
     */
public <T> List<Key<T>> getKeysByRefs(final List<DBRef> refs) {
    final List<Key<T>> keys = new ArrayList<Key<T>>(refs.size());
    for (final DBRef ref : refs) {
        final Key<T> testKey = refToKey(ref);
        keys.add(testKey);
    }
    return keys;
}
Also used : ArrayList(java.util.ArrayList) DBRef(com.mongodb.DBRef) Key(org.mongodb.morphia.Key)

Example 13 with DBRef

use of com.mongodb.DBRef in project morphia by mongodb.

the class ReferenceMapper method createOrReuseProxy.

private Object createOrReuseProxy(final Datastore datastore, final Mapper mapper, final Class referenceObjClass, final Object ref, final EntityCache cache, final Reference anntotation) {
    final Key key = anntotation.idOnly() ? mapper.manualRefToKey(referenceObjClass, ref) : mapper.refToKey((DBRef) ref);
    final Object proxyAlreadyCreated = cache.getProxy(key);
    if (proxyAlreadyCreated != null) {
        return proxyAlreadyCreated;
    }
    final Object newProxy = mapper.getProxyFactory().createProxy(datastore, referenceObjClass, key, anntotation.ignoreMissing());
    cache.putProxy(key, newProxy);
    return newProxy;
}
Also used : DBRef(com.mongodb.DBRef) DBObject(com.mongodb.DBObject) Key(org.mongodb.morphia.Key)

Example 14 with DBRef

use of com.mongodb.DBRef 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;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) DBRef(com.mongodb.DBRef) Key(org.mongodb.morphia.Key)

Example 15 with DBRef

use of com.mongodb.DBRef in project mongo-java-driver by mongodb.

the class MongoCollectionTest method testDBRefEncodingAndDecoding.

// This is really a test that the default registry created in MongoClient and passed down to MongoCollection has been constructed
// properly to handle DBRef encoding and decoding
@Test
public void testDBRefEncodingAndDecoding() {
    // given
    Document doc = new Document("_id", 1).append("ref", new DBRef("foo", 5)).append("refWithDB", new DBRef("db", "foo", 5));
    // when
    collection.insertOne(doc);
    // then
    assertEquals(doc, collection.find().first());
}
Also used : DBRef(com.mongodb.DBRef) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

DBRef (com.mongodb.DBRef)18 ObjectId (org.bson.types.ObjectId)7 BasicDBObject (com.mongodb.BasicDBObject)5 DBObject (com.mongodb.DBObject)5 BasicBSONObject (org.bson.BasicBSONObject)5 Binary (org.bson.types.Binary)5 Date (java.util.Date)4 Map (java.util.Map)4 BSONObject (org.bson.BSONObject)4 MaxKey (org.bson.types.MaxKey)4 MinKey (org.bson.types.MinKey)4 Test (org.junit.Test)4 Key (org.mongodb.morphia.Key)4 DBCollection (com.mongodb.DBCollection)3 UUID (java.util.UUID)3 BSONTimestamp (org.bson.types.BSONTimestamp)3 Code (org.bson.types.Code)3 CodeWScope (org.bson.types.CodeWScope)3 Mongo (com.mongodb.Mongo)2 SimpleDateFormat (java.text.SimpleDateFormat)2