// 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.Gewosued = (function(superClass) {
  extend(Gewosued, superClass);

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

  Gewosued.prototype.name = function() {
    return "gewosued";
  };

  Gewosued.prototype.person_verfuegbar = {
    "verfuegbar_abgebildete_personen": "abgebildete Personen",
    "verfuegbar_autor_herausgeber": "Autor / Herausgeber",
    "verfuegbar_behandelte_personen": "behandelte Personen",
    "verfuegbar_planverfasser": "Planverfasser",
    "verfuegbar_provenienz": "Provenienz",
    "verfuegbar_urheber": "Urheber"
  };

  Gewosued.prototype.person_typ = {
    "typ_person": "Person",
    "typ_koerperschaft": "Körperschaft"
  };

  Gewosued.prototype.dublette = {
    "qualitaet_dublette": "Dublette"
  };

  Gewosued.prototype.payload = function(filename, json, update, store_file) {
    var _tags, add_tag, dfr, i, j, k, len, len1, obj, ref, ref1, ref2, ref3, ref4, ref5, ref6, shortname, v;
    if (json.import_type === "tags") {
      dfr = new CUI.Deferred();
      add_tag = (function(_this) {
        return function(name, displayname) {
          return _tags.push({
            tag: {
              shortname: name,
              displayname: {
                "de-DE": displayname
              },
              is_default: false,
              enabled: true,
              displaytype: "search",
              type: "individual"
            }
          });
        };
      })(this);
      _tags = [];
      json.tags.push({
        taggroup: {
          reference: "easydb4:original_dublette",
          type: "checkbox",
          displayname: {
            "de-DE": "Original/Dublette"
          }
        },
        _tags: _tags
      });
      ref = this.dublette;
      for (k in ref) {
        v = ref[k];
        add_tag(k, v);
      }
      _tags = [];
      json.tags.push({
        taggroup: {
          reference: "easydb4:person_verfuegbar",
          type: "checkbox",
          displayname: {
            "de-DE": "Person-Verfügbarkeit"
          }
        },
        _tags: _tags
      });
      ref1 = this.person_verfuegbar;
      for (k in ref1) {
        v = ref1[k];
        add_tag(k, v);
      }
      _tags = [];
      json.tags.push({
        taggroup: {
          reference: "easydb4:person_typ",
          type: "checkbox",
          displayname: {
            "de-DE": "Person-Typ"
          }
        },
        _tags: _tags
      });
      ref2 = this.person_typ;
      for (k in ref2) {
        v = ref2[k];
        add_tag(k, v);
      }
      _tags = [];
      json.tags.push({
        taggroup: {
          reference: "easydb4:qualitaet",
          type: "checkbox",
          displayname: {
            "de-DE": "Qualität"
          }
        },
        _tags: _tags
      });
      this._migration.query('SELECT id, name FROM "source.public.qualitaet" ORDER BY name').fail(dfr.reject).done((function(_this) {
        return function(result) {
          var i, len, ref3, row;
          ref3 = result.rows;
          for (i = 0, len = ref3.length; i < len; i++) {
            row = ref3[i];
            add_tag("qualitaet:" + row.id, row.name);
          }
          _tags = [];
          json.tags.push({
            taggroup: {
              reference: "easydb4:bearbeitgungsstufe",
              type: "checkbox",
              displayname: {
                "de-DE": "Bearbeitungsstufe"
              }
            },
            _tags: _tags
          });
          _this._migration.query('SELECT id, name FROM "source.public.bearbeitungsstufe" ORDER BY name').fail(dfr.reject).done(function(result) {
            var j, len1, ref4;
            ref4 = result.rows;
            for (j = 0, len1 = ref4.length; j < len1; j++) {
              row = ref4[j];
              add_tag("bearbeitungsstufe:" + row.id, row.name);
            }
            dfr.resolve();
          });
        };
      })(this));
      return dfr.promise();
    }
    if (json.objecttype === "person") {
      ref3 = json.objects;
      for (i = 0, len = ref3.length; i < len; i++) {
        obj = ref3[i];
        if (!obj._tags) {
          obj._tags = [];
        }
        ref4 = this.person_verfuegbar;
        for (k in ref4) {
          v = ref4[k];
          if (obj.person[k]) {
            obj._tags.push({
              "lookup:_id": {
                shortname: k
              }
            });
          }
          delete obj.person[k];
        }
        ref5 = this.person_typ;
        for (k in ref5) {
          v = ref5[k];
          if (obj.person[k]) {
            obj._tags.push({
              "lookup:_id": {
                shortname: k
              }
            });
          }
          delete obj.person[k];
        }
      }
      return;
    }
    if (json.objecttype === "assets") {
      ref6 = json.objects;
      for (j = 0, len1 = ref6.length; j < len1; j++) {
        obj = ref6[j];
        if (!obj._tags) {
          obj._tags = [];
        }
        if (obj.assets.lk_original_dublette_id) {
          obj._tags.push({
            "lookup:_id": {
              shortname: "qualitaet_dublette"
            }
          });
        }
        if (obj.assets.lk_qualitaet_id) {
          shortname = obj.assets.lk_qualitaet_id.qualitaet["lookup:_id"].easydb4_reference;
          obj._tags.push({
            "lookup:_id": {
              shortname: shortname
            }
          });
          delete obj.assets.lk_qualitaet_id;
          delete obj.assets["lk_qualitaet_id:source_name"];
        }
        if (obj.assets.lk_bearbeitungsstufe_id) {
          shortname = obj.assets.lk_bearbeitungsstufe_id.bearbeitungsstufe["lookup:_id"].easydb4_reference;
          obj._tags.push({
            "lookup:_id": {
              shortname: shortname
            }
          });
          delete obj.assets.lk_bearbeitungsstufe_id;
          delete obj.assets["lk_bearbeitungsstufe_id:source_name"];
        }
      }
    }
    if (json.objecttype === "verweistyp") {
      return this.splitByType(filename, json, update, store_file, "verweistyp", "for_pool", {
        "verweistyp_plaene": ["verweistyp_plaene", "verweistyp_medien", "verweistyp_dokumente"],
        "verweistyp_medien": ["verweistyp_plaene", "verweistyp_medien", "verweistyp_dokumente"],
        "verweistyp_dokumente": ["verweistyp_plaene", "verweistyp_medien", "verweistyp_dokumente"],
        "verweistyp_undefined": ["verweistyp_plaene", "verweistyp_medien", "verweistyp_dokumente"]
      });
    }
  };

  return Gewosued;

})(Easydb4MigrationPlugin);

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