<commit_before>var specimenEvent = angular.module('specimen-event-app', ['plus.formsServices', 'plus.directives', 'ui.bootstrap']);

specimenEvent.controller('SpecimenEventController', 
  function($scope, SpecimensEventService, SpecimensService, FormsService){

    $scope.selectedEvent = undefined;
    $scope.specimenLabels = globalSpecimenLabels || [];
    $scope.specimensSummary = {};

    $scope.dataTable = undefined;

    $scope.specimenEvents = [];
    FormsService.getAllSpecimenEventForms().then( 
      function(events) {
        $scope.specimenEvents = events;
      }
    );

    var renderDataTable = function(formId, formDef) {
      var dataTable = new edu.common.de.DataTable({
        formId           : formId,
        idColumnLabel    : 'Specimen Label',
        appColumns       : [{id: 'cpName', label: 'Collection Protocol'}, 
                            {id: 'specimenType', label:'Specimen Type'}],
        formDef          : formDef,
        formDiv          : 'data-table',
        onValidationError: function() {
          Utility.notify($("#notifications"), "There are some errors in below data table. Please rectify them before saving", "error", true);
        },

        onRowSelect: function(isRowSelected) {
          $scope.deleteRows = isRowSelected;
          if (!$scope.$$phase) {
            $scope.$apply();
          }
        }
      });

      dataTable.clear();
      return dataTable;
    };

    $scope.onEventSelect = function(selectedEvent) {
      $scope.dataEntryMode = $scope.editRecords  = $scope.deleteRows = false;
      $scope.dataTable = undefined;
      FormsService.getFormDef(selectedEvent.formId).then(function (formDef) {
        $scope.dataTable = renderDataTable(selectedEvent.formId, formDef);
      });
    };

    $scope.addRecord = function() {
      $("#notifications").hide();

      $scope.loading = true;
      var re = /\s*[\s,]\s*/;
      var specimenLabels = $scope.specimenLabels.trim().split(re);
      var tableData =[];

      SpecimensService.getSpecimens(specimenLabels).then( function(data) {
        $scope.loading = false;
        $scope.specimensSummary = data.specimens;
        if (!validate(specimenLabels, data.specimens)) {
          return;
        }

        $scope.specimenMap = {};
        for (var i = 0; i < data.specimens.length; ++i) {
          var specimen = data.specimens[i];
          $scope.specimenMap[specimen.label] = specimen;
        }

        for (var i = 0; i < specimenLabels.length; i++) {
          var specimen = $scope.specimenMap[specimenLabels[i]];
          var tableRec = {
            key            : {id : specimen.label, label : specimen.label},
            appColumnsData : {cpName: specimen.cpShortTitle, specimenType: specimen.specimenType },
            records        : []
          };

          tableData.push(tableRec);
        }

        $scope.dataEntryMode = true;
        $scope.editRecords = false; // TODO: why so many ctrl variables?
        $scope.dataTable.setMode('add');
        $scope.dataTable.setData(tableData);
      });
    };

    $scope.deleteSelectedRows = function() {
      $scope.dataTable.deleteRows();
      $scope.deleteRows = false;
    };

    var validate = function(specimenLabels, data) {
      if (specimenLabels.length == data.length) {
        return true;
      }
<commit_msg>specimen labels splited by \n,\t,\v<commit_after>var specimenEvent = angular.module('specimen-event-app', ['plus.formsServices', 'plus.directives', 'ui.bootstrap']);

specimenEvent.controller('SpecimenEventController', 
  function($scope, SpecimensEventService, SpecimensService, FormsService){

    $scope.selectedEvent = undefined;
    $scope.specimenLabels = globalSpecimenLabels || [];
    $scope.specimensSummary = {};

    $scope.dataTable = undefined;

    $scope.specimenEvents = [];
    FormsService.getAllSpecimenEventForms().then( 
      function(events) {
        $scope.specimenEvents = events;
      }
    );

    var renderDataTable = function(formId, formDef) {
      var dataTable = new edu.common.de.DataTable({
        formId           : formId,
        idColumnLabel    : 'Specimen Label',
        appColumns       : [{id: 'cpName', label: 'Collection Protocol'}, 
                            {id: 'specimenType', label:'Specimen Type'}],
        formDef          : formDef,
        formDiv          : 'data-table',
        onValidationError: function() {
          Utility.notify($("#notifications"), "There are some errors in below data table. Please rectify them before saving", "error", true);
        },

        onRowSelect: function(isRowSelected) {
          $scope.deleteRows = isRowSelected;
          if (!$scope.$$phase) {
            $scope.$apply();
          }
        }
      });

      dataTable.clear();
      return dataTable;
    };

    $scope.onEventSelect = function(selectedEvent) {
      $scope.dataEntryMode = $scope.editRecords  = $scope.deleteRows = false;
      $scope.dataTable = undefined;
      FormsService.getFormDef(selectedEvent.formId).then(function (formDef) {
        $scope.dataTable = renderDataTable(selectedEvent.formId, formDef);
      });
    };

    $scope.addRecord = function() {
      $("#notifications").hide();
      $scope.loading = true;

      var specimenLabels = [];
      var re = /\n*\t*\v*[\n\t\v,]\n*\t*\v*/;
      $.each($scope.specimenLabels.trim().split(re), function(){
        if(this != "") {
           specimenLabels.push($.trim(this));
        }
      });

      SpecimensService.getSpecimens(specimenLabels).then( function(data) {
        $scope.loading = false;
        $scope.specimensSummary = data.specimens;
        if (!validate(specimenLabels, data.specimens)) {
          return;
        }

        $scope.specimenMap = {};
        for (var i = 0; i < data.specimens.length; ++i) {
          var specimen = data.specimens[i];
          $scope.specimenMap[specimen.label] = specimen;
        }

        var tableData =[];
        for (var i = 0; i < specimenLabels.length; i++) {
          var specimen = $scope.specimenMap[specimenLabels[i]];
          var tableRec = {
            key            : {id : specimen.label, label : specimen.label},
            appColumnsData : {cpName: specimen.cpShortTitle, specimenType: specimen.specimenType },
            records        : []
          };

          tableData.push(tableRec);
        }

        $scope.dataEntryMode = true;
        $scope.editRecords = false; // TODO: why so many ctrl variables?
        $scope.dataTable.setMode('add');
        $scope.dataTable.setData(tableData);
      });
    };

    $scope.deleteSelectedRows = function() {
      $scope.dataTable.deleteRows();
      $scope.deleteRows = false;
    };

    var validate = function(specimenLabels, data) {
      if (specimenLabels.length == data.length) {
        return true;
      }

{'fraction_non_alphanumeric': 0.13532846715328467, 'fraction_numerical': 0.0005839416058394161, 'mean_word_length': 2.317675544794189, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '4611290', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1945, 'n_words': 361}