c43ad8b77b1b96837447a8c8ab6e09ed26a6e658
[portal/sdk.git] /
1 app.controller('workflowsController', function($scope, $http, $uibModal, $log, modalService, $modal) {
2                 
3         $scope.viewPerPage = 5;
4     $scope.scrollViewsPerPage = 20;
5     $scope.currentPage = 2;
6     $scope.totalPage;
7     $scope.searchCategory = "";
8     $scope.searchString = "";
9     $scope.radio = {
10         value: ""
11     };
12         
13         
14     $scope.showModal = false;
15     $scope.toggleModal = function(){
16         $scope.showModal = !$scope.showModal;
17     };
18
19     $scope.workflow = {};
20     $scope.workflow.active = "true";
21
22     $scope.updateAllWorkflowStatus = function() {
23                 angular.forEach($scope.workflows,function(value){
24                         $scope.checkWorkflowStatus(value);
25             })
26         }
27
28     $scope.fetchWorkflowsList = function() {
29         $http.get('workflows/list').then(function(workflowList){
30                 console.log('Got new list from server = ' + workflowList.data);
31             $scope.workflows = workflowList.data;
32             $scope.updateAllWorkflowStatus();
33         });     
34     };
35
36     $scope.addNewWorkflow = function(newWorkflow) {
37         $http.post('workflows/addWorkflow/', JSON.stringify(newWorkflow)).success(function() {
38             $scope.fetchWorkflowsList();
39         });
40         
41         $scope.workflow.name = '';
42
43     };
44
45     $scope.updateWorkflow = function (workflowToEdit) {
46         //workflowToEdit.active='true';
47                  var modalInstance = $uibModal.open({
48                          animation: $scope.animationsEnabled,
49                          templateUrl: 'app/fusion/scripts/view-models/workflows/workflow-new.html',
50                          //size : modalSize,
51                          controller: ['$scope', '$uibModalInstance', '$http', function ($scope, $uibModalInstance, $http) {
52                                 $scope.workflow = workflowToEdit;                               
53                                         $scope.ok = function() {                          
54                               console.log('Updating existing workflow ... ' + JSON.stringify($scope.workflow));               
55                               $http.post('workflows/editWorkflow/', JSON.stringify($scope.workflow)).then(function(returnedWorkflow){
56                                   console.log('Returned Workflow = ' + JSON.stringify(returnedWorkflow));
57                                   $uibModalInstance.close($scope.workflow);
58                               });
59                           };
60                 
61                           $scope.cancel = function() {
62                                   $uibModalInstance.dismiss();
63                           };
64                     }],
65                     //End of inner controller
66                     resolve: {
67                         workflow: function() {
68                                 console.log('Passing ' + JSON.stringify($scope.workflow)); 
69                                 return $scope.workflow; 
70                         }
71                     }
72                   });
73
74               modalInstance.result.then(function (editedWorkFlow) {
75                   //Need to convert to proper date - later
76                   delete editedWorkFlow.created;
77                   delete editedWorkFlow.updated;
78                   
79                   delete editedWorkFlow.createdBy;
80                   delete editedWorkFlow.modifiedBy;
81                   
82                   console.log('selected Item ' + JSON.stringify(editedWorkFlow));                 
83                   $scope.$emit('workflowAdded', editedWorkFlow);
84                   
85               }, function () {
86                 $log.info('Modal dismissed at: ' + new Date());
87               });
88     };
89     
90     $scope.reset = function(){
91         console.log("Resetting ....");
92     };
93       
94         $scope.update = function(){
95                 console.log("updating ....");
96         };
97
98     $scope.createWorkflow = function (modalSize) {
99         
100                  var modalInstance = $uibModal.open({
101                  animation: $scope.animationsEnabled,
102                  templateUrl: 'app/fusion/scripts/view-models/workflows/workflow-new.html',
103                  size : modalSize,               
104                  controller: ['$scope', '$uibModalInstance', '$http', function ($scope, $uibModalInstance, $http) {
105                         $scope.workflow = {};
106                         $scope.workflow.active = 'true'; 
107                                 $scope.ok = function() {                          
108                       console.log('Saving new workflow ... ' + JSON.stringify($scope.workflow));                      
109                       $http.post('workflows/addWorkflow/', JSON.stringify($scope.workflow)).then(function(returnedWorkflow){
110                           console.log('Returned Workflow = ' + JSON.stringify(returnedWorkflow));
111                           $uibModalInstance.close($scope.workflow);
112                       });
113                   };
114 /*                console.log(size);*/
115                   $scope.cancel = function() {
116                           $uibModalInstance.dismiss();
117                   };
118             }],
119             //End of inner controller
120             resolve: {
121                 workflow: function() {
122                         console.log('Passing ' + JSON.stringify($scope.workflow)); 
123                         return $scope.workflow; 
124                 }
125             }
126           });
127
128       modalInstance.result.then(function (newWorkflow) {
129           console.log('selected Item ' + JSON.stringify(newWorkflow));
130           $scope.$emit('workflowAdded', newWorkflow);
131           
132       }, function () {
133         $log.info('Modal dismissed at: ' + new Date());
134       });
135    };//End of createWorkflow function
136    
137  
138    $scope.removeWorkflow = function(workflowToRemove){     
139            var modalInstance = $uibModal.open({
140                          animation: $scope.animationsEnabled,
141                          templateUrl: 'app/fusion/scripts/view-models/workflows/workflow-remove.html',
142                          controller: ['$scope', '$uibModalInstance', '$http', function ($scope, $uibModalInstance, $http) {
143                                 $scope.workflowToRemove = workflowToRemove;
144                                         $scope.ok = function() {                          
145                               console.log('Removing workflow ... ' + JSON.stringify($scope.workflowToRemove) + ' on client request.');                
146                               $http.post('workflows/removeWorkflow/', JSON.stringify($scope.workflowToRemove.id)).then(function(){
147                                   console.log('Workflow successfully removed !!!');
148                                   $uibModalInstance.close();
149                               });
150                           };
151                 
152                           $scope.cancel = function() {
153                                   $uibModalInstance.dismiss();
154                           };
155                     }]
156                   });
157
158               modalInstance.result.then(function () {
159                   $scope.$emit('workflowRemoved');                
160               }, function () {
161                 $log.info('Modal dismissed at: ' + new Date());
162               });
163            
164         };
165         
166         
167         
168            $scope.scheduleWorkflow = function(workflowToSchedule){         
169                    var modalInstance = $uibModal.open({
170                                  animation: $scope.animationsEnabled,
171                                  templateUrl: 'app/fusion/scripts/view-models/workflows/workflow-schedule.html',
172                                  size:'lg',
173
174                                  controller: ['$scope', '$uibModalInstance', '$http','dateFilter', function ($scope, $uibModalInstance, $http,dateFilter) {
175                                          
176                                          $scope.workflowToSchedule = workflowToSchedule;
177                                          $scope.dt = new Date();
178                                          $scope.dt2 = new Date();
179                                          $scope.dateformat = 'MM/dd/yyyy',
180                                          $scope.datetimeformat = "hh:mm a";
181                                          
182                                          $scope.recurrenceOptions =[{                                              
183                                                   index:0, value:'One-Time', title:'One-Time'
184                                          },{
185                                                  index:1, value: 'Hourly',title:'Hourly'
186                                          },{
187                                                  index:2, value: 'Daily',title:'Daily'
188                                          },{
189                                                  index:3, value: 'Weekly',title:'Weekly'                                                                                                 
190                          }]                                      
191                          $scope.selectRecurrenceOpt = $scope.recurrenceOptions[0];                                       
192                                          
193                                          $scope.hours = [];
194                                          for (var i=0; i<24; i++){
195                                                  var newObj={}
196                                                  newObj.index = i;
197                                                  newObj.value = ""+i;
198                                                  newObj.title = ""+i;
199                                                  $scope.hours.push(newObj);
200                                          }
201                                          
202                                          $scope.minutes = [];
203                                          for (var i=0; i<60; i++){
204                                                  var newObj={}
205                                                  newObj.index = i;
206                                                  newObj.value = ""+i;
207                                                  newObj.title = ""+i;
208                                                  $scope.minutes.push(newObj);
209                                          }                                       
210                                          
211                                          $scope.AMPMOptions =[
212                                            {
213                                                   index:0, value:'AM', title:'AM'
214                                          },{
215                                                  index:1, value: 'PM',title:'PM'
216                                          }]
217                                          
218                                          $scope.selectFirstHour =$scope.hours[0];                                        
219                                          $scope.selectFirstMinute =$scope.minutes[0];
220                                          
221                                          $scope.selectLastHour =$scope.hours[0];                                         
222                                          $scope.selectLastMinute =$scope.minutes[0];                                                                                    
223                                          
224                                          $scope.selectStartAMPMOption=$scope.AMPMOptions[0];
225                                          $scope.selectLastAMPMOption=$scope.AMPMOptions[0];
226
227                                          var GenerateCronExpression = function(trigger_dt, RecurrenceOpt) {
228                                                 var CRON_sec = trigger_dt.getSeconds();
229                                                 var CRON_min = trigger_dt.getMinutes();
230                                                 var CRON_hr = trigger_dt.getHours();
231                                                 var CRON_date= trigger_dt.getDate();
232                                                 var CRON_month = trigger_dt.toLocaleString('en-US', {month: 'short'}).toUpperCase();
233                                                 var CRON_day = trigger_dt.toLocaleString('en-US', {weekday: 'short'}).toUpperCase();
234                                                 var CRON_year = trigger_dt.getFullYear();
235                                                 if (RecurrenceOpt ==="One-Time") {
236                                                         CRON_day = '?'
237                                                 } else {
238                                                         if (RecurrenceOpt ==="Hourly") {
239                                                                 CRON_hr = '*';
240                                                                 CRON_date = '*'
241                                                                 CRON_month = '*'
242                                                                 CRON_day = '?'
243                                                                 CRON_year = '*'
244                                                         } else if (RecurrenceOpt ==="Daily") {
245                                                                 CRON_date = '*'
246                                                                 CRON_month = '*'
247                                                                 CRON_day = '?'
248                                                                 CRON_year = '*'
249                                                         } else if (RecurrenceOpt ==="Weekly") {
250                                                                 CRON_date = '*'
251                                                                 CRON_month = '*'
252                                                                 CRON_year = '*'                 
253                                                         }
254                                                 }
255
256                                                 var CRON_Expression = [CRON_sec, CRON_min, CRON_hr, CRON_date, CRON_month, CRON_day, CRON_year];
257                                                 return CRON_Expression.join(" ");
258                                         }
259
260                                         $scope.ok = function() {
261                                                         
262                                                         // DateTime for the start time: it should be noted that the start time
263                                                         // for a CRON job should be prior to the trigger time.
264                                                         $scope.trigger_dt = new Date( $scope.dt.getFullYear() +
265                                                                                                 "-" + ("0"+($scope.dt.getMonth()+1)).slice(-2) +
266                                                                                                 "-" +("0"+ $scope.dt.getDate()).slice(-2) +
267                                                                                                 " " + ("0" + $scope.selectFirstHour.value).slice(-2) +
268                                                                                                 ":" +("0" + $scope.selectFirstMinute.value).slice(-2) +
269                                                                                                 ":00.0");
270
271                                                         $scope.startDateTime_CRON = GenerateCronExpression($scope.trigger_dt, $scope.selectRecurrenceOpt.value)
272
273                                                         //roll back the the start date time by 30 seconds (start time should be 30 seconds prior to trigger time)
274                                                         dt_st = new Date($scope.trigger_dt - 30*1000)
275                                                         
276                                                         startDateTime = dt_st.getFullYear() +
277                                                                                                 "-" + ("0"+(dt_st.getMonth()+1)).slice(-2) +
278                                                                                                 "-" +("0"+ dt_st.getDate()).slice(-2) +
279                                                                                                 " " + ("0" + dt_st.getHours()).slice(-2) +
280                                                                                                 ":" +("0" + dt_st.getMinutes()).slice(-2) +
281                                                                                                 ":" + ("0" + dt_st.getSeconds()).slice(-2) +".0";
282                                                         $scope.startDateTime = startDateTime;
283                                                         
284                                                         $scope.endDateTime = $scope.dt2.getFullYear() + 
285                                                                                                 "-" + ("0"+($scope.dt2.getMonth()+1)).slice(-2) +
286                                                                                                 "-" +("0"+ $scope.dt2.getDate()).slice(-2) +
287                                                                                                 " " + ("0"+ $scope.selectLastHour.value).slice(-2) +
288                                                                                                 ":" +("0" + $scope.selectLastMinute.value).slice(-2) +
289                                                                                                 ":00.0"
290                                                                                                 
291                                                         $scope.WorkflowScheduleObject = {};
292                                                         $scope.WorkflowScheduleObject['startDateTime_CRON'] = $scope.startDateTime_CRON;
293                                                         $scope.WorkflowScheduleObject['startDateTime'] = $scope.startDateTime;
294                                                         $scope.WorkflowScheduleObject['endDateTime'] = $scope.endDateTime;
295                                                         $scope.WorkflowScheduleObject['workflowKey'] = $scope.workflowToSchedule.workflowKey;
296                                                         $scope.WorkflowScheduleObject['recurrence'] = $scope.selectRecurrenceOpt.value;
297                                                         $scope.WorkflowScheduleObject['workflow_arguments'] = "test";
298                                                         $scope.WorkflowScheduleObject['workflow_server_url'] = $scope.workflowToSchedule.runLink;
299                                                         
300                                                         
301                                                         TimeFromNowToStart = new Date($scope.startDateTime)-new Date()
302                                                         TimeStartToEnd = new Date($scope.endDateTime)-new Date($scope.startDateTime)
303                                                         
304                                                         if (TimeFromNowToStart<=0) {
305                                                                 console.log("invalid start time input")
306                                                                 alert("Please ensure the scheduled start date time is later than current time.")
307                                                                 return;
308                                                         }
309                                                         if (TimeStartToEnd<=0) {
310                                                                 console.log("invalid end time input")
311                                                                 alert("Please ensure the schduled end date time is later than the start time.")
312                                                                 return;                                                         
313                                                         }
314                                                         // if successful then save and close
315                                                         $scope.saveCronJob($scope.WorkflowScheduleObject);
316                                                         $uibModalInstance.close();
317                                                         
318                                                 };
319                                                 
320                                                 $scope.saveCronJob = function(cronJobData){
321                                                         
322                                                         console.log('saving cron job data: ' + cronJobData);
323                                                         var uuu = "workflows/saveCronJob.htm";
324                                                         var postData={cronJobDataObj: cronJobData};
325                                                         $.ajax({
326                                                                          type : 'POST',
327                                                                          url : uuu,
328                                                                          //dataType: 'json',                            // data type expected from server
329                                                                          contentType: 'application/json',  
330                                                                          data: JSON.stringify(postData),   // data type sent to server  
331                                                                          success : function(data){
332                                                                                                                                 $scope.$apply(function(){
333                                                                                                                                         //$scope.availableRoleFunctions=[];$scope.$apply();
334                                                                                                                                         // new // $scope.availableFnMenuItems=data.availableFnMenuItems;
335                                                                                                                                         }
336                                                                                                                                         );  
337                                                                                                                                 //alert("Update Successful.") ;
338                                                                                                                                 //$scope.editRoleFunction = null;
339                                                                                                                                 // new /// $modalInstance.close({availableFnMenuItems:$scope.availableRoleFunctions});
340                                                                                                                         },
341                                                                          error : function(data){
342                                                                                  alert("Error while saving."); 
343                                                                          }
344                                                                   });                                                   
345                                                         
346                                                 };
347                         
348                                   $scope.cancel = function() {
349                                           console.log("cancel triggered")
350                                           $uibModalInstance.dismiss();
351                                   };
352                             }]
353                           });
354
355                       modalInstance.result.then(function () {
356                           $scope.$emit('workflowRemoved');                
357                       }, function () {
358                         $log.info('Modal dismissed at: ' + new Date());
359                       });
360                    
361                 };      
362         
363         
364         
365         
366         
367         
368         
369         
370            $scope.previewWorkflow = function(workflowToPreview,modalSize){         
371                    var modalInstance = $uibModal.open({
372                                  animation: $scope.animationsEnabled,
373                                  templateUrl: 'app/fusion/scripts/view-models/workflows/workflow-preview.html',
374                                  size:modalSize,
375                                  controller: ['$scope', '$uibModalInstance', '$http', function ($scope, $uibModalInstance, $http) {
376                                         $scope.workflowToPreview = workflowToPreview;
377                                         console.log('previewWorkFlow invoked');
378                                         console.log($scope.workflowToPreview);
379                         
380                                   $scope.cancel = function() {
381                                           $uibModalInstance.dismiss();
382                                   };
383                             }]
384                           });
385
386                       modalInstance.result.then(function () {
387                           $scope.$emit('workflowRemoved');  
388                       }, function () {
389                         $log.info('Modal dismissed at: ' + new Date());
390                       });
391                    
392                 };   
393         
394         
395         /* change work flow status based on the boolean variable "suspendBool" which corresponds whether 
396          * we would like to suspend or activate a workflow specified by key. */ 
397         $scope.changeWorkflowStatus = function(workflowToChangeStatus,suspendBool){
398                 if (workflowToChangeStatus!==null) {
399                 var statusUrl= workflowToChangeStatus.runLink+"/engine-rest/process-definition/key/"+workflowToChangeStatus.workflowKey
400                 var suspendedUrl= statusUrl+"/suspended"
401                 var xmlHttp = new XMLHttpRequest();
402                 xmlHttp.open('PUT', suspendedUrl, false); 
403                 xmlHttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
404                 xmlHttp.onload = function() {
405                         if (suspendBool) {
406                                 console.log("process definition is now suspended"); 
407                                 workflowToChangeStatus.active="false"
408                         } else {
409                                 console.log("process definition is now activated");                             
410                                 workflowToChangeStatus.active="true"
411                         }
412                 };
413                 xmlHttp.send(JSON.stringify({
414                         "suspended" : suspendBool,
415                         "includeProcessInstances" : true,
416                         "executionDate" : "2013-11-21T10:49:45"
417                 }));
418                 }
419                 
420         };
421
422         $scope.activateWorkflow = function(workflowToActivate){
423                 $scope.changeWorkflowStatus(workflowToActivate,false)
424
425         };
426
427         $scope.suspendWorkflow = function(workflowToActivate){
428                 $scope.changeWorkflowStatus(workflowToActivate,true)
429         };              
430         
431         $scope.checkWorkflowStatus = function(workflow) {
432                 if (workflow!==null) {
433                 var statusUrl= workflow.runLink+"/engine-rest/process-definition/key/"+workflow.workflowKey
434                 var xmlHttp3 = new XMLHttpRequest();
435                 xmlHttp3.open('GET', statusUrl, true);
436                 xmlHttp3.withCredentials = true;
437                 xmlHttp3.send();                
438                 xmlHttp3.onreadystatechange = function() {
439                         if (xmlHttp3.readyState == 4 && xmlHttp3.status == 200) {
440                                 // do something with the response in the variable data
441                                 var temp = JSON.parse(xmlHttp3.responseText)
442                                 if (temp.suspended == false){
443                                         console.log("Activated")
444                                         workflow.active="true"
445                                 } else {
446                                         console.log("Suspended")
447                                         workflow.active="false"
448                                 }
449                         }
450                 }
451                 }
452         };
453         
454         $scope.StartWorkflowInstance = function(workflowToStart){
455                 if (workflowToStart!==null) {
456                 var statusUrl= workflowToStart.runLink+"/engine-rest/process-definition/key/"+workflowToStart.workflowKey
457                 var suspendedUrl= statusUrl+"/submit-form"
458                 var xmlHttp = new XMLHttpRequest();
459                 xmlHttp.open('POST', suspendedUrl, false); 
460                 xmlHttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
461                 xmlHttp.onload = function() {
462                 };
463                 xmlHttp.send(JSON.stringify({
464                           "variables": {
465                                     "customerId": {"value":"asdasda","type":"String"},
466                                     "amount":{"value":"100","type":"String"}
467                                   }
468                                 }));
469                 }
470                 
471         };      
472         
473         
474         $scope.$on('workflowAdded', function(event, newWorkflow) { 
475                 console.log("New Workflow to be added in list scope " + JSON.stringify(newWorkflow)); 
476                 //$scope.workflows.push(newWorkflow);
477                 $scope.fetchWorkflowsList();
478                 console.log('newly added workflow = ' + JSON.stringify(newWorkflow));
479         });
480         
481         $scope.$on('workflowRemoved', function(event) {
482                 $scope.fetchWorkflowsList();
483         });
484
485         $scope.fetchWorkflowsList();
486
487         
488         
489 });
490