<commit_before>/*!
    Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
    Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
    Created By: <EMAIL>
    Maintained By: <EMAIL>
*/

;(function(GGRC, can) {

  var Proxy = GGRC.MapperHelpers.Proxy,
      Direct = GGRC.MapperHelpers.Direct,
      Indirect = GGRC.MapperHelpers.Indirect,
      Search = GGRC.MapperHelpers.Search,
      Multi = GGRC.MapperHelpers.Multi,
      TypeFilter = GGRC.MapperHelpers.TypeFilter,
      CustomFilter = GGRC.MapperHelpers.CustomFilter,
      Cross = GGRC.MapperHelpers.Cross;


  /*
    class GGRC.Mappings
    represents everything known about how GGRC objects connect to each other.

    a Mappings instance contains the set of known mappings for a module, such as "ggrc_core"
    or "ggrc_gdrive_integration".  The set of all Mappings instances is used throughout the
    system to build widgets, map and unmap objects, etc.

    To configure a new Mappings instance, use the following format :
    { <mixin name or source object type> : { 
        _mixins : [ <mixin name>, ... ],
        _canonical : { <option type> : <name of mapping in parent object>, ... }
        <mapping name> : GGRC.Mappings.Proxy(...) | GGRC.Mappings.Direct(...) | GGRC.Mappings.Indirect(...) 
                        | GGRC.Mappings.Multi(...) | GGRC.Mappings.TypeFilter(...) | GGRC.Mappings.Cross(...)
                        | GGRC.Mappings.CustomFilter(...),
        ...
      } 
    }
  */
  can.Construct("GGRC.Mappings", {
    // Convenience properties for building mappings types.
    Proxy : Proxy,
    Direct : Direct,
    Indirect : Indirect,
    Search : Search,
    Multi : Multi,
    TypeFilter : TypeFilter,
    CustomFilter : CustomFilter,
    Cross : Cross,
    modules : {},

    /*
      return all mappings from all modules for an object type.
      object - a string representing the object type's shortName

      return: a keyed object of all mappings (instances of GGRC.ListLoaders.BaseListLoader) by mapping name
    */
    get_mappings_for : function(object) {
      var mappings = {};
      can.each(this.modules, function(mod, name) {
        if (mod[object]) {
          can.each(mod[object], function(mapping, mapping_name) {
            if(mapping_name === "_canonical")
              return;
            mappings[mapping_name] = mapping;
          });
        }
      });
      return mappings;
    },
    /*
      return the canonical mapping (suitable for joining) between two objects.
      object - the string type (shortName) of the "from" object's class
      option - the string type (shortName) of the "to" object's class

      return: an instance of GGRC.ListLoaders.BaseListLoader (mappings are implemented as ListLoaders)
    */
    get_canonical_mapping : function(object, option) {
      var mapping = null;
      can.each(this.modules, function(mod, name) {
        if (mod._canonical_mappings && mod._canonical_mappings[object] && mod._canonical_mappings[object][option]) {
          mapping = CMS.Models[object].get_mapper(mod._canonical_mappings[object][option]);
          return false;
        }
      });
      return mapping;
    },
    /*
      return all canonical mappings (suitable for joining) from all modules for an object type.
      object - a string representing the object type's shortName

      return: a keyed object of all mappings (instances of GGRC.ListLoaders.BaseListLoader) by option type
    */
    get_canonical_mappings_for : function(object) {
      var mappings = {};
      can.each(this.modules, function(mod, name) {
        if (mod._canonical_mappings && mod._canonical_mappings[object]) {
          can.each(mod._canonical_mappings[object], function(mapping_name, option) {
            mappings[option] = CMS.Models[object].get_mapper(mapping_name);
          });
        }
      });
      return mappings;
    },
    /*
      return the join model for the canonical mapping between two objects if and only if the canonical mapping is a Proxy.
      model_name_a - the string type (shortName) of the "from" object's class
      model_name_b - the string type (shortName) of the "to" object's class

      return: an string of the shortName of the join model (subclass of can.Model.Join) or null
    */
    join_model_name_for: function (model_name_a, model_name_b) {
      var join_descriptor = this.get_canonical_mapping(model_name_a, model_name_b);
      if (join_descriptor instanceof GGRC.ListLoaders.ProxyListLoader) {
        return join_descriptor.model_name;
      } else {
       return null;
      }
    },
    /*
      make a new instance of the join model for the canonical mapping between two objects
       if and only if the canonical mapping is a Proxy.
      object - the string type (shortName) of the "from" object's class
      option - the string type (shortName) of the "to" object's class
      join_attrs - any other attributes to add to the new instance

      return: an instance of the join model (subclass of can.Model.Join) or null
    */
    make_join_object: function(object, option, join_attrs) {
      var join_model
        , join_mapping = this.get_canonical_mapping(object.constructor.shortName, option.constructor.shortName)
        , object_attrs = { id: object.id, type: object.constructor.shortName }
        , option_attrs = { id: option.id, type: option.constructor.shortName }
        ;

      if(join_mapping) {
        join_model = CMS.Models[join_mapping.model_name];
        join_attrs = $.extend({}, join_attrs || {});
        join_attrs[join_model.option_attr] = option_attrs;
        join_attrs[join_model.object_attr] = object_attrs;

        return new join_model(join_attrs);
      } else {
        return null;
      }
    }
  }, {
    /*
      On init:
      kick off the application of mixins to the mappings and resolve canonical mappings
    */
    init : function(name, opts) {
      var created_mappings, that = this;
      this.constructor.modules[name] = this;
      this._canonical_mappings = {};
      if(this.groups) {
        can.each(this.groups, function(group, name) {
          if(typeof group === "function") {
            that.groups[name] = $.proxy(group, that.groups);
          }
        });
      }
      created_mappings = this.create_mappings(opts);

      can.each(created_mappings, function(mappings, object_type) {
        if(mappings._canonical) {<commit_msg>Correct the lookup of keys for mapping POST.<commit_after>/*!
    Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
    Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
    Created By: <EMAIL>
    Maintained By: <EMAIL>
*/

;(function(GGRC, can) {

  var Proxy = GGRC.MapperHelpers.Proxy,
      Direct = GGRC.MapperHelpers.Direct,
      Indirect = GGRC.MapperHelpers.Indirect,
      Search = GGRC.MapperHelpers.Search,
      Multi = GGRC.MapperHelpers.Multi,
      TypeFilter = GGRC.MapperHelpers.TypeFilter,
      CustomFilter = GGRC.MapperHelpers.CustomFilter,
      Cross = GGRC.MapperHelpers.Cross;


  /*
    class GGRC.Mappings
    represents everything known about how GGRC objects connect to each other.

    a Mappings instance contains the set of known mappings for a module, such as "ggrc_core"
    or "ggrc_gdrive_integration".  The set of all Mappings instances is used throughout the
    system to build widgets, map and unmap objects, etc.

    To configure a new Mappings instance, use the following format :
    { <mixin name or source object type> : { 
        _mixins : [ <mixin name>, ... ],
        _canonical : { <option type> : <name of mapping in parent object>, ... }
        <mapping name> : GGRC.Mappings.Proxy(...) | GGRC.Mappings.Direct(...) | GGRC.Mappings.Indirect(...) 
                        | GGRC.Mappings.Multi(...) | GGRC.Mappings.TypeFilter(...) | GGRC.Mappings.Cross(...)
                        | GGRC.Mappings.CustomFilter(...),
        ...
      } 
    }
  */
  can.Construct("GGRC.Mappings", {
    // Convenience properties for building mappings types.
    Proxy : Proxy,
    Direct : Direct,
    Indirect : Indirect,
    Search : Search,
    Multi : Multi,
    TypeFilter : TypeFilter,
    CustomFilter : CustomFilter,
    Cross : Cross,
    modules : {},

    /*
      return all mappings from all modules for an object type.
      object - a string representing the object type's shortName

      return: a keyed object of all mappings (instances of GGRC.ListLoaders.BaseListLoader) by mapping name
    */
    get_mappings_for : function(object) {
      var mappings = {};
      can.each(this.modules, function(mod, name) {
        if (mod[object]) {
          can.each(mod[object], function(mapping, mapping_name) {
            if(mapping_name === "_canonical")
              return;
            mappings[mapping_name] = mapping;
          });
        }
      });
      return mappings;
    },
    /*
      return the canonical mapping (suitable for joining) between two objects.
      object - the string type (shortName) of the "from" object's class
      option - the string type (shortName) of the "to" object's class

      return: an instance of GGRC.ListLoaders.BaseListLoader (mappings are implemented as ListLoaders)
    */
    get_canonical_mapping : function(object, option) {
      var mapping = null;
      can.each(this.modules, function(mod, name) {
        if (mod._canonical_mappings && mod._canonical_mappings[object] && mod._canonical_mappings[object][option]) {
          mapping = CMS.Models[object].get_mapper(mod._canonical_mappings[object][option]);
          return false;
        }
      });
      return mapping;
    },
    /*
      return all canonical mappings (suitable for joining) from all modules for an object type.
      object - a string representing the object type's shortName

      return: a keyed object of all mappings (instances of GGRC.ListLoaders.BaseListLoader) by option type
    */
    get_canonical_mappings_for : function(object) {
      var mappings = {};
      can.each(this.modules, function(mod, name) {
        if (mod._canonical_mappings && mod._canonical_mappings[object]) {
          can.each(mod._canonical_mappings[object], function(mapping_name, option) {
            mappings[option] = CMS.Models[object].get_mapper(mapping_name);
          });
        }
      });
      return mappings;
    },
    /*
      return the join model for the canonical mapping between two objects if and only if the canonical mapping is a Proxy.
      model_name_a - the string type (shortName) of the "from" object's class
      model_name_b - the string type (shortName) of the "to" object's class

      return: an string of the shortName of the join model (subclass of can.Model.Join) or null
    */
    join_model_name_for: function (model_name_a, model_name_b) {
      var join_descriptor = this.get_canonical_mapping(model_name_a, model_name_b);
      if (join_descriptor instanceof GGRC.ListLoaders.ProxyListLoader) {
        return join_descriptor.model_name;
      } else {
       return null;
      }
    },
    /*
      make a new instance of the join model for the canonical mapping between two objects
       if and only if the canonical mapping is a Proxy.
      object - the string type (shortName) of the "from" object's class
      option - the string type (shortName) of the "to" object's class
      join_attrs - any other attributes to add to the new instance

      return: an instance of the join model (subclass of can.Model.Join) or null
    */
    make_join_object: function(object, option, join_attrs) {
      var join_model
        , join_mapping = this.get_canonical_mapping(object.constructor.shortName, option.constructor.shortName)
        , object_attrs = { id: object.id, type: object.constructor.shortName }
        , option_attrs = { id: option.id, type: option.constructor.shortName }
        ;

      if(join_mapping) {
        join_model = CMS.Models[join_mapping.model_name];
        join_attrs = $.extend({}, join_attrs || {});
        join_attrs[join_mapping.option_attr] = option_attrs;
        join_attrs[join_mapping.object_attr] = object_attrs;

        return new join_model(join_attrs);
      } else {
        return null;
      }
    }
  }, {
    /*
      On init:
      kick off the application of mixins to the mappings and resolve canonical mappings
    */
    init : function(name, opts) {
      var created_mappings, that = this;
      this.constructor.modules[name] = this;
      this._canonical_mappings = {};
      if(this.groups) {
        can.each(this.groups, function(group, name) {
          if(typeof group === "function") {
            that.groups[name] = $.proxy(group, that.groups);
          }
        });
      }
      created_mappings = this.create_mappings(opts);

      can.each(created_mappings, function(mappings, object_type) {
        if(mappings._canonical) {
{'fraction_non_alphanumeric': 0.0994351156852124, 'fraction_numerical': 0.0009285769558152132, 'mean_word_length': 2.594993045897079, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5135750', 'n_tokens_mistral': 3794, 'n_tokens_neox': 3689, 'n_words': 1067}