Example usage for com.mongodb BasicDBList BasicDBList

List of usage examples for com.mongodb BasicDBList BasicDBList

Introduction

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

Prototype

BasicDBList

Source Link

Usage

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

License:Open Source License

public static boolean updateDocument(final DBCollection coll, final DBCollection hcoll, final String docId,
        final String fixedUrl, final String user, final boolean automatic) throws IOException {
    if (coll == null) {
        throw new NullPointerException("coll");
    }//  www.  ja  va  2  s .  com
    if (hcoll == null) {
        throw new NullPointerException("hcoll");
    }
    if (docId == null) {
        throw new NullPointerException("docId");
    }
    if (fixedUrl == null) {
        throw new NullPointerException("fixedUrl");
    }
    if (user == null) {
        throw new NullPointerException("user");
    }
    if (fixedUrl.length() >= 900) {
        throw new IOException("fixedUrl is too long >= 900. [" + fixedUrl + "]");
    }

    final BasicDBObject query = new BasicDBObject(ID_FIELD, docId);
    final BasicDBObject doc = (BasicDBObject) coll.findOne(query);

    if (doc == null) {
        throw new IOException("document not found id[" + docId + "]");
    }

    final BasicDBList lsthdoc;
    BasicDBObject hdoc = (BasicDBObject) hcoll.findOne(query);
    if (hdoc == null) {
        hdoc = new BasicDBObject();
        hdoc.append(ID_FIELD, docId);
        hdoc.append(MST_FIELD, (String) doc.get(MST_FIELD));
        hdoc.append(DATE_FIELD, (Date) doc.get(DATE_FIELD));
        lsthdoc = new BasicDBList();
        hdoc.append(ELEM_LST_FIELD, lsthdoc);
    } else {
        lsthdoc = (BasicDBList) hdoc.get(ELEM_LST_FIELD);
    }

    final String brokenUrl = doc.getString(BROKEN_URL_FIELD);
    final String brokenUrl_D = EncDecUrl.decodeUrl(brokenUrl);
    final String fixedUrl_E = EncDecUrl.encodeUrl(fixedUrl, CODEC, false);
    //final String fixedUrl_D = EncDecUrl.decodeUrl(fixedUrl);
    final BasicDBObject hcurdoc = new BasicDBObject();
    hcurdoc.append(BROKEN_URL_FIELD, brokenUrl).append(PRETTY_BROKEN_URL_FIELD, brokenUrl_D)
            .append(FIXED_URL_FIELD, fixedUrl_E).append(MSG_FIELD, (String) doc.get(MSG_FIELD))
            .append(CENTER_FIELD, (BasicDBList) doc.get(CENTER_FIELD)).append(AUTO_FIX_FIELD, automatic)
            .append(EXPORTED_FIELD, false).append(LAST_UPDATE_FIELD, new Date()).append(USER_FIELD, user);

    lsthdoc.add(0, hcurdoc);

    final boolean ret1 = coll.remove(doc, WriteConcern.ACKNOWLEDGED).getLastError().ok();
    final boolean ret2 = hcoll.save(hdoc).getLastError().ok();

    return ret1 && ret2;
}

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

License:Open Source License

private static void reset(final String host, final int port, final String database, final String collection,
        final String sdate) throws UnknownHostException, ParseException {
    assert host != null;
    assert port > 0;
    assert database != null;
    assert collection != null;

    final MongoClient client = new MongoClient(host, port);
    final DB db = client.getDB(database);
    final DBCollection coll = db.getCollection(collection);
    final String prefix = ELEM_LST_FIELD + ".0.";
    final BasicDBObject query;
    final DBCursor cursor;

    if (sdate == null) {
        query = new BasicDBObject(prefix + EXPORTED_FIELD, true);
    } else {/*from w w  w  .j  a v a 2s .c o  m*/
        final SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd");
        final Date date = simple.parse(sdate);

        final BasicDBList list = new BasicDBList();
        list.add(new BasicDBObject(prefix + EXPORTED_FIELD, true));
        list.add(new BasicDBObject(prefix + LAST_UPDATE_FIELD, new BasicDBObject("$gte", date)));
        query = new BasicDBObject("$and", list);
    }
    cursor = coll.find(query);

    while (cursor.hasNext()) {
        final BasicDBObject doc = (BasicDBObject) cursor.next();
        final BasicDBList list = (BasicDBList) doc.get(ELEM_LST_FIELD);
        final BasicDBObject elem = (BasicDBObject) list.get(0);
        elem.put(EXPORTED_FIELD, false);
        coll.save(doc);
    }
    cursor.close();
}

From source file:br.com.binarti.simplesearchexpr.builders.mongodb.SimpleSearchMongoDBBuilder.java

License:Open Source License

private BasicDBList mongoList(Collection<Object> collection) {
    BasicDBList result = new BasicDBList();
    for (Object o : collection) {
        result.add(o);/*from   ww w.  j a  v a  2 s. co  m*/
    }
    return result;
}

From source file:bruma.tools.Isis2Mongo.java

