TableColumnDate.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 csip.api.server.ServiceException;
import csip.utils.JSONUtils;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import soils.utils.EvalResult;

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

  public TableColumnDate(String[] nameDesc) {
    super(nameDesc);
  }

  public TableColumnDate(String name, String desc) {
    super(name, desc);
  }

  public TableColumnDate(String name, String desc, String unit) {
    super(name, desc, unit);
  }

  public TableColumnDate(String name, String desc, String unit, String format) {
    super(name, desc, unit);
  }

  @Override
  public void readSQLValue(ResultSet results) throws SQLException {
    setValue(EvalResult.getDate(results, name));
  }

  @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.writeDate(value, format), description, unit);
      } else {
        ret_val = JSONUtils.dataDesc(name, EvalResult.writeDate(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.writeDate(value, format));
        ret_val.put(KEY_UNIT, unit);
      } else {
        ret_val.put(name, EvalResult.writeDate(value, format));
      }
    }
    return ret_val;
  }

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

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

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

  }
}