Search in sources :

Example 16 with RecordNotFoundException

use of edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException in project GNS by MobilityFirst.

the class MongoRecords method lookupSomeFields.

@Override
public HashMap<ColumnField, Object> lookupSomeFields(String collectionName, String guid, ColumnField nameField, ColumnField valuesMapField, ArrayList<ColumnField> valuesMapKeys) throws RecordNotFoundException, FailedDBOperationException {
    if (guid == null) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} GUID is null: {1}", new Object[] { dbName, guid });
        throw new RecordNotFoundException(guid);
    }
    db.requestStart();
    try {
        String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
        db.requestEnsureConnection();
        DBCollection collection = db.getCollection(collectionName);
        BasicDBObject query = new BasicDBObject(primaryKey, guid);
        BasicDBObject projection = new BasicDBObject().append("_id", 0);
        if (valuesMapField != null && valuesMapKeys != null) {
            for (int i = 0; i < valuesMapKeys.size(); i++) {
                String fieldName = valuesMapField.getName() + "." + valuesMapKeys.get(i).getName();
                projection.append(fieldName, 1);
            }
        }
        DBObject dbObject = collection.findOne(query, projection);
        if (dbObject == null) {
            throw new RecordNotFoundException(guid);
        }
        HashMap<ColumnField, Object> hashMap = new HashMap<>();
        // put the name in the hashmap!! very important!!
        hashMap.put(nameField, guid);
        if (valuesMapField != null && valuesMapKeys != null) {
            // first we pull all the user values from the dbObject and put in a bson object
            // FIXME: Why not convert this to a JSONObject right now? We know that's what it is.
            BasicDBObject bson = (BasicDBObject) dbObject.get(valuesMapField.getName());
            DatabaseConfig.getLogger().log(Level.FINER, "{0} @@@@@@@@ {1}", new Object[] { dbName, bson });
            // then we run thru each userkey in the valuesMapKeys and pull the
            // value put stuffing it into the values map
            ValuesMap valuesMap = new ValuesMap();
            for (int i = 0; i < valuesMapKeys.size(); i++) {
                String userKey = valuesMapKeys.get(i).getName();
                if (containsFieldDotNotation(userKey, bson) == false) {
                    DatabaseConfig.getLogger().log(Level.FINE, "{0} DBObject doesn't contain {1}", new Object[] { dbName, userKey });
                    continue;
                }
                try {
                    switch(valuesMapKeys.get(i).type()) {
                        case USER_JSON:
                            Object value = getWithDotNotation(userKey, bson);
                            DatabaseConfig.getLogger().log(Level.FINE, "{0} Object is {1}", new Object[] { dbName, value.toString() });
                            valuesMap.put(userKey, value);
                            break;
                        case LIST_STRING:
                            valuesMap.putAsArray(userKey, JSONUtils.JSONArrayToResultValue(new JSONArray(getWithDotNotation(userKey, bson).toString())));
                            break;
                        default:
                            DatabaseConfig.getLogger().log(Level.SEVERE, "{0} ERROR: Error: User keys field {1} is not a known type: {2}", new Object[] { dbName, userKey, valuesMapKeys.get(i).type() });
                            break;
                    }
                } catch (JSONException e) {
                    DatabaseConfig.getLogger().log(Level.SEVERE, "{0} Error parsing json: {1}", new Object[] { dbName, e.getMessage() });
                    e.printStackTrace();
                }
            }
            hashMap.put(valuesMapField, valuesMap);
        }
        return hashMap;
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} lookupSomeFields failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, guid, "Original mongo exception:" + e.getMessage());
    } finally {
        db.requestDone();
    }
}
Also used : MongoException(com.mongodb.MongoException) HashMap(java.util.HashMap) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)16 JSONObject (org.json.JSONObject)15 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)14 JSONException (org.json.JSONException)12 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)10 Test (org.junit.Test)6 NameRecord (edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord)5 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException)4 HashMap (java.util.HashMap)3 BasicDBObject (com.mongodb.BasicDBObject)2 DBCollection (com.mongodb.DBCollection)2 DBObject (com.mongodb.DBObject)2 MongoException (com.mongodb.MongoException)2 RecordExistsException (edu.umass.cs.gnscommon.exceptions.server.RecordExistsException)2 DBCursor (com.mongodb.DBCursor)1 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)1 GNSRecordMap (edu.umass.cs.gnsserver.gnsapp.recordmap.GNSRecordMap)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1