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:act.shared.helpers.XMLToImportantChemicals.java

License:Open Source License

private void putElemOrList(DBObject json, String tag, Object add) {
    // if tag already present then make it a list
    if (json.containsField(tag)) {
        BasicDBList l;/*from ww w.j  a v  a 2  s .  c om*/
        Object already = json.get(tag);
        if (already instanceof BasicDBList) {
            l = (BasicDBList) already;
        } else {
            l = new BasicDBList();
            l.add(already);
        }
        l.add(add);
        json.removeField(tag);
        json.put(tag, l);
        return;
    }

    // else, just add as is
    json.put(tag, add);
    return;
}

From source file:ARS.DBManager.java

public void insertPlane(Plane pObj) {
    try {//  w w w  . j a v a 2 s  . com
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        //Connecting

        MongoDatabase db = mongoClient.getDatabase("ars");
        System.out.println("Connecting to the db...");
        MongoCollection collection = db.getCollection("plane"); //Choosing the collection to insert
        System.out.println(collection);

        List<Object> seatDBList = new BasicDBList();

        Document planeObj = new Document();
        planeObj.put("pNo", 2);
        planeObj.put("flightNo", 2);
        planeObj.put("Type", "Airbus A380");
        for (Seat seatList : pObj.getSeatVector()) //TODO: Change it
        {
            DBObject seatDBObject = new BasicDBObject();
            seatDBObject.put("sNo", seatList.getSeatNumber());
            seatDBObject.put("isEmpty", seatList.getIsEmpty());
            seatDBObject.put("isEconomy", seatList.getIsEconomy());
            seatDBList.add(seatDBObject);
        }
        planeObj.put("Seats", seatDBList);
        collection.insertOne(planeObj);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:at.ac.tuwien.dsg.smartcom.services.dao.MongoDBMessageInfoDAO.java

License:Apache License

BasicDBObject serializeMessageInfo(MessageInformation mi) {
    BasicDBObject doc = new BasicDBObject();
    doc.append("_id", serializeKey(mi.getKey()));
    doc.append("purpose", mi.getPurpose());
    doc.append("validAnswer", mi.getValidAnswer());

    BasicDBList types = new BasicDBList();
    for (MessageInformation.Key key : mi.getValidAnswerTypes()) {
        types.add(serializeKey(key));/*from   w  ww  .  ja  v a 2 s  .  c  om*/
    }
    doc.append("validAnswerTypes", types);

    BasicDBList rel = new BasicDBList();
    for (MessageInformation.Key key : mi.getRelatedMessages()) {
        rel.add(serializeKey(key));
    }
    doc.append("relatedMessages", rel);

    log.trace("Saving message info in mongoDB: {}", doc);
    return doc;
}

From source file:at.oneminutedistraction.mongodbrealm.UserCollection.java

private BasicDBObject create(String username, String password, String... groupList) {
    BasicDBObject user = create(username, password);

    BasicDBList groups = new BasicDBList();
    groups.addAll(Arrays.asList(groupList));
    user.append(ATTR_GROUPS, groups);//  w w  w.j  a  v a2 s.c om

    return (user);
}

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

License:Open Source License

/**
 * Creates the CC Fields Collection and insert a lilacs metadoc document.
 * @param coll CC Fields Collection//from  w w w .ja  va  2s .c o m
 * @return true if ok, false if error
 */
private static boolean createCcFieldsCollection(final DBCollection coll) {
    assert coll != null;

    final BasicDBObject doc = new BasicDBObject();
    final BasicDBList lst = new BasicDBList();

    lst.add(1);
    lst.add(920);
    lst.add(930);
    doc.put(MST_FIELD, "LILACS");
    doc.put(ID_TAG_FIELD, 2);
    doc.put(URL_TAG_FIELD, 8);
    doc.put(CC_TAGS_FIELD, lst);

    final WriteResult ret = coll.save(doc, WriteConcern.ACKNOWLEDGED);

    return ret.getCachedLastError().ok();
}

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

License:Open Source License

private static BasicDBList getCCS(final Record rec, final List<Integer> ccsFlds)
        throws BrumaException, IOException {
    assert rec != null;
    assert ccsFlds != null;

    final BasicDBList lst = new BasicDBList();
    final Set<String> set = new HashSet<String>();

    for (int ccsFld : ccsFlds) {
        final List<Field> fldList = rec.getFieldList(ccsFld);

        for (Field fld : fldList) {
            final Subfield sub = fld.getSubfield('_', 1);
            if (sub == null) {
                throw new IOException("Missing subfield. Mfn=" + rec.getMfn() + " tag=" + fld.getId());
            }//from w ww. java2 s .  c o m
            set.add(sub.getContent().trim());
        }
    }
    for (String elem : set) {
        lst.add(elem);
    }

    return lst;
}

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

License:Open Source License

private DBCollection getTitle(final String mstName, final String mstEncoding, final int indexTag,
        final int returnTag) throws UnknownHostException, BrumaException {
    assert mstName != null;
    assert indexTag > 0;
    assert returnTag > 0;

    final DBCollection coll = db.getCollection(COL_NAME);
    coll.ensureIndex(new BasicDBObject(INDEX_TAG, 1));

    final Master mst = MasterFactory.getInstance(mstName).setEncoding(mstEncoding).open();
    for (Record rec : mst) {
        if (rec.isActive()) {
            final int mfn = rec.getMfn();
            final List<Field> lst1 = rec.getFieldList(indexTag);
            if (!lst1.isEmpty()) {
                final List<Field> lst2 = rec.getFieldList(returnTag);
                if (!lst2.isEmpty()) {
                    final BasicDBList dblist1 = new BasicDBList();
                    for (Field fld1 : lst1) {
                        final String str = removeAccents(fld1.getContent()).toUpperCase();
                        dblist1.add(str);
                    }// w w w.ja  v  a  2  s. co m
                    final BasicDBList dblist2 = new BasicDBList();
                    for (Field fld2 : lst2) {
                        dblist2.add(fld2.getContent());
                    }
                    coll.insert(new BasicDBObject(INDEX_TAG, dblist1).append(RETURN_TAG, dblist2).append("mfn",
                            mfn));
                }
            }
        }
    }
    mst.close();

    return coll;
}

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

License:Open Source License

/**
 * Obtem uma lista com objetos IdUrl obtidos da base de dados MongoDb
 * @param coll coleo onde esto as urls/*from   w w w . j a  v a  2s .c  o m*/
 * @param centerIds filtro dos centros colaboradores desejados. Nunca  nulo
 * @param filter se no nulo filtra as urls com um cc especfico
 * @param from indice inicial da lista a ser recuperado. Comea de 1.
 * @param count numero de elementos a serem devolvidos
 * @param ascendingOrder se retorna por ordem de data ascendente ou descendente
 * @return lista de objetos IdUrl
 */
private static List<IdUrl> getCenterUrls(final DBCollection coll, final Set<String> centerIds,
        final String filter, final int from, final int count, final boolean ascendingOrder) {
    if (coll == null) {
        throw new NullPointerException("coll");
    }
    if (centerIds == null) {
        throw new NullPointerException("centerIds");
    }
    if (centerIds.isEmpty()) {
        throw new IllegalArgumentException("empty centerIds");
    }
    if (from < 1) {
        throw new IllegalArgumentException("from[" + from + "] < 1");
    }
    if (count < 1) {
        throw new IllegalArgumentException("count[" + count + "] < 1");
    }
    //final Set<IdUrl> lst = new TreeSet<IdUrl>();
    final List<IdUrl> lst = new ArrayList<IdUrl>();
    final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    final BasicDBObject sort = new BasicDBObject(DATE_FIELD, ascendingOrder ? 1 : -1);

    if (filter == null) {
        final BasicDBList cclst = new BasicDBList();

        for (String centerId : centerIds) {
            cclst.add(centerId);
        }
        final BasicDBObject in = new BasicDBObject("$in", cclst);
        final BasicDBObject query = new BasicDBObject(CENTER_FIELD, in);
        //final DBCursor cursor = coll.find(query).skip(from - 1).limit(count);
        final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);
        while (cursor.hasNext()) {
            final DBObject doc = cursor.next();
            final BasicDBList ccsLst = (BasicDBList) doc.get(CENTER_FIELD);
            final Set<String> ccs = new TreeSet<String>();

            for (Object cc : ccsLst) {
                ccs.add((String) cc);
            }

            final IdUrl iu = new IdUrl((String) doc.get(ID_FIELD), (String) doc.get(PRETTY_BROKEN_URL_FIELD),
                    ccs, format.format((Date) (doc.get(DATE_FIELD))), (String) doc.get(MST_FIELD));
            lst.add(iu);
        }
        cursor.close();
    } else {
        final BasicDBObject query = new BasicDBObject(CENTER_FIELD, filter);
        final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);

        while (cursor.hasNext()) {
            final DBObject doc = cursor.next();
            final Set<String> ccs = new TreeSet<String>();
            ccs.add(filter);

            final IdUrl iu = new IdUrl((String) doc.get(ID_FIELD), (String) doc.get(PRETTY_BROKEN_URL_FIELD),
                    ccs, format.format((Date) doc.get(DATE_FIELD)), (String) doc.get(MST_FIELD));
            lst.add(iu);
        }
        cursor.close();
    }
    return lst;
}

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

