Yangman - fix delete operation
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / controllers / request-header.controller.js
index 4bc167948ed3f51423f61cc8f04e3632476c801a..79ea725c381e15b43d2e83096dc52a22303aae55 100644 (file)
@@ -1,5 +1,6 @@
 define([
     'app/yangman/controllers/params-admin.controller',
+    'app/yangman/services/time-tracking.services',
 ], function (ParamsAdminCtrl) {
     'use strict';
 
@@ -7,11 +8,12 @@ define([
 
     RequestHeaderCtrl.$inject = [
         '$mdDialog', '$mdToast', '$scope', '$rootScope', 'ENV', 'YangmanService', 'ParametersService',
-        'PathUtilsService', 'RequestsService', '$filter', 'DataBackupService', 'constants'
+        'PathUtilsService', 'RequestsService', '$filter', 'DataBackupService', 'constants', 'TimeTrackingService'
     ];
 
     function RequestHeaderCtrl($mdDialog, $mdToast, $scope, $rootScope, ENV, YangmanService, ParametersService,
-                               PathUtilsService, RequestService, $filter, DataBackupService, constants) {
+                               PathUtilsService, RequestService, $filter, DataBackupService, constants,
+                               TimeTrackingService) {
         var requestHeader = this;
 
         requestHeader.allOperations = [constants.OPERATION_GET, constants.OPERATION_POST, constants.OPERATION_PUT, constants.OPERATION_DELETE];
@@ -42,7 +44,7 @@ define([
         /**
          * Set selected operations based on data store
          */
-        $scope.$on('SET_SEL_OPERATIONS', function (event, operations, setUrl) {
+        $scope.$on(constants.SET_SEL_OPERATIONS, function (event, operations, setUrl) {
             setAllowedMethods(operations);
 
             if ( setUrl ) {
@@ -53,7 +55,7 @@ define([
         /**
          * Watching for changes in shown detail data type (radio button)
          */
-        $scope.$on('YANGMAN_HEADER_INIT', function (event, args) {
+        $scope.$on(constants.YANGMAN_HEADER_INIT, function (event, args) {
             init();
             setRequestUrl(args.params.path);
             setRequestMethod(args.params.method);
@@ -62,12 +64,12 @@ define([
             (args.cbk || angular.noop)();
         });
 
-        $scope.$on('YANGMAN_FILL_NODE_FROM_REQ', function (event, args) {
+        $scope.$on(constants.YANGMAN_FILL_NODE_FROM_REQ, function (event, args) {
             setNodeDataFromRequestData(args.params.requestUrl, args.params.leftpanel);
             (args.cbk || angular.noop)();
         });
 
-        $scope.$on('YANGMAN_EXECUTE_WITH_DATA', function (event, args) {
+        $scope.$on(constants.YANGMAN_EXECUTE_WITH_DATA, function (event, args) {
             executeOperation(args.params.data ? angular.fromJson(args.params.data) : {}, args.cbk);
         });
 
@@ -104,10 +106,14 @@ define([
                 locals: {
                     parametersList: $scope.parametersList,
                 },
-            }).then(function (){
-                $scope.parametersList.loadListFromStorage();
-
-            });
+            }).then(
+                function (){
+                    $scope.parametersList.loadListFromStorage();
+                },
+                function (){
+                    $scope.parametersList.loadListFromStorage();
+                }
+            );
         }
 
         /**
@@ -121,8 +127,6 @@ define([
             } else {
                 $scope.setJsonView(true, false);
             }
-
-            // sendRequestData({}, 'RECEIVED');
         }
 
         /**
@@ -170,8 +174,8 @@ define([
                         var sentData = { reqData: null },
                             allData = { sent: null, received: null };
 
-                        $scope.rootBroadcast('YANGMAN_GET_CODEMIRROR_DATA_RECEIVED', params);
-                        $scope.rootBroadcast('YANGMAN_GET_CODEMIRROR_DATA_SENT', sentData);
+                        $scope.rootBroadcast(constants.YANGMAN_GET_CODEMIRROR_DATA_RECEIVED, params);
+                        $scope.rootBroadcast(constants.YANGMAN_GET_CODEMIRROR_DATA_SENT, sentData);
 
                         allData.sent = sentData.reqData ? angular.fromJson(sentData.reqData) : {};
                         allData.received = params.reqData ? angular.fromJson(params.reqData) : {};
@@ -193,7 +197,7 @@ define([
                             dataType = requestHeader.selectedOperation === constants.OPERATION_GET ? constants.REQUEST_DATA_TYPE_RECEIVED : 'SENT';
                         }
 
-                        $scope.rootBroadcast('YANGMAN_GET_CODEMIRROR_DATA_' + dataType, params);
+                        $scope.rootBroadcast(constants.YANGMAN_GET_CODEMIRROR_DATA + dataType, params);
                         return params.reqData ? angular.fromJson(params.reqData) : {};
                     },
                 };
@@ -206,11 +210,14 @@ define([
          * @param data
          */
         function sendRequestData(data, type){
-            $scope.rootBroadcast('YANGMAN_SET_CODEMIRROR_DATA_' + type, { data: JSON.stringify(data, null, 4) });
+            $scope.rootBroadcast(
+                constants.YANGMAN_SET_CODEMIRROR_DATA + type,
+                { data: data instanceof Object ? JSON.stringify(data, null, 4) : data }
+            );
         }
 
         function sendErrorData(response) {
-            $scope.rootBroadcast('YANGMAN_SET_ERROR_DATA', response);
+            $scope.rootBroadcast(constants.YANGMAN_SET_ERROR_DATA, response);
         }
 
         /**
@@ -279,8 +286,14 @@ define([
             });
         }
 
+        /**
+         * Try to set current (depending on url) selectedApi and selectedSubApi to $scope if it exists in api tree data
+         * @param url
+         * @param cbk
+         * @param fill
+         */
         function setApiByUrl(url, cbk, fill){
-            $scope.rootBroadcast('YANGMAN_GET_API_TREE_DATA', null, function (treeApis) {
+            $scope.rootBroadcast(constants.YANGMAN_GET_API_TREE_DATA, null, function (treeApis) {
                 var apisIndexes =
                     PathUtilsService.searchNodeByPath(url, treeApis, null, true, true);
 
@@ -316,54 +329,57 @@ define([
                 null, null, requestHeader.requestUrl, requestHeader.selectedOperation, '', '', ''
             );
 
-            $scope.rootBroadcast('YANGMAN_GET_CODEMIRROR_DATA_SENT', sentData);
-            $scope.rootBroadcast('YANGMAN_GET_CODEMIRROR_DATA_RECEIVED', receivedData);
+            $scope.rootBroadcast(constants.YANGMAN_GET_CODEMIRROR_DATA_SENT, sentData);
+            $scope.rootBroadcast(constants.YANGMAN_GET_CODEMIRROR_DATA_RECEIVED, receivedData);
 
             RequestService.fillRequestByMethod(
                 historyReq, sentData, receivedData, requestHeader.selectedOperation, $scope.node,
                 requestHeader.selectedShownDataType
             );
 
-            $scope.rootBroadcast('YANGMAN_SAVE_REQUEST_TO_COLLECTION', { event: event, reqObj: historyReq });
+            $scope.rootBroadcast(constants.YANGMAN_SAVE_REQUEST_TO_COLLECTION, { event: event, reqObj: historyReq });
         }
 
         function showRequestProgress(){
-            $scope.rootBroadcast('YANGMAN_EXECUTING_REQUEST_PROGRESS_START');
+            $scope.rootBroadcast(constants.YANGMAN_EXECUTING_REQUEST_PROGRESS_START);
         }
 
 
-        function finishRequestProgress (message){
-            $scope.rootBroadcast('YANGMAN_EXECUTING_REQUEST_PROGRESS_STOP');
-            // $mdToast.show(
-            //     $mdToast.simple()
-            //         .textContent(message)
-            //         .position('bottom right')
-            //         .parent(angular.element('.yangmanModule__right-panel__header'))
-            //         .hideDelay(1500)
-            // );
+        function finishRequestProgress(message){
+            $scope.rootBroadcast(constants.YANGMAN_EXECUTING_REQUEST_PROGRESS_STOP);
         }
 
-
         /**
          * Execute request operation
          */
         function executeOperation(requestData, executeCbk){
+            TimeTrackingService.startTimer();
             var allowExecuteOperation =
                 requestHeader.selectedShownDataType === constants.DISPLAY_TYPE_FORM && $scope.selectedSubApi ?
                     !PathUtilsService.checkEmptyIdentifiers($scope.selectedSubApi.pathArray) : true;
 
+
             if ( allowExecuteOperation ) {
 
                 showRequestProgress();
-                setRequestUrl(requestHeader.selectedShownDataType === constants.DISPLAY_TYPE_REQ_DATA ? requestHeader.requestUrl : null);
-                $scope.rootBroadcast('YANGMAN_SET_ERROR_MESSAGE', '');
+                $scope.rootBroadcast(constants.YANGMAN_SET_ERROR_MESSAGE, '');
 
-                if ( requestHeader.selectedShownDataType !== constants.DISPLAY_TYPE_FORM ){
+                setRequestUrl(
+                    requestHeader.selectedShownDataType === constants.DISPLAY_TYPE_REQ_DATA ?
+                        requestHeader.requestUrl :
+                        null
+                );
+                if ( requestHeader.selectedShownDataType === constants.DISPLAY_TYPE_REQ_DATA ){
                     setApiByUrl(requestHeader.requestUrl, null, true);
                 }
 
-                var historyReq = RequestService.createHistoryRequest(null, null, requestHeader.requestUrl,
-                    requestHeader.selectedOperation, '', '', '');
+                var historyReq = RequestService.createHistoryRequest(
+                    null,
+                    null,
+                    requestHeader.requestUrl,
+                    requestHeader.selectedOperation,
+                    '', '', ''
+                );
 
                 YangmanService.executeRequestOperation(
                     $scope.selectedApi,
@@ -380,8 +396,8 @@ define([
                 requestHeader.executedOperation = requestHeader.selectedOperation;
             } else {
                 $scope.rootBroadcast(
-                    'YANGMAN_SET_ERROR_MESSAGE',
-                    $filter('translate')('YANGMAN_ERROR_EMPTY_IDENTIFIERS')
+                    constants.YANGMAN_SET_ERROR_MESSAGE,
+                    $filter('translate')(constants.YANGMAN_ERROR_EMPTY_IDENTIFIERS)
                 );
             }
 
@@ -391,6 +407,7 @@ define([
              * @param response
              */
             function executeReqSuccCbk(reqInfo, response) {
+
                 var preparedReceivedData = YangmanService.prepareReceivedData(
                     $scope.node,
                     requestHeader.selectedOperation,
@@ -412,7 +429,7 @@ define([
                     sendRequestData(reqInfo.requestSrcData || {}, 'SENT');
                 } else {
 
-                    if ($scope.node){
+                    if ($scope.node && requestHeader.selectedOperation !== constants.OPERATION_DELETE){
 
                         YangmanService.fillNodeFromResponse($scope.node, preparedReceivedData);
                         YangmanService.handleNodeIdentifier(
@@ -420,29 +437,30 @@ define([
                         );
                         $scope.node.expanded = true;
 
-                        $scope.rootBroadcast('YANGMAN_DISABLE_ADDING_LIST_ELEMENT');
+                        $scope.rootBroadcast(constants.YANGMAN_DISABLE_ADDING_LIST_ELEMENT);
                         preparedReceivedData = YangmanService.checkRpcReceivedData(preparedReceivedData, $scope.node);
                         sendRequestData(preparedReceivedData, constants.REQUEST_DATA_TYPE_RECEIVED);
                     }
                 }
 
                 // create and set history request
+                requestHeader.statusObj.time = TimeTrackingService.returnTime();
+
                 historyReq.setExecutionData(
                     reqInfo.requestSrcData,
                     preparedReceivedData,
                     reqInfo.status,
                     reqInfo.status,
                     reqInfo.statusText,
-                    reqInfo.time
+                    requestHeader.statusObj.time
                 );
 
-                $scope.rootBroadcast('YANGMAN_SAVE_EXECUTED_REQUEST', historyReq, function (){
-                    $scope.rootBroadcast('YANGMAN_SELECT_THE_NEWEST_REQUEST');
+                $scope.rootBroadcast(constants.YANGMAN_SAVE_EXECUTED_REQUEST, historyReq, function (){
+                    $scope.rootBroadcast(constants.YANGMAN_SELECT_THE_NEWEST_REQUEST);
                 });
 
                 (executeCbk || angular.noop)(historyReq);
 
-
             }
 
             /**
@@ -455,20 +473,20 @@ define([
 
                 finishRequestProgress();
 
+                requestHeader.statusObj.time = TimeTrackingService.returnTime();
+
                 historyReq.setExecutionData(
                     reqInfo.requestSrcData,
                     response.data,
                     reqInfo.status,
                     reqInfo.status,
                     reqInfo.statusText,
-                    reqInfo.time
+                    requestHeader.statusObj.time
                 );
-                $scope.rootBroadcast('YANGMAN_SAVE_EXECUTED_REQUEST', historyReq, function (){
-                    $scope.rootBroadcast('YANGMAN_SELECT_THE_NEWEST_REQUEST');
+                $scope.rootBroadcast(constants.YANGMAN_SAVE_EXECUTED_REQUEST, historyReq, function (){
+                    $scope.rootBroadcast(constants.YANGMAN_SELECT_THE_NEWEST_REQUEST);
                 });
 
-                //setNodeDataFromRequestData(requestHeader.requestUrl);
-
                 if (response.data) {
                     // try to fill code mirror editor
                     sendRequestData(response.data, constants.REQUEST_DATA_TYPE_RECEIVED);
@@ -500,12 +518,17 @@ define([
          * Check data before executin operations
          */
         function prepareDataAndExecute(cbk){
+            if (requestHeader.statusObj) {
+                requestHeader.statusObj.statusText = null;
+                requestHeader.statusObj.time = null;
+            }
+
             if ( requestHeader.requestUrl.length ) {
 
                 if ( requestHeader.selectedShownDataType === constants.DISPLAY_TYPE_REQ_DATA ) {
                     // get json data
                     var params = { reqData: null };
-                    $scope.rootBroadcast('YANGMAN_GET_CODEMIRROR_DATA_SENT', params);
+                    $scope.rootBroadcast(constants.YANGMAN_GET_CODEMIRROR_DATA_SENT, params);
                     executeOperation(params.reqData ? angular.fromJson(params.reqData) : {}, cbk);
                 } else {
                     executeOperation({}, cbk);
@@ -530,7 +553,7 @@ define([
                 'MAIN_SCOPE'
             );
 
-            $scope.rootBroadcast('YANGMAN_GET_API_TREE_DATA', null, function (treeApis) {
+            $scope.rootBroadcast(constants.YANGMAN_GET_API_TREE_DATA, null, function (treeApis) {
                 DataBackupService.storeFromScope(
                     ['treeApis'],
                     { treeApis: treeApis },
@@ -543,7 +566,7 @@ define([
             $scope.setGlobalParams(mountPointApis, augmentations);
             $scope.setDataStore(null);
             requestHeader.statusObj = reqObj;
-            $scope.rootBroadcast('YANGMAN_SET_API_TREE_DATA', mountPointTreeApis);
+            $scope.rootBroadcast(constants.YANGMAN_SET_API_TREE_DATA, mountPointTreeApis);
         }
 
         /**