Example usage for com.mongodb DBCollection findOne

List of usage examples for com.mongodb DBCollection findOne

Introduction

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

Prototype

@Nullable
public DBObject findOne(final Object id) 

Source Link

Document

Get a single document from collection by '_id'.

Usage

From source file:net.autosauler.ballance.server.model.AbstractStructuredData.java

License:Apache License

/**
 * Gets the record./* ww w.  jav  a  2s.c om*/
 * 
 * @param number
 *            the number
 * @return the record
 */
private DBObject getRecord(Long number) {
    DBObject doc = null;
    DB db = Database.get(getDomain());
    if (db != null) {
        Database.retain();
        DBCollection coll = db.getCollection(getTableName());
        BasicDBObject query = new BasicDBObject();
        query.put(fieldname_domain, getDomain());
        addGetRecordParams(query);
        query.put(fieldname_number, number);

        doc = coll.findOne(query);
        Database.release();
        onGetRecord(number);
    }

    return doc;
}

From source file:net.autosauler.ballance.server.model.Currency.java

License:Apache License

/**
 * Find database record for currency for date.
 * /*from  w  w w  .  j  a v  a2s  . c  o  m*/
 * @param mnemo
 *            the mnemo
 * @param day
 *            the day
 * @return the dB object
 */
private static DBObject find(String mnemo, String day) {
    DBObject myDoc = null;
    DB db = Database.get(null);
    if (db != null) {
        DBCollection coll = db.getCollection(CURRENCYTABLE);
        BasicDBObject query = new BasicDBObject();
        query.put("date", day);
        query.put("mnemo", mnemo);
        myDoc = coll.findOne(query);
    }
    return myDoc;
}

From source file:net.autosauler.ballance.server.model.GlobalSettings.java

License:Apache License

/**
 * Save changed values./*from  ww  w .  j  av a2 s .  co m*/
 */
public void save() {
    if (changed) {
        DBObject myDoc = null;
        Database.retain();
        DB db = Database.get(domain);
        if (db != null) {
            DBCollection coll = db.getCollection(SETTINGSTABLE);
            Set<String> keys = values.keySet();
            Iterator<String> i = keys.iterator();
            while (i.hasNext()) {
                String name = i.next();
                String val = values.get(name);

                BasicDBObject query = new BasicDBObject();
                query.put("name", name);
                query.put("domain", domain);
                myDoc = coll.findOne(query);
                if (myDoc != null) {
                    if (!val.equals(myDoc.get("val"))) {
                        myDoc.put("val", val);
                        coll.save(myDoc);
                    }
                } else {
                    myDoc = new BasicDBObject();
                    myDoc.put("name", name);
                    myDoc.put("domain", domain);
                    myDoc.put("val", val);
                    coll.insert(myDoc);
                }
            }
            changed = false;
        }
        Database.release();

    }
}

From source file:net.autosauler.ballance.server.model.Scripts.java

License:Apache License

/**
 * Load text./*from   ww  w.  ja v  a2s .  co  m*/
 */
private void loadText() {
    String txt = "";

    DBObject doc = null;
    DB db = Database.get(domain);
    if (db != null) {
        Database.retain();
        DBCollection coll = db.getCollection(TABLENAME);
        BasicDBObject query = new BasicDBObject();
        query.put("domain", domain);
        query.put("name", name);

        doc = coll.findOne(query);
        Database.release();
        if (doc != null) {
            txt = (String) doc.get("text");
        }
    }

    if ((txt == null) || txt.isEmpty()) {
        if (!domain.equals("127.0.0.1")) {
            Scripts s = new Scripts(caller, "127.0.0.1", username, name, vm);
            txt = s.getText();
        }

    }

    if ((txt == null) || txt.isEmpty()) {
        if (domain.equals("127.0.0.1") && name.equals("global")) {
            txt = "import java.lang\nLog.error('Global script evaluated')\n";
        }
        if (caller != null) {
            txt = caller.generateDefaultScript();

        }

        setText(txt, true);

        return;
    }

    setText(txt, false);

}

From source file:net.autosauler.ballance.server.model.Scripts.java

License:Apache License

/**
 * Sets the text.//from  ww  w  . j  av a  2 s  . c om
 * 
 * @param txt
 *            the txt
 * @param andstore
 *            the andstore
 */
public void setText(String txt, boolean andstore) {
    text = txt;
    if (andstore) {
        DBObject doc = null;
        DB db = Database.get(domain);
        if (db != null) {
            Database.retain();
            DBCollection coll = db.getCollection(TABLENAME);
            BasicDBObject query = new BasicDBObject();
            query.put("domain", domain);
            query.put("name", name);

            doc = coll.findOne(query);

            if (doc != null) {
                doc.put("text", text);
                coll.save(doc);
            } else {
                doc = new BasicDBObject();
                doc.put("domain", domain);
                doc.put("name", name);
                doc.put("text", text);
                coll.insert(doc);
            }

            Database.release();

            initVM(null);

        }
    }
}

