<commit_before>'use strict';

/**
 * @ngdoc function
 * @name grafterizerApp.controller:ComputetriplesCtrl
 * @description
 * # ComputetriplesCtrl
 * Controller of the grafterizerApp
 */
angular.module('grafterizerApp')
  .controller('ComputetriplesCtrl', function(
    $scope,
    $rootScope,
    $mdDialog,
    ontotextAPI,
    PipeService,
    datagraftPostMessage) {

    $scope.downloadLink = PipeService.computeTuplesHref(
      $scope.distribution, $scope.transformation, $scope.type);

    $scope.ugly = function() {
      // TODO fixme
      window.setTimeout(function() {
        $mdDialog.hide();
      }, 1);
    };
  
    var showError = function() {
      $mdDialog.hide();
      $mdDialog.show(
        $mdDialog.alert({
          title: 'An error occured',
          content: 'The datapage cannot be created.',
          ok: 'Ok'
        })
      );
    };

    $scope.makeNewDataset = function() {
      var type = 'pipe';
      if ($rootScope.transformation.graphs &&
        $rootScope.transformation.graphs.length !== 0) {
        type = 'graft';
      }

      $scope.processing = true;
      $scope.processingStatus = 'Making the dataset';

      var today = new Date();
      today = today.toISOString().substring(0, 10);

      var metadataDataset = {
        '@context': ontotextAPI.getContextDeclaration(),
        'dct:title': $scope.dataset.title,
        'dct:description': $scope.dataset.description,
        'dcat:public': 'false',
        'dct:modified': today,
        'dct:issued': today
      };

      ontotextAPI.newDataset(metadataDataset)
        .success(function(datasetData) {
          var datasetId = datasetData['@id'];

          $scope.processingStatus = 'Transforming the data';
          PipeService.save(datasetId, $scope.distribution,
            $scope.transformation, type).success(
            function(distributionData) {

              var distributionId = distributionData['@id'];

              $scope.processingStatus = 'Fetching information';
              ontotextAPI.distribution(distributionId)
              .success(function(metadataDistribution) {

                $scope.processingStatus = 'Creating the RDF repository';
                ontotextAPI.createRepository(distributionId)
                .success(function(repositoryData) {
                  var accessUrl = repositoryData['access-url'];

                  metadataDistribution['dcat:accessURL'] = accessUrl;

                  $scope.processingStatus = 'Connecting the repository to the distribution';
                  ontotextAPI.updateDistribution(metadataDistribution)
                    .success(function(data) {

                      $scope.processingStatus = 'Filling the repository';

                      var successFilling = function(data) {
                        $mdDialog.hide();

                        var location = '/pages/publish/details.jsp?id=' +
                          window.encodeURIComponent(datasetId);
                        
                        if (datagraftPostMessage.isConnected()) {
                          datagraftPostMessage.setLocation(location);
                        } else {
                          if (location.protocol === 'https:') {
                            location = 'https://datagraft.net' + location;
                          } else {
                            location = 'http://datagraft.net' + location;
                          }

                          window.location = location;
                        }
                      };

                      var nbTryToFillRDFrepo = 0;
                      var tryToFillRDFrepo = function() {
                        ++nbTryToFillRDFrepo;

                        window.setTimeout(function() {
                          PipeService.fillRDFrepo(distributionId, accessUrl)
                            .success(successFilling)
                            .error(nbTryToFillRDFrepo < 6 ? tryToFillRDFrepo : showError);
                        }, 2000);
                      };

                      tryToFillRDFrepo();

                    }).error(showError);
                }).error(showError);
              }).error(showError);
            }).error(showError);
        }).error(showError);
    };

    $scope.cancel = function() {
      $mdDialog.cancel();
    };
  });
<commit_msg>More tentative when filling a RDF repository the first time, it can be quite slow<commit_after>'use strict';

/**
 * @ngdoc function
 * @name grafterizerApp.controller:ComputetriplesCtrl
 * @description
 * # ComputetriplesCtrl
 * Controller of the grafterizerApp
 */
