Source: model/QueryConfig.js

Ext.ns('Oracle.BDD.Portlets.Visualization.Model');

/**
 * <b>Do NOT instantiate this class directly.</b>  A QueryConfig object is acquired through one of two means:
 * via {@link Oracle.BDD.Portlets.Visualization.Renderers.BaseRenderer#getQueryConfig} or from the
 * QueryResults object that is provided as an argument to the callback function of 
 * {@link Oracle.BDD.Portlets.Visualization.Renderers.BaseRenderer#executeQuery}
 * <p>
 *  For example:
 * <pre>
 *     init: function() {
 *        
 *        var queryConfig = this.getQueryConfig("eql");
 *        this.executeQuery("eql", true, this.renderVisualization);
 *
 *     },
 *   
 *     renderVisualization: function(queryResults) {
 *
 *        var queryConfig = queryResults.getQueryConfig();
 *        
 *         ...
 *	   }
 * </pre>   
 * @class Oracle.BDD.Portlets.Visualization.Model.QueryConfig
 * 
 */
Oracle.BDD.Portlets.Visualization.Model.QueryConfig = function(config) {
		
	this._queryName = config.queryName;
	this._tokens = config.tokens || {};
	this._refinements = config.refinements || {};
		
	return this;
};

Oracle.BDD.Portlets.Visualization.Model.QueryConfig.prototype = {
	
	_queryName: undefined,
	_refinements: {},
	_tokens: {},
	
    /** 
     * Adds a refinement of the attribute of the given token and value to the query.  Adding a refinement not only refines the 
	 * visualization's data, but also refreshes all portlets on the page that respond to data change events call.
     * @param {string} tokenName The name of the attribute token the attribute of which the refinement is to apply
     * @param {string|Number} value The value to refine by
	 * @param {string|Number|Object[]} value The value to refine by.  
	 * An array may be  passed to add all enclosed values to the refinement.
     */
    addRefinement: function(tokenName, value) {

        if (Ext.isEmpty(tokenName) || Ext.isEmpty(value)) return;
    	
        if (this._tokens && this._tokens[tokenName]) {
        	
        	var token = this._tokens[tokenName];
        	var attributeKey = token.attributeKey;
        	var viewKey = token.dataviewKey;
        	
        	value = !Ext.isArray(value) ? [value] : value; 
            this._refinements = this._refinements || {};
            
            this._refinements[viewKey] = this._refinements[viewKey] || {};
            var currentRefinements = this._refinements[viewKey][attributeKey] || [];
        	
            Ext.each(value, function(val) {
                if (currentRefinements.indexOf(val) < 0) {
                    currentRefinements.push(val);
                }
            });

            this._refinements[viewKey][attributeKey] = currentRefinements;
        }
        
        return;
		
    },
    
    /** 
     * Returns the subclassed {@link Oracle.BDD.Portlets.Visualization.Model.Token} 
     * of the given name.
     * @memberof Oracle.BDD.Portlets.Visualization.Model.QueryConfig#
     * @param {string} tokenName The name of the token to return
    */
    getToken: function(tokenName) {
    	
    	if (this._tokens && this._tokens.hasOwnProperty(tokenName)) {
    		
    		var token = this._tokens[tokenName];
    			    		 
    		var tkn;
    		switch(token.type) {
    			case Oracle.BDD.Portlets.Visualization.Constants.TOKEN_TYPE_METRIC:
					tkn = new Oracle.BDD.Portlets.Visualization.Model.MetricToken(token);
					break;
				case Oracle.BDD.Portlets.Visualization.Constants.TOKEN_TYPE_DIMENSION:
					tkn = new Oracle.BDD.Portlets.Visualization.Model.DimensionToken(token);
					break;
				case Oracle.BDD.Portlets.Visualization.Constants.TOKEN_TYPE_SORT:
					tkn = new Oracle.BDD.Portlets.Visualization.Model.SortToken(token);
    				break;
    			case Oracle.BDD.Portlets.Visualization.Constants.TOKEN_TYPE_DATA:
    				tkn = new Oracle.BDD.Portlets.Visualization.Model.DataToken(token);
    				break;
    			case Oracle.BDD.Portlets.Visualization.Constants.TOKEN_TYPE_DATAVIEW:
    				tkn = new Oracle.BDD.Portlets.Visualization.Model.DataViewToken(token);
    				break;
    			default:
    				tkn = new Oracle.BDD.Portlets.Visualization.Model.Token(token);
    		}
    		
    		
    		return tkn;
    	}
    	
    	return;
    	//TODO: throw exception if no token is found
    },
    
    /**
	 * Gets the query name
	 * @return {string} queryName The queryName
	 */
    getQueryName: function() {
    	return this._queryName;
    },
     
    /** Private **/
    		
	/**
     * @private
     */
    getRefinements: function() {
		return this._refinements || {};
	},
 
	    /**
     * @private
     */
    getTokens: function() {
    	return this._tokens || {};
    },
    
    /**
	 * @private
	 */
    setQueryName: function(queryName) {
    	this._queryName = queryName;
    },
    	    
    /**
     * @private
     */
    setTokens: function(tokens) {
    	this._tokens = tokens;
    },
    
    /**
     * @private
     */
    toRequest: function() {
    	return {
			queryName : this.getQueryName(),
			tokens : this.getTokens(),
			refinements : this.getRefinements()
		};
	}

};
//@ sourceURL=QueryConfig.js