<commit_before>
angular.module('os.biospecimen.models.form', ['os.common.models'])
  .factory('Form', function(osModel, $http, $q, Util) {
    var Form =
      osModel(
        'forms',
        function(form) {
          form.FormContextModel =  osModel('forms/' + form.$id() + '/contexts');
          form.FormContextModel.prototype.$saveOrUpdate = updateFormContext;
          form.FormContextModel.prototype.$remove = removeFormContext;
        }
      );

    function updateFormContext() {
      var form = this.form;
      var formCtxts = [];
      for (var i = 0; i < this.cpIds.length; ++i) {
        formCtxts.push({formId: form.$id(), collectionProtocol: {id: this.cpIds[i]}, level: this.entity, multiRecord: this.isMultiRecord});
      }

      return $http.put(form.FormContextModel.url(), formCtxts).then(form.FormContextModel.modelRespTransform);
    };

    function removeFormContext() {
      var form = this.form;
      var param = {params: {cpId: this.cpId, entityType: this.entityType}}

      return $http.delete(form.FormContextModel.url(), param).then(form.FormContextModel.noTransform);
    }

    Form.prototype.$id = function() {
      return this.formId;
    };

    Form.prototype.getType = function() {
      return 'form';
    }

    Form.prototype.getDisplayName = function() {
      return this.caption;
    }

    Form.prototype.getFormContexts = function() {
      return this.FormContextModel.query();
    };

    Form.prototype.newFormContext = function(formCtxt) {
      return new this.FormContextModel(formCtxt);
    };

    Form.prototype.$remove = function () {
      return $http.delete( Form.url() + this.$id()).then(function(result) { return result.data; });
    };

    Form.getDefinition = function(formId) {
      return $http.get(Form.url() + formId + '/definition', {params: {maxPvs: 100}}).then(
        function(result) {
          return result.data;
        }
      );
    };

    Form.listForms = function(formType) {
      return $http.get(Form.url(), {params: {formType: formType}}).then(
        function(result) {
          return result.data.map(function(form) {
            form.id = form.formId;
            return new Form(form);
          });
        }
      );
    };

    Form.listQueryForms = function() {
      return Form.listForms('query');
    };

    Form.listFor = function(url, objectId, params) {
      return $http.get(url + objectId + '/forms', {params: params}).then(
        function(resp) {
          var opts = angular.extend({objectId: objectId}, params);
          var result = [];
          angular.forEach(resp.data, function(form) {
            form.id = form.formId;
            result.push(new Form(angular.extend(form, opts)));
          });
          return result;
        }
      );
    };

    Form.listRecords = function(url) {
      return $http.get(url).then(
        function(resp) {
          return createRecordsList(resp.data);
        }
      );
    };

    Form.deleteRecord = function(formId, recordId) {
      var url = Form.url() + formId + '/data/' + recordId;
      return $http.delete(url).then(
        function(resp) {
          return resp.data;
        }
      );
    };

    Form.isExtendedField = function(fieldName) {
      return fieldName == 'extensions' || fieldName == 'customFields';
    }

    Form.bulkDelete = function(formIds) {
      return $http.delete(Form.url(), {params: {id: formIds}})
        .then(Form.modelArrayRespTransform);
    }

    Form.getPvs = function(formName, fieldName, search) {
      var params = {formName: formName, controlName: fieldName, searchString: search, useUdn: true};
      return $http.get(Form.url() + 'permissible-values', {params: params}).then(
        function(resp) {
          return resp.data.map(function(pv) { return pv.value; });
        }
      );
    }

    Form.getLatestRecords = function(formId, entityType, objectIds) {
      var params = {entityType: entityType, objectId: objectIds};
      return $http.get(Form.url() + formId + '/latest-records', {params: params}).then(
        function(resp) {
          return resp.data;
        }
      );
    }

    Form.prototype.getRecords = function() {
      var result = [];
      var params = {objectId: this.objectId, entityType: this.entityType};
      $http.get(Form.url() + this.$id() + '/records', {params: params}).then(
        function(resp) {
          Util.unshiftAll(result, resp.data[0].records);
        }
      );

      return result;
    };

    Form.prototype.getRecord = function(recordId, opts) {
      var params = {params: opts};
      return $http.get(Form.url() + this.$id() + '/data/' + recordId, params).then(
        function(resp) {
          return resp.data;
        }
      );
    }

    Form.prototype.getFields = function() {
      var cpId = -1, cpGroupId = undefined;
      if (!!this.cp) {
        cpId = this.cp.id;
      } else if (!!this.cpGroup) {
        cpGroupId = this.cpGroup.id
      }

      var params = {cpId: cpId, cpGroupId: cpGroupId, extendedFields: true};
      var d = $q.defer();
      var that = this;
      if (this.fields) {
        d.resolve(this.fields);
      } else {
        $http.get(Form.url() + this.$id() + '/fields', {params: params}).then(
          function(resp) {
            that.fields = resp.data;
            that.staticFields = flattenStaticFields("", that.fields);
            that.extnForms = getExtnForms("", that.fields);
            that.extnFields = flattenExtnFields(that.extnForms);

            d.resolve(that.fields);
            return resp.data;
          }
        );
      }

      return d.promise;
    }

    Form.prototype.getField = function(fieldName) {
      for (var i = 0; i < this.staticFields.length; ++i) {
        if (fieldName == this.staticFields[i].name) {
          return this.staticFields[i];
        }
      }

      for (var i = 0; i < this.extnFields.length; ++i) {
        if (fieldName == this.extnFields[i].name) {
          return this.extnFields[i];
        }
      }
 
      return undefined;
    }

    function createRecordsList(formsRecords) {
      var recordsList = [];
      angular.forEach(formsRecords, function(formRecs) {
        angular.forEach(formRecs.records, function(record) {
          record.formCaption = formRecs.caption;
          record.formId = formRecs.id;
          recordsList.push(record);
        });
      });

      return recordsList.sort(function(rec1, rec2) {
        return +rec2.updateTime - +rec1.updateTime;
      });
    };

    function flattenFields(fqn, fields) {
      var result = [];
      angular.forEach(fields, function(field) {
        if (field.type == 'SUBFORM') {
          result = result.concat(flattenFields(fqn + field.name + '.', field.subFields));
        } else {
          var f = angular.extend({}, field);
          f.name = fqn + field.name;
          result.push(f);
        }
      });

      return result;
    };

    function filterAndFlattenFields(fqn, fields, filterfn) {
      var result = [];
      angular.forEach(fields, function(field) {
        if (filterfn(field)) {
          result.push(field);
        }
      });

      return flattenFields(fqn, result);
    };
      
    function flattenStaticFields(fqn, fields) {
      return filterAndFlattenFields(
        fqn, 
        fields, 
        function(field) {
          // return field.type != 'SUBFORM' || (!Form.isExtendedField(field.name) && field.flatten);
          return field.type != 'SUBFORM';
        });
    };

    function flattenExtnFields(extnForms) {
      var fields = [];
      angular.forEach(extnForms, function(extnForm) {
        fields = fields.concat(extnForm.fields);
      });

      return fields;
    };

    function getExtnForms(fqn, fields) {
      var extnForms = [];
      for (var i = 0; i < fields.length; ++i) {
        // if (fields[i].type != 'SUBFORM' || (!Form.isExtendedField(fields[i].name) && fields[i].flatten)) {
        //   continue;
        // }

        if (fields[i].type != 'SUBFORM') {
          continue;
        }

        var extnSubForm;
        if (Form.isExtendedField(fields[i].name)) {
          extnSubForm = fields[i];
        } else {
          extnSubForm = {name: '', subFields: [fields[i]]};
        }

        for (var j = 0; j < extnSubForm.subFields.length; ++j) {
          var subForm = extnSubForm.subFields[j];
          var prefix  = !!extnSubForm.name ? (extnSubForm.name + '.') : ''
          var extnFields = flattenFields(fqn + prefix + subForm.name + '.', subForm.subFields);
          for (var k = 0; k < extnFields.length; ++k) {
            extnFields[k].extensionForm = subForm.caption;
          }
          extnForms.push({name: subForm.name, caption: subForm.caption, fields: extnFields});
        }
      }

      return extnForms.sort(function(f1, f2) { return f1.caption < f2.caption ? -1 : (f1.caption > f2.caption ? 1 : 0); });
    };

    return Form;
  });
