예제 #1
0
  public Data save(Data data) {
    ContentValues values = new ContentValues();

    values.put("entity_name", data.getEntityName());
    values.put("attribute_name", data.getAttributeName());
    values.put("entity_id", data.getEntityId());
    values.put("field_value", data.getFieldValue());
    double num = 0;
    if (isNumeric(data.getFieldValue())) {
      try {
        num = Double.valueOf(data.getFieldValue());
      } catch (Exception e) {
      }
    }
    values.put("field_value_num", num);

    if (data.getId() == 0) {
      long insertId = database.insert(TABLE_NAME, null, values);
      return getById(insertId);
    } else {
      int rows = database.update(TABLE_NAME, values, "_id = " + data.getId(), null);
      if (rows == 0) throw new RuntimeException("Invalid id " + data.getId());
      return data; // if there were triggers, would need to re-read row here.
    }
  }
예제 #2
0
 public Map<String, String> getEntityDataById(String entityName, long id) {
   List<Data> list = list(entityName, id);
   Map<String, String> record = new HashMap<String, String>();
   for (Data data : list) {
     record.put(data.getAttributeName(), data.getFieldValue());
   }
   return record;
 }
예제 #3
0
  public Map<String, String> saveEntityData(String entityName, Map<String, String> fieldValues) {

    Map<String, Data> oldRecord = new HashMap<String, Data>();
    long id;
    String oid = fieldValues.get(ID);
    if (oid == null) {
      // entity "insert"
      Data data = new Data();
      data.setAttributeName(ID);
      data.setFieldValue("0");
      data.setEntityId(0);
      data.setEntityName(entityName);
      id =
          getMaxLogicalId(entityName)
              + 1; // needs to be max of logical ids in case of import foreign ids
      data.setEntityId(id);
      data.setFieldValue("" + id);
      data = save(data);
      fieldValues.put(ID, "" + id);
    } else {
      // entity "update"
      id = Long.valueOf(oid);
      List<Data> list = list(entityName, id);
      for (Data data : list) {
        oldRecord.put(data.getAttributeName(), data);
      }
    }

    List<Data> adds = new ArrayList<Data>();
    List<Data> deletes = new ArrayList<Data>();
    for (Map.Entry<String, String> me : fieldValues.entrySet()) {
      String newValue = me.getValue();
      Data oldValue = oldRecord.get(me.getKey());
      if (oldValue == null) {
        Data newData = new Data();
        newData.setAttributeName(me.getKey());
        newData.setEntityId(id);
        newData.setEntityName(entityName);
        newData.setFieldValue("" + me.getValue());
        adds.add(newData);
      } else if (newValue != null && !newValue.equals(oldValue.getFieldValue())) {
        oldValue.setFieldValue(newValue);
        adds.add(oldValue);
      }
    }
    for (Map.Entry<String, Data> me : oldRecord.entrySet()) {
      if (fieldValues.get(me.getKey()) == null) {
        deletes.add(me.getValue());
      }
    }
    for (Data data : deletes) delete(data);
    for (Data data : adds) save(data);

    return fieldValues;
  }