Search in sources :

Example 6 with Key

use of org.mongodb.morphia.Key in project morphia by mongodb.

the class ReferenceMapper method resolveObject.

Object resolveObject(final Datastore datastore, final Mapper mapper, final EntityCache cache, final MappedField mf, final boolean idOnly, final Object ref) {
    if (ref == null) {
        return null;
    }
    final DBRef dbRef = idOnly ? null : (DBRef) ref;
    final Key key = mapper.createKey(mf.isSingleValue() ? mf.getType() : mf.getSubClass(), idOnly ? ref : dbRef.getId());
    final Object cached = cache.getEntity(key);
    if (cached != null) {
        return cached;
    }
    final DBObject refDbObject;
    DBCollection collection;
    Object id;
    if (idOnly) {
        collection = datastore.getCollection(key.getType());
        id = ref;
    } else {
        collection = datastore.getDB().getCollection(dbRef.getCollectionName());
        id = dbRef.getId();
    }
    if (id instanceof DBObject) {
        ((DBObject) id).removeField(Mapper.CLASS_NAME_FIELDNAME);
    }
    refDbObject = collection.findOne(id);
    if (refDbObject != null) {
        Object refObj = mapper.getOptions().getObjectFactory().createInstance(mapper, mf, refDbObject);
        refObj = mapper.fromDb(datastore, refDbObject, refObj, cache);
        cache.putEntity(key, refObj);
        return refObj;
    }
    final boolean ignoreMissing = mf.getAnnotation(Reference.class) != null && mf.getAnnotation(Reference.class).ignoreMissing();
    if (!ignoreMissing) {
        throw new MappingException("The reference(" + ref.toString() + ") could not be fetched for " + mf.getFullName());
    } else {
        return null;
    }
}
Also used : DBCollection(com.mongodb.DBCollection) DBRef(com.mongodb.DBRef) DBObject(com.mongodb.DBObject) DBObject(com.mongodb.DBObject) Key(org.mongodb.morphia.Key)

Example 7 with Key

use of org.mongodb.morphia.Key in project morphia by mongodb.

the class ReferenceMapper method addValue.

private void addValue(final List values, final Object o, final Mapper mapper, final boolean idOnly) {
    if (o == null && mapper.getOptions().isStoreNulls()) {
        values.add(null);
        return;
    }
    final Key key = o instanceof Key ? (Key) o : getKey(o, mapper);
    values.add(idOnly ? mapper.keyToId(key) : mapper.keyToDBRef(key));
}
Also used : Key(org.mongodb.morphia.Key)

Example 8 with Key

use of org.mongodb.morphia.Key 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 9 with Key

use of org.mongodb.morphia.Key in project morphia by mongodb.

the class ReferenceMapper method getKey.

private Key<?> getKey(final Object entity, final Mapper mapper) {
    try {
        if (entity instanceof ProxiedEntityReference) {
            final ProxiedEntityReference proxy = (ProxiedEntityReference) entity;
            return proxy.__getKey();
        }
        final MappedClass mappedClass = mapper.getMappedClass(entity);
        Object id = mappedClass.getIdField().get(entity);
        if (id == null) {
            throw new MappingException("@Id field cannot be null!");
        }
        return new Key(mappedClass.getClazz(), mappedClass.getCollectionName(), id);
    } catch (IllegalAccessException iae) {
        throw new RuntimeException(iae);
    }
}
Also used : DBObject(com.mongodb.DBObject) ProxiedEntityReference(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference) Key(org.mongodb.morphia.Key)

Example 10 with Key

use of org.mongodb.morphia.Key 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)

Aggregations

Key (org.mongodb.morphia.Key)25 DBObject (com.mongodb.DBObject)13 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)8 BasicDBObject (com.mongodb.BasicDBObject)7 MappedField (org.mongodb.morphia.mapping.MappedField)5 ProxiedEntityReference (org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference)5 DBRef (com.mongodb.DBRef)4 Map (java.util.Map)3 ObjectId (org.bson.types.ObjectId)3 FacebookUser (org.mongodb.morphia.TestDatastore.FacebookUser)3 MappedClass (org.mongodb.morphia.mapping.MappedClass)3 Mapper (org.mongodb.morphia.mapping.Mapper)3 ReflectionUtils.getParameterizedClass (org.mongodb.morphia.utils.ReflectionUtils.getParameterizedClass)3 BasicDBList (com.mongodb.BasicDBList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Datastore (org.mongodb.morphia.Datastore)2 KeysKeysKeys (org.mongodb.morphia.TestDatastore.KeysKeysKeys)2 Reference (org.mongodb.morphia.annotations.Reference)2