1 app.controller('workflowsController', function($scope, $http, $uibModal, $log, modalService, $modal) {
3 $scope.viewPerPage = 5;
4 $scope.scrollViewsPerPage = 20;
5 $scope.currentPage = 2;
7 $scope.searchCategory = "";
8 $scope.searchString = "";
14 $scope.showModal = false;
15 $scope.toggleModal = function(){
16 $scope.showModal = !$scope.showModal;
20 $scope.workflow.active = "true";
22 $scope.updateAllWorkflowStatus = function() {
23 angular.forEach($scope.workflows,function(value){
24 $scope.checkWorkflowStatus(value);
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();
36 $scope.addNewWorkflow = function(newWorkflow) {
37 $http.post('workflows/addWorkflow/', JSON.stringify(newWorkflow)).success(function() {
38 $scope.fetchWorkflowsList();
41 $scope.workflow.name = '';
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',
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);
61 $scope.cancel = function() {
62 $uibModalInstance.dismiss();
65 //End of inner controller
67 workflow: function() {
68 console.log('Passing ' + JSON.stringify($scope.workflow));
69 return $scope.workflow;
74 modalInstance.result.then(function (editedWorkFlow) {
75 //Need to convert to proper date - later
76 delete editedWorkFlow.created;
77 delete editedWorkFlow.updated;
79 delete editedWorkFlow.createdBy;
80 delete editedWorkFlow.modifiedBy;
82 console.log('selected Item ' + JSON.stringify(editedWorkFlow));
83 $scope.$emit('workflowAdded', editedWorkFlow);
86 $log.info('Modal dismissed at: ' + new Date());
90 $scope.reset = function(){
91 console.log("Resetting ....");
94 $scope.update = function(){
95 console.log("updating ....");
98 $scope.createWorkflow = function (modalSize) {
100 var modalInstance = $uibModal.open({
101 animation: $scope.animationsEnabled,
102 templateUrl: 'app/fusion/scripts/view-models/workflows/workflow-new.html',
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);
114 /* console.log(size);*/
115 $scope.cancel = function() {
116 $uibModalInstance.dismiss();
119 //End of inner controller
121 workflow: function() {
122 console.log('Passing ' + JSON.stringify($scope.workflow));
123 return $scope.workflow;
128 modalInstance.result.then(function (newWorkflow) {
129 console.log('selected Item ' + JSON.stringify(newWorkflow));
130 $scope.$emit('workflowAdded', newWorkflow);
133 $log.info('Modal dismissed at: ' + new Date());
135 };//End of createWorkflow function
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();
152 $scope.cancel = function() {
153 $uibModalInstance.dismiss();
158 modalInstance.result.then(function () {
159 $scope.$emit('workflowRemoved');
161 $log.info('Modal dismissed at: ' + new Date());
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',
174 controller: ['$scope', '$uibModalInstance', '$http','dateFilter', function ($scope, $uibModalInstance, $http,dateFilter) {
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";
182 $scope.recurrenceOptions =[{
183 index:0, value:'One-Time', title:'One-Time'
185 index:1, value: 'Hourly',title:'Hourly'
187 index:2, value: 'Daily',title:'Daily'
189 index:3, value: 'Weekly',title:'Weekly'
191 $scope.selectRecurrenceOpt = $scope.recurrenceOptions[0];
194 for (var i=0; i<24; i++){
199 $scope.hours.push(newObj);
203 for (var i=0; i<60; i++){
208 $scope.minutes.push(newObj);
211 $scope.AMPMOptions =[
213 index:0, value:'AM', title:'AM'
215 index:1, value: 'PM',title:'PM'
218 $scope.selectFirstHour =$scope.hours[0];
219 $scope.selectFirstMinute =$scope.minutes[0];
221 $scope.selectLastHour =$scope.hours[0];
222 $scope.selectLastMinute =$scope.minutes[0];
224 $scope.selectStartAMPMOption=$scope.AMPMOptions[0];
225 $scope.selectLastAMPMOption=$scope.AMPMOptions[0];
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") {
238 if (RecurrenceOpt ==="Hourly") {
244 } else if (RecurrenceOpt ==="Daily") {
249 } else if (RecurrenceOpt ==="Weekly") {
256 var CRON_Expression = [CRON_sec, CRON_min, CRON_hr, CRON_date, CRON_month, CRON_day, CRON_year];
257 return CRON_Expression.join(" ");
260 $scope.ok = function() {
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) +
271 $scope.startDateTime_CRON = GenerateCronExpression($scope.trigger_dt, $scope.selectRecurrenceOpt.value)
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)
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;
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) +
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;
301 TimeFromNowToStart = new Date($scope.startDateTime)-new Date()
302 TimeStartToEnd = new Date($scope.endDateTime)-new Date($scope.startDateTime)
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.")
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.")
314 // if successful then save and close
315 $scope.saveCronJob($scope.WorkflowScheduleObject);
316 $uibModalInstance.close();
320 $scope.saveCronJob = function(cronJobData){
322 console.log('saving cron job data: ' + cronJobData);
323 var uuu = "workflows/saveCronJob.htm";
324 var postData={cronJobDataObj: cronJobData};
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;
337 //alert("Update Successful.") ;
338 //$scope.editRoleFunction = null;
339 // new /// $modalInstance.close({availableFnMenuItems:$scope.availableRoleFunctions});
341 error : function(data){
342 alert("Error while saving.");
348 $scope.cancel = function() {
349 console.log("cancel triggered")
350 $uibModalInstance.dismiss();
355 modalInstance.result.then(function () {
356 $scope.$emit('workflowRemoved');
358 $log.info('Modal dismissed at: ' + new Date());
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',
375 controller: ['$scope', '$uibModalInstance', '$http', function ($scope, $uibModalInstance, $http) {
376 $scope.workflowToPreview = workflowToPreview;
377 console.log('previewWorkFlow invoked');
378 console.log($scope.workflowToPreview);
380 $scope.cancel = function() {
381 $uibModalInstance.dismiss();
386 modalInstance.result.then(function () {
387 $scope.$emit('workflowRemoved');
389 $log.info('Modal dismissed at: ' + new Date());
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() {
406 console.log("process definition is now suspended");
407 workflowToChangeStatus.active="false"
409 console.log("process definition is now activated");
410 workflowToChangeStatus.active="true"
413 xmlHttp.send(JSON.stringify({
414 "suspended" : suspendBool,
415 "includeProcessInstances" : true,
416 "executionDate" : "2013-11-21T10:49:45"
422 $scope.activateWorkflow = function(workflowToActivate){
423 $scope.changeWorkflowStatus(workflowToActivate,false)
427 $scope.suspendWorkflow = function(workflowToActivate){
428 $scope.changeWorkflowStatus(workflowToActivate,true)
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;
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"
446 console.log("Suspended")
447 workflow.active="false"
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() {
463 xmlHttp.send(JSON.stringify({
465 "customerId": {"value":"asdasda","type":"String"},
466 "amount":{"value":"100","type":"String"}
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));
481 $scope.$on('workflowRemoved', function(event) {
482 $scope.fetchWorkflowsList();
485 $scope.fetchWorkflowsList();