<commit_before>scApp.controller('SiteUploadController', ['$scope', '$http', 'Upload', '$timeout', function($scope, $http, Upload, $timeout) {
	
  function UploadData(fileName, docTypeSelected) {
    this.fileName = fileName;
    this.docTypeSelected = docTypeSelected;
  }
  $scope.ccdaUploadData = new UploadData("Unknown File (Upload)", "Unknown Document Type");

  $scope.userMessageConstant = Object.freeze ({
	GENERIC: "Please try a different file and report the issue to <EMAIL>.",
	UPLOAD_ERROR: "Error uploading <unknownFileName>: " 
  });
  
  $scope.uploadErrorData = {
    getValidationResultsAsJsonError: "was uploaded successfully.",
    serviceTypeError: "No error encountered.",
    uploadError: $scope.userMessageConstant.UPLOAD_ERROR + $scope.userMessageConstant.GENERIC,
    validationServiceError: ""
  };

  $scope.jsonValidationData = $scope.metaResults = $scope.ccdaResults = {};
  $scope.mdhtMetaIssues = $scope.vocabMetaIssues = {};
  $scope.jsonScorecardData = {};
    
  $scope.calculatedValidationData = {
      passedCertification: false,
      certificationResult: ""
  }
  
  $scope.uploadDisplay = {
  		isLoading: true,
  		isValidationLoading: true,
  };

  var ServiceTypeEnum = Object.freeze({
    CCDA_VALIDATOR: "C-CDA R2.1 Validator Service",
    SCORECARD: "C-CDA R2.1 Scorecard Service",
    DEBUG: "Debug Service"
  });
  
  $scope.validationOptions = [
	{id: 1, value: "Scorecard and Validation results"}, 
	{id: 2, value: "Scorecard results only"}		  
  ];

    //default to run both services since we no longer have a selection option
    $scope.selectedValidationOption = $scope.validationOptions[0];
  
  var resetValidationData = function() {	  	  
	$scope.jsonScorecardData = {};
	$scope.ngFileUploadError = null;
	$scope.uploadDisplay.isLoading = true;
	$scope.uploadDisplay.isValidationLoading = true;
	$scope.uploadErrorData.validationServiceError = "";
	$scope.uploadErrorData.uploadError = $scope.userMessageConstant.UPLOAD_ERROR + $scope.userMessageConstant.GENERIC;
    $scope.calculatedValidationData.certificationResult = "";
    $scope.calculatedValidationData.passedCertification = false;
  };  

  //called by Validate Document button on SiteUploadForm
  $scope.uploadCcdaScFileAndCallServices = function(ccdaScFile, callDebug) {
    console.log("$scope.uploadDisplay.isValidationLoading (before load):")
    console.log($scope.uploadDisplay.isValidationLoading);
    
    resetValidationData();
    
	//static for now since we are not using the selector/sending this manually
    $scope.ccdaUploadData.docTypeSelected = "C-CDA_IG_Only";
    $scope.ccdaUploadData.fileName = (!$scope.mainDebug.inDebugMode || $scope.mainDebug.inDebugMode && ccdaScFile) 
    	? ccdaScFile.name
		: "No file selected: In debug mode";

     if(callDebug) {
       console.log("In main debug mode");
       if($scope.mainDebug.useLocalTestDataForServices) {
    	   $scope.getLocalJsonResultsForDebugging("dataValidation.json", ServiceTypeEnum.CCDA_VALIDATOR);
    	   $scope.getLocalJsonResultsForDebugging("data.json", ServiceTypeEnum.SCORECARD);
       } else {
    	   callDebugService(ccdaScFile);
       }
     } else if ($scope.selectedValidationOption.id === 1) {
       callCcdaR2ValidatorService(ccdaScFile);    	 
       callCcdaScorecardService(ccdaScFile);
     } else if ($scope.selectedValidationOption.id === 2) {
       $scope.uploadDisplay.isValidationLoading = false;
       callCcdaScorecardService(ccdaScFile);
     } else {
       callCcdaR2ValidatorService(ccdaScFile);    	 
       callCcdaScorecardService(ccdaScFile);    	 
     }
  };

  var callDebugService = function(ccdaScFile) {
    var dataObject = {
      username: 'testName',
      file: ccdaScFile
    };
    uploadFileAndCallServices(ccdaScFile, 'https://angular-file-upload-cors-srv.appspot.com/upload', dataObject);
  };

  var callCcdaR2ValidatorService = function(ccdaScFile) {
    var externalUrl = 'http://192.168.3.11:8080/referenceccdaservice/';
    var localUrl = 'ccdavalidatorservice/';
    var dataObject = {
      ccdaFile: ccdaScFile,
      referenceFileName: 'test',
      validationObjective: 'NonSpecificCCDAR2',
      debug_mode: true
    };
    uploadFileAndCallServices(ccdaScFile, localUrl, dataObject, ServiceTypeEnum.CCDA_VALIDATOR);
  };

  var callCcdaScorecardService = function(ccdaScFile, newLocalUrl) {
    var externalUrl = 'http://192.168.3.11:8080/ccda-smart-scorecard/ccdascorecardservice/';
    var localUrl = 'ccdascorecardservice/';
    if(newLocalUrl) {
    	localUrl = newLocalUrl;
    }
    var dataObject = {
      ccdaFile: ccdaScFile
    };
    uploadFileAndCallServices(ccdaScFile, localUrl, dataObject, ServiceTypeEnum.SCORECARD);
  };
  
  var uploadFileAndCallServices = function(ccdaFile, urlOfServiceToCall, dataObject, serviceType) {
    ccdaFile.upload = Upload.upload({
      url: urlOfServiceToCall,
      data: dataObject
    });

    ccdaFile.upload.then(function(response) {
      $timeout(function() {
    	  console.log("response.data" + response.data);
        if (serviceType !== ServiceTypeEnum.DEBUG) {
          cacheAndProcessReturnedJsonData(response, serviceType);
        }
      });
    }, function(response) {
      if (response.status > 0) {
    	$scope.uploadErrorData.uploadError = $scope.uploadErrorData.uploadError.replace("unknownFileName", $scope.ccdaUploadData.fileName);
        $scope.ngFileUploadError = 'Status: ' + response.status + ' - ' + "Data: " + response.data;
        console.log("Error uploading file or calling service(s):");
        console.log($scope.uploadErrorData.uploadError);
        console.log($scope.ngFileUploadError);
        $scope.$scope.disableAllLoading();
      }
    }, function(evt) {
      ccdaFile.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
    });
  };

  var cacheAndProcessReturnedJsonData = function(response, serviceType) {
    switch (serviceType) {
      case ServiceTypeEnum.CCDA_VALIDATOR:
    	collectAndHandleValidationData(response);
        break;<commit_msg>Fix typo<commit_after>scApp.controller('SiteUploadController', ['$scope', '$http', 'Upload', '$timeout', function($scope, $http, Upload, $timeout) {
	
  function UploadData(fileName, docTypeSelected) {
    this.fileName = fileName;
    this.docTypeSelected = docTypeSelected;
  }
  $scope.ccdaUploadData = new UploadData("Unknown File (Upload)", "Unknown Document Type");

  $scope.userMessageConstant = Object.freeze ({
	GENERIC: "Please try a different file and report the issue to <EMAIL>.",
	UPLOAD_ERROR: "Error uploading <unknownFileName>: " 
  });
  
  $scope.uploadErrorData = {
    getValidationResultsAsJsonError: "was uploaded successfully.",
    serviceTypeError: "No error encountered.",
    uploadError: $scope.userMessageConstant.UPLOAD_ERROR + $scope.userMessageConstant.GENERIC,
    validationServiceError: ""
  };

  $scope.jsonValidationData = $scope.metaResults = $scope.ccdaResults = {};
  $scope.mdhtMetaIssues = $scope.vocabMetaIssues = {};
  $scope.jsonScorecardData = {};
    
  $scope.calculatedValidationData = {
      passedCertification: false,
      certificationResult: ""
  }
  
  $scope.uploadDisplay = {
  		isLoading: true,
  		isValidationLoading: true,
  };

  var ServiceTypeEnum = Object.freeze({
    CCDA_VALIDATOR: "C-CDA R2.1 Validator Service",
    SCORECARD: "C-CDA R2.1 Scorecard Service",
    DEBUG: "Debug Service"
  });
  
  $scope.validationOptions = [
	{id: 1, value: "Scorecard and Validation results"}, 
	{id: 2, value: "Scorecard results only"}		  
  ];

    //default to run both services since we no longer have a selection option
    $scope.selectedValidationOption = $scope.validationOptions[0];
  
  var resetValidationData = function() {	  	  
	$scope.jsonScorecardData = {};
	$scope.ngFileUploadError = null;
	$scope.uploadDisplay.isLoading = true;
	$scope.uploadDisplay.isValidationLoading = true;
	$scope.uploadErrorData.validationServiceError = "";
	$scope.uploadErrorData.uploadError = $scope.userMessageConstant.UPLOAD_ERROR + $scope.userMessageConstant.GENERIC;
    $scope.calculatedValidationData.certificationResult = "";
    $scope.calculatedValidationData.passedCertification = false;
  };  

  //called by Validate Document button on SiteUploadForm
  $scope.uploadCcdaScFileAndCallServices = function(ccdaScFile, callDebug) {
    console.log("$scope.uploadDisplay.isValidationLoading (before load):")
    console.log($scope.uploadDisplay.isValidationLoading);
    
    resetValidationData();
    
	//static for now since we are not using the selector/sending this manually
    $scope.ccdaUploadData.docTypeSelected = "C-CDA_IG_Only";
    $scope.ccdaUploadData.fileName = (!$scope.mainDebug.inDebugMode || $scope.mainDebug.inDebugMode && ccdaScFile) 
    	? ccdaScFile.name
		: "No file selected: In debug mode";

     if(callDebug) {
       console.log("In main debug mode");
       if($scope.mainDebug.useLocalTestDataForServices) {
    	   $scope.getLocalJsonResultsForDebugging("dataValidation.json", ServiceTypeEnum.CCDA_VALIDATOR);
    	   $scope.getLocalJsonResultsForDebugging("data.json", ServiceTypeEnum.SCORECARD);
       } else {
    	   callDebugService(ccdaScFile);
       }
     } else if ($scope.selectedValidationOption.id === 1) {
       callCcdaR2ValidatorService(ccdaScFile);    	 
       callCcdaScorecardService(ccdaScFile);
     } else if ($scope.selectedValidationOption.id === 2) {
       $scope.uploadDisplay.isValidationLoading = false;
       callCcdaScorecardService(ccdaScFile);
     } else {
       callCcdaR2ValidatorService(ccdaScFile);    	 
       callCcdaScorecardService(ccdaScFile);    	 
     }
  };

  var callDebugService = function(ccdaScFile) {
    var dataObject = {
      username: 'testName',
      file: ccdaScFile
    };
    uploadFileAndCallServices(ccdaScFile, 'https://angular-file-upload-cors-srv.appspot.com/upload', dataObject);
  };

  var callCcdaR2ValidatorService = function(ccdaScFile) {
    var externalUrl = 'http://192.168.3.11:8080/referenceccdaservice/';
    var localUrl = 'ccdavalidatorservice/';
    var dataObject = {
      ccdaFile: ccdaScFile,
      referenceFileName: 'test',
      validationObjective: 'NonSpecificCCDAR2',
      debug_mode: true
    };
    uploadFileAndCallServices(ccdaScFile, localUrl, dataObject, ServiceTypeEnum.CCDA_VALIDATOR);
  };

  var callCcdaScorecardService = function(ccdaScFile, newLocalUrl) {
    var externalUrl = 'http://192.168.3.11:8080/ccda-smart-scorecard/ccdascorecardservice/';
    var localUrl = 'ccdascorecardservice/';
    if(newLocalUrl) {
    	localUrl = newLocalUrl;
    }
    var dataObject = {
      ccdaFile: ccdaScFile
    };
    uploadFileAndCallServices(ccdaScFile, localUrl, dataObject, ServiceTypeEnum.SCORECARD);
  };
  
  var uploadFileAndCallServices = function(ccdaFile, urlOfServiceToCall, dataObject, serviceType) {
    ccdaFile.upload = Upload.upload({
      url: urlOfServiceToCall,
      data: dataObject
    });

    ccdaFile.upload.then(function(response) {
      $timeout(function() {
    	  console.log("response.data" + response.data);
        if (serviceType !== ServiceTypeEnum.DEBUG) {
          cacheAndProcessReturnedJsonData(response, serviceType);
        }
      });
    }, function(response) {
      if (response.status > 0) {
    	$scope.uploadErrorData.uploadError = $scope.uploadErrorData.uploadError.replace("unknownFileName", $scope.ccdaUploadData.fileName);
        $scope.ngFileUploadError = 'Status: ' + response.status + ' - ' + "Data: " + response.data;
        console.log("Error uploading file or calling service(s):");
        console.log($scope.uploadErrorData.uploadError);
        console.log($scope.ngFileUploadError);
        $scope.disableAllLoading();
      }
    }, function(evt) {
      ccdaFile.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
    });
  };

  var cacheAndProcessReturnedJsonData = function(response, serviceType) {
    switch (serviceType) {
      case ServiceTypeEnum.CCDA_VALIDATOR:
    	collectAndHandleValidationData(response);
        break;
{'fraction_non_alphanumeric': 0.11691542288557213, 'fraction_numerical': 0.007794361525704809, 'mean_word_length': 3.837946249498596, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '7045224', 'n_tokens_mistral': 3783, 'n_tokens_neox': 3709, 'n_words': 628}