pcwa.core.scope('pcwa.report.jasper');

pcwa.report.jasper.JasperReportService = {
	serverMap: {
		'vm-coldfusion.co.pierce.wa.us': {
			protocol:	'http',
			host:		'vm-jasper.co.pierce.wa.us',
			port:		8080
		},
		'toto.co.pierce.wa.us': {
			protocol:	'http',
			host:		'jaspertest-2-0-5.co.pierce.wa.us',
			port:		8080
		},
		'oz.co.pierce.wa.us': {
			protocol:	'http',
			host:		'jasper-2-0-5.co.pierce.wa.us',
			port:		8080
		},
		'sbapp.co.pierce.wa.us': {
			protocol:	'http',
			host:		'jasper-2-0-5.co.pierce.wa.us',
			port:		8080
		}
	},
	
	request: function requestReport(request){
		var url = encodeURI(this.getUrl(request) + request.getQuery());
		var popup = window.open(url, request.windowName || '', request.getWindowOptions());
		if ( popup ){
			popup.focus();
		}
		return popup;
	},	
	getUrl: function getUrl(request){
		var h = location.host;
		var url = this.getServerMap(h);
		url += request.reportServer || this.getHost(h);
		url += request.reportService;
		return url;
	},
	getServerMap: function getServerMap(h){
		var url = this.serverMap[h].protocol + '://';
		return url;
	},	
	getHost: function getHost(h){
		var url = this.serverMap[h].host + ':' + this.serverMap[h].port;
		return url;
	},
	getServerUrl: function getServerUrl(){
		var h = location.host;
		var url = this.getServerMap(h) + this.getHost(h);
		return url;
	}
};

pcwa.report.jasper.ReportRequest = function ReportRequest(config){
	/* @config
	 *	{
	 *		reportServer	//optional - (String) the address of the report server to use
	 *		windowName:		//optional - (String) the name to use for the popup window
	 *		windowOptions:	//optional - (String/Array) used to build the options parameter for
	 *									 the open method of the window object when opening the popup.
	 *									 See window.open() in the HTML DOM Reference for details. Strings
	 *									 may be comma delimited.
	 *	}
	 */
	this.reportServer = '';
	this.windowName = '';
	this.windowOptions = '';
	
	Ext.apply(this, config);
};
pcwa.report.jasper.ReportRequest.prototype = {
	reportService: '',
	
	reportRoot: '/pierce-application/report/',
	
	getWindowOptions: function getWindowOptions(){
		var ostr = '';
		
		if ( this.windowOptions ){
	    	if ( !(this.windowOptions instanceof Array) ){
	    		this.windowOptions = [this.windowOptions];
	    	}
			for (var i = 0; i < this.windowOptions.length; i++ ){
				ostr += (ostr.length > 0 ? ',' : '') + this.windowOptions[i];
			}
		}
		return ostr;
	},
	
	getQuery: function getQuery(){
	}
};

pcwa.report.jasper.DynamicReportRequest = function DynamicReportRequest(config){
	/* @config
	 *	{
	 *		reportPath:		//required - (String) relative to the service defined report root
	 *		reportName:		//required - (String) name of the .jasper report file minus the extension
	 *		exportType:		//optional - (String) valid values are 'pdf'(default), 'xls', 'xml'
	 *		params:			//optional - (Object) an object hash of additional parameters to the report
	 *		reportServer	//optional - (String) the address of the report server to use
	 *		windowName:		//optional - (String) the name to use for the popup window
	 *		windowOptions:	//optional - (String/Array) used to build the options parameter for
	 *									 the open method of the window object when opening the popup.
	 *									 See window.open() in the HTML DOM Reference for details. Strings
	 *									 may be comma delimited.
	 *	}
	 */
	this.reportPath = '';
	this.reportName = '';
	this.exportType = '';
	this.params = '';
	
	pcwa.report.jasper.DynamicReportRequest.superclass.constructor.call(this, config);
};
Ext.extend(pcwa.report.jasper.DynamicReportRequest, pcwa.report.jasper.ReportRequest, {
	
	getQuery: function getQuery(){
		var qstr = '?reportpath=' + this.reportRoot + this.reportPath;
		qstr += '&reportname=' + this.reportName;
		
		if ( this.exportType ){
			qstr += '&exporttype=' + this.exportType;
		}
		
		if ( this.params ){
			for (var param in this.params){
				qstr += '&' + param + '=' + this.params[param];
			}
		}
		return qstr;
	}
});

