Search in sources :

Example 6 with DBRef

use of com.mongodb.DBRef 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 DBRef

use of com.mongodb.DBRef in project v7files by thiloplanz.

the class BucketsServlet method doEchoPut.

private void doEchoPut(HttpServletRequest request, HttpServletResponse response, BSONObject bucket) throws IOException {
    byte[] sha;
    BSONObject content = storage.insertContentsAndBackRefs(request.getInputStream(), new DBRef(null, bucketCollection.getName(), bucket.get("_id")), null, null);
    sha = (byte[]) content.get("sha");
    if (sha == null) {
        sha = ((InlineContent) storage.getContentPointer(content)).getSHA();
    }
    response.setContentType("text/plain");
    response.getWriter().write(Hex.encodeHexString(sha));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) DBRef(com.mongodb.DBRef)

Example 8 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)

Example 9 with DBRef

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

the class TestMapping method testDbRefMapping.

@Test
public void testDbRefMapping() throws Exception {
    getMorphia().map(ContainsRef.class).map(Rectangle.class);
    final DBCollection stuff = getDb().getCollection("stuff");
    final DBCollection rectangles = getDb().getCollection("rectangles");
    assertTrue("'ne' field should not be persisted!", !getMorphia().getMapper().getMCMap().get(ContainsRef.class.getName()).containsJavaFieldName("ne"));
    final Rectangle r = new Rectangle(1, 1);
    final DBObject rDbObject = getMorphia().toDBObject(r);
    rDbObject.put("_ns", rectangles.getName());
    rectangles.save(rDbObject);
    final ContainsRef cRef = new ContainsRef();
    cRef.rect = new DBRef((String) rDbObject.get("_ns"), rDbObject.get("_id"));
    final DBObject cRefDbObject = getMorphia().toDBObject(cRef);
    stuff.save(cRefDbObject);
    final BasicDBObject cRefDbObjectLoaded = (BasicDBObject) stuff.findOne(BasicDBObjectBuilder.start("_id", cRefDbObject.get("_id")).get());
    final ContainsRef cRefLoaded = getMorphia().fromDBObject(getDs(), ContainsRef.class, cRefDbObjectLoaded, new DefaultEntityCache());
    assertNotNull(cRefLoaded);
    assertNotNull(cRefLoaded.rect);
    assertNotNull(cRefLoaded.rect.getId());
    assertNotNull(cRefLoaded.rect.getCollectionName());
    assertEquals(cRefLoaded.rect.getId(), cRef.rect.getId());
    assertEquals(cRefLoaded.rect.getCollectionName(), cRef.rect.getCollectionName());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Rectangle(org.mongodb.morphia.testmodel.Rectangle) DBRef(com.mongodb.DBRef) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 10 with DBRef

use of com.mongodb.DBRef in project mongo-hadoop by mongodb.

the class PigBoxedDBRef method getObject.

@Override
public DBRef getObject() {
    byte[] bytes = get();
    ObjectId id = new ObjectId(Arrays.copyOfRange(bytes, bytes.length - 12, bytes.length));
    String collectionName = new String(Arrays.copyOfRange(bytes, 0, bytes.length - 13));
    return new DBRef(collectionName, id);
}
Also used : ObjectId(org.bson.types.ObjectId) DBRef(com.mongodb.DBRef)

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