Example usage for com.mongodb MongoClient getDB

List of usage examples for com.mongodb MongoClient getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:br.bireme.scl.SendEmailToCcs.java

License:Open Source License

public void sendEmails(final String toFile, final String host, final int port, final String user,
        final String password, final String database, final String collection) throws UnknownHostException {
    if (toFile == null) {
        throw new NullPointerException("toFile");
    }//from w  w  w .ja v a2 s  . co  m
    if (host == null) {
        throw new NullPointerException("host");
    }
    if (port <= 0) {
        throw new IllegalArgumentException("port <= 0");
    }
    if (database == null) {
        throw new NullPointerException("database");
    }
    if (collection == null) {
        throw new NullPointerException("collection");
    }

    final Map<String, CcProfile> profiles = getProfiles(toFile);
    final MongoClient mongoClient = new MongoClient(host, port);
    final DB db = mongoClient.getDB(database);
    if (user != null) {
        final boolean auth = db.authenticate(user, password.toCharArray());
        if (!auth) {
            throw new IllegalArgumentException("invalid user/password");
        }
    }
    final DBCollection coll = db.getCollection(collection);

    prepareEmails(coll, profiles);
}

From source file:br.bireme.scl.ShowBrokenLinks.java

License:Open Source License

public ShowBrokenLinks(final String host, final int port, final String user, final String password,
        final String database, final String collection) throws UnknownHostException {
    if (host == null) {
        throw new NullPointerException("host");
    }/*from   w  w w . j a  v a2s  .c o m*/
    if (port <= 0) {
        throw new IllegalArgumentException("port=" + port);
    }
    if (database == null) {
        throw new NullPointerException("database");
    }
    if (collection == null) {
        throw new NullPointerException("collection");
    }

    final MongoClient mongoClient = new MongoClient(host, port);
    final DB db = mongoClient.getDB(database);
    if (user != null) {
        final boolean auth = db.authenticate(user, password.toCharArray());
        if (!auth) {
            throw new IllegalArgumentException("invalid user/password");
        }
    }
    coll = db.getCollection(collection);
}

From source file:br.bireme.scl.ShowFixedLinks.java

License:Open Source License

public ShowFixedLinks(final String host, final int port, final String user, final String password,
        final String database, final String collection) throws UnknownHostException {
    if (host == null) {
        throw new NullPointerException("host");
    }//from w w w. j a  v a2s  .  com
    if (port <= 0) {
        throw new IllegalArgumentException("port=" + port);
    }
    if (database == null) {
        throw new NullPointerException("database");
    }
    if (collection == null) {
        throw new NullPointerException("collection");
    }

    final MongoClient mongoClient = new MongoClient(host, port);
    final DB db = mongoClient.getDB(database);
    if (user != null) {
        final boolean auth = db.authenticate(user, password.toCharArray());
        if (!auth) {
            throw new IllegalArgumentException("invalid user/password");
        }
    }
    coll = db.getCollection(collection);
}

From source file:br.bireme.scl.TabulateField.java

License:Open Source License

public static void tabulate(final String host, final int port, final String user, final String password,
        final String database, final String collection, final String path) throws UnknownHostException {
    if (host == null) {
        throw new NullPointerException("host");
    }/*www .ja  v a2  s  .  c o m*/
    if (port <= 0) {
        throw new IllegalArgumentException("port <= 0");
    }
    if (collection == null) {
        throw new NullPointerException("collection");
    }
    if (path == null) {
        throw new NullPointerException("path");
    }
    final MongoClient mongoClient = new MongoClient(host, port);
    final DB db = mongoClient.getDB(database);
    if (user != null) {
        final boolean auth = db.authenticate(user, password.toCharArray());
        if (!auth) {
            throw new IllegalArgumentException("invalid user/password");
        }
    }
    final DBCollection coll = db.getCollection(collection);
    final DBCursor cursor = coll.find();
    final TreeMap<String, Integer> map1 = new TreeMap<String, Integer>();
    final TreeMap<Integer, String> map2 = new TreeMap<Integer, String>();
    final int ADD_VALUE = 99999999;
    final int size = cursor.size();

    while (cursor.hasNext()) {
        final BasicDBObject doc = (BasicDBObject) cursor.next();
        final String key = getValue(doc, path);

        if (key != null) {
            Integer val = map1.get(key);
            if (val == null) {
                val = 0;
            }
            val += 1;
            map1.put(key, val);
        }
    }
    cursor.close();
    for (Map.Entry<String, Integer> entry : map1.entrySet()) {
        map2.put(ADD_VALUE - entry.getValue(), entry.getKey());
    }

    System.out.println("Total # of docs: " + size + "\n");

    int idx = 0;
    for (Map.Entry<Integer, String> entry : map2.entrySet()) {
        final int val = ADD_VALUE - entry.getKey();
        final String percent = String.format("%.2f", ((float) val / size) * 100);
        System.out.println((++idx) + ") " + entry.getValue() + ": " + val + " (" + percent + "%)");
    }
}

