// Generated by CoffeeScript 1.12.7
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  hasProp = {}.hasOwnProperty;

Easydb4MigrationPlugin.UniRostock = (function(superClass) {
  extend(UniRostock, superClass);

  function UniRostock() {
    return UniRostock.__super__.constructor.apply(this, arguments);
  }

  UniRostock.prototype.name = function() {
    return "uni-rostock";
  };

  UniRostock.prototype.payload = function(filename, json, update, store_file) {
    var map;
    map = {
      "AttributeTyp:1": "attribut_kontrast",
      "AttributeTyp:2": "attribut_groesse",
      "AttributeTyp:3": "attribut_zustand",
      "AttributeTyp:4": "attribut_format",
      "AttributeTyp:6": "attribut_gefaessform",
      "AttributeTyp:7": "attribut_gattung",
      "AttributeTyp:8": "attribut_epoche",
      "AttributeTyp:9": "attribut_developmental_stage",
      "AttributeTyp:10": "attribut_sex",
      "AttributeTyp:11": "attribut_circulatory_system",
      "AttributeTyp:12": "attribut_fixation_storage",
      "AttributeTyp:142": "attribut_embedding_medium",
      "AttributeTyp:143": "attribut_thickness_of_sections",
      "AttributeTyp:144": "attribut_staining",
      "AttributeTyp:145": "attribut_sectioning_plane",
      "AttributeTyp:146": "attribut_resin_type",
      "AttributeTyp:147": "attribut_add_ons",
      "AttributeTyp:148": "attribut_post_injection_treatment_1",
      "AttributeTyp:149": "attribut_post_injection_treatment_2",
      "AttributeTyp:150": "attribut_sectioning",
      "AttributeTyp:151": "attribut_mounting_medium",
      "AttributeTyp:152": "attribut_dna_isolation_method",
      "AttributeTyp:153": "attribut_pcr_product_purification",
      "AttributeTyp:154": "attribut_sequencing",
      "AttributeTyp:155": "attribut_light_microscope",
      "AttributeTyp:156": "attribut_objective_magnification",
      "AttributeTyp:157": "attribut_correcture_lens",
      "AttributeTyp:158": "attribut_lm_mode",
      "AttributeTyp:159": "attribut_micro_ct",
      "AttributeTyp:160": "attribut_volume_reconstruction",
      "AttributeTyp:161": "attribut_drying_technique",
      "AttributeTyp:162": "attribut_sputter_coating",
      "AttributeTyp:163": "attribut_electronmicroscope",
      "AttributeTyp:164": "attribut_data_type",
      "AttributeTyp:165": "attribut_video_camera"
    };
    if (json.objecttype === "attribute") {
      this.__renameDuplicates(json.objects);
      return this.splitByType(filename, json, update, store_file, "attribute", "typ", null, (function(_this) {
        return function(object) {
          var key;
          key = _this.__getAttributeType(object);
          return map[key];
        };
      })(this));
    }
  };

  UniRostock.prototype.__renameDuplicates = function(objects) {
    var attributeMap, attributeType, duplicatedIndex, duplicatedObjects, i, j, len, len1, map, nameMap, nameValue, newName, object;
    map = {};
    for (i = 0, len = objects.length; i < len; i++) {
      object = objects[i];
      attributeType = this.__getAttributeType(object);
      attributeMap = map[attributeType];
      if (!map[attributeType]) {
        map[attributeType] = {};
        attributeMap = map[attributeType];
      }
      nameValue = object.attribute.name;
      nameMap = attributeMap[nameValue];
      if (!nameMap) {
        attributeMap[nameValue] = [];
      } else {
        if (object.attribute) {
          nameMap.push(object.attribute);
        }
      }
    }
    for (attributeType in map) {
      nameMap = map[attributeType];
      for (nameValue in nameMap) {
        duplicatedObjects = nameMap[nameValue];
        duplicatedIndex = 2;
        for (j = 0, len1 = duplicatedObjects.length; j < len1; j++) {
          object = duplicatedObjects[j];
          newName = object.name + (" [" + duplicatedIndex + "]");
          object.name = newName;
          this._migration.logwarn("Renaming duplicated name value [" + nameValue + "] to [" + newName + "] for type: [" + attributeType + "])");
          duplicatedIndex++;
        }
      }
    }
  };

  UniRostock.prototype.__getAttributeType = function(object) {
    var ref, ref1, ref2, ref3;
    return object != null ? (ref = object.attribute) != null ? (ref1 = ref.lk_typ_id) != null ? (ref2 = ref1.attributetyp) != null ? (ref3 = ref2["lookup:_id"]) != null ? ref3.easydb4_reference : void 0 : void 0 : void 0 : void 0 : void 0;
  };

  return UniRostock;

})(Easydb4MigrationPlugin);

ez5.session_ready((function(_this) {
  return function() {
    return Easydb4Migration.plugins.registerPlugin(Easydb4MigrationPlugin.UniRostock);
  };
})(this));
