Example usage for com.mongodb DBCursor hasNext

List of usage examples for com.mongodb DBCursor hasNext

Introduction

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

Prototype

@Override
public boolean hasNext() 

Source Link

Document

Checks if there is another object available.

Usage

From source file:achmad.rifai.erp1.entity.Suplier.java

public static Suplier of(Db d, String kode) throws Exception {
    Suplier v = null;/*from  ww  w  .j  av  a  2  s .  com*/
    achmad.rifai.erp1.util.RSA r = achmad.rifai.erp1.util.Work.loadRSA();
    com.mongodb.DBObject p = new com.mongodb.BasicDBObject();
    p.put("berkas", kode);
    com.mongodb.DBCursor c = d.getD().getCollectionFromString("suplier").find(p);
    while (c.hasNext()) {
        com.mongodb.DBObject o = c.next();
        com.mongodb.BasicDBList l = (com.mongodb.BasicDBList) o.get("bin");
        String json = "";
        for (int x = 0; x < l.size(); x++)
            json += r.decrypt("" + l.get(x));
        v = new Suplier(json);
        break;
    }
    return v;
}

From source file:achmad.rifai.erp1.entity.Terima.java

public static Terima of(Db d, String kode) throws Exception {
    Terima t = null;/* w  ww  .  ja  v a  2  s.c om*/
    achmad.rifai.erp1.util.RSA r = achmad.rifai.erp1.util.Work.loadRSA();
    com.mongodb.DBObject p = new com.mongodb.BasicDBObject();
    p.put("berkas", kode);
    com.mongodb.DBCursor c = d.getD().getCollectionFromString("terima").find(p);
    if (c.hasNext()) {
        com.mongodb.BasicDBList l = (com.mongodb.BasicDBList) c.next().get("bin");
        String json = "";
        for (int x = 0; x < l.size(); x++)
            json += r.decrypt("" + l.get(x));
        t = new Terima(json);
    }
    return t;
}

From source file:achmad.rifai.erp1.entity.Tracks.java

public static Tracks of(Db d, String kode) throws Exception {
    Tracks v = null;/*from ww  w.  j  ava 2 s  .  c o  m*/
    achmad.rifai.erp1.util.RSA r = achmad.rifai.erp1.util.Work.loadRSA();
    com.mongodb.DBObject p = new com.mongodb.BasicDBObject();
    p.put("berkas", kode);
    com.mongodb.DBCursor c = d.getD().getCollectionFromString("tracks").find(p);
    while (c.hasNext()) {
        com.mongodb.DBObject o = c.next();
        com.mongodb.BasicDBList l = (com.mongodb.BasicDBList) o.get("bin");
        String json = "";
        for (int x = 0; x < l.size(); x++)
            json += r.decrypt("" + l.get(x));
        v = new Tracks(json);
        break;
    }
    return v;
}

From source file:achmad.rifai.erp1.entity.Tugas.java

public static Tugas of(Db d, String kode) throws Exception {
    Tugas t = null;//from   w w  w  .ja va 2  s. c  o m
    achmad.rifai.erp1.util.RSA r = achmad.rifai.erp1.util.Work.loadRSA();
    com.mongodb.DBObject p = new com.mongodb.BasicDBObject();
    p.put("berkas", kode);
    com.mongodb.DBCursor c = d.getD().getCollection("tugas").find(p);
    while (c.hasNext()) {
        com.mongodb.DBObject o = c.next();
        com.mongodb.BasicDBList l = (com.mongodb.BasicDBList) o.get("bin");
        String json = "";
        for (int x = 0; x < l.size(); x++)
            json += r.decrypt("" + l.get(x));
        t = new Tugas(json);
        break;
    }
    return t;
}

From source file:achmad.rifai.erp1.Main.java

