Example usage for com.mongodb DBRef DBRef

List of usage examples for com.mongodb DBRef DBRef

Introduction

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

Prototype

public DBRef(final String collectionName, final Object id) 

Source Link

Document

Construct an instance.

Usage

From source file:com.amertkara.tinkerpop.blueprints.impl.mongodb.MongoDBGraph.java

License:Apache License

@Override
public Edge addEdge(Object id, Vertex outVertex, Vertex inVertex, String label)
        throws MongoWriteException, MongoWriteConcernException {
    if (label == null) {
        throw ExceptionFactory.edgeLabelCanNotBeNull();
    }/*from ww w  .j a  v  a 2 s  . c o  m*/

    if (id == null) {
        id = getCollectionNextID(edgeCollection);
    }

    MongoDBEdgeIterable edgeIt;
    MongoDBEdge edge;

    // Insert the new edge
    this.rawGraph.getCollection(edgeCollection)
            .insertOne(new Document(MongoDBConstants.FIELD_ID, id).append(MongoDBConstants.FIELD_LABEL, label)
                    .append(MongoDBConstants.FIELD_OUTV, new DBRef(vertexCollection, outVertex.getId()))
                    .append(MongoDBConstants.FIELD_INV, new DBRef(vertexCollection, inVertex.getId())));

    // Fetch the newly created edge
    edgeIt = new MongoDBEdgeIterable(
            this.rawGraph.getCollection(edgeCollection).find(new Document(MongoDBConstants.FIELD_ID, id)),
            this);

    try {
        edge = (MongoDBEdge) edgeIt.iterator().next();
    } catch (NoSuchElementException nsee) {
        logger.warn("Newly created edge could not be fetched. The error message: " + nsee.getMessage());
        edge = null;
    }

    return edge;
}

From source file:com.amertkara.tinkerpop.blueprints.impl.mongodb.MongoDBVertex.java

License:Apache License

@Override
public Iterable<Edge> getEdges(Direction direction, String... labels) {
    if (direction.equals(Direction.OUT)) {
        Document query = new Document(MongoDBConstants.FIELD_OUTV,
                new DBRef(MongoDBConstants.DEFAULT_VERTEX_COLLECTION, getId()))
                        .append(MongoDBConstants.FIELD_LABEL, new Document("$in", Arrays.asList(labels)));
        FindIterable<Document> result = this.graph.getEdgeCollection().find(query);
        return new MongoDBEdgeIterable(result, graph);
    } else if (direction.equals(Direction.IN)) {
        Document query = new Document(MongoDBConstants.FIELD_INV,
                new DBRef(MongoDBConstants.DEFAULT_VERTEX_COLLECTION, getId()))
                        .append(MongoDBConstants.FIELD_LABEL, new Document("$in", Arrays.asList(labels)));
        FindIterable<Document> result = this.graph.getEdgeCollection().find(query);
        return new MongoDBEdgeIterable(result, graph);
    } else {/* ww w.ja v  a2  s.co  m*/
        Document query = new Document("$or", Arrays.asList(
                new Document(MongoDBConstants.FIELD_OUTV,
                        new DBRef(MongoDBConstants.DEFAULT_VERTEX_COLLECTION, getId())).append(
                                MongoDBConstants.FIELD_LABEL, new Document("$in", Arrays.asList(labels))),
                new Document(MongoDBConstants.FIELD_INV,
                        new DBRef(MongoDBConstants.DEFAULT_VERTEX_COLLECTION, getId())).append(
                                MongoDBConstants.FIELD_LABEL, new Document("$in", Arrays.asList(labels)))));
        FindIterable<Document> result = this.graph.getEdgeCollection().find(query);
        return new MongoDBEdgeIterable(result, graph);
    }
}

From source file:com.amertkara.tinkerpop.blueprints.impl.mongodb.MongoDBVertex.java

License:Apache License

