Beispiel #1
0
 @Override
 public void updateIndividualFields(
     String collection,
     String name,
     ColumnField valuesMapField,
     ArrayList<ColumnField> valuesMapKeys,
     ArrayList<Object> valuesMapValues)
     throws FailedDBOperationException {
   DatabaseConfig.getLogger()
       .log(Level.FINE, "Update fields {0}/{1}", new Object[] {name, valuesMapKeys});
   JSONObject record;
   try {
     record = lookupEntireRecord(collection, name);
   } catch (RecordNotFoundException e) {
     throw new FailedDBOperationException(collection, name, "Record not found.");
   }
   DatabaseConfig.getLogger().log(Level.FINE, "Record before:{0}", record);
   if (record == null) {
     throw new FailedDBOperationException(collection, name);
   }
   if (valuesMapField != null && valuesMapKeys != null) {
     try {
       JSONObject json = record.getJSONObject(valuesMapField.getName());
       for (int i = 0; i < valuesMapKeys.size(); i++) {
         String fieldName = valuesMapKeys.get(i).getName();
         switch (valuesMapKeys.get(i).type()) {
           case LIST_STRING:
             JSONDotNotation.putWithDotNotation(json, fieldName, valuesMapValues.get(i));
             // json.put(fieldName, valuesMapValues.get(i));
             break;
           case USER_JSON:
             JSONDotNotation.putWithDotNotation(
                 json, fieldName, JSONParse(valuesMapValues.get(i)));
             // json.put(fieldName, JSONParse(valuesMapValues.get(i)));
             break;
           default:
             DatabaseConfig.getLogger()
                 .log(Level.WARNING, "Ignoring unknown format: {0}", valuesMapKeys.get(i).type());
             break;
         }
       }
       DatabaseConfig.getLogger().log(Level.FINE, "Json after:{0}", json);
       record.put(valuesMapField.getName(), json);
       DatabaseConfig.getLogger().log(Level.FINE, "Record after:{0}", record);
     } catch (JSONException e) {
       DatabaseConfig.getLogger().log(Level.SEVERE, "Problem updating json: {0}", e.getMessage());
     }
   }
   getMap(collection).put(name, record);
 }
Beispiel #2
0
  @Override
  public HashMap<ColumnField, Object> lookupSomeFields(
      String collection,
      String name,
      ColumnField nameField,
      ColumnField valuesMapField,
      ArrayList<ColumnField> valuesMapKeys)
      throws RecordNotFoundException, FailedDBOperationException {

    JSONObject record = lookupEntireRecord(collection, name);
    //    DatabaseConfig.getLogger().log(Level.FINE, "Full record " + record.toString());
    HashMap<ColumnField, Object> hashMap = new HashMap<>();
    hashMap.put(nameField, name);
    if (valuesMapField != null && valuesMapKeys != null) {
      try {
        JSONObject readValuesMap = record.getJSONObject(valuesMapField.getName());
        //        DatabaseConfig.getLogger().log(Level.FINE, "Read valuesMap " +
        // readValuesMap.toString());
        ValuesMap valuesMapOut = new ValuesMap();
        for (int i = 0; i < valuesMapKeys.size(); i++) {
          String userKey = valuesMapKeys.get(i).getName();
          if (JSONDotNotation.containsFieldDotNotation(userKey, readValuesMap) == false) {
            //            DatabaseConfig.getLogger().fine("valuesMap doesn't contain " + userKey);
            continue;
          }
          try {
            switch (valuesMapKeys.get(i).type()) {
              case USER_JSON:
                Object value = JSONDotNotation.getWithDotNotation(userKey, readValuesMap);
                DatabaseConfig.getLogger()
                    .log(Level.FINE, "Object is {0}", new Object[] {value.toString()});
                valuesMapOut.put(userKey, value);
                break;
              case LIST_STRING:
                valuesMapOut.putAsArray(
                    userKey,
                    JSONUtils.JSONArrayToResultValue(
                        new JSONArray(
                            JSONDotNotation.getWithDotNotation(userKey, readValuesMap)
                                .toString())));
                break;
              default:
                DatabaseConfig.getLogger()
                    .log(
                        Level.SEVERE,
                        "ERROR: Error: User keys field {0} is not a known type:{1}",
                        new Object[] {userKey, valuesMapKeys.get(i).type()});
                break;
            }
          } catch (JSONException e) {
            DatabaseConfig.getLogger().log(Level.SEVERE, "Error parsing json: {0}", e.getMessage());
          }
        }
        hashMap.put(valuesMapField, valuesMapOut);
      } catch (JSONException e) {
        DatabaseConfig.getLogger()
            .log(Level.SEVERE, "Problem getting values map: {0}", e.getMessage());
      }
    }
    return hashMap;
  }