Texture.java [src/soils] 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;

import csip.api.server.ServiceException;
import csip.utils.JSONUtils;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import soils.db.tables.TableTexture;

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

    public static String TEXTURE_LIST = "textures";

    protected static ArrayList<String> textureRequiredInputs = new ArrayList<>();
    protected static ArrayList<String> textureUsedColumns = new ArrayList<>();

    public synchronized static void setRequiredInputs(List<String> requiredInputs) {
        textureRequiredInputs.clear();
        if (null != requiredInputs) {
            textureRequiredInputs.addAll(requiredInputs);
            textureUsedColumns.addAll(requiredInputs);
        }
    }

    public synchronized static void setDefaultUsedColumns(List<String> usedColumns) {
        textureUsedColumns.clear();
        if (null != usedColumns) {
            textureUsedColumns.addAll(usedColumns);
        }
    }

    public static ArrayList<String> getDefaultUsedColumns() {
        return ((null != textureUsedColumns) ? ((ArrayList<String>) textureUsedColumns.clone()) : null);
    }

    public static ArrayList<String> getRequiredInputs() {
        return ((null != textureRequiredInputs) ? ((ArrayList<String>) textureRequiredInputs.clone()) : null);
    }

    private final TableTexture tableTexture = new TableTexture();

    public Texture() {
        tableTexture.setUsedColumns(textureUsedColumns);
        tableTexture.setRequiredColumns(textureRequiredInputs);
    }

    public Texture(JSONArray dataJSON) throws ServiceException, JSONException {
        tableTexture.setUsedColumns(textureUsedColumns);
        tableTexture.setRequiredColumns(textureRequiredInputs);

        if ((null != dataJSON) && (dataJSON.length() > 0)) {
            Map<String, JSONObject> fragmentsDataArray = JSONUtils.preprocess(dataJSON);

            tableTexture.readValuesFromJSON(fragmentsDataArray);

        } else {
            throw new ServiceException("No mapunit JSON specified.  A NULL or empty value was passed to TextureGroup()");
        }
    }

    public Texture(ResultSet results) throws ServiceException {
        tableTexture.setUsedColumns(textureUsedColumns);
        tableTexture.setRequiredColumns(textureRequiredInputs);

        tableTexture.readValuesFromSQL(results);

    }

    public Texture deepCopy(){
        Texture tTexture = new Texture();
        deepCopy(tTexture);
        return tTexture;
    }
    
    public void deepCopy(Texture newTexture){
        if (null != newTexture){
            tableTexture.deepCopy(newTexture.tableTexture);
        }
    }
    public void setOutputColumns(List<String> usedList) {
        tableTexture.setOutputColumns(usedList);
    }

    public void setOutputColumnOrdering(List<String> usedList) {
        tableTexture.setOutputColumnOrdering(usedList);
    }

    public void setUsedColumns(List<String> usedList) {
        tableTexture.setUsedColumns(usedList);
    }

    public void readFromSQL(ResultSet results) throws ServiceException {
        tableTexture.readValuesFromSQL(results);
    }

    public JSONArray toJSON() throws JSONException {
        JSONArray outArray = new JSONArray();

        tableTexture.toJSON(outArray);

        return outArray;
    }
    
    public JSONObject toBasicJSON() throws JSONException {
        JSONObject outArray = new JSONObject();

        tableTexture.toBasicJSON(outArray);

        return outArray;
    }    

    public TableTexture getTableTexture() {
        return tableTexture;
    }

    public void merge(TextureGroup mergeThisUnit) throws ServiceException {
        tableTexture.merge(mergeThisUnit.getTableTextureGroup());
    }

    public final String chtgkey() {
        return tableTexture.chtgkey();
    }

    public final String chtkey() {
        return tableTexture.chtkey();
    }

    public final String texcl() {
        return tableTexture.texcl();
    }

    public final String lieutex() {
        return tableTexture.lieutex();
    }

}