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