Yangman - update collection saving
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / services / yangman.services.js
index a13ca474f1e8b0a6dca660d1f06958027813db7c..c26ac05b718ef8f96a8ba9882d9c230fdc99806e 100644 (file)
@@ -10,6 +10,7 @@ define([], function () {
         'ENV',
         'ParsingJsonService',
         'RequestsService',
+        'PathUtilsService',
 
     ];
 
@@ -19,20 +20,47 @@ define([], function () {
         YangUtilsRestangularService,
         ENV,
         ParsingJsonService,
-        RequestsService
+        RequestsService,
+        PathUtilsService
     ){
         var service = {
+            cutUrl: cutUrl,
             checkRpcReceivedData: checkRpcReceivedData,
             executeRequestOperation: executeRequestOperation,
             fillNodeFromResponse: fillNodeFromResponse,
             getDataStoreIndex: getDataStoreIndex,
+            handleNodeIdentifier: handleNodeIdentifier,
             prepareAllRequestData: prepareAllRequestData,
             prepareReceivedData: prepareReceivedData,
+            putIntoObj: putIntoObj,
+            setSrcDataByDataType: setSrcDataByDataType,
             validateFile: validateFile,
         };
 
         return service;
 
+        /**
+         * Handle param continuum between header path array and node data
+         * @param parametersList
+         * @param selectedSubApi
+         * @param node
+         */
+        function handleNodeIdentifier(parametersList, selectedSubApi, node){
+            var identifier = RequestsService.findIdentifierByParam(
+                parametersList, selectedSubApi.pathArray[selectedSubApi.pathArray.length - 1]
+            );
+
+            if ( identifier ){
+                PathUtilsService.fillListNode(node, identifier.label, identifier.value);
+            }
+        }
+
+        /**
+         * Put data to output container if root node is rpc
+         * @param data
+         * @param node
+         * @returns {*}
+         */
         function checkRpcReceivedData(data, node){
             return node.type === 'rpc' ? cutData(data) : data;
 
@@ -43,6 +71,20 @@ define([], function () {
             }
         }
 
+        /**
+         * Put source object into destination object by source properties
+         * @param sourceObj
+         * @param destinationObj
+         */
+        function putIntoObj(sourceObj, destinationObj, containter){
+            if ( sourceObj ) {
+                Object.keys(sourceObj).forEach(function(prop){
+                    destinationObj[containter] = destinationObj[containter] ? destinationObj[containter] : {};
+                    destinationObj[containter][prop] = sourceObj[prop];
+                });
+            }
+        }
+
         /**
          * Prepare request date before filling into node depends on method and node type
          * @param node
@@ -54,49 +96,44 @@ define([], function () {
          */
         function prepareReceivedData(node, method, rData, sData, outputType){
             var prepareType = {
-                rpc: function(){
+                rpc: function (){
 
                     if ( outputType === 'form' ){
                         var dObj = {};
+
+                        if ( !sData ) {
+                            sData = {};
+                            sData[node.label] = {};
+                        }
+
                         putIntoObj(rData, dObj, node.label);
                         putIntoObj(sData[node.label] ? sData[node.label] : sData, dObj, node.label);
                         return dObj;
                     } else {
                         return rData;
                     }
-
-                    /**
-                     * Put source object into destination object by source properties
-                     * @param sourceObj
-                     * @param destinationObj
-                     */
-                    function putIntoObj(sourceObj, destinationObj, containter){
-                        Object.keys(sourceObj).forEach(function(prop){
-                            destinationObj[containter] = destinationObj[containter] ? destinationObj[containter] : {};
-                            destinationObj[containter][prop] = sourceObj[prop];
-                        });
-                    }
                 },
                 default: function (){
                     var methodType = {
                         GET: function () {
-                            node.clear();
+                            if ( node ){
+                                node.clear();
+                            }
                             return rData;
                         },
                         DELETE: function () {
-                            node.clear();
+                            if ( node ) {
+                                node.clear();
+                            }
                             return {};
                         },
-                        PUT: function () {
+                        DEFAULT: function () {
                             return rData;
                         },
-                        DEFAULT: function () {
-                            return outputType === 'form' ? sData : rData;
-                        }
                     };
 
                     return (methodType[method] || methodType.DEFAULT)();
-                }
+                },
             };
 
             return (prepareType[node ? node.type : 'default'] || prepareType.default)();
@@ -138,6 +175,45 @@ define([], function () {
             return result ? rIndex : null;
         }
 
+        /**
+         * Apply all parametrized values into request (data, url, pathArray)
+         * @param allPreparedData
+         * @param params
+         * @param selSubApiCopy
+         * @param requestUrl
+         */
+        function setParametrizedData(allPreparedData, params, selSubApiCopy, requestUrl){
+            allPreparedData.reqFullUrl = RequestsService.applyParamsToStr(params, requestUrl);
+
+            // apply parametrized value into request data in string form
+            allPreparedData.reqString =
+                selSubApiCopy ? RequestsService.applyParamsToStr(params, selSubApiCopy.buildApiRequestString()) : '';
+
+            if ( !angular.equals(allPreparedData.reqFullUrl, requestUrl) && selSubApiCopy ){
+                // fill parametrized data into path array
+                PathUtilsService.fillPath(selSubApiCopy.pathArray, allPreparedData.reqFullUrl);
+            }
+
+            allPreparedData.reqData = RequestsService.applyParamsToObj(params, allPreparedData.srcData);
+        }
+
+        /**
+         * Set source data into request object based on shown data type
+         * @param allPreparedData
+         * @param node
+         * @param requestData
+         * @param dataType
+         */
+        function setSrcDataByDataType(allPreparedData, node, requestData, dataType){
+            if ( dataType === 'form' && node){
+                node.buildRequest(RequestBuilderService, requestData, node.module);
+                allPreparedData.srcData = angular.copy(requestData);
+            }
+            else {
+                allPreparedData.srcData = requestData;
+            }
+        }
+
         /**
          * Prepare all necessary data for executing or saving request
          * @param selectedApi
@@ -154,41 +230,39 @@ define([], function () {
         function prepareAllRequestData(selectedApi, selectedSubApi, operation, node, dataType, requestUrl, requestData,
                                        params) {
             var allPreparedData = {
-                customRestangular: null,
-                headers: {},
-                operation: '',
-                reqString: selectedSubApi ? selectedSubApi.buildApiRequestString() : '',
-                reqHeaders: {},
-                reqData: '',
-                srcData: '',
-            };
-
-            if ( dataType === 'form' && node){
-                node.buildRequest(RequestBuilderService, requestData, node.module);
-                allPreparedData.srcData = angular.copy(requestData);
-            }
-            else {
-                allPreparedData.srcData = requestData;
-            }
+                    customRestangular: null,
+                    headers: {},
+                    operation: '',
+                    reqString: '',
+                    reqHeaders: {},
+                    reqData: '',
+                    srcData: '',
+                    reqFullUrl: '',
+                },
+                selSubApiCopy = angular.copy(selectedSubApi);
 
-            allPreparedData.reqData = RequestsService.applyParams(params, allPreparedData.srcData);
+            setSrcDataByDataType(allPreparedData, node, requestData, dataType);
+            setParametrizedData(allPreparedData, params, selSubApiCopy, requestUrl);
 
             // prepare req data
             if (operation === 'GET' || operation === 'DELETE'){
                 allPreparedData.srcData = null;
                 allPreparedData.reqData = null;
             }
-            else if (operation === 'POST'){
-                allPreparedData.reqData = YangUtilsService.postRequestData(
-                    allPreparedData.reqData,
-                    allPreparedData.reqString,
-                    selectedSubApi
-                );
+            else if ( operation === 'POST' ){
+
+                if ( selSubApiCopy ) {
+                    allPreparedData.reqData = YangUtilsService.postRequestData(
+                        allPreparedData.reqData,
+                        allPreparedData.reqString,
+                        selSubApiCopy
+                    );
+                }
             }
 
             // set correct host into restangular based on shown data type and prepare data
             if ( dataType === 'req-data' ){
-                var parser = locationHelper(requestUrl, ['pathname', 'origin']),
+                var parser = locationHelper(allPreparedData.reqFullUrl, ['pathname', 'origin']),
                     raParam = '';
 
                 YangUtilsRestangularService.setBaseUrl(parser.origin);
@@ -212,6 +286,10 @@ define([], function () {
             return allPreparedData;
         }
 
+        function cutUrl(url){
+            return url.indexOf('restconf') > -1 ? url.split('restconf')[1].substring(1) : url;
+        }
+
         /**
          * Execute request built from this data
          * @param selectedApi