Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

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

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

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);

    return (user);
}

From source file:backend.facades.UserController.java

public boolean updateImageId(Long userId, String imageId) {
    try {//from  w  w  w . ja v  a 2 s.  c o  m
        BasicDBObject document = new BasicDBObject();
        document.append("$set", new BasicDBObject().append("imageId", imageId));
        userCollection.update(new BasicDBObject().append("_id", userId), document);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}

From source file:backend.facades.UserController.java

public void updatePassword(Long id, String password) {
    BasicDBObject document = new BasicDBObject();
    document.append("$set", new BasicDBObject().append("passwd", hashPassword(password)));
    userCollection.update(new BasicDBObject().append("_id", id), document);
}

From source file:backend.facades.UserController.java

public void updateUserProfile(UserEntity userEntity) {
    BasicDBObject document = new BasicDBObject();

    if (userEntity.getPasswd() != null) {
        document.append("$set", new BasicDBObject().append("email", userEntity.getEmail())
                .append("userRole", userEntity.getUserRole()).append("username", userEntity.getUsername())
                .append("firstname", userEntity.getFirstname()).append("lastname", userEntity.getLastname())
                .append("passwd", hashPassword(userEntity.getPasswd() != null ? userEntity.getPasswd() : null))
                .append("webpage", userEntity.getPersonalWebPage()).append("imageId", userEntity.getImageId())
                .append("subscribe",
                        userEntity.isAcceptSubscr() ? StatusTypes.ACCEPT_SUBSCRB : StatusTypes.DISABLE_SUBSCRB)
                .append("status", userEntity.getStatus()));
    } else {//  w w w . j a  v  a  2 s. c o  m
        document.append("$set", new BasicDBObject().append("email", userEntity.getEmail())
                .append("userRole", userEntity.getUserRole()).append("username", userEntity.getUsername())
                .append("firstname", userEntity.getFirstname()).append("lastname", userEntity.getLastname())
                .append("imageId", userEntity.getImageId()).append("webpage", userEntity.getPersonalWebPage())
                .append("subscribe",
                        userEntity.isAcceptSubscr() ? StatusTypes.ACCEPT_SUBSCRB : StatusTypes.DISABLE_SUBSCRB)
                .append("status", userEntity.getStatus()));
    }

    userCollection.update(new BasicDBObject().append("_id", userEntity.getId()), document);
}

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

License:Open Source License

private static void createIndex(final DBCollection coll) {
    assert coll != null;

    final BasicDBObject flds = new BasicDBObject();
    flds.append(CENTER_FIELD, 1);
    flds.append(BROKEN_URL_FIELD, 1); //Btree::insert: key too large to index
    flds.append(MST_FIELD, 1);//from  ww w .  j a v  a  2 s  .  c o  m
    coll.ensureIndex(flds);
}

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");
    }//from  w w w. j  a v  a  2s. c  om
    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");
    }// w ww . jav a2s .c  o  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);
}

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");
    }//  w  ww . j av  a 2s.c om
    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.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++;//  w  w w.j a  v  a  2s  .co m
                    } 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.com.binarti.simplesearchexpr.builders.mongodb.SimpleSearchMongoDBBuilder.java

License:Open Source License

@Override
public DBObject build(SimpleSearchExpression searchExpression) {
    BasicDBObject root = new BasicDBObject();
    if (searchExpression.getOperations().isEmpty()) {
        return root;
    }//from ww w . j a  va  2s.  c om
    for (SimpleSearchRelationalOperation op : searchExpression.getOperations()) {
        final String field = op.getField().getFieldExpr();
        final Object value = op.getValue();
        switch (op.getOperator()) {
        case EQUALS:
            root.put(field, value);
            break;
        case LIKE:
            root.put(field,
                    new BasicDBObject("$regex", StringUtils.trim((String) value)).append("$options", "i"));
            break;
        case INTERVAL:
            Iterator<Object> it = op.getValueAsCollection().iterator();
            Object value1 = it.next();
            Object value2;
            if (it.hasNext()) {
                value2 = it.next();
            } else {
                value2 = value1;
            }
            root.append(field, new BasicDBObject("$gte", value1).append("$lte", value2));
            break;
        case LIST:
            root.append(field, new BasicDBObject("$in", mongoList(op.getValueAsCollection())));
            break;
        default:
            throw new SimpleSearchExpressionException("Operator " + op.getOperator() + " not supported");
        }
    }
    return root;
}