<commit_before>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2017 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU Affero General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Affero General Public License for more details. You should have received a copy of
 * the GNU Affero General Public License along with this program. If not, see
 * http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

(function(){

    'use strict';

    /**
     * @ngdoc service
     * @name requisition.requisitionService
     *
     * @description
     * Extends program service for this module.
     */
    angular
        .module('requisition-convert-to-order')
        .factory('requisitionsForConvert', requisitionsForConvert);

    requisitionsForConvert.$inject = ['requisitionService', '$q'];

    function requisitionsForConvert(requisitionService, $q) {
        var lastParams, lastPage, factory = {
            forConvert: forConvert,
            convertToOrder: convertToOrder
        };

        return factory;

        function forConvert(params) {
            var newParams = convertParams(params);

            if (shouldFetchFromServer(params)) {
                return requisitionService.forConvert(newParams)
                .then(function(page) {
                    lastParams = params;
                    lastPage = page;
                    return getRequestedPage(page, params, newParams);
                });
            }
            return getRequestedPage(lastPage, params, lastPage);
        }

        function convertToOrder(requisitions) {
            return requisitionService.convertToOrder(requisitions)
            .then(function() {
                removeConvertedRequisitions(requisitions);
            });
        }

        function removeConvertedRequisitions(requisitions) {
            if (lastPage) {
                requisitions.forEach(function(requisition) {
                    var id = requisition.requisition.id;
                    lastPage.content = lastPage.content.filter(function(requisition) {
                        return requisition.requisition.id !== id;
                    });
                    shrinkLastPage();
                });
            }
        }

        function shrinkLastPage() {
            if (lastPage) {
                lastPage.totalElements--;
                lastPage.numberOfElements--;
                lastPage.last = lastPage.totalElements - 1 <= calculateTo(lastPage);
                lastPage.totalPages = Math.ceil(parseInt(lastPage.totalElements) / parseInt(lastPage.size));

                if (!lastPage.totalElements) {
                    lastPage = undefined;
                }
            }
        }

        function getRequestedPage(page, params, pageable) {
            var originalFrom = calculateFrom(params),
                from = calculateFrom(pageable),
                offset = originalFrom - from,
                contentPromise,
                newPage = {
                    first: originalFrom === 0,
                    last: page.totalElements - 1 <= calculateTo(params),
                    number: parseInt(params.page),
                    size: parseInt(params.size),
                    totalPages: Math.ceil(parseInt(page.totalElements) / parseInt(params.size)),
                    totalElements: parseInt(page.totalElements),
                    content: page.content.slice(offset, offset + parseInt(params.size)),

                };

            newPage.numberOfElements = newPage.content.length;

            return $q.resolve(newPage);
        }

        function shouldFetchFromServer(params) {
            if (!lastPage) {
                return true;
            }

            if (!lastPage.numberOfElements) {
                return true;
            }

            if (calculateFrom(lastPage) > calculateFrom(params)) {
                return true;
            }

            if (calculateTo(lastPage) < calculateTo(params) && !lastPage.last) {
                return true;
            }

            if (!angular.equals(lastParams.sort, params.sort)) {
                return true;
            }

            if (!angular.equals(lastParams.filterBy, params.filterBy)) {<commit_msg>OLMIS-3318: Made requisitionsForConvert no longer share lastPage and lastParams between instances<commit_after>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2017 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU Affero General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Affero General Public License for more details. You should have received a copy of
 * the GNU Affero General Public License along with this program. If not, see
 * http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

(function(){

    'use strict';

    /**
     * @ngdoc service
     * @name requisition.requisitionService
     *
     * @description
     * Extends program service for this module.
     */
    angular
        .module('requisition-convert-to-order')
        .factory('requisitionsForConvert', requisitionsForConvert);

    requisitionsForConvert.$inject = ['requisitionService', '$q'];

    function requisitionsForConvert(requisitionService, $q) {
        var factory = {
            forConvert: forConvert,
            convertToOrder: convertToOrder
        };

        return factory;

        function forConvert(params) {
            var newParams = convertParams(params);

            var factory = this;
            if (shouldFetchFromServer(params, this.lastParams, this.lastPage)) {
                return requisitionService.forConvert(newParams)
                .then(function(page) {
                    factory.lastParams = params;
                    factory.lastPage = page;
                    return getRequestedPage(page, params, newParams);
                });
            }
            return getRequestedPage(this.lastPage, params, this.lastPage);
        }

        function convertToOrder(requisitions) {
            var lastPage = this.lastPage;
            return requisitionService.convertToOrder(requisitions)
            .then(function() {
                removeConvertedRequisitions(requisitions, lastPage);
            });
        }

        function removeConvertedRequisitions(requisitions, lastPage) {
            if (lastPage) {
                requisitions.forEach(function(requisition) {
                    var id = requisition.requisition.id;
                    lastPage.content = lastPage.content.filter(function(requisition) {
                        return requisition.requisition.id !== id;
                    });
                    shrinkLastPage(lastPage);
                });
            }
        }

        function shrinkLastPage(lastPage) {
            if (lastPage) {
                lastPage.totalElements--;
                lastPage.numberOfElements--;
                lastPage.last = lastPage.totalElements - 1 <= calculateTo(lastPage);
                lastPage.totalPages = Math.ceil(parseInt(lastPage.totalElements) / parseInt(lastPage.size));

                if (!lastPage.totalElements) {
                    lastPage = undefined;
                }
            }
        }

        function getRequestedPage(page, params, pageable) {
            var originalFrom = calculateFrom(params),
                from = calculateFrom(pageable),
                offset = originalFrom - from,
                contentPromise,
                newPage = {
                    first: originalFrom === 0,
                    last: page.totalElements - 1 <= calculateTo(params),
                    number: parseInt(params.page),
                    size: parseInt(params.size),
                    totalPages: Math.ceil(parseInt(page.totalElements) / parseInt(params.size)),
                    totalElements: parseInt(page.totalElements),
                    content: page.content.slice(offset, offset + parseInt(params.size)),

                };

            newPage.numberOfElements = newPage.content.length;

            return $q.resolve(newPage);
        }

        function shouldFetchFromServer(params, lastParams, lastPage) {
            if (!lastPage) {
                return true;
            }

            if (!lastPage.numberOfElements) {
                return true;
            }

            if (calculateFrom(lastPage) > calculateFrom(params)) {
                return true;
            }

            if (calculateTo(lastPage) < calculateTo(params) && !lastPage.last) {
                return true;
            }

            if (!angular.equals(lastParams.sort, params.sort)) {
                return true;
            }

            if (!angular.equals(lastParams.filterBy, params.filterBy)) {
{'fraction_non_alphanumeric': 0.08444676409185804, 'fraction_numerical': 0.0020876826722338203, 'mean_word_length': 1.8220913107511045, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6961732', 'n_tokens_mistral': 2387, 'n_tokens_neox': 2230, 'n_words': 590}