License:Open Source License

public static SearchResult getDocuments(final DBCollection coll, final String docMast, final String docId,
        final String docUrl, final Set<String> centerIds, final boolean decreasingOrder, final int from,
        final int count) {
    if (coll == null) {
        throw new NullPointerException("coll");
    }/*www  .j a va2s  .  co  m*/
    if (from < 1) {
        throw new IllegalArgumentException("from[" + from + "] < 1");
    }
    if (count < 1) {
        throw new IllegalArgumentException("count[" + count + "] < 1");
    }
    final List<IdUrl> lst = new ArrayList<IdUrl>();
    final BasicDBObject query = new BasicDBObject();

    if (docMast != null) {
        query.append(MST_FIELD, docMast);
    }
    if (docId != null) {
        final Pattern pat = Pattern.compile("^" + docId.trim() + "_\\d+");
        query.append(ID_FIELD, pat);
    }
    if (docUrl != null) {
        query.append(BROKEN_URL_FIELD, docUrl.trim());
    }
    if (centerIds != null) {
        final BasicDBList cclst = new BasicDBList();
        for (String centerId : centerIds) {
            cclst.add(centerId);
        }
        final BasicDBObject in = new BasicDBObject("$in", cclst);
        query.append(CENTER_FIELD, in);
    }
    final BasicDBObject sort = new BasicDBObject(DATE_FIELD, decreasingOrder ? -1 : 1);
    final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);
    final int size = cursor.count();
    final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
    while (cursor.hasNext()) {
        final DBObject doc = cursor.next();
        final BasicDBList ccsLst = (BasicDBList) doc.get(CENTER_FIELD);
        final Set<String> ccs = new TreeSet<String>();

        for (Object cc : ccsLst) {
            ccs.add((String) cc);
        }
        final IdUrl iu = new IdUrl((String) doc.get(ID_FIELD), (String) doc.get(PRETTY_BROKEN_URL_FIELD), ccs,
                format.format((Date) (doc.get(DATE_FIELD))), (String) doc.get(MST_FIELD));
        lst.add(iu);
    }
    cursor.close();

    return new SearchResult(size, lst);
}

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

