1 /**
  2  * Created by I308065 on 23/12/2015.
  3  * URLHelper API.
  4  */
  5 (function () {
  6     "use strict";
  7     jQuery.sap.declare("sap.ushell.cpv2.services.cloudServices.URLHelper");
  8 
  9     /**
 10      * @class Generates platform and account dependent URLs
 11      * @constructor
 12      */
 13     sap.ushell.cpv2.services.cloudServices.URLHelper = function URLHelper() {
 14         this._removeSlashPrefix = function _removeSlashPrefix(sPath) {
 15             // returns path start without preffix '/'
 16             if (sPath.charAt(0) === "/") {
 17                 return sPath.substring(1);
 18             }
 19             return sPath;
 20 
 21         };
 22 
 23         this._getCmpName = function _getCmpName(sComponentID) {
 24             var cmp = sap.ui.getCore().getComponent(sComponentID),
 25                 name = cmp.getMetadata().getName();
 26             //cuts the suffix .Component
 27             return name.substring(0, name.length - 10);
 28 
 29         };
 30 
 31         this._getComponentManifet = function _getComponentManifet(componentID){
 32             var cmp = sap.ui.getCore().getComponent(componentID);
 33             return cmp.getMetadata().getManifest();
 34         };
 35 
 36         this._getDataSourceUri = function _getDataSourceUri(sComponentID, sDataSourceKey) {
 37             var manifest = this._getComponentManifet(sComponentID),
 38                 uri = "/";
 39             if (manifest['sap.app'] && manifest['sap.app'].dataSources && manifest['sap.app'].dataSources[sDataSourceKey] && manifest['sap.app'].dataSources[sDataSourceKey].uri) {
 40                 uri = manifest['sap.app'].dataSources[sDataSourceKey].uri;
 41             }
 42             return uri;
 43 
 44         };
 45     };
 46 
 47     /**
 48      * Creates Data Source URI
 49      * Creates URLs that are calculated with the same rules as URI of data source in manifest.json
 50      * @param sComponentID - Component ID
 51      * @param sDataSourceKey - Data source key (under sap.ui5/models) , e.g: "equipment"
 52      * @param sPath - e.g:"/materials/1/parts?skip=10&limit=10"
 53      * @returns {string} e.g: "/sap/fiori/app_subscription_name/path/to/component/equipment/materials/1/parts?skip=10&limit=10".
 54      */
 55     sap.ushell.cpv2.services.cloudServices.URLHelper.prototype.createDataSourceURI = function createDataSourceURI(sComponentID, sDataSourceKey, sPath) {
 56         if (!sDataSourceKey) {
 57             jQuery.sap.log.error("Data source key is mandatory");
 58             return null;
 59         }
 60         var fullUri = "", cmpName, modulePath, dataSourceUri,
 61             sNewPath = sPath;
 62         
 63         if (sComponentID) {
 64             cmpName = this._getCmpName(sComponentID);
 65             modulePath = jQuery.sap.getModulePath(cmpName);
 66             dataSourceUri = this._getDataSourceUri(sComponentID, sDataSourceKey);
 67             if (dataSourceUri.substr(0, 2) === "./") {
 68                 dataSourceUri = dataSourceUri.substr(1);
 69             }
 70             if (dataSourceUri.substr(0, 1) !== "/") {
 71                 dataSourceUri = "/" + dataSourceUri;
 72             }
 73             if (sNewPath && sNewPath.substr(0, 2) === "./") {
 74                 sNewPath = sNewPath.substr(1);
 75             }
 76             //Check if data dataSources uri already has componentName preffix
 77             if (dataSourceUri.substr(0, modulePath.length) === modulePath) {
 78                 fullUri = dataSourceUri + (sNewPath ? sNewPath : '');
 79             } else {
 80                 fullUri = modulePath + dataSourceUri + (sNewPath ? sNewPath : '');
 81             }
 82 
 83         }
 84         return fullUri;
 85 
 86     };
 87     /**
 88      * Creates URI relative to component.
 89      * Generates platform and account dependent URL
 90      * @param sComponentID - Component ID
 91      * @param sPath - e.g: "/img/logo.jpg"
 92      * @returns {string} e.g: "/sap/fiori/app_subscription_name/path/to/component/img/logo.jpg".
 93      */
 94     sap.ushell.cpv2.services.cloudServices.URLHelper.prototype.createComponentURI = function createComponentURI(sComponentID, sPath) {
 95         var fullUri = "", cmpName, sNewPath;
 96         if (sComponentID) {
 97             if (sPath && sPath.substr(0, 2) === "./") {
 98                 sNewPath = sPath.substr(1);
 99             } else {
100                 sNewPath = sPath;
101             }
102             cmpName = this._getCmpName(sComponentID);
103             fullUri = jQuery.sap.getModulePath(cmpName) + (sNewPath ? sNewPath : '');
104         }
105         return fullUri;
106 
107     };
108 
109     sap.ushell.cpv2.services.cloudServices.URLHelper.hasNoAdapter = true;
110 
111 }());