Skip to content
Snippets Groups Projects
aggregator.js 15.9 KiB
Newer Older
Stefanie Lemcke's avatar
Stefanie Lemcke committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
/**
 * This module representing a Context Aggregator. 
 * It aggregates data from multiple widgets.
 * 
 * @module Aggregator
 * @fileOverview
 */
define(['easejs', 'MathUuid','widget', 'widgetHandle', 'widgetHandleList', 
        'attributeType', 'attributeValue', 'attributeValueList', 'subscriber', 
        'subscriberList', 'callbackList', 'storage'],
 	function( easejs, MathUuid, Widget, WidgetHandle,WidgetHandleList, AttributeType,
 			AttributeValue, AttributeValueList, Subscriber, SubscriberList,
 			CallbackList, Storage){

 	var Class = easejs.Class;
 	var AbstractClass = easejs.AbstractClass;
	var Aggregator =  AbstractClass('Aggregator').
				extend(Widget, 
			
	{
	   /**
	    * @alias name
	    * @public
	    * @type {string}
	    * @memberof Aggregator#
	    * @desc Name of the Widget.
        */
		'public name' : 'Aggregator',
		
		/**
		 * @alias id
		 * @public
		 * @type {string}
		 * @memberof Aggregator#
		 * @desc ID of the Aggregator. Will be generated.
		 */
		'public id' : '', 
		
		/**
		 * @alias widgets
		 * @protected
		 * @type {WidgetHandleList}
		 * @memberof Aggregator#
		 * @desc List of subscribed Widgets.
		 */
		'protected widgets' : [],		
			
		/**
		 * @alias db
		 * @protected
		 * @type {Storage}
		 * @memberof Aggregator#
		 * @desc Database of the Aggregator.
		 */
		'protected db' : '',
		
		/**
		 * Constructor: Generates the id and initializes the Aggregator.
		 * 
		 * @abstract
		 * @class Aggregator
		 * @extends Widget
		 * @classdesc The Widget handles the access to sensors.
		 * @requires easejs
		 * @requires MathUuid
		 * @requires CallbackList
		 * @requires AttributeType
		 * @requires AttributeValue
		 * @requires AttributeValueList
		 * @requires Subscriber
		 * @requires SubscriberList
		 * @requires Storage
		 * @requires Widget
		 * @requires WidgetHandle
		 * @requires WidgetHandleList
		 * @constructs Aggregator
		 */
		'override public __construct': function()
        {
			this.id = Math.uuid();
			this.widgets = new WidgetHandleList();
			this.initWidgetHandles();
			this.__super();	
			this.aggregatorSetup();
        },
        
        /**
		 * Returns the type of this class, in this case
		 * "Aggregator".
		 * 
		 * @override
		 * @public
		 * @alias getType
		 * @memberof Aggregator#
		 * @returns {string}
		 */
		'override public getType' : function(){
		    return 'Aggregator';
		 },
		 
		/**
		 * Adds new AttributeTypes, useful when a new Widget is subscribed.
		 * 
		 * @protected
	   	 * @alias addAttributeType
		 * @memberof Aggregator#
		 * @param {AttributeType} _attributeType attributeType
	     */
		'protected addAttributeType' : function(_attributeType){
			if(Class.isA( AttributeType, _attributeType )){			
				this.attributeTypes.put(_attributeType);
				var attVal = new AttributeValue().buildFromAttributeType(_attributeType);
				this.attributes.put(attVal);
			};
		},
		
		/**
		 * Sets WidgetHandles.
		 * 
		 * @protected
	   	 * @alias setWidgets
		 * @memberof Aggregator#
		 * @param {(WidgetHandleList|Array)} _widgetList List of WidgetHandles
	     */
		'protected setWidgets' : function(_widgetList){
			this.widgets = new WidgetHandleList().withItems(_widgetList);		
		},
		
		/**
		 * Adds WidgetHandle.
		 * 
		 * @public
	   	 * @alias addWidget
		 * @memberof Aggregator#
		 * @param {WidgetHandle} _widget WidgetHandle
	     */
		'public addWidget' : function(_widget){
			this.widgets.put(_widget);
		},
		
		/**
		 * Returns the available WidgetHandles.
		 * 
		 * @public
		 * @alias getWidgets
		 * @memberof Aggregator#
		 * @returns {WidgetHandleList}
		 */
		'public getWidgets' : function() {
			return this.widgets;
		},
		
		/**
		 * Removes WidgetHandle from list.
		 * 
		 * @protected
	   	 * @alias removeWidget
		 * @memberof Aggregator#
		 * @param {String} _key Id of the WidgetHandle
	     */
		'protected removeWidget' : function(_key){
			this.widgets.removeItem(_key);
		},
		
		/**
		 * Retrieves all Attributes of the specified widgets.
         * If the defined name in WidgetHandle does not match the name of the 
         * returned instance, the WidgetHandle will be removed from the list.
		 * 
		 * @protected
	   	 * @alias initAttributes
		 * @memberof Aggregator#
	     */
		'protected initAttributes' : function(){
			if(this.widgets.size() > 0){
				var widgetList = this.widgets.getItems();
				for(var i in widgetList){
					var widgetHandle = widgetList[i];
					var widgetInstance = this.discoverer.getComponent(widgetHandle.getId());
					if(widgetInstance && widgetInstance.getName() === widgetHandle.getName()){
						this.setAttributes(widgetInstance.queryAttributes());
					} else {
						this.removeWidget(widgetHandle.getName());
					}
				};
			};
		},
		
		/**
		 * Retrieves all ConstantAttributes of the specified widgets.
         * If the defined name in WidgetHandle does not match the name of the 
         * returned instance, the WidgetHandle will be removed from the list.
		 * 
		 * @protected
	   	 * @alias initConstantAttributes
		 * @memberof Aggregator#
	     */
		'protected initConstantAttributes' : function(){
			if(this.widgets.size() > 0){
				var widgetList = this.widgets.getItems();
				for(var i in widgetList){
					var widgetHandle = widgetList[i];					
					var widgetInstance = this.discoverer.getComponent(widgetHandle.getid());
					if(widgetInstance && widgetInstance.getName() === widgetHandle.getName()){
						this.setConstantAttributes(widgetInstance.queryConstantAttributes());
					} else {
						this.removeWidget(widgetHandle.getName());
					};
				};
			};

		},
		
		/**
		 * Retrieves all actual Callbacks of the specified Widgets.
		 * 
		 * @protected
	   	 * @alias initCallbacks
		 * @memberof Aggregator#
	     */
		'protected initCallbacks' : function(){
			if(this.widgets.size() > 0){
				var widgetList = this.widgets.getItems();
				for(var i in widgetList){
					var widgetHandle = widgetList[i];
					this.initWidgetSubscription(widgetHandle);
				};
			};
		},
		
		/**
		 * InitMethod for Aggregators. Called by constructor.
		 * Initializes the associated Storage.
		 * 
		 * @protected
	   	 * @alias aggregatorSetup
		 * @memberof Aggregator#
	     */
		'protected aggregatorSetup' : function(){
			this.initStorage('DB_'+ this.name);
			this.setAggregatorAttributeValues();
			this.setAggregatorConstantAttributeValues();
			this.setAggregatorCallbacks();
		},
		
		/**
		 * Initializes the Widget that should be subscribed.
		 * Called by aggregatorSetup().
		 * 
		 * @function
		 * @abstract
		 * @protected
		 * @alias initWidgetHandles
		 * @memberof Aggregator#
		 */
		'abstract protected initWidgetHandles' : [],
		
		/**
		 * Initializes the provided attributeValues that are only specific to the Aggregator.
		 * Called by aggregatorSetup().
		 * 
		 * @function
		 * @abstract
		 * @protected
		 * @alias setAggregatorAttributeValues
		 * @memberof Aggregator#
		 */
		'abstract protected setAggregatorAttributeValues' : [],
		/**
		 * Initializes the provided ConstantAttributeValues that are only specific to the Aggregator.
		 * Called by aggregatorSetup().
		 * 
		 * @function
		 * @abstract
		 * @protected
		 * @alias setAggregatorConstantAttributeValues
		 * @memberof Aggregator#
		 */
		'abstract protected setAggregatorConstantAttributeValues' : [],
		/**
		 * Initializes the provided Callbacks that are only specific to the Aggregator.
		 * Called by aggregatorSetup().
		 * 
		 * @function
		 * @abstract
		 * @protected
		 * @alias setAggregatorCallbacks
		 * @memberof Aggregator#
		 */
		'abstract protected setAggregatorCallbacks' : [],

		/**
		 * Returns the current Attributes that are saved in the cache.
		 * 
		 * @public
	   	 * @alias getCurrentData
		 * @memberof Aggregator#
		 * @returns {AttributeValueList}
	     */
		'public getCurrentData' : function(){
			var response = new AttributeValueList();
			response.putAll(this.attributes);
			return response;
		},
		
		/**
		 * Subscribes to the given widget for the specified Callbacks.
		 * 
		 * @protected
	   	 * @alias subscribeTo
		 * @memberof Aggregator#
		 * @param {Widget} _widget Widget that should be subscribed to.
		 * @param {CallbackList} _callbacks required Callbacks
	     */
		'protected subscribeTo' : function(_widget, _callbacks, _subSet, _conditions){	
			if(Class.isA(Widget, _widget)){
				var subscriber = new Subscriber().withSubscriberId(this.id).
									withSubscriberName(this.name).
									withSubscriptionCallbacks(_callbacks).
									withAttributesSubset(_subSet).
									withConditions(_conditions);	
				console.log(this.name + ' subscribeTo: ' + _widget.getName());
				_widget.addSubscriber(subscriber);
			};
		},
		
		/**
		 * Subscribes to the widgets that are defined in the WidgetHandleList
         * used in the initCallback method.
         * If the defined name in WidgetHandle does not match the name of the 
         * returned instance, the WidgetHandle will be removed from the list.
		 * 
		 * 
		 * @protected
	   	 * @alias initWidgetSubscription
		 * @memberof Aggregator#
		 * @param {WidgetHandle} _widgetHandle Widget that should be subscribed.
		 * @returns {?CallbackList}
	     */
		'protected initWidgetSubscription' : function(_widgetHandle){
			var calls = null;
			if(Class.isA(WidgetHandle, _widgetHandle)){				
				var widget = this.discoverer.getComponent(_widgetHandle.getId());
				if(widget && widget.getName() === _widgetHandle.getName()){
					//subscribe to all callbacks
					calls = widget.queryCallbacks();
					this.subscribeTo(widget, calls);
				} else {
					this.removeWidget(_widgetHandle.getName());
				};			
			};			
			return calls;
		},
		
		/**
		 * Adds a new subscription to this Aggregator.
         * 
		 * @public
	   	 * @alias addWidgetSubscription
		 * @memberof Aggregator#
		 * @param {WidgetHandle} _widgetHandle Widget that should be subscribed.
		 * @param {CallbackList} _callbacks required Callbacks
	     */
		'public addWidgetSubscription' : function(_widgetHandle, _callbackList){			
			if(Class.isA(WidgetHandle, _widgetHandle) && Class.isA(CallbackList, _callbackList)){
				var widget = this.discoverer.getComponent(_widgetHandle.getId());
				if(widget && widget.getName() === _widgetHandle.getName()){
					this.subscribeTo(widget, _callbackList);			
					this.callbacks.putAll(_callbackList);			
					var callsList = _callbackList.getItems();		
					for(var x in callsList){
						var singleCallback = callsList[x];			
						var typeList = singleCallback.getAttributeTypes().getItems();
						for(var y in typeList){
							var singleType = typeList[y];
							this.addAttributeType(singleType);
						};				
					};
					this.addWidget(_widgetHandle);
				};
			};		
		},
		
		/**
		 * Removes subscribed Widgets and deletes the entry 
		 * for subscribers in the associated Widget.
		 * 
		 * @public
	   	 * @alias unsubscribeFrom
		 * @memberof Aggregator#
		 * @param {WidgetHandle} _widgetHandle Widget that should be removed.
	     */
		'public unsubscribeFrom' : function(_widgetHandle){
			if(Class.isA(WidgetHandle, _widgetHandle)){
				var widget = this.discoverer.getComponent(_widgetHandle.getId());
				if(widget && widget.getName() === _widgetHandle.getName()){
					console.log('aggregator unsubscribeFrom: ' + widget.getName());
					widget.removeSubscriber(this.id);
					this.widgets.removeItem(_widgetHandle.getName());
				};				
			};	
		},
		
		/**
		 * Puts context data to Widget and expects an array.
		 * 
		 * @override
		 * @public
	   	 * @alias putData
		 * @memberof Aggregator#
		 * @param {(AttributeValueList|Array)}  _data data that shall be input
	     */
		'override public putData' : function(_data){
			var list = new Array();
			if(_data instanceof Array){
				list = _data;
			} else if (Class.isA( AttributeValueList, _data)) {
				list = _data.getItems();
			}
			for(var i in list){
				var x = list[i];
				if(Class.isA( AttributeValue, x ) && this.isAttribute(x)){
					this.addAttribute(x);
					if(this.db){
						this.store(x);
					}
				};
			};
			
		},
		
		/**
		 * Calls the given Interpreter for interpretation the data.
		 * 
		 * @public
	   	 * @alias interpretData
		 * @memberof Aggregator#
		 * @param {String} _interpreterId ID of the searched Interpreter
		 * @param {(AttributeValueList|Array)} _data data that should be interpreted
		 * @param {?function} _function for additional actions, if an asynchronous function is used
	     */
		'public interpretData' : function(_interpreterId, _data, _function){
			var interpreter = this.discoverer.getComponent(_interpreterId);
			if(interpreter){
				interpreter.callInterpreter(_data, _function);
			}
		},
		
		/**
		 * Calls the given Interpreter for getting the data.
		 * 
		 * @public
	   	 * @alias getInterpretedData
		 * @memberof Aggregator#
		 * @param {String} _interpreterId ID of the searched Interpreter
		 * @returns {?AttributeValueList}
	     */
		'public getInterpretedData' : function(_interpreterId){
			var response = 'undefined';
			var interpreter = this.discoverer.getComponent(_interpreterId);
			if(interpreter){
				response = interpreter.getInterpretedData();
				var list = response.getOutAttributes().getItems();
				for(var i in list){
					var x = list[i];
					if(Class.isA( AttributeValue, x ) && this.isAttribute(x)){
						this.addAttribute(x);
						if(this.db){
							this.store(x);
						}
					};
				};
			}
			return response;
		},
		
		/**
		 * Initializes the database with the specified name.
		 * 
		 * @protected
	   	 * @alias initStorage
		 * @memberof Aggregator#
		 * @param {String} _name Name of the Storage
	     */
		'protected initStorage' : function(_name){
			this.db = new Storage(_name, 7200000, 5);
		},
		
		/**
		 * Stores the data.
		 * 
		 * @protected
	   	 * @alias store
		 * @memberof Aggregator#
		 * @param {AttributeValue} _attributeValue data that should be stored
	     */
		'protected store' : function(_attributeValue){
			this.db.store(_attributeValue);
		},
		
		/**
		 * Queries the database and returns the last retrieval result. 
		 * It may be that the retrieval result is not up to date, 
		 * because an asynchronous function is used for the retrieval.
		 * For retrieving the current data, this function can be used as callback function
		 * in retrieveStorage().
		 * 
		 * @public
	   	 * @alias queryAttribute
		 * @memberof Aggregator#
		 * @param {String} _name Name of the searched AtTributes.
		 * @param {?function} _function for alternative  actions, because an asynchronous function is used
	     */
		'public queryAttribute' : function(_name, _function){
			this.db.retrieveAttributes(_name, _function);	
		},
		
		/**
		 * Queries a specific table and only actualizes the storage cache.
		 * For an alternativ action can be used a callback.
		 * 
		 * @public
	   	 * @alias retrieveStorage
		 * @memberof Aggregator#
		 * @returns {RetrievalResult}
	     */
		'public retrieveStorage' : function(){
			return this.db.getCurrentData();
		},
		
		/**
		 * Returns an overview about the stored attributes.
		 * It may be that the overview about the stored attributes is not up to date, 
		 * because an asynchronous function is used for the retrieval.
		 * For retrieving the current data, this function can be used as callback function
		 * in queryTables().
		 * 
		 * @public
	   	 * @alias getStorageOverview
		 * @memberof Aggregator#
		 * @returns {?Array}
	     */
		'public getStorageOverview' : function(){
			return this.db.getAttributesOverview();
		},
		
		/**
		 * Only actualizes the attributeType cache in th database.
		 * For an alternativ action can be used a callback.
		 * 
		 * @public
	   	 * @alias queryTables
		 * @memberof Aggregator#
		 * @param {?function} _function for alternative actions, because an asynchronous function is used
	     */
		'public queryTables' : function(_function){
			this.db.getAttributeNames(_function);		
		},
		
		/**
		 * Returns the description of this component.
		 * @virtual
		 * @public
		 * @alias getAggregatorDescription
		 * @memberof Aggregator#
		 * @returns {WidgetDescription} 
		 */
		'virtual public getAggregatorDescription' : function(){
			return this.getWidgetDescription();
		},
		
	});

	return Aggregator;
});