/**
 * Copyright 2016 PT Inovação e Sistemas SA
 * Copyright 2016 INESC-ID
 * Copyright 2016 QUOBIS NETWORKS SL
 * Copyright 2016 FRAUNHOFER-GESELLSCHAFT ZUR FOERDERUNG DER ANGEWANDTEN FORSCHUNG E.V
 * Copyright 2016 ORANGE SA
 * Copyright 2016 Deutsche Telekom AG
 * Copyright 2016 Apizee
 * Copyright 2016 TECHNISCHE UNIVERSITAT BERLIN
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
var dataObject = require('./dataObject');
var hyperty = require('./hyperty');
var Request = require('./request');

var RegistryConnector = function(config) {

  var cfg = JSON.parse(config);
  print("Config: " + cfg);
  print("Retries: " + cfg.retries);
  this._request = new Request(cfg.retries);
  this._registryURL = config.url;
};

RegistryConnector.prototype.processMessage = function(msg, callback) {
  switch(msg.type.toLowerCase()) {
    case "read":
      this.readOperation(msg, callback);
    break;

    case "create":
      this.createOperation(msg, callback);
    break;

    case "update":
      this.updateOperation(msg, callback);
    break;

    case "delete":
      this.deleteOperation(msg, callback);
    break;
  }
};

RegistryConnector.prototype.checkResourceType = function(url) {

  var prefix = url.split('://')[0];

  if(prefix === 'user'){
    return 'hyperty';
  }else {
    return 'dataObject';
  }
};

RegistryConnector.prototype.checkUrlType = function(url) {

  var prefix = url.split('://')[0];

  if(prefix === 'hyperty' || prefix === 'user'){
    return 'hyperty';
  }else {
    return 'dataObject';
  }
};

RegistryConnector.prototype.readOperation = function(msg, callback) {
  if('criteria' in msg.body) {
    if(this.checkResourceType(msg.body.resource) === 'hyperty') {
      hyperty.read(msg.body, this._request, this._registryURL, true, callback);
    }else {
      dataObject.read(msg.body, this._request, this._registryURL, true, callback);
    }
  }else {
    if(this.checkResourceType(msg.body.resource) === 'hyperty') {
      hyperty.read(msg.body, this._request, this._registryURL, false, callback);
    }else {
      dataObject.read(msg.body, this._request, this._registryURL, false, callback);
    }
  }
};

RegistryConnector.prototype.createOperation = function(msg, callback) {
  if(this.checkUrlType(msg.body.value.url) === 'hyperty') {
    hyperty.create(msg.body, this._request, this._registryURL, callback);
  }else {
    dataObject.create(msg.body, this._request, this._registryURL, callback);
  }
};

RegistryConnector.prototype.updateOperation = function(msg, callback) {
  if(this.checkUrlType(msg.body.value.url) === 'hyperty') {
    hyperty.update(msg.body, this._request, this._registryURL, callback);
  }else {
    dataObject.update(msg.body, this._request, this._registryURL, callback);
  }
};

RegistryConnector.prototype.deleteOperation = function(msg, callback) {
  if(this.checkUrlType(msg.body.value.url) === 'hyperty') {
    hyperty.del(msg.body, this._request, this._registryURL, callback);
  }else {
    dataObject.del(msg.body, this._request, this._registryURL, callback);
  }
};

module.exports = RegistryConnector;

@@ -1044,112 +1044,21 @@
 ion(
-config) %7B%0A%0A  var cfg = JSON.parse(config);%0A  print(%22Config: %22 + cfg);%0A  print(%22Retries: %22 + cfg.
+url, 
 retries)
 ;%0A  
@@ -1045,33 +1045,35 @@
 on(url, retries)
-;
+ %7B%0A
 %0A  this._request
@@ -1091,12 +1091,8 @@
 est(
-cfg.
 retr
@@ -1123,15 +1123,8 @@
 L = 
-config.
 url;

Update connector config load.