TableColumnString.java [src/soils/db/tables] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package soils.db.tables;

import static csip.ModelDataService.KEY_UNIT;
import soils.utils.EvalResult;
import csip.api.server.ServiceException;
import csip.utils.JSONUtils;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class TableColumnString extends TableColumn<String> {

  public TableColumnString(String[] nameDesc) {
    super(nameDesc);
    value = EvalResult.getDefaultString();
  }

  public TableColumnString(String name, String desc) {
    super(name, desc);
    value = EvalResult.getDefaultString();
  }

  public TableColumnString(String name, String desc, String unit) {
    super(name, desc, unit);
    value = EvalResult.getDefaultString();
  }

  public TableColumnString(String name, String desc, String unit, String format) {
    super(name, desc, unit, format);
    value = EvalResult.getDefaultString();
  }

  @Override
  protected void readSQLValue(ResultSet results) throws SQLException {
    value = EvalResult.getString(results, name);
  }

  @Override
  public void valueFromJSON(JSONObject dataJSON) throws ServiceException {
    foundInJSON = true;
    value = EvalResult.getStringValue(dataJSON);
  }

  @Override
  public JSONObject toJSON() throws JSONException {
    JSONObject ret_val = null;

    if (includeInOutput()) {
      foundInJSON = true;
      if ((null != unit) && (!unit.isEmpty())) {
        ret_val = JSONUtils.data(name, EvalResult.writeString(value), description, unit);
      } else {
        ret_val = JSONUtils.dataDesc(name, EvalResult.writeString(value), description);
      }
    }

    return ret_val;
  }

  @Override
  public JSONObject toBasicJSON() throws JSONException {
    JSONObject ret_val = null;

    if (includeInOutput()) {
      foundInJSON = true;
      if ((null != unit) && (!unit.isEmpty())) {
        ret_val.put(name, EvalResult.writeString(value));
        ret_val.put(KEY_UNIT, unit);
      } else {
        ret_val.put(name, EvalResult.writeString(value));
      }
    }
    return ret_val;
  }

  @Override
  public String toWriteString() {
    return EvalResult.writeString(value);
  }

  @Override
  public boolean isEqual(Object compareValue) {
    if (compareValue.getClass().getName().equals(this.getClass().getName())) {
      return this.value.equals((String) compareValue);
    }
    return false;
  }
}