<commit_msg>OPSMN-4958: The associations type param in the forms list API is case sensitive. Fixed the UI to use the case sensitive query entity name - Query.<commit_after>
angular.module('os.biospecimen.models.form', ['os.common.models'])
  .factory('Form', function(osModel, $http, $q, Util) {
    var Form =
      osModel(
        'forms',
        function(form) {
          form.FormContextModel =  osModel('forms/' + form.$id() + '/contexts');
          form.FormContextModel.prototype.$saveOrUpdate = updateFormContext;
          form.FormContextModel.prototype.$remove = removeFormContext;
        }
      );

    function updateFormContext() {
      var form = this.form;
      var formCtxts = [];
      for (var i = 0; i < this.cpIds.length; ++i) {
        formCtxts.push({formId: form.$id(), collectionProtocol: {id: this.cpIds[i]}, level: this.entity, multiRecord: this.isMultiRecord});
      }

      return $http.put(form.FormContextModel.url(), formCtxts).then(form.FormContextModel.modelRespTransform);
    };

    function removeFormContext() {
      var form = this.form;
      var param = {params: {cpId: this.cpId, entityType: this.entityType}}

      return $http.delete(form.FormContextModel.url(), param).then(form.FormContextModel.noTransform);
    }

    Form.prototype.$id = function() {
      return this.formId;
    };

    Form.prototype.getType = function() {
      return 'form';
    }

    Form.prototype.getDisplayName = function() {
      return this.caption;
    }

    Form.prototype.getFormContexts = function() {
      return this.FormContextModel.query();
    };

    Form.prototype.newFormContext = function(formCtxt) {
      return new this.FormContextModel(formCtxt);
    };

    Form.prototype.$remove = function () {
      return $http.delete( Form.url() + this.$id()).then(function(result) { return result.data; });
    };

    Form.getDefinition = function(formId) {
      return $http.get(Form.url() + formId + '/definition', {params: {maxPvs: 100}}).then(
        function(result) {
          return result.data;
        }
      );
    };

    Form.listForms = function(formType) {
      return $http.get(Form.url(), {params: {formType: formType}}).then(
        function(result) {
          return result.data.map(function(form) {
            form.id = form.formId;
            return new Form(form);
          });
        }
      );
    };

    Form.listQueryForms = function() {
      return Form.listForms('Query');
    };

    Form.listFor = function(url, objectId, params) {
      return $http.get(url + objectId + '/forms', {params: params}).then(
        function(resp) {
          var opts = angular.extend({objectId: objectId}, params);
          var result = [];
          angular.forEach(resp.data, function(form) {
            form.id = form.formId;
            result.push(new Form(angular.extend(form, opts)));
          });
          return result;
        }
      );
    };

    Form.listRecords = function(url) {
      return $http.get(url).then(
        function(resp) {
          return createRecordsList(resp.data);
        }
      );
    };

    Form.deleteRecord = function(formId, recordId) {
      var url = Form.url() + formId + '/data/' + recordId;
      return $http.delete(url).then(
        function(resp) {
          return resp.data;
        }
      );
    };

    Form.isExtendedField = function(fieldName) {
      return fieldName == 'extensions' || fieldName == 'customFields';
    }

    Form.bulkDelete = function(formIds) {
      return $http.delete(Form.url(), {params: {id: formIds}})
        .then(Form.modelArrayRespTransform);
    }

    Form.getPvs = function(formName, fieldName, search) {
      var params = {formName: formName, controlName: fieldName, searchString: search, useUdn: true};
      return $http.get(Form.url() + 'permissible-values', {params: params}).then(
        function(resp) {
          return resp.data.map(function(pv) { return pv.value; });
        }
      );
    }

    Form.getLatestRecords = function(formId, entityType, objectIds) {
      var params = {entityType: entityType, objectId: objectIds};
      return $http.get(Form.url() + formId + '/latest-records', {params: params}).then(
        function(resp) {
          return resp.data;
        }
      );
    }

    Form.prototype.getRecords = function() {
      var result = [];
      var params = {objectId: this.objectId, entityType: this.entityType};
      $http.get(Form.url() + this.$id() + '/records', {params: params}).then(
        function(resp) {
          Util.unshiftAll(result, resp.data[0].records);
        }
      );

      return result;
    };

    Form.prototype.getRecord = function(recordId, opts) {
      var params = {params: opts};
      return $http.get(Form.url() + this.$id() + '/data/' + recordId, params).then(
        function(resp) {
          return resp.data;
        }
      );
    }

    Form.prototype.getFields = function() {
      var cpId = -1, cpGroupId = undefined;
      if (!!this.cp) {
        cpId = this.cp.id;
      } else if (!!this.cpGroup) {
        cpGroupId = this.cpGroup.id
      }

      var params = {cpId: cpId, cpGroupId: cpGroupId, extendedFields: true};
      var d = $q.defer();
      var that = this;
      if (this.fields) {
        d.resolve(this.fields);
      } else {
        $http.get(Form.url() + this.$id() + '/fields', {params: params}).then(
          function(resp) {
            that.fields = resp.data;
            that.staticFields = flattenStaticFields("", that.fields);
            that.extnForms = getExtnForms("", that.fields);
            that.extnFields = flattenExtnFields(that.extnForms);

            d.resolve(that.fields);
            return resp.data;
          }
        );
      }

      return d.promise;
    }

    Form.prototype.getField = function(fieldName) {
      for (var i = 0; i < this.staticFields.length; ++i) {
        if (fieldName == this.staticFields[i].name) {
          return this.staticFields[i];
        }
      }

      for (var i = 0; i < this.extnFields.length; ++i) {
        if (fieldName == this.extnFields[i].name) {
          return this.extnFields[i];
        }
      }
 
      return undefined;
    }

    function createRecordsList(formsRecords) {
      var recordsList = [];
      angular.forEach(formsRecords, function(formRecs) {
        angular.forEach(formRecs.records, function(record) {
          record.formCaption = formRecs.caption;
          record.formId = formRecs.id;
          recordsList.push(record);
        });
      });

      return recordsList.sort(function(rec1, rec2) {
        return +rec2.updateTime - +rec1.updateTime;
      });
    };

    function flattenFields(fqn, fields) {
      var result = [];
      angular.forEach(fields, function(field) {
        if (field.type == 'SUBFORM') {
          result = result.concat(flattenFields(fqn + field.name + '.', field.subFields));
        } else {
          var f = angular.extend({}, field);
          f.name = fqn + field.name;
          result.push(f);
        }
      });

      return result;
    };

    function filterAndFlattenFields(fqn, fields, filterfn) {
      var result = [];
      angular.forEach(fields, function(field) {
        if (filterfn(field)) {
          result.push(field);
        }
      });

      return flattenFields(fqn, result);
    };
      
    function flattenStaticFields(fqn, fields) {
      return filterAndFlattenFields(
        fqn, 
        fields, 
        function(field) {
          // return field.type != 'SUBFORM' || (!Form.isExtendedField(field.name) && field.flatten);
          return field.type != 'SUBFORM';
        });
    };

    function flattenExtnFields(extnForms) {
      var fields = [];
      angular.forEach(extnForms, function(extnForm) {
        fields = fields.concat(extnForm.fields);
      });

      return fields;
    };

    function getExtnForms(fqn, fields) {
      var extnForms = [];
      for (var i = 0; i < fields.length; ++i) {
        // if (fields[i].type != 'SUBFORM' || (!Form.isExtendedField(fields[i].name) && fields[i].flatten)) {
        //   continue;
        // }

        if (fields[i].type != 'SUBFORM') {
          continue;
        }

        var extnSubForm;
        if (Form.isExtendedField(fields[i].name)) {
          extnSubForm = fields[i];
        } else {
          extnSubForm = {name: '', subFields: [fields[i]]};
        }

        for (var j = 0; j < extnSubForm.subFields.length; ++j) {
          var subForm = extnSubForm.subFields[j];
          var prefix  = !!extnSubForm.name ? (extnSubForm.name + '.') : ''
          var extnFields = flattenFields(fqn + prefix + subForm.name + '.', subForm.subFields);
          for (var k = 0; k < extnFields.length; ++k) {
            extnFields[k].extensionForm = subForm.caption;
          }
          extnForms.push({name: subForm.name, caption: subForm.caption, fields: extnFields});
        }
      }

      return extnForms.sort(function(f1, f2) { return f1.caption < f2.caption ? -1 : (f1.caption > f2.caption ? 1 : 0); });
    };

    return Form;
  });

{'fraction_non_alphanumeric': 0.14638075662463196, 'fraction_numerical': 0.002888728403977557, 'mean_word_length': 2.1637961335676628, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5889440', 'n_tokens_mistral': 5626, 'n_tokens_neox': 5476, 'n_words': 1048}