Example usage for com.mongodb BasicDBObject getBoolean

List of usage examples for com.mongodb BasicDBObject getBoolean

Introduction

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

Prototype

public boolean getBoolean(final String key) 

Source Link

Document

Returns the value of a field as a boolean.

Usage

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

License:Open Source License

Collection<Element> getNotExportedElements(final DBCollection coll, final DBCursor cursor) throws IOException {
    assert coll != null;
    assert cursor != null;

    final Collection<Element> col = new ArrayList<Element>();

    while (cursor.hasNext()) {
        final BasicDBObject obj = (BasicDBObject) cursor.next();
        final String id = obj.getString(ID_FIELD);
        final BasicDBList lst = (BasicDBList) obj.get(ELEM_LST_FIELD);
        if (lst == null) {
            throw new NullPointerException("Elem list espected");
        }//from  www  .  j av  a 2  s  . c  o m
        final BasicDBObject lelem = (BasicDBObject) lst.get(0);
        if (lelem == null) {
            throw new NullPointerException("Elem element espected");
        }
        if (!lelem.getBoolean(EXPORTED_FIELD)) {
            final Element elem = new Element(id, lelem.getString(BROKEN_URL_FIELD),
                    lelem.getString(PRETTY_BROKEN_URL_FIELD), lelem.getString(FIXED_URL_FIELD),
                    obj.getString(MST_FIELD), lelem.getDate(LAST_UPDATE_FIELD).toString(),
                    lelem.getString(USER_FIELD), null, false);
            col.add(elem);

            lelem.put(EXPORTED_FIELD, true);
            final WriteResult res = coll.save(obj, WriteConcern.ACKNOWLEDGED);

            if (!res.getCachedLastError().ok()) {
                throw new IOException("write doc[" + obj.getString(ID_FIELD) + "] failed");
            }
        }
    }

    return col;
}

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  ww w.  ja  va2 s .  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.ShowFixedLinks.java

License:Open Source License

public List<Element> showExportedLinks(final List<String> ccs, final String fromDate) throws ParseException {
    /*if (ccs == null) {
    throw new NullPointerException("ccs");
    }*//*from   w ww  . j  a  v  a  2s. c om*/
    final List<Element> lst = new ArrayList<Element>();
    final SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd");
    final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
    final Date date = (fromDate == null) ? new Date(0) : simple.parse(fromDate);
    final String updated = ELEM_LST_FIELD + ".0." + LAST_UPDATE_FIELD;
    final BasicDBObject qdate = new BasicDBObject("$gte", date);
    final BasicDBObject query = new BasicDBObject(updated, qdate);
    final BasicDBObject sort = new BasicDBObject(updated, -1);
    final DBCursor cursor = coll.find(query).sort(sort);

    while (cursor.hasNext()) {
        final BasicDBObject doc = (BasicDBObject) cursor.next();
        final BasicDBList elems = (BasicDBList) doc.get(ELEM_LST_FIELD);
        final BasicDBObject upd = (BasicDBObject) elems.get(0);
        final BasicDBList ccLst = (BasicDBList) upd.get(CENTER_FIELD);
        final List<String> ccs2 = new ArrayList<String>();

        for (Object cc : ccLst) {
            ccs2.add((String) cc);
        }

        if (ccs == null) {
            final String id = doc.getString(ID_FIELD);
            final Element elem = new Element(id.substring(0, id.indexOf('_')), upd.getString(BROKEN_URL_FIELD),
                    upd.getString(PRETTY_BROKEN_URL_FIELD), upd.getString(FIXED_URL_FIELD),
                    doc.getString(MST_FIELD), sdf.format(upd.getDate(LAST_UPDATE_FIELD)),
                    upd.getString(USER_FIELD), ccs2, upd.getBoolean(EXPORTED_FIELD));
            lst.add(elem);
        } else {
            for (String cc : ccs) {
                if (ccLst.contains(cc)) {
                    //System.out.println("cc=" + cc + " id=" + doc.getString(ID_FIELD));
                    final String id = doc.getString(ID_FIELD);
                    final Element elem = new Element(id.substring(0, id.indexOf('_')),
                            upd.getString(BROKEN_URL_FIELD), upd.getString(PRETTY_BROKEN_URL_FIELD),
                            upd.getString(FIXED_URL_FIELD), doc.getString(MST_FIELD),
                            sdf.format(upd.getDate(LAST_UPDATE_FIELD)), upd.getString(USER_FIELD), ccs2,
                            upd.getBoolean(EXPORTED_FIELD));
                    lst.add(elem);
                    break;
                }
            }
        }
    }
    cursor.close();

    System.out.println("size=" + lst.size() + "\n");

    return lst;
}

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

