/*
 * Copyright (C) 2016 Simbiose Ventures.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.slicingdice.jslicer.utils.validators;

import com.slicingdice.jslicer.exceptions.client.InvalidQueryException;
import com.slicingdice.jslicer.exceptions.client.MaxLimitException;
import org.json.JSONObject;

import java.util.Iterator;

/**
 * Slicing Dice validator to query JSONObject.
 *
 * @author Simbiose Ventures
 * @version 0.2
 * @since 2016-08-10
 */
public class QueryDataExtractionValidator {
    private final JSONObject data;

    public QueryDataExtractionValidator(final JSONObject data) {
        this.data = data;
    }

    /**
     * Check if all keys in JSONObject are valid.
     * @return true if all keys in JSONObject are valid and false otherwise
     */
    private boolean validKeys() {
        final Iterator<?> keys = this.data.keys();
        while (keys.hasNext()) {
            final String key = (String) keys.next();
            if (key.equals("limit")) {
                if (!(this.data.get(key) instanceof Integer)) {
                    throw new InvalidQueryException("The key 'limit' in query has a invalid value.");
                }
            } else if (key.equals("columns")) {
                if (this.data.getJSONArray("columns").length() > 10) {
                    throw new MaxLimitException("The key 'columns' in data extraction result must" +
                            " have up to 10 columns.");
                }
            }
        }
        return true;
    }

    /**
     * Validate data extraction query
     * @return true if data extraction query is valid and false otherwise
     */
    public boolean validator() {
        return this.validKeys();
    }
}

@@ -787,24 +787,51 @@
 tException;%0A
+import org.json.JSONArray;%0A
 import org.j
@@ -1781,44 +1781,153 @@
     
-if (this.data.getJSONArray(%22
+final Object columns = this.data.get(%22columns%22);%0A                if (columns instanceof JSONArray) %7B%0A                    if (((JSONArray) 
 columns
-%22
 ).le
@@ -1953,32 +1953,36 @@
                 
+ 
+   
 throw new MaxLim
@@ -2041,69 +2041,568 @@
 sult
- must%22 +%0A                            %22 have up to 10 columns.
+%22 +%0A                                %22 must have up to 10 columns.%22);%0A                    %7D%0A                %7D else if (columns instanceof String) %7B%0A                    if (!columns.equals(%22all%22)) %7B%0A                        throw new InvalidQueryException(%22The key 'columns' should be a list of%22 +%0A                                %22 columns or the keyword 'all'%22);%0A                    %7D%0A                %7D else %7B%0A                    throw new InvalidQueryException(%22The key 'columns' should be a list of%22 +%0A                            %22 columns or the keyword 'all'
 %22);%0A

Correct data extraction validator