TextureGroup.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.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import static soils.Texture.TEXTURE_LIST;
import soils.db.tables.TableTexture;
import soils.db.tables.TableTextureGroup;

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

    public static String TEXTURE_GROUP_LIST = "texture_groups";

    protected static ArrayList<String> textureGroupRequiredInputs = new ArrayList<>();
    protected static ArrayList<String> textureGroupUsedColumns = new ArrayList<>();

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

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

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

    private final TableTextureGroup tableTextureGroup = new TableTextureGroup();

    public LinkedHashMap<String, Texture> textures = new LinkedHashMap<>();

    public TextureGroup() {
        tableTextureGroup.setUsedColumns(textureGroupUsedColumns);
        tableTextureGroup.setRequiredColumns(textureGroupRequiredInputs);
    }

    public TextureGroup(JSONArray dataJSON) throws ServiceException, JSONException {
        tableTextureGroup.setUsedColumns(textureGroupUsedColumns);
        tableTextureGroup.setRequiredColumns(textureGroupRequiredInputs);

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

            tableTextureGroup.readValuesFromJSON(fragmentsDataArray);

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

    @Deprecated
    public TextureGroup(ResultSet results) throws ServiceException {
        tableTextureGroup.setUsedColumns(textureGroupUsedColumns);
        tableTextureGroup.setRequiredColumns(textureGroupRequiredInputs);

        tableTextureGroup.readValuesFromSQL(results);

    }

    public TextureGroup deepCopy(){
        TextureGroup newGroup = new TextureGroup();
        
        deepCopy(newGroup);
        
        return newGroup;
    }
    
    public void deepCopy(TextureGroup newGroup){
    
        if ( null != newGroup){
            this.tableTextureGroup.deepCopy(newGroup.tableTextureGroup);
            
            LinkedHashMap<String, Texture> tTextures = new LinkedHashMap<>();
            for (Texture tValue : textures.values()) {
                Texture newValue = tValue.deepCopy();
                if (null != newValue) {
                    tTextures.put(newValue.chtkey(), newValue);
                }
            }

            if (tTextures.size() > 0) {
                newGroup.setTextures(tTextures);
            }            
        }
    }
    
    public void setTextures(Map<String, Texture> tTextures){
        if (( null != tTextures ) && (tTextures.size() > 0 )){
            textures.clear();
            for( Texture tTexture: tTextures.values()){
                textures.put(tTexture.chtkey(), tTexture);
            }
        }
    }
    
    
    public void setOutputColumns(List<String> usedList) {
        tableTextureGroup.setOutputColumns(usedList);
    }

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

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

    //TODO:  Does this work?  Test.
    @Deprecated
    public void readFromSQL(ResultSet results) throws ServiceException {
        tableTextureGroup.readValuesFromSQL(results);
        try {
            ResultSetMetaData metaData = results.getMetaData();
            int columnCount = metaData.getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                if (metaData.getColumnName(i).equals(TableTexture.CHTKEY)) {
                    String chtkey = results.getString(TableTexture.CHTKEY);
                    Texture tTexture = textures.get(chtkey);
                    if (null == tTexture) {
                        tTexture = new Texture();
                        textures.put(chtkey, tTexture);
                    }
                    tTexture.readFromSQL(results);
                }
            }

        } catch (SQLException ex) {
            throw new ServiceException("Could not continue to read texture data: " + ex.getMessage(), ex);
        }
    }

    public void readFromSQL(ResultSet results, List<String> textureColumns) throws ServiceException {
        if ((null != results) && (null != textureColumns)) {
            tableTextureGroup.readValuesFromSQL(results);
            try {
                ResultSetMetaData metaData = results.getMetaData();
                int columnCount = metaData.getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    if (metaData.getColumnName(i).equals(TableTexture.CHTKEY)) {
                        String chtkey = results.getString(TableTexture.CHTKEY);
                        Texture tTexture = textures.get(chtkey);
                        if (null == tTexture) {
                            tTexture = new Texture();
                            textures.put(chtkey, tTexture);
                        }
                        tTexture.setUsedColumns(textureColumns);
                        tTexture.readFromSQL(results);
                    }
                }

            } catch (SQLException ex) {
                throw new ServiceException("Could not continue to read texture data: " + ex.getMessage(), ex);
            }
        } else {
            throw new ServiceException("NULL ResultSet or missing textureColumns list passed to readFromSQL.  Cannot continue.");
        }
    }

    public JSONArray toJSON() throws JSONException {
        JSONArray ret_val = new JSONArray();
        JSONArray texturesArray = new JSONArray();

        tableTextureGroup.toJSON(ret_val);

        for (Texture texture : this.textures.values()) {
            texturesArray.put(texture.toJSON());
        }

        if (((texturesArray.length() > 0) &&(texturesArray.getJSONArray(0).length() > 0 )) && (ret_val.length() > 0)) {
            ret_val.put(JSONUtils.data(TEXTURE_LIST, texturesArray));
        }

        return ret_val;
    }

    public JSONObject toBasicJSON() throws JSONException {
        JSONObject ret_val = new JSONObject();
        JSONArray texturesArray = new JSONArray();

        tableTextureGroup.toBasicJSON(ret_val);

        for (Texture texture : this.textures.values()) {
            texturesArray.put(texture.toBasicJSON());
        }

        if ((texturesArray.length() > 0) && (ret_val.length() > 0)) {
            ret_val.put(TEXTURE_LIST, texturesArray);
        }

        return ret_val;
    }    
    public TableTextureGroup getTableTextureGroup() {
        return tableTextureGroup;
    }

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

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

    public final String description() {
        return tableTextureGroup.description();
    }

    public final void texture(String value) {
        tableTextureGroup.texture(value);
    }

    public final String texture() {
        return tableTextureGroup.texture();
    }

    public final void stratextsflag(Boolean value) {
        tableTextureGroup.stratextsflag(value);
    }

    public final Boolean stratextsflag() {
        return tableTextureGroup.stratextsflag();
    }

    public final void rvindicator(Boolean value) {
        tableTextureGroup.rvindicator(value);
    }

    public final Boolean rvindicator() {
        return tableTextureGroup.rvindicator();
    }
    
    public boolean isEqual(TextureGroup tGroup) throws ServiceException{
      return tableTextureGroup.isEqual(tGroup.getTableTextureGroup());
    }

}