Search in sources :

Example 1 with ProxiedEntityReferenceList

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

the class ReferenceMapper method writeCollection.

private void writeCollection(final MappedField mf, final DBObject dbObject, final String name, final Object fieldValue, final Reference refAnn, final Mapper mapper) {
    if (fieldValue != null) {
        final List values = new ArrayList();
        if (ProxyHelper.isProxy(fieldValue) && ProxyHelper.isUnFetched(fieldValue)) {
            final ProxiedEntityReferenceList p = (ProxiedEntityReferenceList) fieldValue;
            final List<Key<?>> getKeysAsList = p.__getKeysAsList();
            for (final Key<?> key : getKeysAsList) {
                addValue(values, key, mapper, refAnn.idOnly());
            }
        } else {
            if (mf.getType().isArray()) {
                for (final Object o : (Object[]) fieldValue) {
                    addValue(values, o, mapper, refAnn.idOnly());
                }
            } else {
                for (final Object o : (Iterable) fieldValue) {
                    addValue(values, o, mapper, refAnn.idOnly());
                }
            }
        }
        if (!values.isEmpty() || mapper.getOptions().isStoreEmpties()) {
            dbObject.put(name, values);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ProxiedEntityReferenceList(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceList) List(java.util.List) ProxiedEntityReferenceList(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceList) DBObject(com.mongodb.DBObject) Key(org.mongodb.morphia.Key)

Example 2 with ProxiedEntityReferenceList

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

the class ReferenceMapper method readCollection.

private void readCollection(final Datastore datastore, final Mapper mapper, final DBObject dbObject, final MappedField mf, final Object entity, final Reference refAnn, final EntityCache cache) {
    // multiple references in a List
    final Class referenceObjClass = mf.getSubClass();
    // load reference class.  this "fixes" #816
    mapper.getMappedClass(referenceObjClass);
    Collection references = mf.isSet() ? mapper.getOptions().getObjectFactory().createSet(mf) : mapper.getOptions().getObjectFactory().createList(mf);
    if (refAnn.lazy() && LazyFeatureDependencies.assertDependencyFullFilled()) {
        final Object dbVal = mf.getDbObjectValue(dbObject);
        if (dbVal != null) {
            references = mapper.getProxyFactory().createListProxy(datastore, references, referenceObjClass, refAnn.ignoreMissing());
            final ProxiedEntityReferenceList referencesAsProxy = (ProxiedEntityReferenceList) references;
            if (dbVal instanceof List) {
                referencesAsProxy.__addAll(refAnn.idOnly() ? mapper.getKeysByManualRefs(referenceObjClass, (List) dbVal) : mapper.getKeysByRefs((List) dbVal));
            } else {
                referencesAsProxy.__add(refAnn.idOnly() ? mapper.manualRefToKey(referenceObjClass, dbVal) : mapper.refToKey((DBRef) dbVal));
            }
        }
    } else {
        final Object dbVal = mf.getDbObjectValue(dbObject);
        final Collection refs = references;
        new IterHelper<String, Object>().loopOrSingle(dbVal, new IterCallback<Object>() {

            @Override
            public void eval(final Object val) {
                final Object ent = resolveObject(datastore, mapper, cache, mf, refAnn.idOnly(), val);
                if (ent == null) {
                    LOG.warning("Null reference found when retrieving value for " + mf.getFullName());
                } else {
                    refs.add(ent);
                }
            }
        });
    }
    if (mf.getType().isArray()) {
        mf.setFieldValue(entity, ReflectionUtils.convertToArray(mf.getSubClass(), ReflectionUtils.iterToList(references)));
    } else {
        mf.setFieldValue(entity, references);
    }
}
Also used : Collection(java.util.Collection) DBCollection(com.mongodb.DBCollection) DBObject(com.mongodb.DBObject) ProxiedEntityReferenceList(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceList) ArrayList(java.util.ArrayList) ProxiedEntityReferenceList(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceList) List(java.util.List)

Aggregations

DBObject (com.mongodb.DBObject)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ProxiedEntityReferenceList (org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceList)2 DBCollection (com.mongodb.DBCollection)1 Collection (java.util.Collection)1 Key (org.mongodb.morphia.Key)1