<commit_before>"use strict";

angular.module("ehelseEditor").factory("FileUpload", ["StorageHandler", "Action", "Document", "DocumentField", "DocumentType" , "LinkCategory", "Mandatory", "Status", "TargetGroup", "Topic", "CSVConverter",
    function (StorageHandler, Action, Document, DocumentField, DocumentType, LinkCategory, Mandatory, Status, TargetGroup, Topic, CSVConverter) {

        var chosenFileEntry = null;
        var isJsonFile = false;
        var contentFromFile;

        function onLoad() {
            chrome.fileSystem.chooseEntry({type: 'openWritableFile'}, function (fileEntry) {
                if (chrome.runtime.lastError){
                    console.warn(chrome.runtime.lastError.message);
                } else {
                    chosenFileEntry = fileEntry;

                    fileEntry.file(function (file) {
                        var reader = new FileReader();
                        reader.onerror = errorHandler;
                        reader.onloadend = function (loadEvent) {
                            clearEverything();
                            contentFromFile = JSON.parse(loadEvent.target.result);
                            StorageHandler.initJSON(contentFromFile);
                            initEverything();
                            isJsonFile = true;
                        };
                        reader.readAsText(file);
                    }, errorHandler)
                }
            })
        }
        function onLoadCSV() {
            chrome.fileSystem.chooseEntry({type: 'openFile'}, function (fileEntry) {
                if (chrome.runtime.lastError){
                    console.warn(chrome.runtime.lastError.message);
                } else {
                    fileEntry.file(function (file) {
                        var reader = new FileReader();
                        reader.onerror = errorHandler;
                        reader.onloadend = function (loadEvent) {
                            contentFromFile = loadEvent.target.result;
                            clearEverything();
                            CSVConverter.uploadCSVContent(contentFromFile);
                            StorageHandler.initCsv();
                            initEverything();
                            isJsonFile = false;
                        };
                        reader.readAsText(file);
                    }, errorHandler)
                }
            })
        }

        function onSave(modifiedJsonObject){
            if(isJsonFile){
                chrome.fileSystem.getWritableEntry(chosenFileEntry, function (writableFileEntry) {
                    if (chrome.runtime.lastError){
                        console.warn(chrome.runtime.lastError.message);
                    } else {
                        writableFileEntry.createWriter(function (writer) {
                            writer.onerror = errorHandler;

                            var blob = new Blob([JSON.stringify(JSON.parse(angular.toJson(modifiedJsonObject)), null, '\t')], {type: "application/json"});
                            writer.truncate(blob.size);
                            writer.onwriteend = function () {
                                writer.onwriteend = function (e) {
                                    console.log("written");
                                };
                            };
                            chosenFileEntry.file(function (file) {
                                writer.write(blob);
                            })
                        }, errorHandler)
                    }
                })
            } else {
                chrome.fileSystem.chooseEntry({type: 'saveFile'}, function(writableFileEntry) {
                    if (chrome.runtime.lastError){
                        console.warn(chrome.runtime.lastError.message);
                    } else {
                        writableFileEntry.createWriter(function (writer) {
                            writer.onerror = errorHandler;
                            writer.onwriteend = function (e) {
                                chosenFileEntry = writableFileEntry;
                                isJsonFile = true;
                            };
                            writer.write(new Blob(['MIME_JSON'], {type: "application/json"}));
                        }, errorHandler);
                    }
                });

            }
        }

        function errorHandler(e) {
            var msg = "";
            switch (e.code) {
                case FileError.QUOTA_EXCEEDED_ERR:
                    msg = "QUOTA_EXCEEDED_ERR";
                    break;
                case FileError.NOT_FOUND_ERR:
                    msg = "NOT_FOUND_ERR";
                    break;
                case FileError.SECURITY_ERR:
                    msg = "SECURITY_ERR";
                    break;
                case FileError.INVALID_MODIFICATION_ERR:
                    msg = "INVALID_MODIFICATION_ERR";
                    break;
                case FileError.INVALID_STATE_ERR:
                    msg = "INVALID_STATE_ERR";
                    break;
                default:
                    msg = "Unknown Error";
                    break;
            }
        }

        function initEverything() {
            Action.init();
            Document.init();
            DocumentField.init();
            DocumentType.init();
            LinkCategory.init();
            Mandatory.init();
            Status.init();
            TargetGroup.init();
            Topic.init();
        }

        function clearEverything() {
            Action.clear();
            Document.clear();
            DocumentField.clear();
            DocumentType.clear();
            LinkCategory.clear();
            Mandatory.clear();
            Status.clear();<commit_msg>Fixed error with headings when uploading file<commit_after>"use strict";

angular.module("ehelseEditor").factory("FileUpload",
    ["StorageHandler", "Action", "Document", "DocumentField", "DocumentType" , "LinkCategory", "Mandatory", "Status", "TargetGroup", "Topic", "CSVConverter", "Heading", "ContactAddress",
    function (StorageHandler, Action, Document, DocumentField, DocumentType, LinkCategory, Mandatory, Status, TargetGroup, Topic, CSVConverter, Heading, ContactAddress) {

        var chosenFileEntry = null;
        var isJsonFile = false;
        var contentFromFile;

        function onLoad() {
            chrome.fileSystem.chooseEntry({type: 'openWritableFile'}, function (fileEntry) {
                if (chrome.runtime.lastError){
                    console.warn(chrome.runtime.lastError.message);
                } else {
                    chosenFileEntry = fileEntry;

                    fileEntry.file(function (file) {
                        var reader = new FileReader();
                        reader.onerror = errorHandler;
                        reader.onloadend = function (loadEvent) {
                            clearEverything();
                            contentFromFile = JSON.parse(loadEvent.target.result);
                            StorageHandler.initJSON(contentFromFile);
                            initEverything();
                            isJsonFile = true;
                        };
                        reader.readAsText(file);
                    }, errorHandler)
                }
            })
        }
        function onLoadCSV() {
            chrome.fileSystem.chooseEntry({type: 'openFile'}, function (fileEntry) {
                if (chrome.runtime.lastError){
                    console.warn(chrome.runtime.lastError.message);
                } else {
                    fileEntry.file(function (file) {
                        var reader = new FileReader();
                        reader.onerror = errorHandler;
                        reader.onloadend = function (loadEvent) {
                            contentFromFile = loadEvent.target.result;
                            clearEverything();
                            CSVConverter.uploadCSVContent(contentFromFile);
                            StorageHandler.initCsv();
                            initEverything();
                            isJsonFile = false;
                        };
                        reader.readAsText(file);
                    }, errorHandler)
                }
            })
        }

        function onSave(modifiedJsonObject){
            if(isJsonFile){
                chrome.fileSystem.getWritableEntry(chosenFileEntry, function (writableFileEntry) {
                    if (chrome.runtime.lastError){
                        console.warn(chrome.runtime.lastError.message);
                    } else {
                        writableFileEntry.createWriter(function (writer) {
                            writer.onerror = errorHandler;

                            var blob = new Blob([JSON.stringify(JSON.parse(angular.toJson(modifiedJsonObject)), null, '\t')], {type: "application/json"});
                            writer.truncate(blob.size);
                            writer.onwriteend = function () {
                                writer.onwriteend = function (e) {
                                    console.log("written");
                                };
                            };
                            chosenFileEntry.file(function (file) {
                                writer.write(blob);
                            })
                        }, errorHandler)
                    }
                })
            } else {
                chrome.fileSystem.chooseEntry({type: 'saveFile'}, function(writableFileEntry) {
                    if (chrome.runtime.lastError){
                        console.warn(chrome.runtime.lastError.message);
                    } else {
                        writableFileEntry.createWriter(function (writer) {
                            writer.onerror = errorHandler;
                            writer.onwriteend = function (e) {
                                chosenFileEntry = writableFileEntry;
                                isJsonFile = true;
                            };
                            writer.write(new Blob(['MIME_JSON'], {type: "application/json"}));
                        }, errorHandler);
                    }
                });

            }
        }

        function errorHandler(e) {
            var msg = "";
            switch (e.code) {
                case FileError.QUOTA_EXCEEDED_ERR:
                    msg = "QUOTA_EXCEEDED_ERR";
                    break;
                case FileError.NOT_FOUND_ERR:
                    msg = "NOT_FOUND_ERR";
                    break;
                case FileError.SECURITY_ERR:
                    msg = "SECURITY_ERR";
                    break;
                case FileError.INVALID_MODIFICATION_ERR:
                    msg = "INVALID_MODIFICATION_ERR";
                    break;
                case FileError.INVALID_STATE_ERR:
                    msg = "INVALID_STATE_ERR";
                    break;
                default:
                    msg = "Unknown Error";
                    break;
            }
        }

        function initEverything() {
            Action.init();
            Document.init();
            DocumentField.init();
            DocumentType.init();
            LinkCategory.init();
            Mandatory.init();
            Status.init();
            TargetGroup.init();
            Topic.init();
            Heading.init();
            ContactAddress.init();
        }

        function clearEverything() {
            Action.clear();
            Document.clear();
            DocumentField.clear();
            DocumentType.clear();
            LinkCategory.clear();
            Mandatory.clear();
            Status.clear();
{'fraction_non_alphanumeric': 0.09079402582060586, 'fraction_numerical': 0.0, 'mean_word_length': 1.1175629801679472, 'pattern_counts': {'":': 0, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3597327', 'n_tokens_mistral': 2705, 'n_tokens_neox': 2531, 'n_words': 431}