From source file:net.autosauler.ballance.server.model.User.java

License:Apache License

/**
 * Find object.//from www.ja  v  a  2 s  .  c o  m
 * 
 * @param login
 *            the login
 * @return the dB object
 */
private static DBObject findObject(String login, String domain) {
    DBObject myDoc = null;
    DB db = Database.get(domain);
    if (db != null) {
        DBCollection coll = db.getCollection(USERSTABLE);
        BasicDBObject query = new BasicDBObject();
        query.put("login", login);
        query.put("domain", domain);
        myDoc = coll.findOne(query);
    }

    return myDoc;
}

From source file:net.handle.server.MongoDBHandleStorage.java

License:Open Source License

/**
 * ******************************************************************
 * Returns true if this server is responsible for the given naming
 * authority.//from  w w  w . jav a  2s.  c o  m
 * *******************************************************************
 */
public boolean haveNA(byte authHandle[]) throws HandleException {

    if (Util.startsWithCI(authHandle, Common.NA_HANDLE_PREFIX))
        authHandle = Util.getSuffixPart(authHandle);
    authHandle = Util.upperCase(authHandle);

    final DBCollection collection = getCollection(database, collection_nas);

    BasicDBObject query = new BasicDBObject();
    query.put("na", Util.decodeString(authHandle));
    DBObject na = collection.findOne(query);
    if (na != null)
        return true;
    if (Util.hasSlash(authHandle))
        return false;

    authHandle = Util.getZeroNAHandle(authHandle);
    query.clear();
    query.put("na", Util.decodeString(authHandle));
    na = collection.findOne(query);
    return (na != null);
}

From source file:net.handle.server.MongoDBHandleStorage.java

License:Open Source License

protected boolean handleExists(byte handle[]) throws HandleException {

    final DBCollection collection = getCollection(handle);
    final BasicDBObject query = new BasicDBObject();
    query.put("handle", Util.decodeString(handle));
    final DBObject result = collection.findOne(query);
    return (result != null);
}

From source file:net.handle.server.MongoDBHandleStorage.java

License:Open Source License

/**
 * ******************************************************************
 * Return the pre-packaged values of the given handle that are either
 * in the indexList or the typeList.  This method should return any
 * values of type ALIAS or REDIRECT, even if they were not requested.
 * *******************************************************************
 *//* w  ww  .  ja va 2  s.c o  m*/
public byte[][] getRawHandleValues(byte handle[], int indexList[], byte typeList[][]) throws HandleException {

    final String handleStr = Util.decodeString(handle);
    final BasicDBObject query = new BasicDBObject("handle", handleStr);
    final DBCollection collection = getCollection(handle);
    final DBObject _handles = collection.findOne(query);
    if (_handles == null)
        return null;
    final Object handles = _handles.get("handles");
    final BasicDBList results = (BasicDBList) handles;

    boolean allValues = ((typeList == null || typeList.length == 0)
            && (indexList == null || indexList.length == 0));

    Vector values = new Vector();

    for (Object result : results) {
        HandleValue value = getHandleValue((BasicDBObject) result);
        if (allValues) {
        } else if (!Util.isParentTypeInArray(typeList, value.getType())
                && !Util.isInArray(indexList, value.getIndex())) // ignore non-requested types
            continue;
        values.addElement(value);
    }

    byte rawValues[][] = new byte[values.size()][];
    for (int i = 0; i < rawValues.length; i++) {
        HandleValue value = (HandleValue) values.elementAt(i);
        rawValues[i] = new byte[Encoder.calcStorageSize(value)];
        Encoder.encodeHandleValue(rawValues[i], 0, value);
    }

    return rawValues;
}

From source file:net.handle.server.MongoDBHandleStorage.java

License:Open Source License

/**
 * Equivalent to getRawHandleValues. Added for the beneficial of non Handle System clients.
 *
 * @param handle/*from w w  w.  j  av a2s.  co m*/
 * @return
 * @throws HandleException
 */
public List<HandleValue> getHandleValues(String handle) {

    final BasicDBObject query = new BasicDBObject("handle", handle);
    final DBCollection collection = getCollection(Util.encodeString(handle));
    final DBObject h = collection.findOne(query);
    final List<HandleValue> handles = new ArrayList<HandleValue>();
    if (h == null) {
        return handles;
    }

    final BasicDBList results = (BasicDBList) h.get("handles");
    for (Object result : results) {
        HandleValue value = getHandleValue((BasicDBObject) result);
        handles.add(value);
    }
    return handles;
}