License:Apache License

private <T> Property<T> unpack(Database db, BasicDBObject obj) throws T2DBException {
    try {/*from  w  w w.j a  v a2s.c  o  m*/
        ObjectId id = obj.getObjectId(MongoDatabase.FLD_ID);
        String name = obj.getString(MongoDatabase.FLD_PROP_NAME);
        ObjectId vtId = obj.getObjectId(MongoDatabase.FLD_PROP_VT);
        Boolean indexed = obj.getBoolean(MongoDatabase.FLD_PROP_INDEXED);
        Surrogate vts = makeSurrogate(db, DBObjectType.VALUE_TYPE, new MongoDBObjectId(vtId));
        ValueType<T> vt = db.getValueType(vts);
        Surrogate s = makeSurrogate(db, DBObjectType.PROPERTY, new MongoDBObjectId(id));
        checkIntegrity(vt, vts, s);
        return new PropertyImpl<T>(name, vt, indexed, s);
    } catch (ClassCastException e) {
        throw T2DBMMsg.exception(e, J.J81010, obj.toString());
    }
}

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

License:Apache License

@SuppressWarnings({ "rawtypes", "unchecked" })
private AttributeDefinition<?> attributeDefinition(Surrogate schemaSurrogate, int seriesNr, Database db,
        BasicDBObject bo) throws T2DBException {
    int number = bo.getInt(MongoDatabase.FLD_ATTRIBDEF_NUM);
    boolean era = bo.getBoolean(MongoDatabase.FLD_ATTRIBDEF_ERASING);
    AttributeDefinitionImpl<?> def = null;
    if (era) {/*w ww .  j a  v a  2s . co m*/
        def = new AttributeDefinitionImpl(seriesNr, number, null, null);
        def.edit();
        def.setErasing(true);
    } else {
        ObjectId propId = bo.getObjectId(MongoDatabase.FLD_ATTRIBDEF_PROP);
        Surrogate propSurr = makeSurrogate(db, DBObjectType.PROPERTY, new MongoDBObjectId(propId));
        Property<?> prop = ((MongoDatabase) db).getReadMethodsForProperty().getProperty(propSurr);
        String val = bo.getString(MongoDatabase.FLD_ATTRIBDEF_VAL);
        checkIntegrity(prop, propSurr, schemaSurrogate);
        def = new AttributeDefinitionImpl(seriesNr, number, prop, prop.getValueType().scan(val));
    }
    return def;
}

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

License:Apache License

private SeriesDefinition seriesDefinition(Surrogate schemaSurrogate, Database db, BasicDBObject bo)
        throws T2DBException {
    int number = bo.getInt(MongoDatabase.FLD_SERIESDEF_NUM);
    boolean era = bo.getBoolean(MongoDatabase.FLD_SERIESDEF_ERASING);
    SeriesDefinitionImpl def = null;/*from   w  w  w  .  ja v a  2 s . c om*/
    if (era) {
        def = new SeriesDefinitionImpl(number, null, null);
        def.edit();
        def.setErasing(true);
    } else {
        String desc = bo.getString(MongoDatabase.FLD_SERIESDEF_DESC);
        BasicDBList list = (BasicDBList) bo.get(MongoDatabase.FLD_SERIESDEF_ATTRIBS);
        Collection<AttributeDefinition<?>> attribs = attributeDefinitions(schemaSurrogate, number, db, list);
        def = new SeriesDefinitionImpl(number, desc, attribs);
    }
    return def;
}

From source file:com.edgytech.umongo.EditAggGeoNearDialog.java

License:Apache License

@Override
public void setParameters(Object value) {
    BasicDBObject cmd = (BasicDBObject) value;
    ((DocBuilderField) getBoundUnit(Item.near)).setDBObject((DBObject) cmd.get("near"));
    setStringFieldValue(Item.distanceField, cmd.getString("distanceField"));
    setDoubleFieldValue(Item.maxDistance, cmd.getDouble("maxDistance"));
    if (cmd.containsField("distanceMultiplier")) {
        setDoubleFieldValue(Item.distanceMultiplier, cmd.getDouble("distanceMultiplier"));
    }//from ww w.j  av a2  s . c  o m
    if (cmd.containsField("query")) {
        ((DocBuilderField) getBoundUnit(Item.query)).setDBObject((DBObject) cmd.get("query"));
    }
    if (cmd.containsField("spherical")) {
        setBooleanFieldValue(Item.spherical, cmd.getBoolean("spherical"));
    }

    if (cmd.containsField("query")) {
        ((DocBuilderField) getBoundUnit(Item.query)).setDBObject((DBObject) cmd.get("query"));
    }

    if (cmd.containsField("includeLocs")) {
        setStringFieldValue(Item.includeLocs, cmd.getString("includeLocs"));
    }

    if (cmd.containsField("uniqueDocs")) {
        setBooleanFieldValue(Item.uniqueDocs, cmd.getBoolean("uniqueDocs"));
    }
}

