1 var Class = require("../lib/mootools/mootools-node.js").Class;
  2 
  3 var DriverAbstract = require('../lib/DriverAbstract.js').DriverAbstract;
  4 
  5 /**
  6  * @class Driver Manager Responsible for managing drivers (creating, adding, fetching). 
  7  * @requires Class
  8  * @requires DriverAbstract
  9  */
 10 var DriverManager = function(){
 11 
 12     /**
 13      * array of drivers to handle
 14      *
 15      * @type Array[DriverAbstract]
 16      * @private
 17      */
 18     this._drivers = [];
 19 
 20     /**
 21      * Adds a driver to collection
 22      * @public
 23      * @param {DriverAbstract} driver Driver instance.
 24      * @throws {DriverManager.Exception.WRONG_INSTANCE} if given driver is not an instance of DriverAbstract class.
 25      * @returns {boolean} Returns {false} if given driver is arledy in collection otherwise returns {true}
 26      */
 27     this.addDriver = function(driver){
 28         if(driver instanceof DriverAbstract) {
 29             for(var i = 0, l = this._drivers.length; i < l; i++){
 30                 if(this._drivers[i] == driver){
 31                     return false;
 32                 }
 33             }
 34 
 35             this._drivers.push(drisver);
 36             return true;
 37         }
 38         else {
 39             throw DriverManager.Exception.WRONG_INSTANCE;
 40         }
 41         return false;
 42     };
 43 
 44     /**
 45      * Loads model from each added driver. See: {@link DriverAbstract#load}
 46      * @public
 47      */
 48     this.fetch = function(){
 49         for(var i=0,max=this._drivers.length;i<max;i++){
 50             this._drivers[i].load();
 51         }
 52     };
 53 };
 54 
 55 DriverManager = new Class(new DriverManager());
 56 
 57 /**
 58  * Factory method - createing new drivers of given <i>driverClassName</i>.
 59  *
 60  * @static
 61  * @param {String} driverClassName Name of a driver class.
 62  * @param mixed data Data passed to the driver's constructor
 63  * @return {DriverAbstract}
 64  * @throws {DriverManager.Exception.DRIVER_DOES_NOT_EXIST} if given driver class name cannot be resolved.
 65  */
 66 DriverManager.createDriver = function(driverClassName, data){
 67     //TODO przerbic domyslnÄ… sciezke
 68     try{
 69         var driverLib = require('../lib/' + driverClassName);
 70         var DriverClass = driverLib[driverClassName];
 71         var driver = new DriverClass(data);
 72         return driver;
 73     }catch(e){
 74         throw DriverManager.Exception.DRIVER_DOES_NOT_EXIST;
 75     }
 76 };
 77 
 78 /**
 79  * @constant
 80  * @static
 81  */
 82 DriverManager.LOADED = "DriverManager_LOADED";
 83 /**
 84  * @constant
 85  * @static
 86  */
 87 DriverManager.ERROR = "DriverManager_ERROR";
 88 
 89 /**
 90  * Namespace for exeptions messages.
 91  * @constant
 92  * @static
 93  * @namespace
 94  */
 95 DriverManager.Exception = {};
 96 /**
 97  * @constant
 98  * @static
 99  */
100 DriverManager.Exception.WRONG_INSTANCE = 'Driver must be instance of DriverAbstract class.';
101 /**
102  * @constant
103  * @static
104  */
105 DriverManager.Exception.DRIVER_DOES_NOT_EXIST = 'Given driver does not exist';
106 
107 exports.DriverManager = DriverManager;