<commit_before>/*
 * Copyright 2015 University of Oxford
 *
 * 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 uk.ac.ox.it.ords.api.database.queries;

import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import uk.ac.ox.it.ords.api.database.data.DataCell.DataType;
import uk.ac.ox.it.ords.api.database.data.DataTypeMap;

public class FilteredQuery {
	
	String filter;
	ParameterList parameters;
	
	public FilteredQuery(String filter, String params, boolean isCaseSensitive){
		try {
			this.parameters = getParameterList(params);
		} catch (Exception e) {
			this.parameters = new ParameterList();
		}
		this.filter = createFilterFromParameters(normaliseFilterQuery(filter), parameters, isCaseSensitive);
		if (this.parameters == null){
			this.parameters = new ParameterList();
		}
	}
	
	public FilteredQuery(String filter, ParameterList params, boolean isCaseSensitive){
		this.parameters = params;
		this.filter = createFilterFromParameters(normaliseFilterQuery(filter), parameters, isCaseSensitive);
		if (this.parameters == null){
			this.parameters = new ParameterList();
		}
	}
	
	public FilteredQuery(){
		
	}
	
	/**
	 * Returns a query based on a filter string and parameter string
	 */
	public String getFilter( 
			String filter, 
			String params, 
			boolean isCaseSensitive
			) throws ClassNotFoundException, SQLException, Exception {
		ParameterList parameters = getParameterList(params);
		String query;
		try {<commit_msg>Added comments and protected non-public methods<commit_after>/*
 * Copyright 2015 University of Oxford
 *
 * 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 uk.ac.ox.it.ords.api.database.queries;

import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import uk.ac.ox.it.ords.api.database.data.DataCell.DataType;
import uk.ac.ox.it.ords.api.database.data.DataTypeMap;

/**
 * 
 * This is a helper class for working with filter parameters supplied when using RedQueryBuilder on
 * the front end.
 * 
 * RQB supplies a Filter SQL string, and an array of parameters.
 * 
 * These need to be reformatted to be fed into queries, which is where this class comes in. 
 * 
 * Use the "raw" input from RQB in the constructor; then the Get methods to access the parsed
 * filter string and the normalized ParameterList. If the filter supplied is invalid, null will
 * be returned.
 *
 */
public class FilteredQuery {
	
	String filter;
	ParameterList parameters;
	
	public FilteredQuery(String filter, String params, boolean isCaseSensitive){
		try {
			this.parameters = getParameterList(params);
		} catch (Exception e) {
			this.parameters = new ParameterList();
		}
		this.filter = createFilterFromParameters(normaliseFilterQuery(filter), parameters, isCaseSensitive);
		if (this.parameters == null){
			this.parameters = new ParameterList();
		}
	}
	
	/*
	 * The default constructor is only used for unit testing
	 */
	public FilteredQuery(){
	}
	
	/**
	 * Returns a query based on a filter string and parameter string
	 * @param filter
	 * @param params
	 * @param isCaseSensitive
	 * @return the filtered query, or null if invalid
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 * @throws Exception
	 */
	protected String getFilter( 
			String filter, 
			String params, 
			boolean isCaseSensitive
			) throws ClassNotFoundException, SQLException, Exception {
		ParameterList parameters = getParameterList(params);
		String query;
		try {
{'fraction_non_alphanumeric': 0.08464354942924833, 'fraction_numerical': 0.0034460478139134183, 'mean_word_length': 4.069868995633188, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6721769', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1243, 'n_words': 462}