Search in sources :

Example 1 with ProxiedEntityReferenceMap

use of org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceMap in project morphia by mongodb.

the class ReferenceMapper method writeMap.

private void writeMap(final MappedField mf, final DBObject dbObject, final String name, final Object fieldValue, final Reference refAnn, final Mapper mapper) {
    final Map<Object, Object> map = (Map<Object, Object>) fieldValue;
    if ((map != null)) {
        final Map values = mapper.getOptions().getObjectFactory().createMap(mf);
        if (ProxyHelper.isProxy(map) && ProxyHelper.isUnFetched(map)) {
            final ProxiedEntityReferenceMap proxy = (ProxiedEntityReferenceMap) map;
            final Map<Object, Key<?>> refMap = proxy.__getReferenceMap();
            for (final Map.Entry<Object, Key<?>> entry : refMap.entrySet()) {
                final Object key = entry.getKey();
                values.put(key, refAnn.idOnly() ? mapper.keyToId(entry.getValue()) : mapper.keyToDBRef(entry.getValue()));
            }
        } else {
            for (final Map.Entry<Object, Object> entry : map.entrySet()) {
                final String strKey = mapper.getConverters().encode(entry.getKey()).toString();
                values.put(strKey, refAnn.idOnly() ? mapper.keyToId(getKey(entry.getValue(), mapper)) : mapper.keyToDBRef(getKey(entry.getValue(), mapper)));
            }
        }
        if (!values.isEmpty() || mapper.getOptions().isStoreEmpties()) {
            dbObject.put(name, values);
        }
    }
}
Also used : ProxiedEntityReferenceMap(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceMap) DBObject(com.mongodb.DBObject) ProxiedEntityReferenceMap(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceMap) Map(java.util.Map) Key(org.mongodb.morphia.Key)

Example 2 with ProxiedEntityReferenceMap

use of org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceMap 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)

Aggregations

DBObject (com.mongodb.DBObject)2 Map (java.util.Map)2 ProxiedEntityReferenceMap (org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceMap)2 DBRef (com.mongodb.DBRef)1 Key (org.mongodb.morphia.Key)1