angular.module('grafterizerApp')
  .controller('ComputetriplesCtrl', function(
    $scope,
    $rootScope,
    $mdDialog,
    ontotextAPI,
    PipeService,
    datagraftPostMessage) {

    $scope.downloadLink = PipeService.computeTuplesHref(
      $scope.distribution, $scope.transformation, $scope.type);

    $scope.ugly = function() {
      // TODO fixme
      window.setTimeout(function() {
        $mdDialog.hide();
      }, 1);
    };
  
    var showError = function() {
      $mdDialog.hide();
      $mdDialog.show(
        $mdDialog.alert({
          title: 'An error occured',
          content: 'The datapage cannot be created.',
          ok: 'Ok'
        })
      );
    };

    $scope.makeNewDataset = function() {
      var type = 'pipe';
      if ($rootScope.transformation.graphs &&
        $rootScope.transformation.graphs.length !== 0) {
        type = 'graft';
      }

      $scope.processing = true;
      $scope.processingStatus = 'Making the dataset';

      var today = new Date();
      today = today.toISOString().substring(0, 10);

      var metadataDataset = {
        '@context': ontotextAPI.getContextDeclaration(),
        'dct:title': $scope.dataset.title,
        'dct:description': $scope.dataset.description,
        'dcat:public': 'false',
        'dct:modified': today,
        'dct:issued': today
      };

      ontotextAPI.newDataset(metadataDataset)
        .success(function(datasetData) {
          var datasetId = datasetData['@id'];

          $scope.processingStatus = 'Transforming the data';
          PipeService.save(datasetId, $scope.distribution,
            $scope.transformation, type).success(
            function(distributionData) {

              var distributionId = distributionData['@id'];

              $scope.processingStatus = 'Fetching information';
              ontotextAPI.distribution(distributionId)
              .success(function(metadataDistribution) {

                $scope.processingStatus = 'Creating the RDF repository';
                ontotextAPI.createRepository(distributionId)
                .success(function(repositoryData) {
                  var accessUrl = repositoryData['access-url'];

                  metadataDistribution['dcat:accessURL'] = accessUrl;

                  $scope.processingStatus = 'Connecting the repository to the distribution';
                  ontotextAPI.updateDistribution(metadataDistribution)
                    .success(function(data) {

                      $scope.processingStatus = 'Filling the repository';

                      var successFilling = function(data) {
                        $mdDialog.hide();

                        var location = '/pages/publish/details.jsp?id=' +
                          window.encodeURIComponent(datasetId);
                        
                        if (datagraftPostMessage.isConnected()) {
                          datagraftPostMessage.setLocation(location);
                        } else {
                          if (location.protocol === 'https:') {
                            location = 'https://datagraft.net' + location;
                          } else {
                            location = 'http://datagraft.net' + location;
                          }

                          window.location = location;
                        }
                      };

                      var nbTryToFillRDFrepo = 0;
                      var waitingDelay = 1000;
                      var maxTentatives = 20;

                      var tryToFillRDFrepo = function() {
                        ++nbTryToFillRDFrepo;
                        waitingDelay = Math.min(waitingDelay + waitingDelay, 32000);

                        window.setTimeout(function() {
                          PipeService.fillRDFrepo(distributionId, accessUrl)
                            .success(successFilling)
                            .error(nbTryToFillRDFrepo < maxTentatives ? tryToFillRDFrepo : showError);
                        }, waitingDelay);
                      };

                      tryToFillRDFrepo();

                    }).error(showError);
                }).error(showError);
              }).error(showError);
            }).error(showError);
        }).error(showError);
    };

    $scope.cancel = function() {
      $mdDialog.cancel();
    };
  });

{'fraction_non_alphanumeric': 0.10747402524857558, 'fraction_numerical': 0.003128142107027148, 'mean_word_length': 1.6152497808939528, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4369556', 'n_tokens_mistral': 2342, 'n_tokens_neox': 2190, 'n_words': 396}