/**
 * various utility functions
 */
angular.module(PKG.name+'.services')
  .factory('myHelpers', function(myCdapUrl){

   /**
    * set a property deep in an object
    * adapted from Y.namespace
    * http://yuilibrary.com/yui/docs/api/files/yui_js_yui.js.html#l1370
    * @param  {Object} obj object on which to set a value
    * @param  {String} key potentially nested jsonpath, eg "foo.bar.baz"
    * @param  {Mixed} val value to set at the key
    * @return {Object}     modified obj
    */
  function deepSet(obj, key, val) {
    var it = obj, j, d, m;

    if (key.indexOf('.') > -1) {
      d = key.split('.');
      m = d.length-1;
      for (j = 0; j <= m; j++) {
        if(j!==m) {
          it[d[j]] = it[d[j]] || {};
          it = it[d[j]];
        }
        else { // last part
          it[d[m]] = val;
        }
      }
    } else {
      obj[key] = val;
    }

    return obj;
  }

  /* ----------------------------------------------------------------------- */

  /**
   * get to a property deep in an obj by jsonpath
   * @param  {Object} obj object to inspect
   * @param  {String} key jsonpath eg "foo.bar.baz"
   * @return {Mixed}     value at the
   */
  function deepGet(obj, key) {
    return objectQuery.apply(null, [obj].concat(key.split('.')));
  }

  /* ----------------------------------------------------------------------- */

  /*
    Purpose: Query a json object or an array of json objects
    Return: Returns undefined if property is not defined(never set) and
            and a valid value (including null) if defined.
    Usage:
      var obj1 = [
        {
          p1: 'something',
          p2: {
            p21: 'angular',
            p22: 21,
            p23: {
              p231: 'ember',
              p232: null
            }
          },
          p3: 1296,
          p4: [1, 2, 3],
          p5: null
        },
        {
          p101: 'somethingelse'
        }
      ]
      1. query(obj1, 0, 'p1') => 'something'
      2. query(obj1, 0, 'p2', 'p22') => 21
      3. query(obj1, 0, 'p2', 'p32') => { p231: 'ember'}
      4. query(obj1, 0, 'notaproperty') => undefined
      5. query(obj1, 0, 'p2', 'p32', 'somethingelse') => undefined
      6. query(obj1, 1, 'p2', 'p32') => undefined
      7. query(obj1, 0, 'p2', 'p23', 'p232') => null
      8. query(obj1, 0, 'p5') => null
   */

  function objectQuery(obj) {
    if (!angular.isObject(obj)) {
      return null;
    }
    for (var i = 1; i < arguments.length; i++) {
      obj = obj[arguments[i]];
      if (!angular.isObject(obj)) {
        return obj;
      }
    }
    return obj;
  }

  /* ----------------------------------------------------------------------- */

  /*
    Purpose: construct a resource config object for endpoints API services
  */

  function getConfigNs (method, type, path, isArray) {
    var config = {
      url: myCdapUrl.constructUrl({ _cdapNsPath: path }),
      method: method,
      options: { type: type}
    };
    if (isArray) {
      config.isArray = true;
    }
    return config;
  }

  function getConfig (method, type, path, isArray) {
    var config = {
      url: myCdapUrl.constructUrl({ _cdapPath: path }),
      method: method,
      options: { type: type}
    };
    if (isArray) {
      config.isArray = true;
    }
    return config;
  }

  /* ----------------------------------------------------------------------- */

  return {
    deepSet: deepSet,
    deepGet: deepGet,
    objectQuery: objectQuery,
    getConfig: getConfig,
    getConfigNs: getConfigNs
  };
});

@@ -2679,176 +2679,190 @@
 %0A%0A  
-/*%0A    Purpose: construct a resource config object for endpoints API services%0A  */%0A%0A  function getConfigNs (method, type, path, isArray) %7B%0A    var config = %7B%0A      url:
+function __generateConfig(isNsPath, method, type, path, isArray) %7B%0A    var config = %7B%0A      method: method,%0A      options: %7B type: type%7D%0A    %7D;%0A    if (isNsPath) %7B%0A      config.url =
  myC
@@ -2907,67 +2907,92 @@
 h %7D)
-,
+;
 %0A    
-  method: method,%0A      options: %7B type: type%7D
+%7D else %7B%0A      config.url = myCdapUrl.constructUrl(%7B _cdapPath: path %7D);
 %0A    %7D
-;
 %0A   
@@ -3066,16 +3066,102 @@
 g;%0A  %7D%0A%0A
+  /*%0A    Purpose: construct a resource config object for endpoints API services%0A  */%0A%0A
   functi
@@ -3172,16 +3172,18 @@
 etConfig
+Ns
  (method
@@ -3215,208 +3215,189 @@
     
-var config = %7B%0A      url: myCdapUrl.constructUrl(%7B _cdapPath: path %7D),%0A      method: method,%0A      options: %7B type: type%7D%0A    %7D;%0A    if (isArray) %7B%0A      config.isArray = true;%0A    %7D%0A    return config
+return __generateConfig(true, method, type, path, isArray);%0A  %7D%0A%0A  function getConfig (method, type, path, isArray) %7B%0A    return __generateConfig(false, method, type, path, isArray)
 ;%0A  

refactor getConfig helper function