Example usage for com.mongodb Block Block

List of usage examples for com.mongodb Block Block

Introduction

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

Prototype

Block

Source Link

Usage

From source file:com.redhat.jielicious.storage.mongo.response.MongoResponseBuilder.java

License:Open Source License

public static String buildJsonDocuments(FindIterable<Document> documents) {
    final StringBuilder s = new StringBuilder();

    s.append("\"response\" : [");

    final int[] i = { 0 };

    documents.forEach(new Block<Document>() {
        @Override//from w w  w  .j a  va 2  s . c  o  m
        public void apply(Document document) {
            s.append(((Document) document.get("obj")).toJson()).append(",");
            i[0]++;
        }
    });

    if (i[0] != 0) {
        s.deleteCharAt(s.length() - 1);
    }

    s.append("]");

    return s.toString();
}

From source file:com.redhat.thermostat.gateway.service.jvms.mongo.JvmInfoMongoStorageHandler.java

License:Open Source License

public String getJvmsTree(MongoCollection<Document> collection, boolean aliveOnly, String excludes,
        String includes, int limit, int offset) {
    FindIterable<Document> documents;

    if (aliveOnly) {
        documents = collection.find(lt(StorageFields.STOP_TIME, 0));
    } else {/*from  ww  w .  ja  v a2s.  co  m*/
        documents = collection.find();
    }

    documents = documents.limit(limit).skip(offset);

    boolean includeSystemId = true;
    if (excludes != null) {
        List<String> excludesList = new ArrayList<>(Arrays.asList(excludes.split(",")));
        if (excludesList.contains(StorageFields.SYSTEM_ID)) {
            excludesList.remove(StorageFields.SYSTEM_ID);
            includeSystemId = false;
        }
        if (excludesList.size() > 0) {
            documents = documents.projection(fields(exclude(excludesList), excludeId()));
        } else {
            documents = documents.projection(excludeId());
        }
    } else if (includes != null) {
        List<String> includesList = new ArrayList<>(Arrays.asList(includes.split(",")));
        if (!includesList.contains(StorageFields.SYSTEM_ID)) {
            includesList.add(StorageFields.SYSTEM_ID);
            includeSystemId = false;
        }
        documents = documents.projection(fields(include(includesList), excludeId()));
    } else {
        documents = documents.projection(excludeId());
    }

    final Map<String, StringBuilder> map = new HashMap<>();

    final boolean finalIncludeSystemId = includeSystemId;
    documents.forEach(new Block<Document>() {
        @Override
        public void apply(Document document) {
            String systemId = document.getString(StorageFields.SYSTEM_ID);
            if (!finalIncludeSystemId) {
                document.remove(StorageFields.SYSTEM_ID);
            }

            if (!map.containsKey(systemId)) {
                map.put(systemId, new StringBuilder().append("{\"" + StorageFields.SYSTEM_ID + "\":\""
                        + systemId + "\", \"" + StorageFields.JVMS + "\":["));
            }

            map.get(systemId).append(document.toJson()).append(",");
        }
    });

    StringBuilder responseBuilder = new StringBuilder().append("{ \"" + StorageFields.RESPONSE + "\" : [");
    if (map.size() > 0) {
        for (StringBuilder systemBuilder : map.values()) {
            responseBuilder.append(systemBuilder.deleteCharAt(systemBuilder.length() - 1).toString());
            responseBuilder.append("]},");
        }
        responseBuilder.deleteCharAt(responseBuilder.length() - 1);
    }
    responseBuilder.append("]}");

    return responseBuilder.toString();
}

From source file:com.zns.vehicles.service.dao.impl.VehicleDAOImpl.java

