
package usda.weru.remoteDataAccess.jdbc.soil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import usda.weru.remoteDataAccess.exceptions.RdaConnectException;
import usda.weru.util.wepsFileChooser2.WepsFileTypes2;

/**
 *
 * @author mhaas
 */
public class JdbcSoilInterfaceMapunit extends JdbcSoilInterface {
       
    public JdbcSoilInterfaceMapunit (SoilCatParms parms) {
        super(parms);
    }
    public JdbcSoilInterfaceMapunit (JdbcSoilInterface parent, SoilCatParms parms) {
        super(parent, parms);
    }
    
    //
    // Catalog items from a legend node are new mapunit items.
    //
    @Override
    protected ArrayList<SoilCatParms> callSoilCatalog () throws RdaConnectException {
        ArrayList<SoilCatParms> temp = new ArrayList<>();
        
        try {

            // from LegendNode.createChildren()
            Statement stmt = soilsDbConnection.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT cokey,comppct_r,compname,localphase,mukey FROM component WHERE mukey = '" + parms.c_mukey + "'");

            while (rs.next()) {
                String c_localphase = getString(rs, "localphase");
                String itemName = getString(rs, "comppct_r") + " - " + getString(rs, "compname");

                if (c_localphase != null && c_localphase.trim().length() > 0) {
                    itemName = itemName + " - " + c_localphase;
                }
                if (!itemName.endsWith(WepsFileTypes2.Soil.getExtension())) {
                    itemName += WepsFileTypes2.Soil.getExtension();
                }

                temp.add(
                    new SoilCatParms (itemName).
                        setComponentVals(getString(rs, "cokey"), getString(rs, "mukey"), getString(rs, "compname"), getString(rs, "localphase"), getString(rs, "comppct_r"))
                );
            }

//                Collections.sort(temp, c_sorter);  // From previous implementation.  Needed?
            return temp;

        } catch (SQLException e) {
            throw (new RdaConnectException("Unable to get soil map unit data", e, this.interfaceName));
        }
    }
}