License:Open Source License

private static BasicDBObject createDocument(final Record rec, final int idTag,
        final Map<Integer, ZeFDT.ZeFDTField> mfdt, final boolean useOnlyFDT) throws BrumaException {
    assert rec != null;
    assert idTag > 0;

    final BasicDBObject doc = new BasicDBObject();
    final Map<Integer, List<Field>> flds = new TreeMap<Integer, List<Field>>();
    if (idTag <= 0) {
        doc.put("_id", rec.getMfn());
    } else {//ww  w  . j  ava  2  s .c  o  m
        final ZeFDT.ZeFDTField fdt = (mfdt == null) ? null : mfdt.get(idTag);
        final boolean isNumeric = (fdt == null) ? false : fdt.isNumeric();
        final String id = rec.getField(idTag, 1).getContent();

        if (isNumeric) {
            doc.put("_id", Integer.valueOf(id));
        } else {
            doc.put("_id", id);
        }
    }

    doc.put("nvf", rec.getNvf());

    for (Field fld : rec) {
        List<Field> fldLst = flds.get(fld.getId());
        if (fldLst == null) {
            fldLst = new ArrayList<Field>();
            flds.put(fld.getId(), fldLst);
        }
        fldLst.add(fld);
    }
    for (Map.Entry<Integer, List<Field>> entry : flds.entrySet()) {
        final int tag = entry.getKey();
        final ZeFDT.ZeFDTField fdt = (mfdt == null) ? null : mfdt.get(tag);
        final String auxId = (fdt == null) ? null : fdt.getDescription();
        final boolean isNumeric = (fdt == null) ? false : fdt.isNumeric();
        final String fid = (auxId == null) ? (useOnlyFDT ? null : "v" + tag) : auxId;

        if (fid != null) {
            final List<Field> fields = entry.getValue();
            final int size = fields.size();

            if (size == 1) {
                if (isNumeric) {
                    doc.put(fid, getNumSubfields(fields.get(0)));
                } else {
                    doc.put(fid, getStrSubfields(fields.get(0)));
                }
            } else if (size > 1) {
                final BasicDBList objL = new BasicDBList();
                for (Field fld : fields) {
                    if (isNumeric) {
                        objL.add(getNumSubfields(fld));
                    } else {
                        objL.add(getStrSubfields(fld));
                    }
                }
                doc.put(fid, objL);
            }
        }
    }

    return doc;
}

From source file:bruma.tools.Isis2Mongo.java

License:Open Source License

private static BasicDBObject getStrSubfields(final Field fld) {
    final BasicDBObject obj = new BasicDBObject();
    final Map<Character, List<String>> map = new HashMap<Character, List<String>>();

    for (Subfield sub : fld) {
        final char id = sub.getId();
        final List<String> lst;

        if (map.containsKey(id)) {
            lst = map.get(id);/*from ww  w . j  a v a 2s.c  om*/
        } else {
            lst = new ArrayList<String>();
            map.put(id, lst);
        }
        lst.add(sub.getContent());
    }
    for (Map.Entry<Character, List<String>> entry : map.entrySet()) {
        final String id = entry.getKey().toString();
        final List<String> lst = entry.getValue();

        if (lst.size() == 1) {
            obj.put(id, lst.get(0));
        } else {
            final BasicDBList bobj = new BasicDBList();
            for (String elem : lst) {
                bobj.add(elem);
            }
            obj.put(id, bobj);
        }
    }

    return obj;
}

From source file:bruma.tools.Isis2Mongo.java

License:Open Source License

private static BasicDBObject getNumSubfields(final Field fld) {
    final BasicDBObject obj = new BasicDBObject();
    final Map<Character, List<Integer>> map = new HashMap<Character, List<Integer>>();

    for (Subfield sub : fld) {
        final char id = sub.getId();
        final List<Integer> lst;

        if (map.containsKey(id)) {
            lst = map.get(id);//from w  w w  . j  a  v a 2 s . c om
        } else {
            lst = new ArrayList<Integer>();
            map.put(id, lst);
        }
        lst.add(Integer.valueOf(sub.getContent()));
    }
    for (Map.Entry<Character, List<Integer>> entry : map.entrySet()) {
        final String id = entry.getKey().toString();
        final List<Integer> lst = entry.getValue();

        if (lst.size() == 1) {
            obj.put(id, lst.get(0));
        } else {
            final BasicDBList bobj = new BasicDBList();
            for (Integer elem : lst) {
                bobj.add(elem);
            }
            obj.put(id, bobj);
        }
    }

    return obj;
}

From source file:ch.agent.crnickl.mongodb.WriteMethodsForSchema.java

License:Apache License

private BasicDBList attributeDefinitions(Collection<AttributeDefinition<?>> defs) throws T2DBException {
    BasicDBList list = new BasicDBList();
    if (defs != null) {
        int i = 0;
        for (AttributeDefinition<?> def : defs) {
            list.put(i++, attributeDefinition(def));
        }//  www . j  av  a2  s. c  o  m
    }
    return list;
}