public static void main(String[] args) {
    try {/*w  w  w  .  j  a  v  a 2 s.co  m*/
        achmad.rifai.erp1.util.Db d = achmad.rifai.erp1.util.Work.loadDB();
        com.mongodb.DBCursor c = d.getD().getCollectionFromString("keluar").find();
        achmad.rifai.erp1.util.RSA r = achmad.rifai.erp1.util.Work.loadRSA();
        while (c.hasNext()) {
            d.getD().getCollectionFromString("keluar").remove(c.next());
        }
        d.close();
    } catch (Exception ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:act.installer.bing.BingSearchRanker.java

License:Open Source License

/**
 * This function writes the Bing Search ranks for a chunk of inchis in a TSV file, append only option.
 * @param inchis (Set<String>) set of InChI string representations
 * @param outputPath (String) path indicating the output file
 * @param appendOutput (Boolean) whether to append the results to the output file
 * @throws IOException//from   w  w  w .  j  a v a 2  s  . c om
 */
private void writeBingSearchRanksAsTSVForInchiChunk(Set<String> inchis, String outputPath, Boolean appendOutput)
        throws IOException {

    // Define headers
    List<String> bingRankerHeaderFields = new ArrayList<>();
    addChemicalHeaders(bingRankerHeaderFields);

    // Open TSV writer
    try (TSVWriter<String, String> tsvWriter = new TSVWriter<>(bingRankerHeaderFields)) {
        tsvWriter.open(new File(outputPath), appendOutput);

        int counter = 0;
        DBCursor cursor = mongoDB.fetchNamesAndUsageForInchis(inchis);

        // Iterate through the target chemicals
        while (cursor.hasNext()) {
            counter++;
            BasicDBObject o = (BasicDBObject) cursor.next();
            Map<String, String> row = new HashMap<>();
            updateRowWithChemicalInformation(o, row);
            tsvWriter.append(row);
            tsvWriter.flush();
        }
        LOGGER.info("Wrote %d Bing Search results to %s", counter, outputPath);
    }
}

From source file:act.installer.bing.BingSearchRanker.java

License:Open Source License

/**
 * This function is used to write out the conditional reachability results with data on target chemical, root chemical,
 * depth of steps from root to target chemical, the bing search results, all the other names associated with the target
 * and inchi of the target in a tsv file. This function is not scalable since it has to have an in-memory representation
 * of the target and root molecule's bing results to input the data into the TSV file.
 * @param descendantInchiToRootInchi mapping of chemical to its root chemical in the conditional reachability tree
 * @param depthOfPathFromRootToMolecule Since a chemical can be associated with only one root, there is a unique mapping between
 *                        the chemical and it's depth from the root. This structure holds that information.
 * @param outputPath The output path of the tsv file.
 * @throws IOException/* ww  w  .j  a  v a 2 s  .com*/
 */
public void writeBingSearchRanksAsTSVUsingConditionalReachabilityFormat(Set<String> inchisToProcess,
        Map<String, String> descendantInchiToRootInchi, Map<String, Integer> depthOfPathFromRootToMolecule,
        String outputPath) throws IOException {

    // Define headers
    List<String> bingRankerHeaderFields = new ArrayList<>();
    addChemicalHeaders(bingRankerHeaderFields);
    bingRankerHeaderFields.add(ConditionalReachabilityHeaderFields.DEPTH.name());
    bingRankerHeaderFields.add(ConditionalReachabilityHeaderFields.ROOT_MOLECULE_BEST_NAME.name());
    bingRankerHeaderFields.add(ConditionalReachabilityHeaderFields.TOTAL_COUNT_SEARCH_RESULTS_ROOT.name());
    bingRankerHeaderFields.add(ConditionalReachabilityHeaderFields.ROOT_INCHI.name());

    LOGGER.info("The total number of inchis are: %d", inchisToProcess.size());

    LOGGER.info("Creating mappings between inchi and it's DB object");
    DBCursor cursor = mongoDB.fetchNamesAndUsageForInchis(inchisToProcess);

    // TODO: We have to do an in-memory calculation of all the inchis since we need to pair up the descendant and root
    // db objects. This can take up a lot of memory.
    Map<String, BasicDBObject> inchiToDBObject = new HashMap<>();

    int cursorCounter = 0;
    while (cursor.hasNext()) {
        cursorCounter++;
        BasicDBObject o = (BasicDBObject) cursor.next();
        String inchi = o.get("InChI").toString();

        if (inchi == null) {
            LOGGER.error("Inchi could not be parsed.");
            continue;
        }

        inchiToDBObject.put(inchi, o);
    }

    LOGGER.info("The total number of inchis found in the db is: %d", cursorCounter);

    LOGGER.info("Going to write to TSV file.");
    try (TSVWriter<String, String> tsvWriter = new TSVWriter<>(bingRankerHeaderFields)) {
        tsvWriter.open(new File(outputPath));

        int counter = 0;

        for (String descendantInchi : descendantInchiToRootInchi.keySet()) {
            // Add all the descendant field results
            BasicDBObject descendentDBObject = inchiToDBObject.get(descendantInchi);
            if (descendentDBObject == null) {
                LOGGER.info("Could not find info on inchi %s", descendantInchi);
                continue;
            }

            // Add all descendant molecule fields
            Map<String, String> row = new HashMap<>();
            updateRowWithChemicalInformation(descendentDBObject, row);

            // Add all the root molecule fields
            String rootInchi = descendantInchiToRootInchi.get(descendantInchi);
            row.put(ConditionalReachabilityHeaderFields.ROOT_INCHI.name(), rootInchi);
            BasicDBObject rootDBObject = inchiToDBObject.get(rootInchi);
            if (rootDBObject != null) {
                BasicDBObject rootXref = (BasicDBObject) rootDBObject.get("xref");
                BasicDBObject rootBing = (BasicDBObject) rootXref.get("BING");
                BasicDBObject rootMetadata = (BasicDBObject) rootBing.get("metadata");

                String bestNameForRootMolecule = rootMetadata.get("best_name").toString();
                row.put(ConditionalReachabilityHeaderFields.ROOT_MOLECULE_BEST_NAME.name(),
                        bestNameForRootMolecule.equals("") ? rootInchi : bestNameForRootMolecule);

                row.put(ConditionalReachabilityHeaderFields.TOTAL_COUNT_SEARCH_RESULTS_ROOT.name(),
                        rootMetadata.get("total_count_search_results").toString());
            } else {
                row.put(ConditionalReachabilityHeaderFields.ROOT_MOLECULE_BEST_NAME.name(), rootInchi);
                row.put(ConditionalReachabilityHeaderFields.TOTAL_COUNT_SEARCH_RESULTS_ROOT.name(),
                        DEFAULT_COUNT.toString());
            }
            row.put(ConditionalReachabilityHeaderFields.DEPTH.name(),
                    depthOfPathFromRootToMolecule.get(descendantInchi).toString());

            tsvWriter.append(row);
            tsvWriter.flush();
            counter++;
        }

        LOGGER.info("Wrote %d rows to %s", counter, outputPath);
    }
}

From source file:act.server.MongoDB.java

License:Open Source License

public static P<P<List, List>, Map<Object, Object>> compare(String coll, String id_key, int thisport,
        int refport, boolean listsAreSet) throws UnknownHostException {
    String host = "localhost";
    String dbs = "actv01";

    List<Object> add = new ArrayList<Object>();
    List<Object> del = new ArrayList<Object>();
    Set<Object> seen = new HashSet<Object>();
    Map<Object, Object> upd = new HashMap<Object, Object>();

    DBCollection c = new Mongo(host, thisport).getDB(dbs).getCollection(coll);
    DBCollection cref = new Mongo(host, refport).getDB(dbs).getCollection(coll);

    // yes, we indeed need to iterate over the entire collection! so unrestricted find() ok here.
    DBCursor cur = c.find();
    while (cur.hasNext()) {
        DBObject doc = cur.next();//from www  .  ja va2  s .  co m
        Object id = doc.get(id_key);

        DBObject docref = findOneDoc(cref, id_key, id);
        if (docref == null) {
            // reference collection does not have doc, log as newly created
            add.add(id);
        } else {
            // reference collection has doc:
            // compare the differences between these two docs and log it as updated if they differ

            Object diff = compare(doc, docref, listsAreSet);
            if (diff != null) {
                // the docs differ. Log it as updated, and note the diff
                upd.put(id, diff);
            }
        }
        seen.add(id);
    }

    // now iterate over ref db and see if there are any docs deleted (i.e., not in notDeleted)
    DBCursor curref = c.find();
    while (curref.hasNext()) {
        DBObject doc = curref.next();
        Object id = doc.get(id_key);

        if (!seen.contains(id)) {
            // this doc was not seen in the updated collection, so deleted. log that
            del.add(id);
        }
    }

    return new P<P<List, List>, Map<Object, Object>>(new P<List, List>(add, del), upd);
}

From source file:act.server.MongoDB.java

License:Open Source License

public List<Long> getRxnsWith(Long reactant, Long product) {

    BasicDBObject query = new BasicDBObject();
    query.put("enz_summary.products.pubchem", product);
    query.put("enz_summary.substrates.pubchem", reactant);
    DBCursor cur = this.dbReactions.find(query);

    List<Long> reactions = new ArrayList<Long>();
    while (cur.hasNext()) {
        DBObject o = cur.next();// w  ww.j a  va2 s . c  om
        long id = (Integer) o.get("_id"); // checked: db type IS int
        reactions.add(id);
    }
    cur.close();
    return reactions;
}

From source file:act.server.MongoDB.java

License:Open Source License

public List<Reaction> getRxnsWithAll(List<Long> reactants, List<Long> products) {

    if (reactants.size() == 0 && products.size() == 0) {
        throw new IllegalArgumentException("Reactants and products both empty! Query would return entire DB.");
    }//from   w  w  w .  ja va 2  s .  co m
    BasicDBObject query = new BasicDBObject();

    if (!reactants.isEmpty()) {
        BasicDBList substrateIds = new BasicDBList();
        substrateIds.addAll(reactants);
        query.put("enz_summary.substrates.pubchem", new BasicDBObject("$all", substrateIds));
    }

    if (!products.isEmpty()) {
        BasicDBList productIds = new BasicDBList();
        productIds.addAll(products);
        query.put("enz_summary.products.pubchem", new BasicDBObject("$all", productIds));
    }

    DBCursor cur = this.dbReactions.find(query);
    List<Reaction> reactions = new ArrayList<Reaction>();

    try {
        while (cur.hasNext()) {
            DBObject o = cur.next();
            reactions.add(convertDBObjectToReaction(o));
        }
    } finally {
        cur.close();
    }

    return reactions;
}