private ArrayList<String> dbGetUtil(RetrievalRequest requestPartial) {

    MongoClient mongoClient = null;/*from w w w  .j  a  v a2 s.c  o m*/
    ArrayList<String> results = new ArrayList<String>();

    try {
        mongoClient = new MongoClient(props.getProperty(HOST), PORT);
        MongoDatabase db = mongoClient.getDatabase("vehicleApp");

        log.info("querying this collection >>>>>>>>>>>>>>>>>>>>>>>>>> "
                + db.getCollection("vehicles").toString());
        log.info("looking for vehicle type:::::::: " + requestPartial.getVehicleType());

        FindIterable<Document> iterable = db.getCollection("vehicles")
                .find(new Document("username", requestPartial.getUserName()).append("vehicleType",
                        requestPartial.getVehicleType()));

        iterable.forEach(new Block<Document>() {
            @Override
            public void apply(final Document document) {
                log.info("result>>>>>>>>>>>>>> " + document.toString());
                results.add(document.toJson());
            }
        });

    } catch (MongoClientException e) {
        log.error("Error while connecting to DB...");
        e.printStackTrace();
    } catch (MongoException e) {
        log.error("General mongo error");
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        log.error("Illegal argument exception...");
        e.printStackTrace();
    } finally {
        mongoClient.close();
    }
    log.info("arraylist contents for search::::::::: " + results.size());
    return results;
}

From source file:Connections.ConexionMongoDB.java

/**
 * /*from   www  . j  ava  2  s.c  om*/
 * @param collName 
 */
public void listAllfromCollect(String collName) {

    FindIterable<org.bson.Document> iterable = this.db.getCollection(collName).find(); //Buscamos el documento con id: Asus

    iterable.forEach(new Block<Document>() {
        @Override
        public void apply(final Document document) { //Necesitamos comprobar si para ese ID el campo esta vaco.
            System.out.println(document);
            //System.out.println(document.getString("name"));

        }
    });

}

From source file:Connections.ConexionMongoDB.java

public ArrayList<Migration> getAllImages() {
    rMigrationList = new ArrayList<>();
    FindIterable<Document> iterable = db.getCollection(dbName).find().sort(new Document("name", 1));
    iterable.forEach(new Block<Document>() {
        @Override/*from   w  ww  . j  a v a 2  s .  c  om*/
        public void apply(final Document document) {
            Migration m = new Migration(document.getInteger("_id"), document.getString("name"),
                    document.getInteger("size"), document.getString("ext"), document.getString("path"), null);
            rMigrationList.add(m);
        }
    });

    return rMigrationList;
}

From source file:Connections.ConexionMongoDB.java

public ArrayList<Migration> getImagesFilterBy(String filterName, String filterValue) {
    rMigrationList = new ArrayList<>();
    FindIterable<Document> iterable = db.getCollection(dbName).find(Filters.regex(filterName, filterValue))
            .sort(new Document("name", 1));
    iterable.forEach(new Block<Document>() {
        @Override//  w  ww.ja v  a2  s.  c o m
        public void apply(final Document document) {
            Migration m = new Migration(document.getInteger("_id"), document.getString("name"),
                    document.getInteger("size"), document.getString("ext"), document.getString("path"), null);
            rMigrationList.add(m);
        }
    });
    return rMigrationList;
}

From source file:Connections.ConexionMongoDB.java

public ArrayList<MigrationDirectory> getDirectoriesFromImageID(int idImage) {
    rMigrationDirectoryList = new ArrayList<>();
    FindIterable<Document> iterable = db.getCollection(dbName).find(new Document("_id", idImage));
    iterable.forEach(new Block<Document>() {
        @Override/*  w w  w .  ja v  a2  s.  c  o  m*/
        public void apply(final Document document) {
            List<Document> documents = (List<Document>) document.get("directory");
            for (Document d : documents) {
                // Recuperamos las etiquetas
                //System.out.println(d.get("directoryName"));
                List<Document> etDocuments = (List<Document>) d.get("labels");
                ArrayList<Value_Label> valueLabelList = new ArrayList<>();
                for (Document dEt : etDocuments) {
                    Value_Label vl = new Value_Label(dEt.getString("value"), dEt.getString("labelName"));
                    //System.out.println(vl);
                    valueLabelList.add(vl);
                }
                MigrationDirectory md = new MigrationDirectory(d.getString("directoryName"), valueLabelList);
                rMigrationDirectoryList.add(md);
            }

        }
    });

    return rMigrationDirectoryList;
}

From source file:Connections.ConexionMongoDB.java

