Search in sources :

Example 26 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 27 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 28 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 29 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 30 with DBRef

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

the class JSONCallback method objectDone.

@Override
public Object objectDone() {
    String name = curName();
    Object o = super.objectDone();
    if (_lastArray) {
        return o;
    }
    BSONObject b = (BSONObject) o;
    // override the object if it's a special type
    if (b.containsField("$oid")) {
        o = new ObjectId((String) b.get("$oid"));
    } else if (b.containsField("$date")) {
        if (b.get("$date") instanceof Number) {
            o = new Date(((Number) b.get("$date")).longValue());
        } else {
            SimpleDateFormat format = new SimpleDateFormat(_msDateFormat);
            format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
            o = format.parse(b.get("$date").toString(), new ParsePosition(0));
            if (o == null) {
                // try older format with no ms
                format = new SimpleDateFormat(_secDateFormat);
                format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
                o = format.parse(b.get("$date").toString(), new ParsePosition(0));
            }
        }
    } else if (b.containsField("$regex")) {
        o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
    } else if (b.containsField("$ts")) {
        //Legacy timestamp format
        Integer ts = ((Number) b.get("$ts")).intValue();
        Integer inc = ((Number) b.get("$inc")).intValue();
        o = new BSONTimestamp(ts, inc);
    } else if (b.containsField("$timestamp")) {
        BSONObject tsObject = (BSONObject) b.get("$timestamp");
        Integer ts = ((Number) tsObject.get("t")).intValue();
        Integer inc = ((Number) tsObject.get("i")).intValue();
        o = new BSONTimestamp(ts, inc);
    } else if (b.containsField("$code")) {
        if (b.containsField("$scope")) {
            o = new CodeWScope((String) b.get("$code"), (DBObject) b.get("$scope"));
        } else {
            o = new Code((String) b.get("$code"));
        }
    } else if (b.containsField("$ref")) {
        o = new DBRef((String) b.get("$ref"), b.get("$id"));
    } else if (b.containsField("$minKey")) {
        o = new MinKey();
    } else if (b.containsField("$maxKey")) {
        o = new MaxKey();
    } else if (b.containsField("$uuid")) {
        o = UUID.fromString((String) b.get("$uuid"));
    } else if (b.containsField("$binary")) {
        int type = (b.get("$type") instanceof String) ? Integer.valueOf((String) b.get("$type"), 16) : (Integer) b.get("$type");
        byte[] bytes = DatatypeConverter.parseBase64Binary((String) b.get("$binary"));
        o = new Binary((byte) type, bytes);
    } else if (b.containsField("$undefined") && b.get("$undefined").equals(true)) {
        o = new BsonUndefined();
    } else if (b.containsField("$numberLong")) {
        o = Long.valueOf((String) b.get("$numberLong"));
    } else if (b.containsField("$numberDecimal")) {
        o = Decimal128.parse((String) b.get("$numberDecimal"));
    }
    if (!isStackEmpty()) {
        _put(name, o);
    } else {
        o = !BSON.hasDecodeHooks() ? o : BSON.applyDecodingHooks(o);
        setRoot(o);
    }
    return o;
}
Also used : ObjectId(org.bson.types.ObjectId) BSONObject(org.bson.BSONObject) GregorianCalendar(java.util.GregorianCalendar) DBRef(com.mongodb.DBRef) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) SimpleTimeZone(java.util.SimpleTimeZone) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject) Binary(org.bson.types.Binary) SimpleDateFormat(java.text.SimpleDateFormat) BsonUndefined(org.bson.BsonUndefined) ParsePosition(java.text.ParsePosition)

Aggregations

DBRef (com.mongodb.DBRef)68 Document (org.bson.Document)26 Test (org.junit.jupiter.api.Test)24 BasicDBObject (com.mongodb.BasicDBObject)22 ObjectId (org.bson.types.ObjectId)20 DBObject (com.mongodb.DBObject)17 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)13 BasicDBList (com.mongodb.BasicDBList)11 ArrayList (java.util.ArrayList)7 Binary (org.bson.types.Binary)7 Test (org.junit.Test)6 Document (org.springframework.data.mongodb.core.mapping.Document)6 DBCollection (com.mongodb.DBCollection)5 Map (java.util.Map)5 BasicBSONObject (org.bson.BasicBSONObject)5 Date (java.util.Date)4 List (java.util.List)4 UUID (java.util.UUID)4 Bson (org.bson.conversions.Bson)4 MaxKey (org.bson.types.MaxKey)4