2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 app.controller('workflowsController', function($scope, $http, $uibModal, $log, modalService, $modal) {
22 $scope.viewPerPage = 5;
23 $scope.scrollViewsPerPage = 20;
24 $scope.currentPage = 2;
26 $scope.searchCategory = "";
27 $scope.searchString = "";
33 $scope.showModal = false;
34 $scope.toggleModal = function(){
35 $scope.showModal = !$scope.showModal;
39 $scope.workflow.active = "true";
41 $scope.updateAllWorkflowStatus = function() {
42 angular.forEach($scope.workflows,function(value){
43 $scope.checkWorkflowStatus(value);
47 $scope.fetchWorkflowsList = function() {
48 $http.get('workflows/list').then(function(workflowList){
49 console.log('Got new list from server = ' + workflowList.data);
50 $scope.workflows = workflowList.data;
51 $scope.updateAllWorkflowStatus();
55 $scope.addNewWorkflow = function(newWorkflow) {
56 $http.post('workflows/addWorkflow/', JSON.stringify(newWorkflow)).success(function() {
57 $scope.fetchWorkflowsList();
60 $scope.workflow.name = '';
64 $scope.updateWorkflow = function (workflowToEdit) {
65 //workflowToEdit.active='true';
66 var modalInstance = $uibModal.open({
67 animation: $scope.animationsEnabled,
68 templateUrl: 'app/fusion/scripts/view-models/workflows/workflow-new.html',
70 controller: ['$scope', '$uibModalInstance', '$http', function ($scope, $uibModalInstance, $http) {
71 $scope.workflow = workflowToEdit;
72 $scope.ok = function() {
73 console.log('Updating existing workflow ... ' + JSON.stringify($scope.workflow));
74 $http.post('workflows/editWorkflow/', JSON.stringify($scope.workflow)).then(function(returnedWorkflow){
75 console.log('Returned Workflow = ' + JSON.stringify(returnedWorkflow));
76 $uibModalInstance.close($scope.workflow);
80 $scope.cancel = function() {
81 $uibModalInstance.dismiss();
84 //End of inner controller
86 workflow: function() {
87 console.log('Passing ' + JSON.stringify($scope.workflow));
88 return $scope.workflow;
93 modalInstance.result.then(function (editedWorkFlow) {
94 //Need to convert to proper date - later
95 delete editedWorkFlow.created;
96 delete editedWorkFlow.updated;
98 delete editedWorkFlow.createdBy;
99 delete editedWorkFlow.modifiedBy;
101 console.log('selected Item ' + JSON.stringify(editedWorkFlow));
102 $scope.$emit('workflowAdded', editedWorkFlow);
105 $log.info('Modal dismissed at: ' + new Date());
109 $scope.reset = function(){
110 console.log("Resetting ....");
113 $scope.update = function(){
114 console.log("updating ....");
117 $scope.createWorkflow = function (modalSize) {
119 var modalInstance = $uibModal.open({
120 animation: $scope.animationsEnabled,
121 templateUrl: 'app/fusion/scripts/view-models/workflows/workflow-new.html',
123 controller: ['$scope', '$uibModalInstance', '$http', function ($scope, $uibModalInstance, $http) {
124 $scope.workflow = {};
125 $scope.workflow.active = 'true';
126 $scope.ok = function() {
127 console.log('Saving new workflow ... ' + JSON.stringify($scope.workflow));
128 $http.post('workflows/addWorkflow/', JSON.stringify($scope.workflow)).then(function(returnedWorkflow){
129 console.log('Returned Workflow = ' + JSON.stringify(returnedWorkflow));
130 $uibModalInstance.close($scope.workflow);
133 /* console.log(size);*/
134 $scope.cancel = function() {
135 $uibModalInstance.dismiss();
138 //End of inner controller
140 workflow: function() {
141 console.log('Passing ' + JSON.stringify($scope.workflow));
142 return $scope.workflow;
147 modalInstance.result.then(function (newWorkflow) {
148 console.log('selected Item ' + JSON.stringify(newWorkflow));
149 $scope.$emit('workflowAdded', newWorkflow);
152 $log.info('Modal dismissed at: ' + new Date());
154 };//End of createWorkflow function
157 $scope.removeWorkflow = function(workflowToRemove){
158 var modalInstance = $uibModal.open({
159 animation: $scope.animationsEnabled,
160 templateUrl: 'app/fusion/scripts/view-models/workflows/workflow-remove.html',
161 controller: ['$scope', '$uibModalInstance', '$http', function ($scope, $uibModalInstance, $http) {
162 $scope.workflowToRemove = workflowToRemove;
163 $scope.ok = function() {
164 console.log('Removing workflow ... ' + JSON.stringify($scope.workflowToRemove) + ' on client request.');
165 $http.post('workflows/removeWorkflow/', JSON.stringify($scope.workflowToRemove.id)).then(function(){
166 console.log('Workflow successfully removed !!!');
167 $uibModalInstance.close();
171 $scope.cancel = function() {
172 $uibModalInstance.dismiss();
177 modalInstance.result.then(function () {
178 $scope.$emit('workflowRemoved');
180 $log.info('Modal dismissed at: ' + new Date());
187 $scope.scheduleWorkflow = function(workflowToSchedule){
188 var modalInstance = $uibModal.open({
189 animation: $scope.animationsEnabled,
190 templateUrl: 'app/fusion/scripts/view-models/workflows/workflow-schedule.html',
193 controller: ['$scope', '$uibModalInstance', '$http','dateFilter', function ($scope, $uibModalInstance, $http,dateFilter) {
195 $scope.workflowToSchedule = workflowToSchedule;
196 $scope.dt = new Date();
197 $scope.dt2 = new Date();
198 $scope.dateformat = 'MM/dd/yyyy',
199 $scope.datetimeformat = "hh:mm a";
201 $scope.recurrenceOptions =[{
202 index:0, value:'One-Time', title:'One-Time'
204 index:1, value: 'Hourly',title:'Hourly'
206 index:2, value: 'Daily',title:'Daily'
208 index:3, value: 'Weekly',title:'Weekly'
210 $scope.selectRecurrenceOpt = $scope.recurrenceOptions[0];
213 for (var i=0; i<24; i++){
218 $scope.hours.push(newObj);
222 for (var i=0; i<60; i++){
227 $scope.minutes.push(newObj);
230 $scope.AMPMOptions =[
232 index:0, value:'AM', title:'AM'
234 index:1, value: 'PM',title:'PM'
237 $scope.selectFirstHour =$scope.hours[0];
238 $scope.selectFirstMinute =$scope.minutes[0];
240 $scope.selectLastHour =$scope.hours[0];
241 $scope.selectLastMinute =$scope.minutes[0];
243 $scope.selectStartAMPMOption=$scope.AMPMOptions[0];
244 $scope.selectLastAMPMOption=$scope.AMPMOptions[0];
246 var GenerateCronExpression = function(trigger_dt, RecurrenceOpt) {
247 var CRON_sec = trigger_dt.getSeconds();
248 var CRON_min = trigger_dt.getMinutes();
249 var CRON_hr = trigger_dt.getHours();
250 var CRON_date= trigger_dt.getDate();
251 var CRON_month = trigger_dt.toLocaleString('en-US', {month: 'short'}).toUpperCase();
252 var CRON_day = trigger_dt.toLocaleString('en-US', {weekday: 'short'}).toUpperCase();
253 var CRON_year = trigger_dt.getFullYear();
254 if (RecurrenceOpt ==="One-Time") {
257 if (RecurrenceOpt ==="Hourly") {
263 } else if (RecurrenceOpt ==="Daily") {
268 } else if (RecurrenceOpt ==="Weekly") {
275 var CRON_Expression = [CRON_sec, CRON_min, CRON_hr, CRON_date, CRON_month, CRON_day, CRON_year];
276 return CRON_Expression.join(" ");
279 $scope.ok = function() {
281 // DateTime for the start time: it should be noted that the start time
282 // for a CRON job should be prior to the trigger time.
283 $scope.trigger_dt = new Date( $scope.dt.getFullYear() +
284 "-" + ("0"+($scope.dt.getMonth()+1)).slice(-2) +
285 "-" +("0"+ $scope.dt.getDate()).slice(-2) +
286 " " + ("0" + $scope.selectFirstHour.value).slice(-2) +
287 ":" +("0" + $scope.selectFirstMinute.value).slice(-2) +
290 $scope.startDateTime_CRON = GenerateCronExpression($scope.trigger_dt, $scope.selectRecurrenceOpt.value)
292 //roll back the the start date time by 30 seconds (start time should be 30 seconds prior to trigger time)
293 dt_st = new Date($scope.trigger_dt - 30*1000)
295 startDateTime = dt_st.getFullYear() +
296 "-" + ("0"+(dt_st.getMonth()+1)).slice(-2) +
297 "-" +("0"+ dt_st.getDate()).slice(-2) +
298 " " + ("0" + dt_st.getHours()).slice(-2) +
299 ":" +("0" + dt_st.getMinutes()).slice(-2) +
300 ":" + ("0" + dt_st.getSeconds()).slice(-2) +".0";
301 $scope.startDateTime = startDateTime;
303 $scope.endDateTime = $scope.dt2.getFullYear() +
304 "-" + ("0"+($scope.dt2.getMonth()+1)).slice(-2) +
305 "-" +("0"+ $scope.dt2.getDate()).slice(-2) +
306 " " + ("0"+ $scope.selectLastHour.value).slice(-2) +
307 ":" +("0" + $scope.selectLastMinute.value).slice(-2) +
310 $scope.WorkflowScheduleObject = {};
311 $scope.WorkflowScheduleObject['startDateTime_CRON'] = $scope.startDateTime_CRON;
312 $scope.WorkflowScheduleObject['startDateTime'] = $scope.startDateTime;
313 $scope.WorkflowScheduleObject['endDateTime'] = $scope.endDateTime;
314 $scope.WorkflowScheduleObject['workflowKey'] = $scope.workflowToSchedule.workflowKey;
315 $scope.WorkflowScheduleObject['recurrence'] = $scope.selectRecurrenceOpt.value;
316 $scope.WorkflowScheduleObject['workflow_arguments'] = "test";
317 $scope.WorkflowScheduleObject['workflow_server_url'] = $scope.workflowToSchedule.runLink;
320 TimeFromNowToStart = new Date($scope.startDateTime)-new Date()
321 TimeStartToEnd = new Date($scope.endDateTime)-new Date($scope.startDateTime)
323 if (TimeFromNowToStart<=0) {
324 console.log("invalid start time input")
325 alert("Please ensure the scheduled start date time is later than current time.")
328 if (TimeStartToEnd<=0) {
329 console.log("invalid end time input")
330 alert("Please ensure the schduled end date time is later than the start time.")
333 // if successful then save and close
334 $scope.saveCronJob($scope.WorkflowScheduleObject);
335 $uibModalInstance.close();
339 $scope.saveCronJob = function(cronJobData){
341 console.log('saving cron job data: ' + cronJobData);
342 var uuu = "workflows/saveCronJob.htm";
343 var postData={cronJobDataObj: cronJobData};
347 //dataType: 'json', // data type expected from server
348 contentType: 'application/json',
349 data: JSON.stringify(postData), // data type sent to server
350 success : function(data){
351 $scope.$apply(function(){
352 //$scope.availableRoleFunctions=[];$scope.$apply();
353 // new // $scope.availableFnMenuItems=data.availableFnMenuItems;
356 //alert("Update Successful.") ;
357 //$scope.editRoleFunction = null;
358 // new /// $modalInstance.close({availableFnMenuItems:$scope.availableRoleFunctions});
360 error : function(data){
361 alert("Error while saving.");
367 $scope.cancel = function() {
368 console.log("cancel triggered")
369 $uibModalInstance.dismiss();
374 modalInstance.result.then(function () {
375 $scope.$emit('workflowRemoved');
377 $log.info('Modal dismissed at: ' + new Date());
389 $scope.previewWorkflow = function(workflowToPreview,modalSize){
390 var modalInstance = $uibModal.open({
391 animation: $scope.animationsEnabled,
392 templateUrl: 'app/fusion/scripts/view-models/workflows/workflow-preview.html',
394 controller: ['$scope', '$uibModalInstance', '$http', function ($scope, $uibModalInstance, $http) {
395 $scope.workflowToPreview = workflowToPreview;
396 console.log('previewWorkFlow invoked');
397 console.log($scope.workflowToPreview);
399 $scope.cancel = function() {
400 $uibModalInstance.dismiss();
405 modalInstance.result.then(function () {
406 $scope.$emit('workflowRemoved');
408 $log.info('Modal dismissed at: ' + new Date());
414 /* change work flow status based on the boolean variable "suspendBool" which corresponds whether
415 * we would like to suspend or activate a workflow specified by key. */
416 $scope.changeWorkflowStatus = function(workflowToChangeStatus,suspendBool){
417 if (workflowToChangeStatus!==null) {
418 var statusUrl= workflowToChangeStatus.runLink+"/engine-rest/process-definition/key/"+workflowToChangeStatus.workflowKey
419 var suspendedUrl= statusUrl+"/suspended"
420 var xmlHttp = new XMLHttpRequest();
421 xmlHttp.open('PUT', suspendedUrl, false);
422 xmlHttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
423 xmlHttp.onload = function() {
425 console.log("process definition is now suspended");
426 workflowToChangeStatus.active="false"
428 console.log("process definition is now activated");
429 workflowToChangeStatus.active="true"
432 xmlHttp.send(JSON.stringify({
433 "suspended" : suspendBool,
434 "includeProcessInstances" : true,
435 "executionDate" : "2013-11-21T10:49:45"
441 $scope.activateWorkflow = function(workflowToActivate){
442 $scope.changeWorkflowStatus(workflowToActivate,false)
446 $scope.suspendWorkflow = function(workflowToActivate){
447 $scope.changeWorkflowStatus(workflowToActivate,true)
450 $scope.checkWorkflowStatus = function(workflow) {
451 if (workflow!==null) {
452 var statusUrl= workflow.runLink+"/engine-rest/process-definition/key/"+workflow.workflowKey
453 var xmlHttp3 = new XMLHttpRequest();
454 xmlHttp3.open('GET', statusUrl, true);
455 xmlHttp3.withCredentials = true;
457 xmlHttp3.onreadystatechange = function() {
458 if (xmlHttp3.readyState == 4 && xmlHttp3.status == 200) {
459 // do something with the response in the variable data
460 var temp = JSON.parse(xmlHttp3.responseText)
461 if (temp.suspended == false){
462 console.log("Activated")
463 workflow.active="true"
465 console.log("Suspended")
466 workflow.active="false"
473 $scope.StartWorkflowInstance = function(workflowToStart){
474 if (workflowToStart!==null) {
475 var statusUrl= workflowToStart.runLink+"/engine-rest/process-definition/key/"+workflowToStart.workflowKey
476 var suspendedUrl= statusUrl+"/submit-form"
477 var xmlHttp = new XMLHttpRequest();
478 xmlHttp.open('POST', suspendedUrl, false);
479 xmlHttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
480 xmlHttp.onload = function() {
482 xmlHttp.send(JSON.stringify({
484 "customerId": {"value":"asdasda","type":"String"},
485 "amount":{"value":"100","type":"String"}
493 $scope.$on('workflowAdded', function(event, newWorkflow) {
494 console.log("New Workflow to be added in list scope " + JSON.stringify(newWorkflow));
495 //$scope.workflows.push(newWorkflow);
496 $scope.fetchWorkflowsList();
497 console.log('newly added workflow = ' + JSON.stringify(newWorkflow));
500 $scope.$on('workflowRemoved', function(event) {
501 $scope.fetchWorkflowsList();
504 $scope.fetchWorkflowsList();