TableColumn.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 csip.api.server.ServiceException;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import soils.filters.DataFilter;
import soils.filters.DataFilter.Properties;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 * @param <C>
 */
public abstract class TableColumn<C> {

  public static final int COLUMN_DESC = 1;
  public static final int COLUMN_FORMAT = 3;
  public static final int COLUMN_NAME = 0;
  public static final int COLUMN_UNIT = 2;

  public String name;
  public String description;
  public String unit;
  public String format;
  public C value;
  public String altJSONName;
  public String altSQLName;
  private boolean use = true;
  private boolean writeToJSON = false;
  private boolean required = false;
  protected boolean foundInJSON = false;

  TableColumn(String[] nameDesc) {
    this.name = nameDesc[COLUMN_NAME];
    if (nameDesc.length > 1) {
      this.description = nameDesc[COLUMN_DESC];
      if (nameDesc.length > 2) {
        this.unit = nameDesc[COLUMN_UNIT];
        if (nameDesc.length > 3) {
          this.format = nameDesc[COLUMN_FORMAT];
        }
      }
    }
  }

  TableColumn(String name, String desc) {
    this.name = name;
    this.description = desc;
    this.unit = null;
    this.format = null;
  }

  TableColumn(String name, String desc, String unit) {
    this.name = name;
    this.description = desc;
    this.unit = unit;
    this.format = null;
  }

  TableColumn(String name, String desc, String unit, String format) {
    this.name = name;
    this.description = desc;
    this.unit = unit;
    this.format = format;
  }

  public boolean wasSetInJSON() {
    return foundInJSON;
  }

  public void wasSetInJSON(boolean wasSet) {
    foundInJSON = wasSet;
  }

  public boolean isUsed() {
    return use;
  }

  public void setUsed(boolean used) {
    use = used;
  }

  public boolean includeInOutput() {
    return writeToJSON;
  }

  public void setIncludeInOutput(boolean include) {
    writeToJSON = include;
  }

  public void setValue(C value) {
    this.value = value;
  }

  public C getValue() {
    C ret_val = value;
    return ret_val;
  }

  public void setRequired(boolean required) {
    this.required = required;
  }

  public boolean isRequired() {
    return required;
  }

  public void valueFromSQL(ResultSet results) throws ServiceException {
    try {
        readSQLValue(results);
    } catch (SQLException se) {
      throw new ServiceException("Cannot read value from SQL results for " + name + ". Error: " + se.getMessage(), se);
    }
  }

  protected abstract void readSQLValue(ResultSet results) throws SQLException;

  public abstract void valueFromJSON(JSONObject dataJSON) throws ServiceException;

  public abstract JSONObject toJSON() throws JSONException;

  public abstract JSONObject toBasicJSON() throws JSONException;

  public abstract String toWriteString();

  public boolean isEqual(Object compareValue) {
    if (this.value.getClass().getName().equals(compareValue.getClass().getName())) {
      return getValue() == compareValue;
    }
    return false;
  }
}