License:Open Source License

public static SearchResult2 getHistoryDocuments(final DBCollection coll, final Element elem, final int from,
        final int count) throws IOException, ParseException {
    if (coll == null) {
        throw new NullPointerException("coll");
    }/*from  w w  w. jav a2 s .  co m*/
    if (elem == null) {
        throw new NullPointerException("elem");
    }
    if (from < 1) {
        throw new IllegalArgumentException("from[" + from + "] < 1");
    }
    if (count < 1) {
        throw new IllegalArgumentException("count[" + count + "] < 1");
    }
    final List<Element> lst = new ArrayList<Element>();
    final BasicDBObject query = new BasicDBObject();
    final String root = ELEM_LST_FIELD + ".0.";
    final String updated = root + LAST_UPDATE_FIELD;

    if (elem.getDbase() != null) {
        query.append(MST_FIELD, elem.getDbase().trim());
    }
    if (elem.getId() != null) {
        final Pattern pat = Pattern.compile("^" + elem.getId().trim() + "_\\d+");
        query.append(ID_FIELD, pat);
    }
    if (elem.getFurl() != null) {
        query.append(root + FIXED_URL_FIELD, elem.getFurl().trim());
    }
    if (!elem.getCcs().isEmpty()) {
        final BasicDBList cclst = new BasicDBList();
        for (String centerId : elem.getCcs()) {
            cclst.add(centerId.trim());
        }
        final String cc = root + CENTER_FIELD;
        final BasicDBObject in = new BasicDBObject("$in", cclst);
        query.append(cc, in);
    }
    if (elem.getDate() != null) {
        final SimpleDateFormat simple = new SimpleDateFormat("dd-MM-yyyy");
        final Date date = simple.parse(elem.getDate().trim());
        final BasicDBObject qdate = new BasicDBObject("$gte", date);
        query.append(updated, qdate);
    }
    if (elem.getUser() != null) {
        final String user = root + USER_FIELD;
        query.append(user, elem.getUser().trim());
    }

    final BasicDBObject sort = new BasicDBObject(updated, -1);
    final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);
    final int size = cursor.count();
    final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

    while (cursor.hasNext()) {
        final BasicDBObject hdoc = (BasicDBObject) cursor.next();
        final BasicDBList elst = (BasicDBList) hdoc.get(ELEM_LST_FIELD);
        final BasicDBObject hcurdoc = (BasicDBObject) elst.get(0);
        if (hcurdoc == null) {
            throw new IOException("document last element found.");
        }
        final BasicDBList ccLst = (BasicDBList) hcurdoc.get(CENTER_FIELD);
        final List<String> ccs = Arrays.asList(ccLst.toArray(new String[0]));
        final Element elem2 = new Element(hdoc.getString(ID_FIELD), hcurdoc.getString(BROKEN_URL_FIELD),
                hcurdoc.getString(PRETTY_BROKEN_URL_FIELD), hcurdoc.getString(FIXED_URL_FIELD),
                hdoc.getString(MST_FIELD), format.format((Date) (hcurdoc.get(LAST_UPDATE_FIELD))),
                hcurdoc.getString(USER_FIELD), ccs, hcurdoc.getBoolean(EXPORTED_FIELD));
        lst.add(elem2);
    }
    cursor.close();

    return new SearchResult2(size, lst.size(), lst);
}