Newer
Older
define(['attributeList', 'attribute', 'parameter', 'widget', 'interpreter', 'aggregator' ],
function(AttributeList, Attribute, Parameter, Widget, Interpreter, Aggregator) {
return (function() {
/**
* Constructor: All known components given in the associated functions will be registered as startup.
*
* @classdesc The Discoverer handles requests for components and attributes.
* @constructs Discoverer
function Discoverer(widgets, interpreters, translations) {
/**
* List of available Widgets.
*
* @type {Array}
* @private
*/
this._widgets = [];
/**
* List of available Aggregators.
*
* @type {Array}
* @private
*/
this._aggregators = [];
/**
* List of available Interpreter.
*
* @type {Object}
* @private
*/
this._interpreters = [];
/**
* List of translations or synonymous attributes, respectively.
*
* @type {Array}
* @private
*/
this._translations = (translations instanceof Array) ? translations : [];
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
/**
* Registers the specified component.
*
* @param {Widget|Aggregator|Interpreter} component the component that should be registered
*/
Discoverer.prototype.registerNewComponent = function(component) {
if (component instanceof Aggregator && this.getAggregator(component.getId()) == null) this._aggregators.push(component);
if (component instanceof Widget && !(component instanceof Aggregator) && this.getWidget(component.getId()) == null) this._widgets.push(component);
if (component instanceof Interpreter && this.getInterpreter(component.getId()) == null) this._interpreters.push(component);
};
/**
* Deletes a component from the Discoverer.
*
* @param {string} componentId id of the component that should be registered
*/
Discoverer.prototype.unregisterComponent = function(componentId) {
for (var wi in this._widgets) {
var theWidget = this._widgets[wi];
if (componentId == theWidget.getId()) this._widgets.splice(wi, 1);
}
for (var ii in this._interpreters) {
var theInterpreter = this._interpreters[ii];
if (componentId == theInterpreter.getId()) this._interpreters.splice(ii, 1);
}
for (var ai in this._aggregators) {
var theAggregator= this._aggregators[ai];
if (componentId == theAggregator.getId()) this._aggregators.splice(ai, 1);
}
};
/**
* Returns the widget for the specified id.
*
* @param {string} widgetId id of the component that should be returned
* @returns {?Widget}
*/
Discoverer.prototype.getWidget = function(widgetId) {
for (var index in this._widgets) {
var theWidget = this._widgets[index];
if (theWidget.getId() == widgetId) return theWidget;
}
return null;
};
/**
* Returns the aggregator for the specified id.
*
* @param {string} aggregatorId id of the component that should be returned
* @returns {?Aggregator}
*/
Discoverer.prototype.getAggregator = function(aggregatorId) {
for (var index in this._aggregators) {
var theAggregator = this._aggregators[index];
if (theAggregator.getId() == aggregatorId) return theAggregator;
}
return null;
};
/**
* Returns the interpreter for the specified id.
*
* @param {string} interpreterId id of the component that should be returned
* @returns {Interpreter}
*/
Discoverer.prototype.getInterpreter = function(interpreterId) {
for (var index in this._interpreters) {
var theInterpreter = this._interpreters[index];
if (theInterpreter.getId() == interpreterId) return theInterpreter;
}
return null;
};
/**
* Returns all registered components (widget, aggregator and interpreter).
*
* @param {Array} componentTypes Component types to get descriptions for. Defaults to Widget, Interpreter and Aggregator.
* @returns {Array}
*/
Discoverer.prototype.getComponents = function(componentTypes) {
if (typeof componentTypes == "undefined") componentTypes = [Widget, Interpreter, Aggregator];
var response = [];
if (jQuery.inArray(Widget, componentTypes) != -1) response = response.concat(this._widgets);
if (jQuery.inArray(Aggregator, componentTypes) != -1) response = response.concat(this._aggregators);
if (jQuery.inArray(Interpreter, componentTypes) != -1) response = response.concat(this._interpreters);
return response;
};
/**
* Returns the instance (widget, aggregator or interpreter) for the specified id.
*
* @param {string} componentId id of the component that should be returned
* @returns {?(Widget|Aggregator|Interpreter)}
*/
Discoverer.prototype.getComponent = function(componentId) {
var theWidget = this.getWidget(componentId);
if (theWidget) {
return theWidget;
}
var theAggregator = this.getAggregator(componentId);
if (theAggregator) {
return theAggregator;
}
var theInterpreter = this.getInterpreter(componentId);
if (theInterpreter) {
return theInterpreter;
}
return null;
};
/**
* Returns all components that have the specified attribute as
* outAttribute. It can be chosen between the verification of
* all attributes or at least one attribute.
*
* @param {AttributeList|Array} attributeListOrArray list of searched attributes
* @param {Boolean} all choise of the verification mode
* @param {Array} componentTypes Components types to search for
* @returns {Array}
*/
Discoverer.prototype.getComponentsByAttributes = function(attributeListOrArray, all, componentTypes) {
var componentList = [];
var list = [];
if (typeof componentTypes == "undefined") componentTypes = [Widget, Interpreter, Aggregator];
if (attributeListOrArray instanceof Array) {
list = attributeListOrArray;
} else if (attributeListOrArray.constructor === AttributeList) {
list = attributeListOrArray.getItems();
}
if (typeof list != "undefined") {
var components = this.getComponents(componentTypes);
for (var i in components) {
var theComponent = components[i];
if(all && this._containsAllAttributes(theComponent, list)) {
componentList.push(theComponent);
} else if(!all && this._containsAtLeastOneAttribute(theComponent, list)) {
componentList.push(theComponent);
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
/**
* Returns an array of all translations known to the discoverer.
*
* @returns {Array}
*/
Discoverer.prototype.getTranslations = function() {
return this._translations;
};
/**
* Builds a new attribute from given name, type and parameters,
* adding known translations to its synonyms.
*
* @param name
* @param type
* @param parameterList
* @returns {Attribute}
*/
Discoverer.prototype.buildAttribute = function(name, type, parameterList) {
var newAttribute = new Attribute().withName(name).withType(type);
while (typeof parameterList != 'undefined' && parameterList.length > 0)
{
var param = parameterList.pop();
var value = param.pop();
var key = param.pop();
if (typeof key != 'undefined' && typeof value != 'undefined')
newAttribute = newAttribute.withParameter(new Parameter().withKey(key).withValue(value));
}
for (var index in this._translations) {
var translation = this._translations[index];
if (translation.translates(newAttribute))
newAttribute = newAttribute.withSynonym(translation.getSynonym());
}
return newAttribute;
};
/***********************************************************************
* Helper *
**********************************************************************/
/**
* Helper: Verifies whether a component description contains all searched attributes.
*
* @private
* @param {Widget|Interpreter|Aggregator} component description of a component
* @param {Array} list searched attributes
* @returns {boolean}
*/
Discoverer.prototype._containsAllAttributes = function(component, list) {
for (var j in list) {
var attribute = list[j];
if (!component.doesSatisfyTypeOf(attribute)) {
return false;
}
return true;
};
/**
* Helper: Verifies whether a component description contains at least on searched attributes.
*
* @private
* @param {Widget|Interpreter|Aggregator} component description of a component
* @param {Array} list searched attributes
* @returns {boolean}
*/
Discoverer.prototype._containsAtLeastOneAttribute = function(component, list) {
for (var j in list) {
var attribute = list[j];
if (component.doesSatisfyTypeOf(attribute)) {
return true;
}
return Discoverer;
})();
}
);