TableColumnInteger.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 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 TableColumnInteger extends TableColumn<Integer> {
    public TableColumnInteger(String[] nameDesc) {
        super(nameDesc);
        value = EvalResult.getDefaultInteger();
    }

    public TableColumnInteger(String name, String desc) {
        super(name, desc);
        value = EvalResult.getDefaultInteger();
    }

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

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

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

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

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