public ArrayList<Value_Label> getLabelsFilterBy(int idImage, String directoryName, String filterName,
        String filterValue) {//  ww w . jav  a2 s. c o  m
    ArrayList<Value_Label> labelList = new ArrayList<>();
    AggregateIterable iterable = db.getCollection(dbName)
            .aggregate(asList(new Document("$match", new Document("_id", idImage)),
                    new Document("$unwind", "$directory"), new Document("$unwind", "$directory.labels")));

    iterable.forEach(new Block<Document>() {
        @Override
        public void apply(final Document document) {
            Document d = (Document) document.get("directory");
            if (d.getString("directoryName").equals(directoryName)) {
                Document l = (Document) d.get("labels");
                Pattern p = Pattern.compile(".*" + filterValue + ".*", Pattern.CASE_INSENSITIVE);
                Matcher matcher = p.matcher(l.getString(filterName));
                Value_Label vl;
                if (matcher.matches()) {
                    if (filterName.equals("labelName"))
                        vl = new Value_Label(l.getString("value"), l.getString(filterName));
                    else
                        vl = new Value_Label(l.getString(filterName), l.getString("labelName"));
                    labelList.add(vl);
                }
            }
        }
    });
    return labelList;
}

From source file:consultasMongoDB.MongoConsultas.java

private static List<Object> retornaResultadoQueryComDoisParametros(String nomePrimeiroParametroDeProcura,
        String nomePrimeiroParametroDeProcura2) {
    MongoCollection<Document> ptCollection = initiateMongoCollection();
    String regexNome = "^.*$";

    AggregateIterable<Document> agg = ptCollection.aggregate(asList(
            new Document("$match", new Document("modulo", java.util.regex.Pattern.compile(regexNome))),
            new Document("$group", new Document("_id",
                    new Document(nomePrimeiroParametroDeProcura, "$" + nomePrimeiroParametroDeProcura)
                            .append(nomePrimeiroParametroDeProcura2, "$" + nomePrimeiroParametroDeProcura2))
                                    .append("Total", new Document("$sum", "$area"))),
            new Document("$sort", new Document("Total", 1))));

    List<Object> listO = new ArrayList<>();

    agg.forEach(new Block<Document>() {
        @Override/*  w w w  .  ja va2s  .  c om*/
        public void apply(final Document document) {

            int control = 1; //varivel para controlar a leitura (estava gravando dobrado)
            for (Object o : document.values()) {
                if (control == 1) {
                    Document aux = (Document) document.get("_id");
                    String modAux = aux.getString(nomePrimeiroParametroDeProcura);
                    String setAux = aux.getString(nomePrimeiroParametroDeProcura2);

                    listO.add(modAux);
                    listO.add(setAux);
                    control = 2;
                } else {
                    listO.add(document.getDouble("Total"));
                    control = 1;
                }
            }
        }
    });

    return listO;
}

From source file:consultasMongoDB.MongoConsultas.java

private static List<Object> retornaResultadoQueryComTresParametros(String nomePrimeiroParametroDeProcura,
        String nomePrimeiroParametroDeProcura2, String nomePrimeiroParametroDeProcura3) {
    MongoCollection<Document> ptCollection = initiateMongoCollection();
    String regexNome = "^.*$";
    AggregateIterable<Document> agg = ptCollection.aggregate(asList(
            new Document("$match", new Document("modulo", java.util.regex.Pattern.compile(regexNome))),
            new Document("$group", new Document("_id",
                    new Document(nomePrimeiroParametroDeProcura, "$" + nomePrimeiroParametroDeProcura)
                            .append(nomePrimeiroParametroDeProcura2, "$" + nomePrimeiroParametroDeProcura2)
                            .append(nomePrimeiroParametroDeProcura3, "$" + nomePrimeiroParametroDeProcura3))
                                    .append("Total", new Document("$sum", "$area"))),
            new Document("$sort", new Document("Total", 1))));

    List<Object> listO = new ArrayList<>();

    agg.forEach(new Block<Document>() {
        @Override//ww  w  .  ja v  a2s . c  om
        public void apply(final Document document) {

            int control = 1; //varivel para controlar a leitura (estava gravando dobrado)
            for (Object o : document.values()) {
                if (control == 1) {
                    Document aux = (Document) document.get("_id");
                    String modAux = aux.getString(nomePrimeiroParametroDeProcura);
                    String setAux = aux.getString(nomePrimeiroParametroDeProcura2);
                    String setAux2 = aux.getString(nomePrimeiroParametroDeProcura3);

                    listO.add(modAux);
                    listO.add(setAux);
                    listO.add(setAux2);
                    control = 2;
                } else {
                    listO.add(document.getDouble("Total"));
                    control = 1;
                }
            }
        }
    });

    return listO;
}