<commit_before>(function() {
    'use strict';
    angular.module('chaise.recordcreate', ['chaise.errors','chaise.utils']).factory("recordCreate", ['$cookies', '$log', '$window', '$uibModal', 'AlertsService', 'DataUtils', 'UriUtils', 'modalBox',
     function($cookies, $log, $window, $uibModal, AlertsService, DataUtils, UriUtils, modalBox) {

        var viewModel = {};
        var GV_recordEditModel = {},
            completed = {};
        var addRecordRequests = {}; // <generated unique id : reference of related table>
        var editRecordRequests = {}; // generated id: {schemaName, tableName}
        var updated = {};

        /**
         * checkUpdate - to check all recrds are updated; passed as callback to uploadFiles().
         *
         * @param  {array} submissionRowsCopy   array contains updated recrds attributes
         * @param  {array} rsTuples             array with data tuples value from calling function
         * @return  {array} data                key-value pair of updated records
         */
        function checkUpdate(submissionRowsCopy, rsTuples){
            /**
             * After uploading files, the returned submissionRowsCopy contains
             * new file data. This includes filename, filebyte, and md5.
             * The following makes sure that all the data are updated.
             * That's why this for loop must be after uploading files and not before.
             * And we cannot just pass submissionRowsCopy to update function, because
             * update function only accepts array of tuples (and not just key-value pair).
             */
            for (var i = 0; i < submissionRowsCopy.length; i++) {
                var row = submissionRowsCopy[i];
                var data = rsTuples[i].data;
                // assign each value from the form to the data object on tuple
                for (var key in row) {
                    data[key] = (row[key] === '' ? null : row[key]);
                }
            }
            return data;
        }

        /**
         * uploadFiles - uploading files
         *
         * @param  {array} submissionRowsCopy   data that is going to be uploaded
         * @param  {object} rsReference         rootscope reference from calling function
         * @param  {object} onSuccess           callback
         */
        function uploadFiles(submissionRowsCopy, rsReference, onSuccess) {

                $uibModal.open({
                    templateUrl: "../common/templates/uploadProgress.modal.html",
                    controller: "UploadModalDialogController",
                    controllerAs: "ctrl",
                    size: "md",
                    backdrop: 'static',
                    keyboard: false,
                    resolve: {
                        params: {
                            reference: rsReference,
                            rows: submissionRowsCopy
                        }
                    }
                }).result.then(onSuccess, function(exception) {
                    viewModel.readyToSubmit = false;
                    viewModel.submissionButtonDisabled = false;

                    if (exception) AlertsService.addAlert(exception.message, 'error');
                });
        }

        /**
         * addRecords - Function that calls ermrestjs method to add data
         *
         * @param  {bool} isUpdate              flag
         * @param  {object} derivedref          derived referene of the column that is being added
         * @param  {array} recordEditModel      data array
         * @param  {bool} isModalUpdate         if updating through record app
         * @param  {object} rsReference         rootscope reference from calling function
         * @param  {array} rsTuples             ootscope tuples array from calling function
         * @param  {object} rsQueryParams       object contains queryparams of context from calling function
         * @param  {object} onSuccessFunction   callback
         */
        function addRecords(isUpdate, derivedref, recordEditModel, isModalUpdate, rsReference, rsTuples, rsQueryParams, onSuccessFunction) {
            var model = isModalUpdate ? GV_recordEditModel : recordEditModel;
            var form = viewModel.formContainer;

            // this will include updated and previous raw values.
            var submissionRowsCopy = [];

            model.submissionRows.forEach(function(row) {
                submissionRowsCopy.push(Object.assign({}, row));
            });

            /**
             * Add raw values that are not visible to submissionRowsCopy:
             *
             * submissionRowsCopy is the datastructure that will be used for creating
             * the upload url. It must have all the visible and invisible data.
             * The following makes sure that submissionRowsCopy has all the underlying data
             */
            if (isUpdate) {
                for (var i = 0; i < submissionRowsCopy.length; i++) {
                    var newData = submissionRowsCopy[i];
                    var oldData = rsTuples[i].data;

                    // make sure submissionRowsCopy has all the data
                    for (var key in oldData) {
                        if (key in newData) continue;
                        newData[key] = oldData[key];
                    }
                }
            }

            //call uploadFiles which will upload files and callback on success
            uploadFiles(submissionRowsCopy, rsReference, function() {

                var fn = "create",
                    args = [submissionRowsCopy];
                var fnScope = isModalUpdate ? derivedref.unfilteredReference.contextualize.entryCreate : rsReference.unfilteredReference.contextualize.entryCreate;

                if (isUpdate) {

                    var data = checkUpdate(submissionRowsCopy, rsTuples);

                    // submit rootScope.tuples because we are changing and
                    // comparing data from the old data set for the tuple with the updated data set from the UI
                    fn = "update", fnScope = rsReference, args = [rsTuples];
                }

                fnScope[fn].apply(fnScope, args).then(function success(result) {

                    var page = result.successful;
                    var failedPage = result.failed;

                    // the returned reference is contextualized and we don't need to contextualize it again
                    var resultsReference = page.reference;
                    if (isUpdate) {
                        var data = checkUpdate(submissionRowsCopy, rsTuples);
                        // check if there is a window that opened the current one<commit_msg>build trigger<commit_after>(function() {
    'use strict';
    angular.module('chaise.recordcreate', ['chaise.errors','chaise.utils']).factory("recordCreate", ['$cookies', '$log', '$window', '$uibModal', 'AlertsService', 'DataUtils', 'UriUtils', 'modalBox',
     function($cookies, $log, $window, $uibModal, AlertsService, DataUtils, UriUtils, modalBox) {

        var viewModel = {};
        var GV_recordEditModel = {},
            completed = {};
        var addRecordRequests = {}; // <generated unique id : reference of related table>
        var editRecordRequests = {}; // generated id: {schemaName, tableName}
        var updated = {};

        /**
         * checkUpdate - to check all recrds are updated; passed as callback to uploadFiles(). 
         *
         * @param  {array} submissionRowsCopy   array contains updated recrds attributes
         * @param  {array} rsTuples             array with data tuples value from calling function
         * @return  {array} data                key-value pair of updated records
         */
        function checkUpdate(submissionRowsCopy, rsTuples){
            /**
             * After uploading files, the returned submissionRowsCopy contains
             * new file data. This includes filename, filebyte, and md5.
             * The following makes sure that all the data are updated.
             * That's why this for loop must be after uploading files and not before.
             * And we cannot just pass submissionRowsCopy to update function, because
             * update function only accepts array of tuples (and not just key-value pair).
             */
            for (var i = 0; i < submissionRowsCopy.length; i++) {
                var row = submissionRowsCopy[i];
                var data = rsTuples[i].data;
                // assign each value from the form to the data object on tuple
                for (var key in row) {
                    data[key] = (row[key] === '' ? null : row[key]);
                }
            }
            return data;
        }

        /**
         * uploadFiles - uploading files
         *
         * @param  {array} submissionRowsCopy   data that is going to be uploaded
         * @param  {object} rsReference         rootscope reference from calling function
         * @param  {object} onSuccess           callback
         */
        function uploadFiles(submissionRowsCopy, rsReference, onSuccess) {

                $uibModal.open({
                    templateUrl: "../common/templates/uploadProgress.modal.html",
                    controller: "UploadModalDialogController",
                    controllerAs: "ctrl",
                    size: "md",
                    backdrop: 'static',
                    keyboard: false,
                    resolve: {
                        params: {
                            reference: rsReference,
                            rows: submissionRowsCopy
                        }
                    }
                }).result.then(onSuccess, function(exception) {
                    viewModel.readyToSubmit = false;
                    viewModel.submissionButtonDisabled = false;

                    if (exception) AlertsService.addAlert(exception.message, 'error');
                });
        }

        /**
         * addRecords - Function that calls ermrestjs method to add data
         *
         * @param  {bool} isUpdate              flag
         * @param  {object} derivedref          derived referene of the column that is being added
         * @param  {array} recordEditModel      data array
         * @param  {bool} isModalUpdate         if updating through record app
         * @param  {object} rsReference         rootscope reference from calling function
         * @param  {array} rsTuples             ootscope tuples array from calling function
         * @param  {object} rsQueryParams       object contains queryparams of context from calling function
         * @param  {object} onSuccessFunction   callback
         */
        function addRecords(isUpdate, derivedref, recordEditModel, isModalUpdate, rsReference, rsTuples, rsQueryParams, onSuccessFunction) {
            var model = isModalUpdate ? GV_recordEditModel : recordEditModel;
            var form = viewModel.formContainer;

            // this will include updated and previous raw values.
            var submissionRowsCopy = [];

            model.submissionRows.forEach(function(row) {
                submissionRowsCopy.push(Object.assign({}, row));
            });

            /**
             * Add raw values that are not visible to submissionRowsCopy:
             *
             * submissionRowsCopy is the datastructure that will be used for creating
             * the upload url. It must have all the visible and invisible data.
             * The following makes sure that submissionRowsCopy has all the underlying data
             */
            if (isUpdate) {
                for (var i = 0; i < submissionRowsCopy.length; i++) {
                    var newData = submissionRowsCopy[i];
                    var oldData = rsTuples[i].data;

                    // make sure submissionRowsCopy has all the data
                    for (var key in oldData) {
                        if (key in newData) continue;
                        newData[key] = oldData[key];
                    }
                }
            }

            //call uploadFiles which will upload files and callback on success
            uploadFiles(submissionRowsCopy, rsReference, function() {

                var fn = "create",
                    args = [submissionRowsCopy];
                var fnScope = isModalUpdate ? derivedref.unfilteredReference.contextualize.entryCreate : rsReference.unfilteredReference.contextualize.entryCreate;

                if (isUpdate) {
                    var data = checkUpdate(submissionRowsCopy, rsTuples);
                    // submit rootScope.tuples because we are changing and
                    // comparing data from the old data set for the tuple with the updated data set from the UI
                    fn = "update", fnScope = rsReference, args = [rsTuples];
                }

                fnScope[fn].apply(fnScope, args).then(function success(result) {

                    var page = result.successful;
                    var failedPage = result.failed;

                    // the returned reference is contextualized and we don't need to contextualize it again
                    var resultsReference = page.reference;
                    if (isUpdate) {
                        var data = checkUpdate(submissionRowsCopy, rsTuples);
                        // check if there is a window that opened the current one
{'fraction_non_alphanumeric': 0.07743181987069926, 'fraction_numerical': 0.0004458646057813777, 'mean_word_length': 1.632114218658322, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '1642405', 'n_tokens_mistral': 3165, 'n_tokens_neox': 2989, 'n_words': 1038}