pcwa.report.jasper.XmlReportRequest = function XmlReportRequest(config){
	/* @config
	 *	{
	 *		reportPath:		//required - (String) relative to the service defined report root
	 *		reportName:		//required - (String) name of the .jasper report file minus the extension
	 *		xml:			//optional - (String) a string representation of the xml document used to
	 *									 provide source data for the report. Must provide either xml or
	 *									 xmlFile. This option is not recommended for large datasets since
	 *									 the xml will be transferred to the server via the url query string.
	 *		xmlFile:		//optional - (String) the path to a file on the report server used to
	 *									 provide source data for the report. Must provide either xml or
	 *									 xmlFile.
	 *		xPath			//optional - (String) the xpath expression used by the report to select
	 *									 select records from the xml source data.
	 *		exportType:		//optional - (String) valid values are 'pdf'(default), 'xls', 'xml'
	 *		params:			//optional - (Object) an object hash of additional parameters to the report
	 *		reportServer	//optional - (String) the address of the report server to use
	 *		windowName:		//optional - (String) the name to use for the popup window
	 *		windowOptions:	//optional - (String/Array) used to build the options parameter for
	 *									 the open method of the window object when opening the popup.
	 *									 See window.open() in the HTML DOM Reference for details. Strings
	 *									 may be comma delimited.
	 *	}
	 */
	this.xml = '';
	this.xmlFile = '';
	this.xPath = '';
	
	pcwa.report.jasper.XmlReportRequest.superclass.constructor.call(this, config);
};
Ext.extend( pcwa.report.jasper.XmlReportRequest, pcwa.report.jasper.DynamicReportRequest, {
	reportService: '/jasper/FillAndExportXMLReport',
	
	getQuery: function getQuery(){
		var qstr = pcwa.report.jasper.XmlReportRequest.superclass.getQuery.call(this);		
		if ( this.xmlFile ){
			qstr += '&xmlfile=' + this.xmlFile;
		}else if ( this.xml ){
			qstr += '&xml=' + this.xml;
		}	
		if ( this.xPath ){
			qstr += '&xpath=' + this.xPath;
		}
		return qstr;
	}
});

pcwa.report.jasper.DbReportRequest = function DbReportRequest(config){
	/* @config
	 *	{
	 *		reportPath:		//required - (String) relative to the service defined report root
	 *		reportName:		//required - (String) name of the .jasper report file minus the extension
	 *		dsn:			//required - (String) datasource name, e.g. 'venussyb.webuser'
	 *		catalog:		//optional - (String) database name, defaults to ''
	 *		exportType:		//optional - (String) valid values are 'pdf'(default), 'xls', 'xml'
     *      publicRequest:  //optional - (String Y/N) Y to use generic webuser login. Otherwise, use named
     *                                   user account.  Defaults to ''.
	 *		params:			//optional - (Object) an object hash of additional parameters to the report
	 *		reportServer	//optional - (String) the address of the report server to use
	 *		windowName:		//optional - (String) the name to use for the popup window
	 *		windowOptions:	//optional - (String/Array) used to build the options parameter for
	 *									 the open method of the window object when opening the popup.
	 *									 See window.open() in the HTML DOM Reference for details. Strings
	 *									 may be comma delimited.
	 *	}
	 */
	this.dsn = '';
	this.catalog = '';
    this.publicRequest = 'N';
	
	pcwa.report.jasper.DbReportRequest.superclass.constructor.call(this, config);
};
Ext.extend( pcwa.report.jasper.DbReportRequest, pcwa.report.jasper.DynamicReportRequest, {
	reportService: '/jasper/FillAndExportDbReport',
	
	getQuery: function getQuery(){
		var qstr = pcwa.report.jasper.DbReportRequest.superclass.getQuery.call(this);
		qstr += '&dsn=' + this.dsn;
		if ( this.catalog ){
			qstr += '&catalog=' + this.catalog;
		}
		if ( this.publicRequest ){
			qstr += '&publicrequest=' + this.publicRequest;
		}
		return qstr;
	}
});


pcwa.report.jasper.CompileReportRequest = function CompileReportRequest(config){
	/* @config
	 *	{
	 *		reportPath:		//required - (String) Path to the file or directory to be compiled
	 *									 relative to the service defined report root. If a directory
	 *									 is specified, all report design files (.jrxml) will be compiled.
	 *		reportServer	//optional - (String) the host report server to use
	 *		windowName:		//optional - (String) the name to use for the popup window
	 *		windowOptions:	//optional - (String/Array) used to build the options parameter for the
	 *									 open method of the window object when opening the popup.
	 *									 See window.open() in the HTML DOM Reference for details. Strings
	 *									 may be comma delimited.
	 *	}
	 */
	this.reportPath = '';
	
	Ext.apply(this, config);
};
Ext.extend(pcwa.report.jasper.CompileReportRequest, pcwa.report.jasper.ReportRequest, {
	reportService: '/jasper/CompileJasperReport',
	
	getQuery: function getQuery(){
		var qstr = '?reportpath=' + this.reportRoot + this.reportPath;
		return qstr;
	}
});


