Example usage for com.mongodb DBObject keySet

List of usage examples for com.mongodb DBObject keySet

Introduction

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

Prototype

Set<String> keySet();

Source Link

Document

Returns this object's fields' names

Usage

From source file:com.ccoe.build.alerts.connector.Connector.java

License:Apache License

public static double getMovingAverage(DBCollection collection, String field, Date startDate, Date endDate) {
    double weekSum = 0;
    DBObject record = null;/*w w  w  .jav a  2 s  . c  o m*/
    int count = 0;
    try {
        BasicDBObject searchQuery = new BasicDBObject();
        QueryBuilder qb = new QueryBuilder();
        qb.put("Date").greaterThanEquals(startDate).lessThanEquals(endDate);
        searchQuery.putAll(qb.get());
        DBCursor cursor = collection.find(searchQuery);

        while (cursor.hasNext()) {
            record = cursor.next();
            DBObject dbo = (DBObject) record.get("Data");
            Set<String> keySet = dbo.keySet();

            for (String keyName : keySet) {
                if (field.equals(keyName)) {
                    count++;
                    Object keyValue = dbo.get(keyName);
                    double keyValueNum = 0;
                    if (keyValue instanceof Integer) {
                        int intValue = (Integer) keyValue;
                        keyValueNum = Double.parseDouble("" + intValue);
                    } else if (keyValue instanceof Double) {
                        keyValueNum = (Double) keyValue;
                    }
                    weekSum += keyValueNum;
                }
            }
        }
    } catch (MongoException e) {
        e.printStackTrace();
    }
    DAYS = count;
    return weekSum * 1.0 / count;
}

From source file:com.ccoe.build.alerts.devx.DevxReportJob.java

License:Apache License

private double getValueFromDataObj(DBObject dbo, String fieldName) {
    Set<String> keyset = dbo.keySet();

    for (String keyname : keyset) {
        if (fieldName.equals(keyname)) {
            Object keyvalue = dbo.get(keyname);
            if (keyvalue instanceof Integer) {
                int k1 = (Integer) keyvalue;
                return Double.parseDouble("" + k1);
            } else if (keyvalue instanceof Double) {
                return (Double) keyvalue;
            }/*from  www.jav a 2s. c o m*/
        }
    }
    return -1;
}

From source file:com.cedac.security.oauth2.provider.client.MongoClientDetailsService.java

License:Apache License

