// 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,
  indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

Easydb4Migration.UniBern = (function(superClass) {
  extend(UniBern, superClass);

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

  UniBern.prototype.name = function() {
    return "uni-bern";
  };

  UniBern.prototype.payload = function(filename, json, update) {
    var dfr;
    if (json.objecttype !== "bilder") {
      return;
    }
    console.debug("payload:", json);
    dfr = new CUI.Deferred();
    this._migration.query('SELECT * FROM "source.public.pool"').fail(dfr.reject).done((function(_this) {
      return function(result) {
        var antik_pool_refs, i, j, len, len1, obj, ref, ref1, ref2, ref3, row;
        antik_pool_refs = [];
        ref = result.rows;
        for (i = 0, len = ref.length; i < len; i++) {
          row = ref[i];
          if ((ref1 = row.editmask) != null ? ref1.match(/Antik/) : void 0) {
            antik_pool_refs.push("pool:" + row.id);
          }
        }
        ref2 = json.objects;
        for (j = 0, len1 = ref2.length; j < len1; j++) {
          obj = ref2[j];
          if (ref3 = obj.bilder._pool.pool["lookup:_id"].reference, indexOf.call(antik_pool_refs, ref3) < 0) {
            continue;
          }
          if (obj.bilder.lk_aufbewahrungsort_lehre_id) {
            obj.bilder["lk_aufbewahrungsort_lehre_id_antik:source_name"] = obj.bilder["lk_aufbewahrungsort_lehre_id:source_name"];
            obj.bilder.lk_aufbewahrungsort_lehre_id_antik = obj.bilder.lk_aufbewahrungsort_lehre_id;
            delete obj.bilder.lk_aufbewahrungsort_lehre_id;
            delete obj.bilder["lk_aufbewahrungsort_lehre_id:source_name"];
          }
          if (obj.bilder.aufbewahrungsort_remark) {
            obj.bilder.aufbewahrungsort_remark_antik = obj.bilder.aufbewahrungsort_remark;
            delete obj.bilder.aufbewahrungsort_remark;
          }
        }
        return dfr.resolve();
      };
    })(this));
    return dfr.promise();
  };

  return UniBern;

})(Easydb4MigrationPlugin);

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