@Override
public Iterable<Vertex> getVertices(Direction direction, String... labels) {
    List<Integer> ids = new ArrayList<Integer>();
    Iterator<Document> it;
    FindIterable<Document> result;

    if (direction.equals(Direction.OUT)) {
        Document query = new Document(MongoDBConstants.FIELD_INV,
                new DBRef(MongoDBConstants.DEFAULT_VERTEX_COLLECTION, getId()))
                        .append(MongoDBConstants.FIELD_LABEL, new Document("$in", Arrays.asList(labels)));
        result = this.graph.getEdgeCollection().find(query);
        it = result.iterator();/*  w w  w . j  a va 2s.  c om*/

        while (it.hasNext()) {
            Document document = (Document) it.next();
            DBRef inV = (DBRef) document.get(MongoDBConstants.FIELD_OUTV);
            ids.add((Integer) inV.getId());
        }
    } else if (direction.equals(Direction.IN)) {
        Document query = new Document(MongoDBConstants.FIELD_OUTV,
                new DBRef(MongoDBConstants.DEFAULT_VERTEX_COLLECTION, getId()))
                        .append(MongoDBConstants.FIELD_LABEL, new Document("$in", Arrays.asList(labels)));
        result = this.graph.getEdgeCollection().find(query);
        it = result.iterator();

        while (it.hasNext()) {
            Document document = (Document) it.next();
            DBRef outV = (DBRef) document.get(MongoDBConstants.FIELD_INV);
            ids.add((Integer) outV.getId());
        }
    } else {
        Document query = new Document("$or", Arrays.asList(
                new Document(MongoDBConstants.FIELD_OUTV,
                        new DBRef(MongoDBConstants.DEFAULT_VERTEX_COLLECTION, getId())).append(
                                MongoDBConstants.FIELD_LABEL, new Document("$in", Arrays.asList(labels))),
                new Document(MongoDBConstants.FIELD_INV,
                        new DBRef(MongoDBConstants.DEFAULT_VERTEX_COLLECTION, getId())).append(
                                MongoDBConstants.FIELD_LABEL, new Document("$in", Arrays.asList(labels)))));
        result = this.graph.getEdgeCollection().find(query);
        it = result.iterator();

        while (it.hasNext()) {
            Document document = (Document) it.next();
            DBRef inV = (DBRef) document.get(MongoDBConstants.FIELD_INV);
            DBRef outV = (DBRef) document.get(MongoDBConstants.FIELD_OUTV);
            if (inV.getId() != getId()) {
                ids.add((Integer) inV.getId());
            } else {
                ids.add((Integer) outV.getId());
            }
        }
    }

    return new MongoDBVertexIterable(this.graph.getVertexCollection()
            .find(new Document(MongoDBConstants.FIELD_ID, new Document("$in", ids))), graph);
}

From source file:com.intellijob.MongoConfiguration.java

License:Apache License

private void setObjectIdRecursive(Map<String, Object> mapObject) {
    if (mapObject.containsKey("_id")) {
        String objectId = ((Map) mapObject.get("_id")).get("$oid").toString();
        mapObject.put("_id", new ObjectId(objectId));
    }/*from   w  ww .  ja  va2s.  c o m*/

    //set category reference.
    if (mapObject.containsKey("category")) {
        Map<String, Object> categoryRef = (Map) mapObject.get("category");
        String refNamespace = categoryRef.get("$ref").toString();
        String objectId = ((Map) categoryRef.get("$id")).get("$oid").toString();
        mapObject.put("category", new DBRef(refNamespace, new ObjectId(objectId)));
    }

    for (Object object : mapObject.values()) {
        if (object instanceof Map) {
            Map<String, Object> innerMap = (Map) object;
            setObjectIdRecursive(innerMap);
        }

        if (object instanceof List) {
            List<Map<String, Object>> innerListMap = (List) object;
            innerListMap.forEach(this::setObjectIdRecursive);
        }
    }
}

