TableColumnDouble.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 TableColumnDouble extends TableColumn<Double> {

  public TableColumnDouble(String[] nameDesc) {
    super(nameDesc);
    value = EvalResult.getDefaultDouble();
  }

  public TableColumnDouble(String name, String desc) {
    super(name, desc);
    value = EvalResult.getDefaultDouble();
  }

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

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

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

  @Override
  public void valueFromJSON(JSONObject dataJSON) throws ServiceException {
    foundInJSON = true;
    value = EvalResult.getDoubleValue(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.writeDouble(value, format), description, unit);
      } else {
        ret_val = JSONUtils.dataDesc(name, EvalResult.writeDouble(value, format), 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.writeDouble(value, format));
        ret_val.put(KEY_UNIT, unit);
      } else {
        ret_val.put(name, EvalResult.writeDouble(value, format));
      }
    }
    return ret_val;
  }

  @Override
  public String toWriteString() {
    return EvalResult.writeDouble(value, format);
  }

  @Override
  public boolean isEqual(Object compareValue) {
    if (compareValue.getClass().getName().equals(this.getClass().getName())) {
      if (!EvalResult.testDefaultDouble(this.value) && !EvalResult.testDefaultDouble((Double) compareValue)) {
        return this.value == (Double) compareValue;
      }
    }
    return false;
  }
}