<commit_before>'use strict';

angular.module('oncokbApp')
    .controller('GenesCtrl', ['$scope', '$rootScope', '$location', '$timeout',
        '$routeParams', '_', 'config', 'importer', 'storage', 'documents',
        'users', 'DTColumnDefBuilder', 'DTOptionsBuilder', 'DatabaseConnector',
        'OncoKB', 'stringUtils', 'S', 'mainUtils', 'gapi', 'UUIDjs', 'dialogs',
        function($scope, $rootScope, $location, $timeout, $routeParams, _,
                 config, importer, storage, Documents, users,
                 DTColumnDefBuilder, DTOptionsBuilder, DatabaseConnector,
                 OncoKB, stringUtils, S, MainUtils, gapi, UUIDjs, dialogs) {
            function saveGene(docs, docIndex, excludeObsolete, callback) {
                if (docIndex < docs.length) {
                    var fileId = docs[docIndex].id;
                    storage.getRealtimeDocument(fileId).then(function(realtime) {
                        if (realtime && realtime.error) {
                            console.log('did not get realtime document.');
                        } else {
                            console.log(docs[docIndex].title, '\t\t', docIndex);
                            console.log('\t copying');
                            var gene = realtime.getModel().getRoot().get('gene');
                            var vus = realtime.getModel().getRoot().get('vus');
                            if (gene) {
                                var geneData = stringUtils.getGeneData(gene, excludeObsolete, true, true, true);
                                var vusData = stringUtils.getVUSFullData(vus, true);
                                var params = {};

                                if (geneData) {
                                    params.gene = JSON.stringify(geneData);
                                }
                                if (vusData) {
                                    params.vus = JSON.stringify(vusData);
                                }
                                DatabaseConnector.updateGene(params,
                                    function(result) {
                                        console.log('\t success', result);
                                        $timeout(function() {
                                            saveGene(docs, ++docIndex, excludeObsolete, callback);
                                        }, 200, false);
                                    },
                                    function(result) {
                                        console.log('\t failed', result);
                                        $timeout(function() {
                                            saveGene(docs, ++docIndex, excludeObsolete, callback);
                                        }, 200, false);
                                    }
                                );
                            } else {
                                console.log('\t\tNo gene model.');
                                $timeout(function() {
                                    saveGene(docs, ++docIndex, excludeObsolete, callback);
                                }, 200, false);
                            }
                        }
                    });
                } else {
                    if (callback) {
                        callback();
                    }
                    console.log('finished.');
                }
            }

            $scope.showDocs = function() {
                $scope.documents.forEach(function(item) {
                    console.log(item.title);
                });
                // console.log($scope.documents);
            };
            $scope.metaFlags = {};
            $scope.getDocs = function() {
                var docs = Documents.get();
                if (docs.length > 0) {
                    // $scope.$apply(function() {
                    assignReviewColumn();
                    $scope.documents = Documents.get();
                    $scope.status.rendering = false;
                    // });
                } else if (OncoKB.global.genes) {
                    storage.requireAuth(true).then(function() {
                        storage.retrieveAllFiles().then(function(result) {
                            Documents.set(result);
                            Documents.setStatus(OncoKB.global.genes);
                            assignReviewColumn();
                            $scope.documents = Documents.get();
                            $scope.status.rendering = false;
                            // loading_screen.finish();
                        });
                    });
                } else {
                    DatabaseConnector.getAllGene(function(data) {
                        OncoKB.global.genes = data;
                        storage.requireAuth(true).then(function() {
                            storage.retrieveAllFiles().then(function(result) {
                                Documents.set(result);
                                Documents.setStatus(OncoKB.global.genes);
                                if (users.getMe().role === 8) {
                                    storage.retrieveMeta().then(function(result) {
                                        if (result && (result.error || !_.isArray(result) || result.length === 0)) {
                                            dialogs.error('Error', 'Fail to retrieve meta file! Please stop editing and contact the developer!');
                                            var sendTo = '<EMAIL>';
                                            var subject = 'Fail to retrieve meta file';
                                            var content;
                                            if (_.isArray(result) && result.length === 0) {
                                                content = 'There is no meta file inside the Meta folder';
                                            } else {
                                                content = 'System error is ' + JSON.stringify(result.error);
                                            }
                                            MainUtils.sendEmail(sendTo, subject, content);
                                        } else {
                                            storage.getMetaRealtimeDocument(result[0].id).then(function(metaRealtime) {
                                                if (metaRealtime && metaRealtime.error) {
                                                    dialogs.error('Error', 'Fail to get meta document! Please stop editing and contact the developer!');
                                                } else {
                                                    $rootScope.metaRealtime = metaRealtime;
                                                    $rootScope.metaModel = metaRealtime.getModel();
                                                    $rootScope.metaData = metaRealtime.getModel().getRoot().get('review');
                                                    assignReviewColumn();
                                                }
                                                $scope.documents = Documents.get();
                                                $scope.status.rendering = false;
                                            });
                                        }
                                    });
                                } else {
                                    $scope.documents = Documents.get();
                                    $scope.status.rendering = false;
                                }
                            });
                        });
                    });
                }
            };
            function assignReviewColumn() {
                var genes = $rootScope.metaData.keys();
                for (var i = 0; i < genes.length; i++) {
                    var geneMetaData = $rootScope.metaData.get(genes[i]);
                    var uuids = geneMetaData.keys();
                    var flag = true;
                    for (var j = 0; j < uuids.length; j++) {
                        if (geneMetaData.get(uuids[j]).type === 'Map' && geneMetaData.get(uuids[j]).get('review')) {
                            $scope.metaFlags[genes[i]] = true;
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        $scope.metaFlags[genes[i]] = false;
                    }
                }
            }

            $scope.backup = function() {
                $scope.status.backup = false;
                OncoKB.backingUp = true;
                importer.backup($scope.backupFolderName, function() {
                    $scope.status.backup = true;
                    OncoKB.backingUp = false;
                });
            };

            $scope.redirect = function(path) {
                $location.path(path);
            };

            $scope.checkError = function() {
                console.log($rootScope.errors);
            };

            $scope.saveAllGenes = function() {
                $scope.status.saveAllGenes = false;
                saveGene($scope.documents, 0, true, function() {
                    $scope.status.saveAllGenes = true;
                });
            };

            $scope.userRole = users.getMe().role;

            var sorting = [[2, 'asc'], [1, 'desc'], [0, 'asc']];
            if (users.getMe().role === 8) {
                sorting = [[4, 'desc'], [1, 'desc'], [0, 'asc']];
            }<commit_msg>bug fix for backup folder name<commit_after>'use strict';

angular.module('oncokbApp')
    .controller('GenesCtrl', ['$scope', '$rootScope', '$location', '$timeout',
        '$routeParams', '_', 'config', 'importer', 'storage', 'documents',
        'users', 'DTColumnDefBuilder', 'DTOptionsBuilder', 'DatabaseConnector',
        'OncoKB', 'stringUtils', 'S', 'mainUtils', 'gapi', 'UUIDjs', 'dialogs',
        function($scope, $rootScope, $location, $timeout, $routeParams, _,
                 config, importer, storage, Documents, users,
                 DTColumnDefBuilder, DTOptionsBuilder, DatabaseConnector,
                 OncoKB, stringUtils, S, MainUtils, gapi, UUIDjs, dialogs) {
            function saveGene(docs, docIndex, excludeObsolete, callback) {
                if (docIndex < docs.length) {
                    var fileId = docs[docIndex].id;
                    storage.getRealtimeDocument(fileId).then(function(realtime) {
                        if (realtime && realtime.error) {
                            console.log('did not get realtime document.');
                        } else {
                            console.log(docs[docIndex].title, '\t\t', docIndex);
                            console.log('\t copying');
                            var gene = realtime.getModel().getRoot().get('gene');
                            var vus = realtime.getModel().getRoot().get('vus');
                            if (gene) {
                                var geneData = stringUtils.getGeneData(gene, excludeObsolete, true, true, true);
                                var vusData = stringUtils.getVUSFullData(vus, true);
                                var params = {};

                                if (geneData) {
                                    params.gene = JSON.stringify(geneData);
                                }
                                if (vusData) {
                                    params.vus = JSON.stringify(vusData);
                                }
                                DatabaseConnector.updateGene(params,
                                    function(result) {
                                        console.log('\t success', result);
                                        $timeout(function() {
                                            saveGene(docs, ++docIndex, excludeObsolete, callback);
                                        }, 200, false);
                                    },
                                    function(result) {
                                        console.log('\t failed', result);
                                        $timeout(function() {
                                            saveGene(docs, ++docIndex, excludeObsolete, callback);
                                        }, 200, false);
                                    }
                                );
                            } else {
                                console.log('\t\tNo gene model.');
                                $timeout(function() {
                                    saveGene(docs, ++docIndex, excludeObsolete, callback);
                                }, 200, false);
                            }
                        }
                    });
                } else {
                    if (callback) {
                        callback();
                    }
                    console.log('finished.');
                }
            }

            $scope.showDocs = function() {
                $scope.documents.forEach(function(item) {
                    console.log(item.title);
                });
                // console.log($scope.documents);
            };
            $scope.metaFlags = {};
            $scope.getDocs = function() {
                var docs = Documents.get();
                if (docs.length > 0) {
                    // $scope.$apply(function() {
                    assignReviewColumn();
                    $scope.documents = Documents.get();
                    $scope.status.rendering = false;
                    // });
                } else if (OncoKB.global.genes) {
                    storage.requireAuth(true).then(function() {
                        storage.retrieveAllFiles().then(function(result) {
                            Documents.set(result);
                            Documents.setStatus(OncoKB.global.genes);
                            assignReviewColumn();
                            $scope.documents = Documents.get();
                            $scope.status.rendering = false;
                            // loading_screen.finish();
                        });
                    });
                } else {
                    DatabaseConnector.getAllGene(function(data) {
                        OncoKB.global.genes = data;
                        storage.requireAuth(true).then(function() {
                            storage.retrieveAllFiles().then(function(result) {
                                Documents.set(result);
                                Documents.setStatus(OncoKB.global.genes);
                                if (users.getMe().role === 8) {
                                    storage.retrieveMeta().then(function(result) {
                                        if (result && (result.error || !_.isArray(result) || result.length === 0)) {
                                            dialogs.error('Error', 'Fail to retrieve meta file! Please stop editing and contact the developer!');
                                            var sendTo = '<EMAIL>';
                                            var subject = 'Fail to retrieve meta file';
                                            var content;
                                            if (_.isArray(result) && result.length === 0) {
                                                content = 'There is no meta file inside the Meta folder';
                                            } else {
                                                content = 'System error is ' + JSON.stringify(result.error);
                                            }
                                            MainUtils.sendEmail(sendTo, subject, content);
                                        } else {
                                            storage.getMetaRealtimeDocument(result[0].id).then(function(metaRealtime) {
                                                if (metaRealtime && metaRealtime.error) {
                                                    dialogs.error('Error', 'Fail to get meta document! Please stop editing and contact the developer!');
                                                } else {
                                                    $rootScope.metaRealtime = metaRealtime;
                                                    $rootScope.metaModel = metaRealtime.getModel();
                                                    $rootScope.metaData = metaRealtime.getModel().getRoot().get('review');
                                                    assignReviewColumn();
                                                }
                                                $scope.documents = Documents.get();
                                                $scope.status.rendering = false;
                                            });
                                        }
                                    });
                                } else {
                                    $scope.documents = Documents.get();
                                    $scope.status.rendering = false;
                                }
                            });
                        });
                    });
                }
            };
            function assignReviewColumn() {
                var genes = $rootScope.metaData.keys();
                for (var i = 0; i < genes.length; i++) {
                    var geneMetaData = $rootScope.metaData.get(genes[i]);
                    var uuids = geneMetaData.keys();
                    var flag = true;
                    for (var j = 0; j < uuids.length; j++) {
                        if (geneMetaData.get(uuids[j]).type === 'Map' && geneMetaData.get(uuids[j]).get('review')) {
                            $scope.metaFlags[genes[i]] = true;
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        $scope.metaFlags[genes[i]] = false;
                    }
                }
            }

            $scope.backup = function(backupFolderName) {
                $scope.status.backup = false;
                OncoKB.backingUp = true;
                importer.backup(backupFolderName, function() {
                    $scope.status.backup = true;
                    OncoKB.backingUp = false;
                });
            };

            $scope.redirect = function(path) {
                $location.path(path);
            };

            $scope.checkError = function() {
                console.log($rootScope.errors);
            };

            $scope.saveAllGenes = function() {
                $scope.status.saveAllGenes = false;
                saveGene($scope.documents, 0, true, function() {
                    $scope.status.saveAllGenes = true;
                });
            };

            $scope.userRole = users.getMe().role;

            var sorting = [[2, 'asc'], [1, 'desc'], [0, 'asc']];
            if (users.getMe().role === 8) {
                sorting = [[4, 'desc'], [1, 'desc'], [0, 'asc']];
            }
{'fraction_non_alphanumeric': 0.10293505412156537, 'fraction_numerical': 0.002497918401332223, 'mean_word_length': 0.849388894235396, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 162, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2636009', 'n_tokens_mistral': 4106, 'n_tokens_neox': 3806, 'n_words': 727}