PestProdListRequest.java [src/java/m/wqm/wqm03_pesticideattributes] Revision:   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 m.wqm.wqm03_pesticideattributes;

import csip.api.server.ServiceException;
import data.table.Table;
import data.table.column.ColumnInteger;
import data.table.column.ColumnString;
import java.util.ArrayList;
import java.util.Map;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import utils.EvalResult;
import utils.TableException;

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

    static final String FILTER_OPERATOR = "filter_operator";
    static final String MAX_PRODUCTS = "max_products";
    static final String[] FILTER_OPERATOR_DATA = {FILTER_OPERATOR};
    static final String[] MAX_PRODUCTS_DATA = {MAX_PRODUCTS};

    private final static String FILTER_LIST = "filters";
    private ArrayList<SearchFilter> filters = new ArrayList<>();

    PestProdListRequest(Map<String, JSONObject> request) throws ServiceException, JSONException {
        columns.put(FILTER_OPERATOR, new ColumnString(FILTER_OPERATOR_DATA));
        columns.put(MAX_PRODUCTS, new ColumnInteger(MAX_PRODUCTS_DATA));

        columns.get(FILTER_OPERATOR).setRequired(false);
        columns.get(MAX_PRODUCTS).setRequired(false);

        readValuesFromJSON(request);
        if (rowCount == 1) {
            currentRow = 0;
            if (isDefaultValue(MAX_PRODUCTS)) {
                setInteger(MAX_PRODUCTS, 40);
            }

            if (isDefaultValue(FILTER_OPERATOR) || getString(FILTER_OPERATOR).isEmpty()) {
                setString(FILTER_OPERATOR, "AND");
            }
            
            if (!validateFilterOperator()) {
                throw new ServiceException("Invalid filter operator, (" + getString(FILTER_OPERATOR) + "), specified.");
            }

            if (request.containsKey(FILTER_LIST)) {
                JSONArray filterArray = request.get(FILTER_LIST).getJSONArray(csip.ModelDataService.KEY_VALUE);
                for (int i = 0; i < filterArray.length(); i++) {
                    SearchFilter tFilter = new SearchFilter(filterArray.getJSONArray(i));
                    filters.add(tFilter);
                }
            } else {
                throw new ServiceException("No filter list, (" + FILTER_LIST + "), found in reqeust JSON");
            }

        } else {
            throw new ServiceException("Malformed request passed to PestProdList");
        }

    }

    public String filterOperator() throws ServiceException {
        return getString(FILTER_OPERATOR);
    }

    public int maxProducts() throws ServiceException {
        return getInteger(MAX_PRODUCTS);
    }
    
    public int numFilters(){
        return filters.size();
    }
    
    public ArrayList<SearchFilter> searchFilters(){
        return filters;
    }

    protected boolean validateFilterOperator() throws ServiceException {
        switch (getString(FILTER_OPERATOR).trim().toUpperCase()) {
            case "AND":
            case "OR":
                return true;
        }
        return false;
    }
}