From source file:br.bireme.scl.UndoUpdate.java

License:Open Source License

private static void undo(final String host, final int port, final String database, final String fromDate,
        final String docId) throws UnknownHostException, ParseException {
    assert host != null;
    assert port > 0;
    assert database != null;
    assert (fromDate != null || docId != null);

    final MongoClient client = new MongoClient(host, port);
    final DB db = client.getDB(database);
    final DBCollection from_coll = db.getCollection(HISTORY_COL);
    final DBCollection to_coll = db.getCollection(BROKEN_LINKS_COL);
    final String prefix = ELEM_LST_FIELD + ".0.";
    final BasicDBObject query;

    if (fromDate == null) {
        query = new BasicDBObject("_id", docId);
    } else {//from w  w w.jav a2  s  .c o  m
        final SimpleDateFormat simple = new SimpleDateFormat(
                "yyyyMMdd" + (fromDate.contains(":") ? "-HH:mm:ss" : ""));
        final Date fDate = simple.parse(fromDate);
        query = new BasicDBObject(prefix + LAST_UPDATE_FIELD, new BasicDBObject("$gte", fDate));
    }
    final DBCursor cursor = from_coll.find(query);
    int total = 0;
    int reverted = 0;

    System.out.println("host=" + host);
    System.out.println("port=" + port);
    System.out.println("database=" + database);
    if (fromDate == null) {
        System.out.println("doc id=" + docId);
    } else {
        System.out.println("from date=" + fromDate);
    }
    System.out.println("found=" + cursor.size());

    while (cursor.hasNext()) {
        final BasicDBObject doc_from = (BasicDBObject) cursor.next();
        final BasicDBObject doc_to = new BasicDBObject(doc_from);
        final String id = doc_from.getString(ID_FIELD);
        final BasicDBList list = (BasicDBList) doc_from.get(ELEM_LST_FIELD);
        final BasicDBObject elem = (BasicDBObject) list.get(0);
        final Date date = elem.getDate(LAST_UPDATE_FIELD);

        doc_to.put(LAST_UPDATE_FIELD, date);
        doc_to.put(BROKEN_URL_FIELD, elem.getString(BROKEN_URL_FIELD));
        doc_to.put(MSG_FIELD, elem.getString(MSG_FIELD));
        doc_to.put(CENTER_FIELD, elem.get(CENTER_FIELD));
        doc_to.removeField(ELEM_LST_FIELD);

        final WriteResult wr = to_coll.save(doc_to, WriteConcern.ACKNOWLEDGED);
        if (wr.getCachedLastError().ok()) {
            final WriteResult wr2 = from_coll.remove(doc_from, WriteConcern.ACKNOWLEDGED);
            if (wr2.getCachedLastError().ok()) {
                reverted++;
                System.out.println("+++id=" + id + " date=" + date);
            } else {
                System.err.println("Document[" + id + "] delete error.");
            }
        } else {
            System.err.println("Document[" + id + "] update error.");
        }
        total++;
    }
    cursor.close();

    System.out.println("total/undo: " + total + "/" + reverted);
}

From source file:br.bireme.tmp.AddPrettyField.java

License:Open Source License

private static void add(final String mongo_host, final int mongo_port, final String mongo_db,
        final String mongo_col) throws UnknownHostException {
    assert mongo_host != null;
    assert mongo_port > 0;
    assert mongo_db != null;
    assert mongo_col != null;

    final MongoClient client = new MongoClient(mongo_host, mongo_port);
    final DB db = client.getDB(mongo_db);
    final DBCollection coll = db.getCollection(HISTORY_COL);
    final DBCursor cursor = coll.find();
    int total = 0;

    System.out.println("host=" + mongo_host);
    System.out.println("port=" + mongo_port);
    System.out.println("database=" + mongo_db);
    System.out.println("collection=" + mongo_col);
    System.out.println("num of documents=" + cursor.size());

    while (cursor.hasNext()) {
        final BasicDBObject doc1 = (BasicDBObject) cursor.next();
        final BasicDBList list = (BasicDBList) doc1.get(ELEM_LST_FIELD);

        for (Object obj : list) {
            final BasicDBObject doc2 = (BasicDBObject) obj;
            if (!doc2.containsField(PRETTY_BROKEN_URL_FIELD)) {
                final String burl = doc2.getString(BROKEN_URL_FIELD);
                try {
                    final String pburl = EncDecUrl.decodeUrl(burl);
                    doc2.append(PRETTY_BROKEN_URL_FIELD, pburl);

                    final WriteResult wr = coll.save(doc1, WriteConcern.ACKNOWLEDGED);
                    if (wr.getCachedLastError().ok()) {
                        total++;// www .j  a v  a2 s  .  c  om
                    } else {
                        System.err.println("Document[" + doc1.getString("_id") + "] update error.");
                    }
                } catch (IOException ioe) {
                    System.err.println("Document[" + doc1.getString("_id") + "] bad encode conversion"
                            + " url=[" + burl + "]");
                }
            }
        }
    }
    cursor.close();
    System.out.println("num of added fields: " + total);
}

