-
Notifications
You must be signed in to change notification settings - Fork 4
/
controllerManager.js
171 lines (145 loc) · 6.5 KB
/
controllerManager.js
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
import {exists, checkMethod, checkParam} from '../utils';
import ControllerProxy from './controllerProxy.js';
import CommandFactory from './commands/commandFactory.js';
import { SOURCE_SYSTEM } from './connector.js';
import { SOURCE_SYSTEM_CLIENT } from './connector.js';
import { ACTION_CALL_BEAN } from './connector.js';
const CONTROLLER_ID = 'controllerId';
const MODEL = 'model';
const ERROR_CODE = 'errorCode';
export default class ControllerManager {
constructor(dolphin, classRepository, connector) {
checkMethod('ControllerManager(dolphin, classRepository, connector)');
checkParam(dolphin, 'dolphin');
checkParam(classRepository, 'classRepository');
checkParam(connector, 'connector');
this.dolphin = dolphin;
this.classRepository = classRepository;
this.connector = connector;
this.controllers = new Set();
}
createController(name) {
return this._createController(name, null);
}
_createController(name, parentControllerId) {
checkMethod('ControllerManager.createController(name)');
checkParam(name, 'name');
let self = this;
return new Promise((resolve, reject) => {
self.connector.getHighlanderPM().then((highlanderPM) => {
const MSG_ERROR_CREATING_CONTROLLER = 'Error creating controller: ';
self.connector.invoke(CommandFactory.createCreateControllerCommand(name, parentControllerId)).then(() => {
let controllerId;
self.getValueWithRetry(
() => highlanderPM.findAttributeByPropertyName(CONTROLLER_ID).getValue(),
'Could not get an controllerID from highlanderPM.'
).then((ctrlId) => {
controllerId = ctrlId;
return self.getValueWithRetry(
() => highlanderPM.findAttributeByPropertyName(MODEL).getValue(),
'Could not get an modelID from highlanderPM.'
);
})
.then((modelId) => {
return self.getValueWithRetry(
() => self.classRepository.mapDolphinToBean(modelId),
'Could not get an model from classRepository for ID: ' + modelId
);
})
.then((model) => {
try {
const controller = new ControllerProxy(controllerId, model, self);
self.controllers.add(controller);
resolve(controller);
} catch (e) {
reject(MSG_ERROR_CREATING_CONTROLLER + e);
}
}).catch((error) => {
reject(MSG_ERROR_CREATING_CONTROLLER + error);
});
}).catch((error) => {
reject(MSG_ERROR_CREATING_CONTROLLER + error);
});
});
});
}
getValueWithRetry(getValueCall, errorMessage) {
return new Promise((resolve, reject) => {
const RETRIES = 1000;
const RETRY_TIME = 5;
let i = 0;
const intervalID = setInterval(() => {
let value = getValueCall();
if (!(typeof value !== 'undefined' && value !== null)) {
i++;
if (i >= RETRIES) {
clearInterval(intervalID);
reject(errorMessage + " after " + i + " retries.");
}
} else {
clearInterval(intervalID);
resolve(value);
}
}, RETRY_TIME);
});
}
invokeAction(controllerId, actionName, params) {
checkMethod('ControllerManager.invokeAction(controllerId, actionName, params)');
checkParam(controllerId, 'controllerId');
checkParam(actionName, 'actionName');
let self = this;
return new Promise((resolve, reject) => {
let attributes = [
self.dolphin.attribute(SOURCE_SYSTEM, null, SOURCE_SYSTEM_CLIENT),
self.dolphin.attribute(ERROR_CODE)
];
let pm = self.dolphin.presentationModel.apply(self.dolphin, [null, ACTION_CALL_BEAN].concat(attributes));
let actionParams = [];
if (exists(params)) {
for (var param in params) {
if (params.hasOwnProperty(param)) {
let value = self.classRepository.mapParamToDolphin(params[param]);
actionParams.push({
name: param,
value: value
});
}
}
}
self.connector.invoke(CommandFactory.createCallActionCommand(controllerId, actionName, actionParams)).then(() => {
let isError = pm.findAttributeByPropertyName(ERROR_CODE).getValue();
if (isError) {
reject(new Error("Server side ControllerAction " + actionName + " caused an error. Please see server log for details."));
} else {
resolve();
}
self.dolphin.deletePresentationModel(pm);
}).catch(reject);
});
}
destroyController(controller) {
checkMethod('ControllerManager.destroyController(controller)');
checkParam(controller, 'controller');
let self = this;
return new Promise((resolve, reject) => {
self.connector.getHighlanderPM().then((highlanderPM) => {
self.controllers.delete(controller);
highlanderPM.findAttributeByPropertyName(CONTROLLER_ID).setValue(controller.controllerId);
self.connector.invoke(CommandFactory.createDestroyControllerCommand(controller.getId())).then(resolve).catch(reject);
});
});
}
destroy() {
let controllersCopy = this.controllers;
let promises = [];
this.controllers = new Set();
controllersCopy.forEach((controller) => {
try {
promises.push(controller.destroy());
} catch (e) {
// ignore
}
});
return Promise.all(promises);
}
}