Controller.prototype.getObject = getObject;
Controller.prototype.getResult = getResult;
Controller.prototype.getActionClass = getActionClass;
Controller.prototype.getActionMethod = getActionMethod;
Controller.prototype.getServerAction = getServerAction;
Controller.prototype.executeAction = executeAction;
Controller.prototype.getInstance = getInstance;


var persistentObjects = new Array();					// holds all the stateful objects


/**
 * @constructor
 * @param String objName name of persistent object
 */
function Controller(actionID,entityClassName,paramList,objName,isPersistent,isSynch){

	if(entityClassName){
			if(paramList)
				this.paramList=paramList;
			else
				this.paramList='';

			this.result ='';

			this.actionID = actionID;
			this.entityClassName = entityClassName;
			if(objName){
					this.objName=objName;
				}

			if(isPersistent===true){
				this.isPersistent= true;
				if(objName){
					this.objName=objName;
				}
				else
					throw Error('Controller Error: object Name is mandatory, if object is persistent');
			}
			else
				this.isPersistent= false;



			if(isSynch===true)
				this.isSynch = true;
			else
				this.isSynch = false;

			var classObj = this.getObject(); //gets the object of associative action class for this actionID in config.js

			var methodName = this.getActionMethod();
			var serverAction =this.getServerAction();

			this.paramList = addServerAction(this.paramList,"MethodName",serverAction);
			methodName = methodName + "("+ this.paramList+")";

			this.result = eval("classObj."+methodName);	 //call the method of action class and sets the response in this.result
	}
}


function executeAction(actionID,entityClassName,paramList,objName,isPersistent,isSynch){
	if(entityClassName!=null && actionID!=null){
			if(paramList)
				this.paramList=paramList;
			else
				this.paramList='';

			this.result ='';

			this.actionID = actionID;
			this.entityClassName = entityClassName;
			if(objName){
					this.objName=objName;
				}

			if(isPersistent===true){
				this.isPersistent= true;
				if(objName){
					this.objName=objName;
				}
				else
					throw Error('Controller Error: object Name is mandatory, if object is persistent');
			}
			else
				this.isPersistent= false;



			if(isSynch===true)
				this.isSynch = true;
			else
				this.isSynch = false;

			var classObj = this.getObject(); //gets the object of associative action class for this actionID in config.js

			var methodName = this.getActionMethod();
			var serverAction =this.getServerAction();

			this.paramList = addServerAction(this.paramList,"MethodName",serverAction);

			methodName = methodName + "("+ this.paramList+")";

			this.result = eval("classObj."+methodName);	 //call the method of action class and sets the response in this.result
	}
}

/**
 * This method creates object of this class
 * Check whether object is persistent then return persistent object otherwise return new object
 * @return {object} object of the class and read actionClass from config.js
 */
function getObject(){

	if(this.objName && this.isPersistent==false){
		return persistentObjects[this.objName];						// return the persistent object
	}
	else{															// create new object
		var className = this.getActionClass();
		var classObj = eval("new "+ className +"("+this.isSynch +")");

		if(this.isPersistent==true){
			persistentObjects[this.objName]= classObj;
		}
		return classObj;

	}
}

/**
 * This function call the method (actionMethod) of actionClass and sets the response
 * @return result
 */
function getResult(){
	return this.result;
}

/**
 * This method read actionClassName from config.js
 * @return {string} actionClassName
 */

function getActionClass(){
	var actionClassName = eval("ControllerConfig."+this.entityClassName+"[0]."+this.actionID+"[0].ACTIONCLASS");
	return actionClassName;
}

/**
 * This method read actionMethod from config.js
 * @return {string} actionMethod
 */
function getActionMethod(){
	var methodName = eval("ControllerConfig."+this.entityClassName+"[0]."+this.actionID+"[0].METHODNAME");
	return methodName;
}

/**
 * This method read ServerAction from config.js
 * @return {string} ServerAction
 */
function getServerAction(){
	var serverAction = eval("ControllerConfig."+this.entityClassName+"[0]."+this.actionID+"[0].SERVERACTION");
	return serverAction;
}

/*
function getInstance(entityClassName,projectName){
	
	//var className = entityClassName;
	//var classObj = eval("new "+ className +"(projectName)");
	var classObj = eval("new "+ entityClassName +"(projectName)");
	return classObj;
}

  */
function getCommaSeprated(arr){ //make the pass array as comma seperated string
	var str='';
	for( var key in arr ){
		
 			str=arr[key]+","+str;
		}
		
		var param=str.substring(0,(str.length-1)); //remove the comma from last
		return param;
		
}

/**
 * This method can be used on the controller object to get the instance of the entity 
 * class for calling its method directly from it’s instance
 * @param (String) entityClassName
 * @return (object)
 */

function getInstance(entityClassName,paramList){ // return instance of the entity class
	
	this.entityClassName=entityClassName;       
	var className = eval("ControllerConfig."+this.entityClassName);
												// return the class name from config.js
	if(paramList){
	var parm=getCommaSeprated(paramList);
	}else{
		parm = '';
	}
		//var classObj = eval("new "+ entityClassName +"("+parm+")");
		var classObj = eval("new "+ className +"(parm)");
		return classObj;
}