<commit_before>/*
 * Copyright © 2013 VillageReach.  All Rights Reserved.  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 *
 * If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */


function DistributionController(DeliveryZoneFacilities, deliveryZones, DeliveryZoneActivePrograms, messageService, DeliveryZoneProgramPeriods, IndexedDB, navigateBackService, $http, $dialog, $scope) {

  $scope.deliveryZones = deliveryZones;
  var DELIVERY_ZONE_LABEL = messageService.get('label.select.deliveryZone');
  var NONE_ASSIGNED_LABEL = messageService.get('label.noneAssigned');
  var DEFAULT_PROGRAM_MESSAGE = messageService.get('label.select.program');
  var DEFAULT_PERIOD_MESSAGE = messageService.get('label.select.period');

  $scope.zonePlaceholder = !!$scope.deliveryZones.length ? DELIVERY_ZONE_LABEL : NONE_ASSIGNED_LABEL;

  $scope.loadPrograms = function () {
    $scope.programs = $scope.periods = [];
    DeliveryZoneActivePrograms.get({zoneId: $scope.selectedZone.id}, function (data) {
      $scope.programs = data.deliveryZonePrograms;
      if ($scope.selectedProgram && $scope.fromBackNavigation) {
        $scope.selectedProgram = _.where($scope.programs, {id: $scope.selectedProgram.id})[0];
        $scope.loadPeriods();
      }
    }, function (data) {
      $scope.error = data.data.error;
    });
  };

  $scope.loadPeriods = function () {
    $scope.periods = [];
    DeliveryZoneProgramPeriods.get({zoneId: $scope.selectedZone.id, programId: $scope.selectedProgram.id}, function (data) {
      $scope.periods = data.periods.length ? data.periods.slice(0, 13) : [];
      if ($scope.selectedPeriod && $scope.fromBackNavigation) {
        $scope.fromBackNavigation = false;
        $scope.selectedPeriod = _.where($scope.periods, {id: $scope.selectedPeriod.id})[0];
      } else {
        $scope.selectedPeriod = $scope.periods.length ? $scope.periods[0] : NONE_ASSIGNED_LABEL;
      }
    }, function (data) {
      $scope.error = data.data.error;
    });
  };

  $scope.programOptionMessage = function () {
    return optionMessage($scope.programs, DEFAULT_PROGRAM_MESSAGE);
  };

  $scope.periodOptionMessage = function () {
    return optionMessage($scope.periods, DEFAULT_PERIOD_MESSAGE);
  };

  $scope.loadDistributionsFromCache = function () {
    var transaction = IndexedDB.getConnection().transaction('distributions');
    var cursorRequest = transaction.objectStore('distributions').openCursor();

    var aggregate = [];
    cursorRequest.onsuccess = function (event) {
      if (event.target.result) {
        aggregate.push(event.target.result.value);
        event.target.result['continue']();
      }
    };

    transaction.oncomplete = function (e) {
      $scope.distributionList = aggregate;
      $scope.$apply();
    };
  };

  $scope.$on('indexedDBReady', function () {
    $scope.loadDistributionsFromCache();
  });

  $scope.$on('$viewContentLoaded', function () {
    $scope.distributionList = navigateBackService.distributionList;
    $scope.selectedZone = navigateBackService.deliveryZone;
    $scope.selectedProgram = navigateBackService.program;
    $scope.selectedPeriod = navigateBackService.period;

    if ($scope.selectedZone) {
      $scope.fromBackNavigation = true;
    } else {
      $scope.fromBackNavigation = false;

    }
    $scope.$watch('deliveryZones', function () {
      if ($scope.deliveryZones && $scope.selectedZone) {
        $scope.selectedZone = _.where($scope.deliveryZones, {id: $scope.selectedZone.id})[0];
        $scope.loadPrograms();
      }
    });
  });


  $scope.initiateDistribution = function () {

    function isCached() {
      return _.find($scope.distributionList, function (distribution) {
        return distribution.deliveryZone.id == $scope.selectedZone.id &&
          distribution.program.id == $scope.selectedProgram.id &&
          distribution.period.id == $scope.selectedPeriod.id;
      });
    }

    if (isCached()) {
      $scope.message = messageService.get("message.distribution.already.cached",
        $scope.selectedZone.name, $scope.selectedProgram.name, $scope.selectedPeriod.name);
      return;
    }

    DeliveryZoneFacilities.get({"programId": $scope.selectedProgram.id, "deliveryZoneId": $scope.selectedZone.id }, onReferenceDataSuccess, {});


    function onReferenceDataSuccess(data) {

      var distributionReferenceDataTransaction = IndexedDB.getConnection().transaction('distributionReferenceData', 'readwrite');
      var distributionReferenceDataStore = distributionReferenceDataTransaction.objectStore('distributionReferenceData');<commit_msg>|#774|+Shibha|Fixing Smoke test<commit_after>/*
 * Copyright © 2013 VillageReach.  All Rights Reserved.  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 *
 * If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */


function DistributionController(DeliveryZoneFacilities, deliveryZones, DeliveryZoneActivePrograms, messageService, DeliveryZoneProgramPeriods, IndexedDB, navigateBackService, $http, $dialog, $scope, $location) {

  $scope.deliveryZones = deliveryZones;
  var DELIVERY_ZONE_LABEL = messageService.get('label.select.deliveryZone');
  var NONE_ASSIGNED_LABEL = messageService.get('label.noneAssigned');
  var DEFAULT_PROGRAM_MESSAGE = messageService.get('label.select.program');
  var DEFAULT_PERIOD_MESSAGE = messageService.get('label.select.period');

  $scope.zonePlaceholder = !!$scope.deliveryZones.length ? DELIVERY_ZONE_LABEL : NONE_ASSIGNED_LABEL;

  $scope.loadPrograms = function () {
    $scope.programs = $scope.periods = [];
    DeliveryZoneActivePrograms.get({zoneId: $scope.selectedZone.id}, function (data) {
      $scope.programs = data.deliveryZonePrograms;
      if ($scope.selectedProgram && $scope.fromBackNavigation) {
        $scope.selectedProgram = _.where($scope.programs, {id: $scope.selectedProgram.id})[0];
        $scope.loadPeriods();
      }
    }, function (data) {
      $scope.error = data.data.error;
    });
  };

  $scope.loadPeriods = function () {
    $scope.periods = [];
    DeliveryZoneProgramPeriods.get({zoneId: $scope.selectedZone.id, programId: $scope.selectedProgram.id}, function (data) {
      $scope.periods = data.periods.length ? data.periods.slice(0, 13) : [];
      if ($scope.selectedPeriod && $scope.fromBackNavigation) {
        $scope.fromBackNavigation = false;
        $scope.selectedPeriod = _.where($scope.periods, {id: $scope.selectedPeriod.id})[0];
      } else {
        $scope.selectedPeriod = $scope.periods.length ? $scope.periods[0] : NONE_ASSIGNED_LABEL;
      }
    }, function (data) {
      $scope.error = data.data.error;
    });
  };

  $scope.programOptionMessage = function () {
    return optionMessage($scope.programs, DEFAULT_PROGRAM_MESSAGE);
  };

  $scope.periodOptionMessage = function () {
    return optionMessage($scope.periods, DEFAULT_PERIOD_MESSAGE);
  };

  $scope.loadDistributionsFromCache = function () {
    var transaction = IndexedDB.getConnection().transaction('distributions');
    var cursorRequest = transaction.objectStore('distributions').openCursor();

    var aggregate = [];
    cursorRequest.onsuccess = function (event) {
      if (event.target.result) {
        aggregate.push(event.target.result.value);
        event.target.result['continue']();
      }
    };

    transaction.oncomplete = function (e) {
      $scope.distributionList = aggregate;
      $scope.$apply();
    };
  };

  $scope.$on('indexedDBReady', function () {
    $scope.loadDistributionsFromCache();
  });

  $scope.$on('$viewContentLoaded', function () {
    $scope.distributionList = navigateBackService.distributionList;
    $scope.selectedZone = navigateBackService.deliveryZone;
    $scope.selectedProgram = navigateBackService.program;
    $scope.selectedPeriod = navigateBackService.period;

    if ($scope.selectedZone) {
      $scope.fromBackNavigation = true;
    } else {
      $scope.fromBackNavigation = false;

    }
    $scope.$watch('deliveryZones', function () {
      if ($scope.deliveryZones && $scope.selectedZone) {
        $scope.selectedZone = _.where($scope.deliveryZones, {id: $scope.selectedZone.id})[0];
        $scope.loadPrograms();
      }
    });
  });


  $scope.initiateDistribution = function () {

    function isCached() {
      return _.find($scope.distributionList, function (distribution) {
        return distribution.deliveryZone.id == $scope.selectedZone.id &&
            distribution.program.id == $scope.selectedProgram.id &&
            distribution.period.id == $scope.selectedPeriod.id;
      });
    }

    if (isCached()) {
      $scope.message = messageService.get("message.distribution.already.cached",
          $scope.selectedZone.name, $scope.selectedProgram.name, $scope.selectedPeriod.name);
      return;
    }

    DeliveryZoneFacilities.get({"programId": $scope.selectedProgram.id, "deliveryZoneId": $scope.selectedZone.id }, onReferenceDataSuccess, {});


    function onReferenceDataSuccess(data) {

      var distributionReferenceDataTransaction = IndexedDB.getConnection().transaction('distributionReferenceData', 'readwrite');
      var distributionReferenceDataStore = distributionReferenceDataTransaction.objectStore('distributionReferenceData');
{'fraction_non_alphanumeric': 0.13392380749119626, 'fraction_numerical': 0.003521502507736634, 'mean_word_length': 3.911949685534591, 'pattern_counts': {'":': 4, '<': 3, '<?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': '424798', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2696, 'n_words': 450}