Remove old MS UI
[clamp.git] / src / main / resources / META-INF / resources / designer / scripts / CldsModelService.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * 
22  */
23 app
24 .service(
25 'cldsModelService',
26 [
27     'alertService',
28     '$http',
29     '$q',
30     '$rootScope',
31     function(alertService, $http, $q, $rootScope) {
32
33             function checkIfElementType(name) {
34
35         //This will open the methods located in the app.js
36                           if (undefined == name) {
37                                   return;
38                           }else if (name.toLowerCase().indexOf("policy") >= 0){
39                                           PolicyWindow();
40                           } else {
41                                   $rootScope.selectedBoxName = name.toLowerCase();
42                                   ToscaModelWindow();
43                           }
44             }
45             function handleQueryToBackend(def, svcAction, svcUrl, svcPayload) {
46
47                     $http.put(svcUrl, svcPayload).success(
48                     function(data) {
49
50                             def.resolve(data);
51                             if (typeof data.statusCodeValue === 'undefined'
52                             || data.statusCodeValue === 200) {
53                                     alertService.alertMessage(
54                                     "Action Successful: " + svcAction, 1)
55                             } else {
56                                     if (typeof data.body !== 'undefined') {
57                                             alertService.alertMessage("Action Failure: "
58                                             + svcAction + ", " + data.body.errorMessageForUi, 2);
59                                     } else {
60                                             alertService.alertMessage("Action Failure: "
61                                             + svcAction, 2);
62                                     }
63                                     def.reject(svcAction + " not successful");
64                             }
65                     }).error(
66                     function(data) {
67
68                             def.resolve(data);
69                             if (typeof data.body !== 'undefined') {
70                                     alertService.alertMessage("Action Failure: " + svcAction
71                                     + ", " + data.body.errorMessageForUi, 2);
72                             } else {
73                                     alertService
74                                     .alertMessage("Action Failure: " + svcAction, 2);
75                             }
76                             def.reject(svcAction + " not successful");
77                     });
78             }
79             this.toggleDeploy = function(uiAction, modelName, controlNamePrefixIn,
80                                          bpmnTextIn, propTextIn, svgXmlIn,
81                                          templateName, typeID, controlNameUuid,
82                                          modelEventService, deploymentId) {
83
84                     var def = $q.defer();
85                     var sets = [];
86                     var action = uiAction.toLowerCase();
87                     var deployUrl = "/restservices/clds/v1/clds/" + action + "/"
88                     + modelName;
89                     var requestData = {
90                         name : modelName,
91                         controlNamePrefix : controlNamePrefixIn,
92                         bpmnText : bpmnTextIn,
93                         propText : propTextIn,
94                         imageText : svgXmlIn,
95                         templateName : templateName,
96                         typeId : typeID,
97                         controlNameUuid : controlNameUuid,
98                         event : modelEventService,
99                         deploymentId : deploymentId
100                     };
101                     handleQueryToBackend(def, action, deployUrl, requestData);
102                     return def.promise;
103             };
104             this.getModel = function(modelName) {
105
106                     var def = $q.defer();
107                     var sets = [];
108                     var svcUrl = "/restservices/clds/v1/clds/model/" + modelName;
109                     $http.get(svcUrl).success(function(data) {
110
111                             def.resolve(data);
112                     }).error(function(data) {
113
114                             def.reject("Open Model not successful");
115                     });
116                     return def.promise;
117             };
118             this.getSavedModel = function() {
119
120                     var def = $q.defer();
121                     var sets = [];
122                     var svcUrl = "/restservices/clds/v1/clds/model-names";
123                     $http.get(svcUrl).success(function(data) {
124
125                             def.resolve(data);
126                     }).error(function(data) {
127
128                             def.reject("Open Model not successful");
129                     });
130                     return def.promise;
131             };
132             this.setModel = function(modelName, controlNamePrefixIn, bpmnTextIn,
133                                      propTextIn) {
134
135                     var def = $q.defer();
136                     var sets = [];
137                     var svcUrl = "/restservices/clds/v1/clds/model/" + modelName;
138                     var svcRequest = {
139                         name : modelName,
140                         controlNamePrefix : controlNamePrefixIn,
141                         bpmnText : bpmnTextIn,
142                         propText : propTextIn
143                     };
144                     $http.put(svcUrl, svcRequest).success(function(data) {
145
146                             def.resolve(data);
147                     }).error(function(data) {
148
149                             def.reject("Save Model not successful");
150                     });
151                     return def.promise;
152             };
153             this.processAction = function(uiAction, modelName, controlNamePrefixIn,
154                                           bpmnTextIn, propTextIn, svgXmlIn,
155                                           templateName, typeID, deploymentId) {
156
157                     var def = $q.defer();
158                     var sets = [];
159                     // console.log(svgXmlIn);
160                     var svcUrl = "/restservices/clds/v1/clds/";
161                     var svcAction = uiAction.toLowerCase();
162                     if (svcAction == "save" || svcAction == "refresh") {
163                             svcUrl = svcUrl + "model/" + modelName;
164                     } else if (svcAction == "test") {
165                             svcUrl = svcUrl + "action/submit/" + modelName + "?test=true";
166                     } else {
167                             svcUrl = svcUrl + "action/" + svcAction + "/" + modelName;
168                     }
169                     var svcRequest = {
170                         name : modelName,
171                         controlNamePrefix : controlNamePrefixIn,
172                         bpmnText : bpmnTextIn,
173                         propText : propTextIn,
174                         imageText : svgXmlIn,
175                         templateName : templateName,
176                         typeId : typeID,
177                         deploymentId : deploymentId
178                     };
179                     handleQueryToBackend(def, svcAction, svcUrl, svcRequest);
180                     return def.promise;
181             };
182             this.manageAction = function(modelName, typeId, typeName) {
183
184                     var def = $q.defer();
185                     var sets = [];
186                     var config = {
187                         url : "/restservices/clds/v1/clds/getDispatcherInfo",
188                         method : "GET",
189                         params : {
190                             model : modelName,
191                             typeId : typeId,
192                             typeName : typeName
193                         }
194                     };
195                     $http(config).success(function(data) {
196
197                             def.resolve(data);
198                     }).error(function(data) {
199
200                             def.reject("manage action not successful");
201                     });
202                     return def.promise;
203             };
204             this.checkPermittedActionCd = function(permittedActionCd, menuText,
205                                                    actionCd) {
206
207                     if (permittedActionCd.indexOf(actionCd) > -1) {
208                             document.getElementById(menuText).classList.remove('ThisLink');
209                     } else {
210                             document.getElementById(menuText).classList.add('ThisLink');
211                     }
212             };
213             this.processActionResponse = function(modelName, pars) {
214
215                     // populate control name (prefix and uuid here)
216                     var controlNamePrefix = pars.controlNamePrefix;
217                     var controlNameUuid = pars.controlNameUuid;
218                     deploymentId = pars.deploymentId;
219                     var modelEventService = pars.modelEventService;
220                     typeID = pars.typeId;
221                     var headerText = "Closed Loop Modeler - " + modelName;
222                     if (controlNameUuid != null) {
223                             var actionCd = pars.event.actionCd;
224                             var actionStateCd = pars.event.actionStateCd;
225                             headerText = headerText + " [" + controlNamePrefix
226                             + controlNameUuid + "]";
227                     }
228                     document.getElementById("modeler_name").textContent = headerText;
229                     document.getElementById("templa_name").textContent = ("Template Used - " + selected_template);
230                     setStatus(pars)
231                     disableBPMNAddSVG(pars);
232                     this.enableDisableMenuOptions(pars);
233             };
234             this.processRefresh = function(pars) {
235
236                     var newPars = pars;
237                     if (typeof pars.body !== 'undefined') {
238                             newPars = pars.body;
239                     }
240                     typeID = newPars.typeId;
241                     deploymentId = newPars.deploymentId;
242                     setStatus(newPars);
243                     this.enableDisableMenuOptions(newPars);
244                     elementMap = JSON.parse(newPars.propText);
245             }
246             function setStatus(pars) {
247
248                     var status = pars.status;
249                     // apply color to status
250                     var statusColor = 'white';
251                     if (status.trim() === "DESIGN") {
252                             statusColor = 'gray'
253                     } else if (status.trim() === "DISTRIBUTED") {
254                             statusColor = 'blue'
255                     } else if (status.trim() === "ACTIVE") {
256                             statusColor = 'green'
257                     } else if (status.trim() === "STOPPED") {
258                             statusColor = 'red'
259                     } else if (status.trim() === "DELETING") {
260                             statusColor = 'pink'
261                     } else if (status.trim() === "ERROR") {
262                             statusColor = 'orange'
263                     } else if (status.trim() === "UNKNOWN") {
264                             statusColor = 'blue'
265                     } else {
266                             statusColor = null;
267                     }
268                     var statusMsg = '<span style="background-color:'
269                     + statusColor
270                     + ';-moz-border-radius: 50px;  -webkit-border-radius: 50px;  border-radius: 50px;">&nbsp;&nbsp;&nbsp;'
271                     + status + '&nbsp;&nbsp;&nbsp;</span>';
272                     // display status
273                     if ($("#status_clds").length >= 1)
274                             $("#status_clds").remove();
275                     $("#activity_modeler")
276                     .append(
277                     '<span id="status_clds" style="position: absolute;  left: 61%;top: 151px; font-size:20px;">Status: '
278                     + statusMsg + '</span>');
279             }
280             function disableBPMNAddSVG(pars) {
281
282                     var svg = pars.imageText.substring(pars.imageText.indexOf("<svg"))
283                     if ($("#svgContainer").length > 0)
284                             $("#svgContainer").remove();
285                     $("#js-canvas").append(
286                     "<span id=\"svgContainer\">" + svg + "</span>");
287                     /* added code for height width viewBox */
288                     $("#svgContainer svg").removeAttr("height");
289                     $("#svgContainer svg").removeAttr('viewBox');
290                     $("#svgContainer svg").removeAttr('width');
291                     $("#svgContainer svg").attr('width', '100%');
292                     $("#svgContainer svg").attr('height', '100%');
293                     $("#svgContainer").click(
294                     function(event) {
295
296                             // console.log($(event.target).parent().html())
297                             // console.log($($(event.target).parent()).attr("data-element-id"))
298                             var name = $($(event.target).parent()).attr("data-element-id")
299                             lastElementSelected = $($(event.target).parent()).attr(
300                             "data-element-id")
301                             checkIfElementType(name)
302                     });
303             }
304             this.enableDisableMenuOptions = function(pars) {
305
306                     var permittedActionCd = pars.permittedActionCd;
307                     // dropdown options - always true
308                     document.getElementById('Open CL').classList.remove('ThisLink');
309                     if (readMOnly) {
310                             // enable model options
311                             document.getElementById('Properties CL').classList
312                             .remove('ThisLink');
313                             document.getElementById('Close Model').classList
314                             .remove('ThisLink');
315                             // disable models options
316                             document.getElementById('Save CL').classList.add('ThisLink');
317                             document.getElementById('Revert Model Changes').classList
318                             .add('ThisLink');
319                     } else {
320                             // enable menu options
321                             document.getElementById('Save CL').classList.remove('ThisLink');
322                             document.getElementById('Properties CL').classList
323                             .remove('ThisLink');
324                             document.getElementById('Revert Model Changes').classList
325                             .remove('ThisLink');
326                             document.getElementById('Close Model').classList
327                             .remove('ThisLink');
328                             document.getElementById('Validation Test').classList
329                             .remove('ThisLink');
330                             document.getElementById('Refresh Status').classList
331                             .remove('ThisLink');
332                     }
333                     // enable/disable menu options based on permittedActionCd
334                     // list
335                     this.checkPermittedActionCd(permittedActionCd, 'Validation Test',
336                     'TEST');
337                     this.checkPermittedActionCd(permittedActionCd, 'Submit', 'SUBMIT');
338                     this.checkPermittedActionCd(permittedActionCd, 'Resubmit',
339                     'RESUBMIT');
340                     this.checkPermittedActionCd(permittedActionCd, 'Update', 'UPDATE');
341                     this.checkPermittedActionCd(permittedActionCd, 'Stop', 'STOP');
342                     this
343                     .checkPermittedActionCd(permittedActionCd, 'Restart', 'RESTART');
344                     this.checkPermittedActionCd(permittedActionCd, 'Delete', 'DELETE');
345                     this.checkPermittedActionCd(permittedActionCd, 'Deploy', 'DEPLOY');
346                     this.checkPermittedActionCd(permittedActionCd, 'UnDeploy',
347                     'UNDEPLOY');
348             }
349     } ]);