From source file:com.jopss.apostas.db.migration.CadastroPerfil.java

@ChangeSet(order = "001", id = "002.001", author = "apostas")
public void criarPerfil(DB db) {

    DBCollection perfil = db.getCollection("perfil");
    DBCollection permissao = db.getCollection("permissao");

    BasicDBObject filtro = new BasicDBObject();
    filtro.put("papel", new BasicDBObject("$in", Arrays.asList("ROLE_GERAL", "ROLE_ADMIN")));

    List listaPermissoes = new ArrayList();
    DBObject ob1 = permissao.findOne(new BasicDBObject("papel", "ROLE_ADMIN"));
    listaPermissoes.add(new DBRef("permissao", ob1.get("_id")));
    DBObject ob2 = permissao.findOne(new BasicDBObject("papel", "ROLE_GERAL"));
    listaPermissoes.add(new DBRef("permissao", ob2.get("_id")));

    BasicDBObject admin = new BasicDBObject();
    admin.append("dataatualizacao", null);
    admin.append("datacriacao", new Date());
    admin.append("nome", "Perfil Administrador");
    admin.append("descricao", "Perfil com permisso geral.");
    admin.append("permissoes", listaPermissoes);
    perfil.insert(admin);/* w  w w.  j av  a 2s  .  com*/

    BasicDBObject visitante = new BasicDBObject();
    visitante.append("dataatualizacao", null);
    visitante.append("datacriacao", new Date());
    visitante.append("nome", "Perfil Visitante");
    visitante.append("descricao", "Perfil sem permisso para gerenciar uma aposta.");
    perfil.insert(visitante);
}

From source file:com.restfeel.controller.rest.EntityDataController.java

License:Apache License

private void relationToDBRef(DBObject dbObject, String projectId) {
    for (String key : dbObject.keySet()) {
        Object obj = dbObject.get(key);
        if (obj instanceof DBObject) {
            DBObject doc = (DBObject) obj;
            if (doc.containsField("_rel")) {
                DBObject relation = (DBObject) doc.get("_rel");
                dbObject.put(key, new DBRef(projectId + "_" + (String) relation.get("entity"),
                        new ObjectId((String) relation.get("_id"))));
            } else {
                relationToDBRef(doc, projectId);
            }//from ww w . j av a 2  s  .  c  o m
        }
    }
}

From source file:com.sube.daos.mongodb.generators.GenericDBRefGenerator.java

License:Apache License

@Override
public DBRef generateDBRef(T toGenerate) {
    DBCollection dbCollection = db.getCollection(collection);
    // { $ref : <collname>, $id : <idvalue>[, $db : <dbname>] }
    BSONObject dbRefObj = new BasicDBObject("$ref", dbCollection.getName())
            .append("$id", toGenerate.getMongoId()).append("$db", db.getName());
    DBRef dbRef = new DBRef(db, dbRefObj);
    return dbRef;
}

From source file:org.cubeengine.reflect.codec.mongo.Reference.java

License:Open Source License

public DBRef getDBRef() {
    if (this.dbRef == null) {
        this.dbRef = new DBRef(collection.getName(), object.get("_id"));
    }/*  w w  w . j  a  va 2  s  .c o m*/
    return this.dbRef;
}

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

License:Open Source License

public DBRef keyToRef(final Key key) {
    if (key == null) {
        return null;
    }/*from w  ww  .  j a  v a  2s .c o m*/
    if (key.getKindClass() == null && key.getKind() == null) {
        throw new IllegalStateException("How can it be missing both?");
    }
    if (key.getKind() == null) {
        key.setKind(getCollectionName(key.getKindClass()));
    }

    return new DBRef(key.getKind(), key.getId());
}

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

License:Apache License

@Override
public DBRef createDbRef(org.springframework.data.mongodb.core.mapping.DBRef annotation,
        MongoPersistentEntity<?> entity, Object id) {
    return new DBRef(entity.getCollection(), id);
}