From source file:br.bireme.web.AuthenticationServlet.java

License:Open Source License

@Override
public void init() {
    try {/*from  www  .j  av  a 2 s .  c  o  m*/
        final ServletContext context = getServletContext();
        final String host = context.getInitParameter("host");
        final int port = Integer.parseInt(context.getInitParameter("port"));
        final String user = context.getInitParameter("username");
        final String password = context.getInitParameter("password");
        final MongoClient mongoClient = new MongoClient(host, port);
        final DB db = mongoClient.getDB(SOCIAL_CHECK_DB);

        if (!user.trim().isEmpty()) {
            final boolean auth = db.authenticate(user, password.toCharArray());
            if (!auth) {
                throw new IllegalArgumentException("invalid user/password");
            }
        }

        final DBCollection coll = db.getCollection(BROKEN_LINKS_COL);
        final DBCollection hcoll = db.getCollection(HISTORY_COL);
        final Set<String> databases = MongoOperations.getDatabases(coll);

        context.setAttribute("userEmail", user.trim());
        context.setAttribute("collection", coll);
        context.setAttribute("historycoll", hcoll);
        context.setAttribute("readOnlyMode", false);
        context.setAttribute("databases", databases);
    } catch (Exception ex) {
        Logger.getLogger(AuthenticationServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:br.com.teste.mongo.MongoTeste.java

public DB connect() {
    DB db = null;/*from  w  w w .  ja v a2  s . com*/
    try {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        db = mongoClient.getDB("testeTesseract");

    } catch (UnknownHostException ex) {
        Logger.getLogger(MongoTeste.class.getName()).log(Level.SEVERE, null, ex);
    }
    return db;
}

From source file:br.gov.frameworkdemoiselle.component.audit.processors.rest.MONGOProcessors.java

License:Open Source License

/**
 *
 * @param trail/*w  w w . ja v  a  2s. c o  m*/
 */
@Override
public void execute(@Observes @AuditProcessor Trail trail) {

    super.execute(trail);

    try {
        //TODO Verificar alternativas para superar a depreciao das classes abaixo
        MongoClient mongo = new MongoClient(config.getServerUrl());
        DB db = mongo.getDB(config.getDataBaseName());
        DBCollection table = db.getCollection(config.getTableName());

        BasicDBObject document = new BasicDBObject();
        document.put("ClassName", trail.getClassName());
        document.put("How", trail.getHow());
        document.put("IdName", trail.getIdName());
        document.put("LayerName", trail.getLayerName());
        document.put("ObjSerial", trail.getObjSerial());
        document.put("ProcessorName", trail.getProcessorName());
        document.put("Profile", trail.getProfile());
        document.put("SystemName", trail.getSystemName());
        document.put("UserName", trail.getUserName());
        document.put("What", trail.getWhat());
        document.put("When", trail.getWhen());
        document.put("Where", trail.getWhere());
        table.insert(document);

    } catch (Exception e) {
        fail("MONGOProcessors :" + e.getMessage(), trail);
    }
}

From source file:br.gov.frameworkdemoiselle.component.billing.processors.mongo.MONGOProcessors.java

License:Open Source License

/**
 *
 * @param trail//  w  w w  .ja  v  a  2s .  co m
 */
@Override
public void execute(@Observes @BillingProcessor Trail trail) {

    super.execute(trail);

    try {
        //TODO Verificar alternativas para superar a depreciao das classes abaixo
        MongoClient mongo = new MongoClient(config.getServerUrl());
        DB db = mongo.getDB(config.getDataBaseName());
        Boolean authentication = Boolean.TRUE;

        if (!"".equals(config.getDatabaseUser())) {
            authentication = db.authenticate(config.getDatabaseUser(), config.getDatabasePass().toCharArray());
        }

        if (authentication) {
            DBCollection table = db.getCollection(config.getCollectionName());

            BasicDBObject document = new BasicDBObject();
            document.put("ClassName", trail.getClassName());
            document.put("IdName", trail.getIdName());
            document.put("LayerName", trail.getLayerName());
            document.put("ObjSerial", trail.getObjSerial());
            document.put("ProcessorName", trail.getProcessorName());
            document.put("Profile", trail.getProfile());
            document.put("SystemName", trail.getSystemName());
            document.put("UserName", trail.getUserName());
            document.put("What", trail.getWhat());
            document.put("When", trail.getWhen());
            document.put("Where", trail.getWhere());
            table.insert(document);
        } else {
            fail("MONGOProcessors : Authentication failed!", trail);
            throw new RuntimeException("Authentication failed!");
        }

    } catch (Exception e) {
        fail("MONGOProcessors :" + e.getMessage(), trail);
    }
}