TableExcluded.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 org.codehaus.jettison.json.JSONArray;
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 TableExcluded extends Table {

    static final String EXCLUDED_FLAG = "excluded";
    static final String EXCLUDED_REASON = "reason";
    static final String EXCLUDED_DESC = "This soil mapunit and/or any of its components (if any) "
            + "in the area of analysis were excluded from being used for NRCS data quality assessments.  "
            + "To resolve missing data or investigate further, please contact your state NRCS data steward for assistance.";

    private boolean excluded = false;
    private String reason = EvalResult.getDefaultString();

    public void setExcluded(boolean value, String reason) {
        excluded = value;
       setExcludedReason(reason);
    }

    public void exclude() {
        excluded = true;
    }

    public void exclude(boolean value) {
        excluded = value;
        if (!excluded) {            
            reason = EvalResult.getDefaultString();
        }
    }

    public void setExcludedReason(String newReason) {
        if (!EvalResult.testDefaultEmptyString(reason)) {
            reason += " AND " + newReason;
        } else {
            reason = newReason;
        }
    }

    public String getExcludedReason() {

        return reason;
    }

    public boolean isExcluded() {
        return excluded;
    }

    public void valueFromJSON(JSONObject dataJSON) throws ServiceException {
        if (null != dataJSON) {
            dataJSON.optBoolean(EXCLUDED_FLAG, false);
            dataJSON.optString(EXCLUDED_REASON, EvalResult.getDefaultString());
        }
    }

    @Override
    public void toJSON(JSONArray outArray) throws JSONException {
        if (excluded) {
            JSONObject dataObject = new JSONObject();
            dataObject.put(EXCLUDED_FLAG, EvalResult.writeBoolean(excluded));
            dataObject.put(EXCLUDED_REASON, reason);
            dataObject.put("description", EXCLUDED_DESC);
            outArray.put(dataObject);
        }
    }
}