Example usage for com.mongodb DBRef getCollectionName

List of usage examples for com.mongodb DBRef getCollectionName

Introduction

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

Prototype

public String getCollectionName() 

Source Link

Document

Gets the name of the collection in which the referenced document is stored.

Usage

From source file:com.streamsets.pipeline.stage.common.mongodb.MongoDBUtil.java

License:Apache License

@SuppressWarnings("unchecked")
private static Field jsonToField(Object object) throws IOException {
    if (object instanceof ObjectId) {
        String objectId = object.toString();
        return JsonUtil.jsonToField(objectId);
    } else if (object instanceof Binary) {
        byte[] data = ((Binary) object).getData();
        return JsonUtil.jsonToField(data);
    } else if (object instanceof BsonTimestamp) {
        int time = ((BsonTimestamp) object).getTime();
        Date date = new Date(time * 1000L);
        Map<String, Object> jsonMap = new LinkedHashMap<>();
        jsonMap.put(BSON_TS_TIME_T_FIELD, date);
        jsonMap.put(BSON_TS_ORDINAL_FIELD, ((BsonTimestamp) object).getInc());
        return JsonUtil.jsonToField(jsonMap);
    } else if (object instanceof List) {
        List jsonList = (List) object;
        List<Field> list = new ArrayList<>(jsonList.size());
        for (Object element : jsonList) {
            list.add(jsonToField(element));
        }//w w w . j av a  2s .c om
        return Field.create(list);
    } else if (object instanceof Map) {
        Map<String, Object> jsonMap = (Map<String, Object>) object;
        Map<String, Field> map = new LinkedHashMap<>();
        for (Map.Entry<String, Object> entry : jsonMap.entrySet()) {
            map.put(entry.getKey(), jsonToField(entry.getValue()));
        }
        return Field.create(map);
    } else if (object instanceof com.mongodb.DBRef) {
        Map<String, Field> map = new LinkedHashMap<>();
        com.mongodb.DBRef ref = (com.mongodb.DBRef) object;
        map.put(REF, Field.create(ref.getCollectionName()));
        map.put(ID, Field.create(ref.getId().toString()));
        if (ref.getDatabaseName() != null) {
            map.put(DB, Field.create(ref.getDatabaseName()));
        }
        return Field.create(map);
    }
    return JsonUtil.jsonToField(object);
}

From source file:de.gwdg.europeanaqa.client.rest.DocumentTransformer.java

private Document resolveReference(DBRef ref, boolean withFieldRename) {
    String collection = ref.getCollectionName();
    ObjectId id = (ObjectId) ref.getId();
    return resolveReference(collection, id, withFieldRename);
    /*// w  ww.j ava  2 s  . c o m
    Document doc = mongoDb.getCollection(collection).find(Filters.eq("_id", ref.getId())).first();
    if (doc != null) {
       doc.remove("_id");
       doc.remove("className");
       transformLanguageStructure(doc);
       if (collection.equals("PhysicalThing") && withFieldRename) {
    doc.put("europeanaProxy", Arrays.asList(((Boolean)doc.get("europeanaProxy")).toString()));
       }
       if (withFieldRename)
    replaceKeys(doc);
       for (String key : subEntities.keySet()) {
    if (doc.containsKey(key)) {
       List<Document> subDocs = new ArrayList<Document>();
       List<DBRef> subRefs = (List<DBRef>) doc.get(key);
       for (DBRef subRef : subRefs) {
          subDocs.add(resolveReference(subRef, withFieldRename));
       }
       doc.remove(key);
       doc.put(subEntities.get(key), subDocs);
    }
       }
    }
    return doc;
    */
}

From source file:org.codinjutsu.tools.mongo.view.model.JsonTreeUtils.java

License:Apache License

private static void processDBRef(JsonTreeNode parentNode, DBRef dbRef) {
    parentNode//from w ww  .  j  a v a  2 s.c  o  m
            .add(new JsonTreeNode(MongoKeyValueDescriptor.createDescriptor("$ref", dbRef.getCollectionName())));
    parentNode.add(new JsonTreeNode(MongoKeyValueDescriptor.createDescriptor("$id", dbRef.getId())));
    parentNode.add(new JsonTreeNode(MongoKeyValueDescriptor.createDescriptor("$db", dbRef.getDatabaseName())));
}

From source file:org.mongodb.morphia.mapping.Mapper.java

License:Open Source License

public <T> Key<T> refToKey(final DBRef ref) {
    return ref == null ? null : new Key<T>(ref.getCollectionName(), ref.getId());
}

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.  j  ava2 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.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;
    }//  www .ja v a2 s. co  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.MappingMongoConverter.java

License:Apache License

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

    final DBObject readRef = readRef(dbref);
    final String collectionName = dbref.getCollectionName();

    if (readRef != null) {
        maybeEmitEvent(new AfterLoadEvent<T>(readRef, (Class<T>) rawType, collectionName));
    }/* w ww . jav  a 2 s . co m*/

    final T target = (T) read(type, readRef, path);

    if (target != null) {
        maybeEmitEvent(new AfterConvertEvent<T>(readRef, target, collectionName));
    }

    return target;
}

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 w w w.ja  va2s  .  c o  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 ww w .j a  v 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);
}