From source file:com.edgytech.umongo.MongoUtils.java

License:Apache License

public static boolean isBalancerOn(MongoClient mongo) {
    final DB config = mongo.getDB("config");
    final DBCollection settings = config.getCollection("settings");
    BasicDBObject res = (BasicDBObject) settings.findOne(new BasicDBObject("_id", "balancer"));
    if (res == null || !res.containsField("stopped"))
        return true;
    return !res.getBoolean("stopped");
}

From source file:com.edgytech.umongo.RouterPanel.java

License:Apache License

public void balancer(ButtonBase button) {
    final MongoClient mongo = getRouterNode().getMongoClient();
    final DB config = mongo.getDB("config");
    final DBCollection settings = config.getCollection("settings");

    FormDialog diag = (FormDialog) ((MenuItem) getBoundUnit(Item.balancer)).getDialog();
    diag.xmlLoadCheckpoint();/*w w w  . j  a  v a2 s  .co  m*/

    final BasicDBObject query = new BasicDBObject("_id", "balancer");
    BasicDBObject balDoc = (BasicDBObject) settings.findOne(query);
    if (balDoc != null) {
        if (balDoc.containsField("stopped"))
            setIntFieldValue(Item.balStopped, balDoc.getBoolean("stopped") ? 1 : 2);
        if (balDoc.containsField("_secondaryThrottle"))
            setIntFieldValue(Item.balSecThrottle, balDoc.getBoolean("_secondaryThrottle") ? 1 : 2);
        BasicDBObject window = (BasicDBObject) balDoc.get("activeWindow");
        if (window != null) {
            setStringFieldValue(Item.balStartTime, window.getString("start"));
            setStringFieldValue(Item.balStopTime, window.getString("stop"));
        }
    }

    if (!diag.show())
        return;

    if (balDoc == null)
        balDoc = new BasicDBObject("_id", "balancer");
    int stopped = getIntFieldValue(Item.balStopped);
    if (stopped > 0)
        balDoc.put("stopped", stopped == 1 ? true : false);
    else
        balDoc.removeField("stopped");
    int throttle = getIntFieldValue(Item.balSecThrottle);
    if (throttle > 0)
        balDoc.put("_secondaryThrottle", throttle == 1 ? true : false);
    else
        balDoc.removeField("_secondaryThrottle");

    if (!getStringFieldValue(Item.balStartTime).trim().isEmpty()) {
        BasicDBObject aw = new BasicDBObject();
        aw.put("start", getStringFieldValue(Item.balStartTime).trim());
        aw.put("stop", getStringFieldValue(Item.balStopTime).trim());
        balDoc.put("activeWindow", aw);
    }
    final BasicDBObject newDoc = balDoc;

    new DbJob() {

        @Override
        public Object doRun() throws IOException {
            return settings.update(query, newDoc, true, false);
        }

        @Override
        public String getNS() {
            return settings.getFullName();
        }

        @Override
        public String getShortName() {
            return "Balancer";
        }

        @Override
        public void wrapUp(Object res) {
            updateComponent();
            super.wrapUp(res);
        }
    }.addJob();
}

From source file:com.edgytech.umongo.UserDialog.java

License:Apache License

void resetForEdit(BasicDBObject user) {
    xmlLoadCheckpoint();/*from   w ww .j  a  v  a2s.  com*/

    setStringFieldValue(Item.user, user.getString(Item.user.name()));
    ((TextField) getBoundJComponentUnit(Item.user)).editable = false;

    ((PasswordField) getBoundJComponentUnit(Item.password)).nonEmpty = false;
    setStringFieldValue(Item.userSource, user.getString(Item.userSource.name()));

    BasicDBList roles = (BasicDBList) user.get("roles");
    if (roles != null) {
        for (Role role : Role.values()) {
            setBooleanFieldValue(role.item, roles.contains(role.name()));
        }
    } else {
        boolean ro = user.getBoolean("readOnly");
        if (ro)
            setBooleanFieldValue(Item.readWrite, true);
        else
            setBooleanFieldValue(Item.read, true);
    }

    updateComponent();
}