/*
 * Copyright (c) 2008 The Olympos Development Team.
 * 
 * http://sourceforge.net/projects/olympos/
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html. If redistributing this code, this
 * entire header must remain intact.
 */
Ext.namespace("cwe.navigation");

/**
 * @class Routes requests of a grid store through persistency layer.
 * 
 * @extends Ext.data.DataProxy
 * @constructor
 * @param {Object}
 *            config The configuration object.
 * @config modelDescription The Model Description to load object of.
 */
cwe.navigation.GlobalSearchProxy = function(config) {
	this.templateRecord = config.templateRecord;

	this.query = config.query;

	cwe.navigation.GlobalSearchProxy.superclass.constructor.call(this, Ext.apply(this, {}, config));
};

Ext.extend(cwe.navigation.GlobalSearchProxy, Ext.data.DataProxy);

/**
 * Loads the data.
 * 
 * <p>
 * Refer to Ext.data.DataProxy for details.
 * </p>
 */
cwe.navigation.GlobalSearchProxy.prototype.load = function(params, reader, callback, scope, arg) {
	if (this.fireEvent("beforeload", this, params) !== false) {
		var self = this;

		chi.persistency.Persistency.getInstance().search(this.query, undefined, params.limit, params.start, undefined, params.sort, params.dir ? params.dir.toLowerCase() : undefined, function(data) {
			self.loadResponse(params, data, callback, scope, arg);
		}, function(data, errorMsg) {
			self.loadFailed(params, data, errorMsg, callback, scope, arg);
		});
	} else {
		callback.call(scope || this, null, arg, false);
	}
};

/**
 * Handles the response of the JSON call to load the data.
 * 
 * <p>
 * Refer to Ext.data.DataProxy for details.
 * </p>
 */
cwe.navigation.GlobalSearchProxy.prototype.loadResponse = function(params, data, callback, scope, arg) {
	var records = [];

	for ( var currIndex in data.records) {
		var currRecord = data.records[currIndex];
		if (!(currRecord instanceof Function)) {
			var relevance = data.searchData[currRecord.getOid()].relevance;

			records.push(new this.templateRecord( {
			    oid : currRecord.getOid(),
			    iconClass : currRecord.getModelDescription().getTreeIconClass(),
			    label : currRecord.getLabel(),
			    "cwe-relative-relevance" : relevance / data.maxRelevance,
			    record : currRecord
			}));
		}
	}

	var result = {
	    success : true,
	    records : records,
	    totalRecords : data.totalCount,
	    searchData : data.searchData,
	    maxRelevance : data.maxRelevance
	};

	this.fireEvent("load", this, params, arg);
	callback.call(scope, result, arg, true);
};

/**
 * Handles an error of the JSON call to load the data.
 * 
 * <p>
 * Refer to Ext.data.DataProxy for details.
 * </p>
 */
cwe.navigation.GlobalSearchProxy.prototype.loadFailed = function(params, data, errorMsg, callback, scope, arg) {
	this.fireEvent("loadexception", this, params, data);
	callback.call(scope, null, arg, false);
};
