Example usage for com.mongodb DBRef getId

List of usage examples for com.mongodb DBRef getId

Introduction

In this page you can find the example usage for com.mongodb DBRef getId.

Prototype

public Object getId() 

Source Link

Document

Gets the _id of the referenced document

Usage

From source file:org.mongojack.JacksonDBCollection.java

License:Apache License

/**
 * Fetch a collection of dbrefs.  This is more efficient than fetching one at a time.
 *
 * @param collection the collection to fetch
 * @param fields     The fields to retrieve for each of the documents
 * @return The collection of referenced objcets
 *//*from  w  w  w  .  j a v  a2  s.c o  m*/
public <R, RK> List<R> fetch(Collection<org.mongojack.DBRef<R, RK>> collection, DBObject fields) {
    Map<JacksonCollectionKey, List<Object>> collectionsToIds = new HashMap<JacksonCollectionKey, List<Object>>();
    for (org.mongojack.DBRef<R, RK> ref : collection) {
        if (ref instanceof FetchableDBRef) {
            JacksonCollectionKey key = ((FetchableDBRef) ref).getCollectionKey();
            List<Object> ids = collectionsToIds.get(key);
            if (ids == null) {
                ids = new ArrayList<Object>();
                collectionsToIds.put(key, ids);
            }
            ids.add(getReferenceCollection(key).convertToDbId(ref.getId()));
        }
    }
    List<R> results = new ArrayList<R>();
    for (Map.Entry<JacksonCollectionKey, List<Object>> entry : collectionsToIds.entrySet()) {
        for (R result : this.<R, RK>getReferenceCollection(entry.getKey())
                .find(new QueryBuilder().put("_id").in(entry.getValue()).get(), fields)) {
            results.add(result);
        }
    }
    return results;
}

From source file:org.springframework.data.mongodb.core.convert.CustomMappingMongoConverter.java

@SuppressWarnings("unchecked")
private <T> T potentiallyReadOrResolveDbRef(DBRef dbref, TypeInformation<?> type, ObjectPath path,
        Class<?> rawType) {
    if (rawType.equals(DBRef.class)) {
        return (T) dbref;
    }/*from w ww.java2  s.c o  m*/
    Object object = dbref == null ? null : path.getPathItem(dbref.getId(), dbref.getCollectionName());
    //CHANGE STARTS
    if (object != null) {
        return (T) object;
    } else {
        return readAndConvertDBRef(dbref, type, path, rawType);
    }
    //CHANGE ENDS
}

From source file:org.springframework.data.mongodb.core.convert.DefaultDbRefProxyHandler.java

License:Apache License

@Override
public Object populateId(MongoPersistentProperty property, DBRef source, Object proxy) {

    if (source == null) {
        return proxy;
    }/*from w w w  . j a  v a 2 s  .  com*/

    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(property);
    MongoPersistentProperty idProperty = entity.getIdProperty();

    if (idProperty.usePropertyAccess()) {
        return proxy;
    }

    SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(proxy, spELContext);
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(proxy);

    DBObject object = new BasicDBObject(idProperty.getFieldName(), source.getId());
    ObjectPath objectPath = ObjectPath.ROOT.push(proxy, entity, null);
    accessor.setProperty(idProperty, resolver.getValueInternal(idProperty, object, evaluator, objectPath));

    return proxy;
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

License:Apache License

@SuppressWarnings("unchecked")
private <T> T potentiallyReadOrResolveDbRef(DBRef dbref, TypeInformation<?> type, ObjectPath path,
        Class<?> rawType) {

    if (rawType.equals(DBRef.class)) {
        return (T) dbref;
    }//from   ww w  . ja  v  a  2 s. c  o m

    Object object = dbref == null ? null : path.getPathItem(dbref.getId(), dbref.getCollectionName());
    return (T) (object != null ? object : readAndConvertDBRef(dbref, type, path, rawType));
}

From source file:org.springframework.data.mongodb.core.convert.QueryMapper.java

License:Apache License

/**
 * Converts the given source assuming it's actually an association to another object.
 * //from ww  w. j  a v a 2  s.co  m
 * @param source
 * @param property
 * @return
 */
protected Object convertAssociation(Object source, MongoPersistentProperty property) {

    if (property == null || source == null || source instanceof DBObject) {
        return source;
    }

    if (source instanceof DBRef) {

        DBRef ref = (DBRef) source;
        return new DBRef(ref.getCollectionName(), convertId(ref.getId()));
    }

    if (source instanceof Iterable) {
        BasicDBList result = new BasicDBList();
        for (Object element : (Iterable<?>) source) {
            result.add(createDbRefFor(element, property));
        }
        return result;
    }

    if (property.isMap()) {
        BasicDBObject result = new BasicDBObject();
        DBObject dbObject = (DBObject) source;
        for (String key : dbObject.keySet()) {
            result.put(key, createDbRefFor(dbObject.get(key), property));
        }
        return result;
    }

    return createDbRefFor(source, property);
}

From source file:org.springframework.data.mongodb.core.convert.ReflectiveDBRefResolver.java

License:Apache License

/**
 * Fetches the object referenced from the database either be directly calling {@link DBRef#fetch()} or
 * {@link DBCollection#findOne(Object)}.
 *
 * @param db can be {@literal null} when using MongoDB Java driver in version 2.x.
 * @param ref must not be {@literal null}.
 * @return the document that this references.
 *//*from   w  w  w . j av a  2  s  . c o  m*/
public static DBObject fetch(MongoDbFactory factory, DBRef ref) {

    Assert.notNull(ref, "DBRef to fetch must not be null!");

    if (isMongo3Driver()) {

        Assert.notNull(factory, "DbFactory to fetch DB from must not be null!");
        return factory.getDb().getCollection(ref.getCollectionName()).findOne(ref.getId());
    }

    return (DBObject) invokeMethod(FETCH_METHOD, ref);
}

From source file:org.teiid.translator.mongodb.MongoDocument.java

License:Open Source License

public DBObject getEmbeddedDocument(DB mongoDB, String docName) {
    for (MergeDetails ref : this.embeddedKeys) {
        if (ref.getName().equals(docName)) {
            DBRef dbRef = ref.getDBRef(mongoDB, false);
            if (dbRef != null) {
                return mongoDB.getCollection(dbRef.getRef()).findOne(new BasicDBObject("_id", dbRef.getId())); //$NON-NLS-1$
            }//from   ww w . jav a 2  s  . c o  m
        }
    }
    return null;
}