Fragments.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.TableFragments;

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

    public static String FRAGMENTS_LIST = "fragments";
    
    protected static ArrayList<String> fragmentsRequiredInputs = new ArrayList<>();
    protected static ArrayList<String> fragmentsUsedColumns = new ArrayList<>();
    
    public synchronized static void setRequiredInputs(List<String> requiredInputs) {
        fragmentsRequiredInputs.clear();
        if (null != requiredInputs) {
            fragmentsRequiredInputs.addAll(requiredInputs);
            fragmentsUsedColumns.addAll(requiredInputs);
        }
    }
    
    public synchronized static void setDefaultUsedColumns(List<String> usedColumns) {
        fragmentsUsedColumns.clear();
        if (null != usedColumns) {
            fragmentsUsedColumns.addAll(usedColumns);
        }
    }
    
    private final TableFragments tableFragments = new TableFragments();
    //private final TableFragmentsCalculations tableComponentCalculatoins = new TableFragmentsCalculations();

    public Fragments() {
        tableFragments.setUsedColumns(fragmentsUsedColumns);
        tableFragments.setRequiredColumns(fragmentsRequiredInputs);
    }
    
    public Fragments(JSONArray dataJSON) throws ServiceException, JSONException {
        tableFragments.setUsedColumns(fragmentsUsedColumns);
        tableFragments.setRequiredColumns(fragmentsRequiredInputs);
        
        if ((null != dataJSON) && (dataJSON.length() > 0)) {
            Map<String, JSONObject> fragmentsDataArray = JSONUtils.preprocess(dataJSON);
            
            tableFragments.readValuesFromJSON(fragmentsDataArray);
            
        } else {
            throw new ServiceException("No mapunit JSON specified.  A NULL or empty value was passed to Fragments()");
        }
    }
    
    public Fragments(ResultSet results) throws ServiceException {
        tableFragments.setUsedColumns(fragmentsUsedColumns);
        tableFragments.setRequiredColumns(fragmentsRequiredInputs);
        
        tableFragments.readValuesFromSQL(results);
        
    }
    
    public void setUsedColumns(List<String> usedList) {
        tableFragments.setUsedColumns(usedList);
    }
    
    public void setOutputColumns(List<String> usedList) {
        tableFragments.setOutputColumns(usedList);
    }
    
    public void setOutputColumnOrdering(List<String> usedList) {
        tableFragments.setOutputColumnOrdering(usedList);
    }    
    
    public Fragments deepCopy(){
        Fragments tFragment = new Fragments();
        
        deepCopy(tFragment);
        
        return tFragment;
    }
    
    public void deepCopy(Fragments newFragment){
        if (null != newFragment) {
            tableFragments.deepCopy(newFragment.tableFragments);            
        }        
    }
    
    public boolean isEqual(Fragments tFragment) {
        boolean ret_val = false;
        if (null != tFragment) {
            if (this.fraghard() == null ? tFragment.fraghard() == null : this.fraghard().equals(tFragment.fraghard())) {
                if (this.fragkind() == null ? tFragment.fragkind() == null : this.fragkind().equals(tFragment.fragkind())) {
                    if (this.fraground() == null ? tFragment.fraground() == null : this.fraground().equals(tFragment.fraground())) {
                        if (this.fragshp() == null ? tFragment.fragshp() == null : this.fragshp().equals(tFragment.fragshp())) {
                            if (this.fragsize_h() == tFragment.fragsize_h()) {
                                if (this.fragsize_l() == tFragment.fragsize_l()) {
                                    if (this.fragsize_r() == tFragment.fragsize_r()) {
                                        if (this.fragvol_h() == tFragment.fragsize_h()) {
                                            if (this.fragvol_l() == tFragment.fragsize_l()) {
                                                if (this.fragvol_r() == tFragment.fragsize_r()) {
                                                    ret_val = true;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        
        return ret_val;
    }
    
    public void readFromSQL(ResultSet results) throws ServiceException {
        tableFragments.readValuesFromSQL(results);
    }
    
    public final void fragvol_l(double value) {
        tableFragments.fragvol_l(value);
    }
    
    public final double fragvol_l() {
        return tableFragments.fragvol_l();
    }
    
    public final void fragvol_r(double value) {
        tableFragments.fragvol_r(value);
    }
    
    public final double fragvol_r() {
        return tableFragments.fragvol_r();
    }
    
    public final void fragvol_h(double value) {
        tableFragments.fragvol_h(value);
    }
    
    public final double fragvol_h() {
        return tableFragments.fragvol_h();
    }
    
    public final void fragkind(String value) {
        tableFragments.fragkind(value);
    }
    
    public final String fragkind() {
        return tableFragments.fragkind();
    }
    
    public final void fragsize_l(double value) {
        tableFragments.fragsize_l(value);
    }
    
    public final double fragsize_l() {
        return tableFragments.fragsize_l();
    }
    
    public final void fragsize_r(double value) {
        tableFragments.fragsize_r(value);
    }
    
    public final double fragsize_r() {
        return tableFragments.fragsize_r();
    }
    
    public final void fragsize_h(double value) {
        tableFragments.fragsize_h(value);
    }
    
    public final double fragsize_h() {
        return tableFragments.fragsize_h();
    }
    
    public final void fragshp(String value) {
        tableFragments.fragshp(value);
    }
    
    public final String fragshp() {
        return tableFragments.fragshp();
    }
    
    public final void fraground(String value) {
        tableFragments.fraground(value);
    }
    
    public final String fraground() {
        return tableFragments.fraground();
    }
    
    public final void fraghard(String value) {
        tableFragments.fraghard(value);
    }
    
    public final String fraghard() {
        return tableFragments.fraghard();
    }
    
    public final void chkey(String value) {
        tableFragments.chkey(value);
    }
    
    public final String chkey() {
        return tableFragments.chkey();
    }
    
    public final void chfragskey(String value) {
        tableFragments.chfragskey(value);
    }
    
    public final String chfragskey() {
        return tableFragments.chfragskey();
    }

    /**
     *
     * @return @throws JSONException
     */
    public JSONArray toJSON() throws JSONException {
        JSONArray ret_val = new JSONArray();
        this.tableFragments.toJSON(ret_val);
        
        return ret_val;
    }

    /**
     *
     * @return @throws JSONException
     */
    public JSONObject toBasicJSON() throws JSONException {
        JSONObject ret_val = new JSONObject();
        this.tableFragments.toBasicJSON(ret_val);
        
        return ret_val;
    }    
}