From source file:ch.agent.crnickl.mongodb.WriteMethodsForSchema.java

License:Apache License

private BasicDBList seriesDefinitions(Collection<SeriesDefinition> defs) throws T2DBException {
    BasicDBList list = new BasicDBList();
    if (defs != null) {
        int i = 0;
        for (SeriesDefinition def : defs) {
            list.put(i++, seriesDefinition(def));
        }//  w ww  . j  av a 2  s  .c  om
    }
    return list;
}

From source file:ch.hslu.dmg.InitDB.java

public void initStuds() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> stud = database.getCollection("studenten");
    stud.drop();/* w  w  w .j  ava2  s.  co  m*/

    ArrayList<Document> studs = new ArrayList<>();
    studs.add(new Document("Legi", 24002).append("Name", "Xenokrates").append("Semester", 18));
    studs.add(
            new Document("Legi", 25403).append("Name", "Jonas").append("Semester", 12).append("Hoeren", 5022));
    studs.add(
            new Document("Legi", 26120).append("Name", "Fichte").append("Semester", 10).append("Hoeren", 5001));
    studs.add(new Document("Legi", 26830).append("Name", "Aristoxenos").append("Semester", 8));
    BasicDBList hoeren = new BasicDBList();
    hoeren.add(5001);
    hoeren.add(4052);
    studs.add(new Document("Legi", 27550).append("Name", "Schopenhauer").append("Semester", 6).append("Hoeren",
            hoeren));
    BasicDBList hoeren2 = new BasicDBList();
    hoeren2.add(5041);
    hoeren2.add(5052);
    hoeren2.add(5216);
    hoeren2.add(5259);
    studs.add(new Document("Legi", 28106).append("Name", "Carnap").append("Semester", 3).append("Hoeren",
            hoeren2));
    BasicDBList hoeren3 = new BasicDBList();
    hoeren3.add(5001);
    hoeren3.add(5041);
    hoeren3.add(5049);
    studs.add(new Document("Legi", 29120).append("Name", "Theophrastos").append("Semester", 2).append("Hoeren",
            hoeren3));
    BasicDBList hoeren4 = new BasicDBList();
    hoeren4.add(5022);
    hoeren4.add(5001);
    studs.add(new Document("Legi", 29555).append("Name", "Feuerbach").append("Semester", 2).append("Hoeren",
            hoeren4));
    stud.insertMany(studs);

    mongo.close();
}

From source file:ch.hslu.dmg.InitDB.java

public void initVorlesungen() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> vorlesung = database.getCollection("vorlesungen");
    vorlesung.drop();//from   w  ww.ja va  2  s . com

    ArrayList<Document> vorlesungen = new ArrayList<>();
    vorlesungen.add(new Document("VorlNr", 5001).append("Titel", "Grundzge").append("SWS", 4)
            .append("GelesenVon", 2137));
    vorlesungen.add(
            new Document("VorlNr", 5041).append("Titel", "Ethik").append("SWS", 4).append("GelesenVon", 2125));
    vorlesungen.add(new Document("VorlNr", 5043).append("Titel", "Erkentnistheorie").append("SWS", 3)
            .append("GelesenVon", 2126));
    vorlesungen.add(new Document("VorlNr", 5049).append("Titel", "Maeeutik").append("SWS", 2)
            .append("GelesenVon", 2125));
    vorlesungen.add(
            new Document("VorlNr", 4052).append("Titel", "Logik").append("SWS", 4).append("GelesenVon", 2125));
    vorlesungen.add(new Document("VorlNr", 5052).append("Titel", "Wissenschaftstheorie").append("SWS", 3)
            .append("GelesenVon", 2126));
    vorlesungen.add(new Document("VorlNr", 5216).append("Titel", "Bioethik").append("SWS", 2)
            .append("GelesenVon", 2126));
    vorlesungen.add(new Document("VorlNr", 5259).append("Titel", "Der Wiener Kreis").append("SWS", 2)
            .append("GelesenVon", 2133));
    vorlesungen.add(new Document("VorlNr", 5022).append("Titel", "Glaube und Wissen").append("SWS", 2)
            .append("GelesenVon", 2134));
    vorlesungen.add(new Document("VorlNr", 4630).append("Titel", "Die 3 Kritiken").append("SWS", 4)
            .append("GelesenVon", 2137));

    vorlesung.insertMany(vorlesungen);

    vorlesung.updateOne(eq("VorlNr", 5041), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5001)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5043), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5001)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5049), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5001)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5216), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5041)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5259), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5052)).iterator().next().get("_id"))));
    BasicDBList voraussetzungen = new BasicDBList();
    voraussetzungen.add(vorlesung.find(eq("VorlNr", 5043)).iterator().next().get("_id"));
    voraussetzungen.add(vorlesung.find(eq("VorlNr", 5041)).iterator().next().get("_id"));
    vorlesung.updateOne(eq("VorlNr", 5052),
            new Document("$set", new Document("Voraussetzung", voraussetzungen)));

    mongo.close();
}