@SuppressWarnings("unchecked")
private ClientDetails toClientDetails(DBObject dbo) {
    final String clientId = (String) dbo.get(clientIdFieldName);
    final String resourceIds = collectionToCommaDelimitedString((Collection) dbo.get(resourceIdsFieldName));
    final String scopes = collectionToCommaDelimitedString((Collection) dbo.get(scopeFieldName));
    final String grantTypes = collectionToCommaDelimitedString(
            (Collection) dbo.get(authorizedGrantTypesFieldName));
    final String authorities = collectionToCommaDelimitedString((Collection) dbo.get(authoritiesFieldName));
    final String redirectUris = collectionToCommaDelimitedString(
            (Collection) dbo.get(registeredRedirectUrisFieldName));
    BaseClientDetails clientDetails = new BaseClientDetails(clientId, resourceIds, scopes, grantTypes,
            authorities, redirectUris);//from   www.  j av a2s  . co m
    clientDetails.setClientSecret((String) dbo.get(clientSecretFieldName));
    clientDetails.setAccessTokenValiditySeconds((Integer) dbo.get(accessTokenValidityFieldName));
    clientDetails.setRefreshTokenValiditySeconds((Integer) dbo.get(refreshTokenValidityFieldName));
    Object autoApprove = dbo.get(autoApproveFieldName);
    if (autoApprove != null) {
        if (autoApprove instanceof String) {
            clientDetails.setAutoApproveScopes(Collections.singleton((String) autoApprove));
        } else {
            clientDetails.setAutoApproveScopes((Collection<String>) dbo.get(autoApproveFieldName));
        }
    }
    DBObject additionalInfo = (DBObject) dbo.get(additionalInformationFieldName);
    if (additionalInfo != null) {
        for (String key : additionalInfo.keySet()) {
            clientDetails.addAdditionalInformation(key, additionalInfo.get(key));
        }
    }
    return clientDetails;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DBackFileOld.java

License:Open Source License

private int removeFilesIncompleteMetaData(String fileName, String strFileID) {
    int n = 0;/*from   w  w w  .ja va  2 s. c  o  m*/
    Set<String> stMetaKeys = new HashSet<String>();
    stMetaKeys.add("PutFileName");
    stMetaKeys.add("PutServerFileID");
    stMetaKeys.add("PutServerUploadDateTime");
    stMetaKeys.add("PutType");
    stMetaKeys.add("PutContentType");
    stMetaKeys.add("PutClientDateTime");
    stMetaKeys.add("PutClientDateTimeInMillis");
    stMetaKeys.add("PutClientDateTimeInNano");
    stMetaKeys.add("PutStatus");
    stMetaKeys.add("PutLatestNumber");

    BasicDBObject documentQuery = new BasicDBObject();
    // "filename" is MongoDB/GridFS specific meta data name inside fs.files collection for each file
    documentQuery.put("filename", fileName);

    DBCursor cursorExist = m_table.find(documentQuery);
    n = cursorExist.count();
    if (0 == n) {
        log.debug("removeFilesIncompleteMetaData() Information only : No file found for file : " + fileName);
        return 0;
    } else {
        while (cursorExist.hasNext()) {
            DBObject ob = cursorExist.next();
            log.debug("removeFilesIncompleteMetaData() result from ob {}", ob.toString());
            // "_id" is MongoDB/GridFS specific meta data name inside fs.files collection for each file
            ObjectId oid = ((BasicBSONObject) ob).getObjectId("_id");
            if (null == oid) {
                // what's the use in throwing exception here? throw new DScabiException("_id is missing for file : " + fileName, "DBF.RFI.1");
                // let it continue to cleanup as much as possible
                continue;
            }
            if (oid.toHexString().equals(strFileID)) {
                log.debug(
                        "removeFilesIncompleteMetaData() Information only : skipping given input file ID : {}",
                        strFileID);
                continue;
            }
            Set<String> st = ob.keySet();
            if (st.containsAll(stMetaKeys)) {
                continue;
            } else {
                // remove file
                m_gridFSBucket.delete(oid);

            }
        }
    }
    return 0;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DBackFileOld.java

License:Open Source License

public int removeAllFilesIncompleteMetaData() {
    int n = 0;/*from ww  w  . jav  a2 s. c  om*/
    Set<String> stMetaKeys = new HashSet<String>();
    stMetaKeys.add("PutFileName");
    stMetaKeys.add("PutServerFileID");
    stMetaKeys.add("PutServerUploadDateTime");
    stMetaKeys.add("PutType");
    stMetaKeys.add("PutContentType");
    stMetaKeys.add("PutClientDateTime");
    stMetaKeys.add("PutClientDateTimeInMillis");
    stMetaKeys.add("PutClientDateTimeInNano");
    stMetaKeys.add("PutStatus");
    stMetaKeys.add("PutLatestNumber");

    DBCursor cursorExist = m_table.find();
    n = cursorExist.count();
    if (0 == n) {
        log.debug("removeAllFilesIncompleteMetaData() Information only : No file found");
        return 0;
    } else {
        while (cursorExist.hasNext()) {
            DBObject ob = cursorExist.next();
            log.debug("removeAllFilesIncompleteMetaData() result from ob {}", ob.toString());
            // "_id" is MongoDB/GridFS specific meta data name inside fs.files collection for each file
            ObjectId oid = ((BasicBSONObject) ob).getObjectId("_id");
            if (null == oid) {
                // what's the use in throwing exception here? throw new DScabiException("_id is missing for file : " + fileName, "DBF.RAF.1");
                // let it continue to cleanup as much as possible
                continue;
            }
            Set<String> st = ob.keySet();
            if (st.containsAll(stMetaKeys)) {
                continue;
            } else {
                // remove file
                m_gridFSBucket.delete(oid);

            }
        }
    }
    return 0;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DBackFileOld.java

License:Open Source License

public boolean isValidMetaData(String fileName, String strFileID) throws IOException, DScabiException {
    int n = 0;//from   w ww  . j a va  2 s .c  o m
    Set<String> stMetaKeys = new HashSet<String>();
    stMetaKeys.add("PutFileName");
    stMetaKeys.add("PutServerFileID");
    stMetaKeys.add("PutServerUploadDateTime");
    stMetaKeys.add("PutType");
    stMetaKeys.add("PutContentType");
    stMetaKeys.add("PutClientDateTime");
    stMetaKeys.add("PutClientDateTimeInMillis");
    stMetaKeys.add("PutClientDateTimeInNano");
    stMetaKeys.add("PutStatus");
    stMetaKeys.add("PutLatestNumber");

    BasicDBObject documentQuery = new BasicDBObject();
    ObjectId fileID = new ObjectId(strFileID);
    // "_id" is MongoDB/GridFS specific meta data name inside fs.files collection for each file
    documentQuery.put("_id", fileID);

    DBCursor cursorExist = m_table.find(documentQuery);
    n = cursorExist.count();
    if (1 == n) {
        log.debug("isValidMetaData() Inside 1 == n");
        while (cursorExist.hasNext()) {
            DBObject ob = cursorExist.next();
            log.debug("isValidMetaData() result from ob {}", ob.toString());
            Set<String> st = ob.keySet();
            if (st.containsAll(stMetaKeys)) {
                return true;
            } else {
                return false;
            }
        }
    } else if (0 == n) {
        log.debug("isValidMetaData() No matches for file : " + fileName + " fileID : " + fileID.toHexString());
        throw new DScabiException(
                "isValidMetaData() No matches for file : " + fileName + " fileID : " + fileID.toHexString(),
                "DBF.IVM.1");
        //return false;
    } else {
        log.debug("isValidMetaData() Multiple matches for file : " + fileName + " fileID : "
                + fileID.toHexString());
        throw new DScabiException("isValidMetaData() Multiple matches for file : " + fileName + " fileID : "
                + fileID.toHexString(), "DBF.IVM.2");
        //return false;
    }
    return false;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DDBOld.java

License:Open Source License

public ArrayList<String> fieldNamesUsingFindOne(DTableOld table) throws DScabiException {
    if (null == table) {
        throw new DScabiException("Table is null", "DBD.FNU.1");
    }//ww  w.  j  av  a 2 s . c o m
    log.debug("fieldNamesUsingFindOne() table.count() is {}", table.count());
    if (table.count() <= 0) {
        return null;
    }
    DBObject out = table.getCollection().findOne();
    ArrayList<String> fieldNames = new ArrayList<String>();
    Set<String> st = out.keySet();
    for (String s : st) {
        log.debug("fieldNamesUsingFindOne() value is : {}", out.get(s));
        log.debug("fieldNamesUsingFindOne() Key name is : {}", s);
        if (false == s.equals("_id"))
            fieldNames.add(s);
    }
    return fieldNames;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java

License:Open Source License

public ArrayList<String> fieldNamesUsingFindOne() throws DScabiException {
    if (null == m_tableName) {
        throw new DScabiException("Table name is null", "DBT.FNU.1");
    }//  www.  j ava2s  . c o  m
    if (null == m_table) {
        throw new DScabiException("Table is null", "DBT.FNU.2");
    }
    log.debug("fieldNamesUsingFindOne() firstTime is {}", m_firstTime);
    log.debug("fieldNamesUsingFindOne() table.count() is {}", m_table.count());
    if (m_table.count() <= 0) {
        return null;
    }
    if (m_firstTime) {
        DBObject out = m_table.findOne();
        m_fieldNames = new ArrayList<String>();
        Set<String> st = out.keySet();
        for (String s : st) {
            log.debug("fieldNamesUsingFindOne() value is : {}", out.get(s));
            log.debug("fieldNamesUsingFindOne() Key name is : {}", s);
            if (false == s.equals("_id"))
                m_fieldNames.add(s);
        }
        m_firstTime = false;
        return m_fieldNames;
    }
    return m_fieldNames;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java

License:Open Source License

public String executeQuery(String jsonQuery) throws DScabiException, IOException {
    ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames();
    DMJson djson = new DMJson(jsonQuery);
    Set<String> st = djson.keySet();
    BasicDBObject document = new BasicDBObject();
    ArrayList<String> finalList = new ArrayList<String>();
    HashMap<String, String> hmap = new HashMap<String, String>();
    DMJson djson3 = null;/* w w w. j a  v a 2s  . c  o m*/

    if (false == isEmpty(fieldList)) {
        if (false == fieldList.containsAll(st)) {
            throw new DScabiException(
                    "One or more field name in jsonQuery doesn't exist in fieldNames list. jsonQuery : "
                            + jsonQuery + " Field Names list : " + fieldList,
                    "DBT.EQY.1");
        }

    }

    for (String key : st) {
        // create a document to store key and value
        document.put(key, djson.getString(key));
    }
    DBCursor cursorExist = m_table.find(document);

    while (cursorExist.hasNext()) {

        hmap.clear();
        DBObject ob = cursorExist.next();
        Set<String> obkeys = ob.keySet();
        obkeys.remove("_id"); // exclude _id field
        //log.debug("executeQuery() result from ob {}", ob.toString());
        if (false == isEmpty(fieldList)) {
            if (false == obkeys.containsAll(fieldList)) {
                throw new DScabiException(
                        "One or more field name in fieldList doesn't exist in obkeys key set. obkeys : "
                                + obkeys + " Field Names list : " + fieldList,
                        "DBT.EQY.2");
            }
            for (String field : obkeys) {
                //if (field.equals("_id"))
                //   continue;
                String f = (String) ((BasicBSONObject) ob).getString(field);
                if (null == f) {
                    throw new DScabiException(
                            "Field name " + field + " doesn't exist in dbobject in dbcursor. jsonQuery : "
                                    + jsonQuery + " Field Names list : " + fieldList,
                            "DBT.EQY.3");
                }
                //log.debug("executeQuery() field is {}", field);
                //log.debug("executeQuery() f is {}", f);
                hmap.put(field, f);
            }
        } else {
            for (String key : obkeys) {
                //if (key.equals("_id"))
                //   continue;
                String f2 = (String) ((BasicBSONObject) ob).getString(key);
                if (null == f2) {
                    throw new DScabiException("Field name " + key
                            + " doesn't exist in dbobject in dbcursor. jsonQuery : " + jsonQuery, "DBT.EQY.4");
                }
                //log.debug("executeQuery() key is {}", key);
                //log.debug("executeQuery() f2 is {}", f2);
                hmap.put(key, f2);
            }
        }
        DMJson djson2 = null;
        //if (false == fieldList.isEmpty())
        //   djson2 = DJson.createDJsonList(hmap, fieldList);
        //else if (false == st.isEmpty())
        //   djson2 = DJson.createDJsonSet(hmap, st);
        if (false == obkeys.isEmpty())
            djson2 = DMJson.createDJsonSet(hmap, obkeys);
        if (null == djson2) {
            throw new DScabiException("djson2 is null. jsonQuery : " + jsonQuery, "DBT.EQY.5");
        }
        finalList.add(djson2.toString());
    }
    djson3 = DMJson.createDJsonWithCount(finalList);

    return djson3.toString();
}

From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.IndexBuildCommand.java

License:Apache License

private Set<String> getUpdateIndex(Map<String, DBObject> exsitingIndexMap, MetaClass meta) {
    Collection<IndexInfo> collection = meta.getIndexes();
    Set<String> updateIndexes = new HashSet<String>();
    for (IndexInfo info : collection) {
        if (exsitingIndexMap.containsKey(info.getIndexName())) {
            boolean diff = false;
            DBObject dbo = exsitingIndexMap.get(info.getIndexName());
            // key check : cast as BasicDBObject, so that we could go through the insert order
            // FIXME :: don't support compare key order. If we support key order specify, then we need to add this comparison 
            BasicDBObject keyObjs = (BasicDBObject) dbo.get("key");
            DBObject keyObject = buildIndexKeyObject(meta, info, onMainBranch);
            diff = !ListUtils.isEqualList(keyObjs.keySet(), keyObject.keySet());

            if (!diff) {
                // option check when keys check passed
                List<IndexOptionEnum> exsitingOption = new ArrayList<IndexInfo.IndexOptionEnum>();
                for (IndexOptionEnum ioe : IndexOptionEnum.values()) {
                    if (dbo.containsField(ioe.name().toLowerCase())
                            && Boolean.parseBoolean(dbo.get(ioe.name().toLowerCase()).toString())) {
                        exsitingOption.add(ioe);
                    }/*from w w w . ja va  2s.co  m*/
                }
                if (!ListUtils.subtract(exsitingOption, info.getIndexOptions()).isEmpty()
                        || !ListUtils.subtract(info.getIndexOptions(), exsitingOption).isEmpty()) {
                    diff = true;
                }
            }
            if (diff) {
                updateIndexes.add(info.getIndexName());
            }
        }
    }
    return updateIndexes;
}