<commit_before>'use strict';

angular.module('arachne.controllers')

/**
 * Handles the layout for viewing and editing a catalog.
 *
 * @author: <NAME>
 * @author: <NAME>
 * @author: <NAME>
 */
    .controller('CatalogController', ['$rootScope', '$scope', '$state', '$stateParams', '$uibModal', '$window', '$timeout',
        'Catalog', 'CatalogEntry', 'authService', '$http', 'arachneSettings', 'Entity', '$location', 'messageService',
        function ($rootScope, $scope, $state, $stateParams, $uibModal, $window, $timeout,
                  Catalog, CatalogEntry, authService, $http, arachneSettings, Entity, $location, messages) {

            $rootScope.tinyFooter = true;
            $scope.entryMap = {};
            $scope.catalogId = $stateParams.id;
            $scope.childrenLimit = 20;
            $scope.showMoreThumbnailsCount = 0;
            $scope.editable = false;
            $scope.error = false;

            $scope.showThumbnails = false;
            $scope.cells = [];
            $scope.cells.length = 0;

            $scope.removeItems = false;
            $scope.itemsToRemove = [];

            $scope.toAdd = [];

            if ($stateParams.view === 'map') $scope.map = true;

            $scope.treeOptions = {
                beforeDrop: function (event) {
                    var movedEntry = $scope.entryMap[event.source.nodeScope.$modelValue.id];
                    var tempEntry = angular.copy(movedEntry);
                    var newParentId;
                    if (event.dest.nodesScope.$parent.$modelValue) {
                        newParentId = event.dest.nodesScope.$parent.$modelValue.id;
                    } else {
                        newParentId = $scope.catalog.root.id;
                    }
                    tempEntry.parentId = newParentId;
                    tempEntry.indexParent = event.dest.index;
                    if (tempEntry.indexParent !== movedEntry.indexParent || tempEntry.parentId !== movedEntry.parentId) {
                        var promise = CatalogEntry.update({id: movedEntry.id}, tempEntry).$promise.then(function () {
                            if (movedEntry.parentId !== newParentId) {
                                $scope.entryMap[movedEntry.parentId].totalChildren -= 1;
                                $scope.entryMap[newParentId].totalChildren += 1;
                            }
                            movedEntry.parentId = newParentId;
                            movedEntry.indexParent = getIndexParent(movedEntry);
                        }, function () {
                            messages.add('default');
                        });
                        return promise;
                    } else {
                        return true;
                    }
                }
            };

            $scope.addChild = function (scope, entry) {

                if (!entry.children) entry.children = [];
                var editEntryModal = $uibModal.open({
                    templateUrl: 'app/catalog/edit-entry.html',
                    backdrop: 'static'
                });

                editEntryModal.close = function (newEntry, entity) {
                    var time = 0;
                    $scope.toAdd = [];
                    if(newEntry instanceof Array && entity === null) { //Multiple entries are added
                        //Iterate through the array of objects which should be added
                        $scope.toAdd = new Array(newEntry.length);
                        for(var i = 0; i < newEntry.length; i++) {
                            $scope.toAdd[i] = {};
                            if(newEntry[i].index === undefined) { //Array contains entity ids
                                var temp = {};
                                Entity.get({id: parseInt(newEntry[i])}, function (entity) { //Try to find the entity. If found, add it to the catalog
                                    if (entity) {
                                        temp.arachneEntityId = entity.entityId;
                                        temp.label = entity.title;
                                        temp.parentId = entry.id;
                                        temp.indexParent = entry.children.length;
                                    }
                                }, function () {
                                    messages.add('default');
                                });
                                time += 100;
                            }
                            else { //Array contains entries with labels and entities attached to them
                                if (newEntry[i].entity)
                                    $scope.toAdd[i].arachneEntityId = newEntry[i].entity.entityId;
                                else
                                    $scope.toAdd[i].arachneEntityId = null;
                                // Use associated entity title as label if label is not set
                                if (!newEntry[i].label && newEntry[i].entity.title)
                                    $scope.toAdd[i].label = newEntry[i].entity.title;
                                else
                                    $scope.toAdd[i].label = newEntry[i].label;
                                $scope.toAdd[i].parentId = entry.id;
                                $scope.toAdd[i].indexParent = entry.children.length;
                            }
                        }
                    }
                    else { //Only a singular entry is added
                        if (entity) {
                            newEntry.arachneEntityId = entity.entityId;
                        } else {
                            newEntry.arachneEntityId = null;
                        }

                        // Use associated entity title as label if label is not set
                        if (!newEntry.label && entity.title) {
                            newEntry.label = entity.title;
                        }

                        newEntry.parentId = entry.id;
                        newEntry.indexParent = entry.children.length;
                        $scope.toAdd.push(newEntry)
                    }
                    setTimeout(function () {
                        if($scope.toAdd.length > 0) {
                            CatalogEntry.save({}, $scope.toAdd, function (result) {
                                for (var i = 0; i < result.length; i++) {
                                    entry.children.push(result[i]);
                                    entry.totalChildren += 1;
                                    initialize(result[i]);
                                    if (scope && scope.collapsed) {
                                        $scope.toggleNode(scope, entry);
                                    }
                                }
                            }, function (err) {
                                console.error("Error when creating catalog entry!", err);
                                messages.add('default');
                            })
                        }
                        editEntryModal.dismiss();
                    }, time);
                }
            };

            $scope.duplicateChild = function (scope, entry) {
                if (entry.children.length > 0) {
                    return;
                }
                var editableEntry = angular.copy(entry);
                var editEntryModal = $uibModal.open({
                    templateUrl: 'app/catalog/edit-entry.html',
                    controller: 'EditCatalogEntryController',
                    resolve: {
                        entry: function () {
                            return editableEntry
                        }
                    },
                    backdrop: 'static'
                });
                editEntryModal.close = function (editedEntry, entity) {
                    var newEntry = {};
                    angular.copy(editedEntry, newEntry);

                    if (entity) {
                        newEntry.arachneEntityId = entity.entityId;
                    } else {
                        newEntry.arachneEntityId = null;
                    }
                    // Use associated entity title as label if label is not set
                    if (!newEntry.label && entity.title) {
                        newEntry.label = entity.title;
                    }
                    newEntry.parentId = entry.id;
                    newEntry.indexParent = entry.children.length;

                    CatalogEntry.save({}, [newEntry], function (result) {
                        for (var i = 0; i < result.length; i++) {
                            entry.children.push(result[i]);
                            entry.totalChildren += 1;
                            initialize(result[i]);
                            if (scope && scope.collapsed) {
                                $scope.toggleNode(scope, entry);
                            }
                        }
                    }, function (err) {
                        console.error("Error when creating catalog entry!", err);
                        messages.add('default');<commit_msg>completes implementation of "Import multiple entries via entity ID to catalog" - feature<commit_after>'use strict';

angular.module('arachne.controllers')

/**
 * Handles the layout for viewing and editing a catalog.
 *
 * @author: <NAME>
 * @author: <NAME>
 * @author: <NAME>
 */
    .controller('CatalogController', ['$rootScope', '$scope', '$state', '$stateParams', '$uibModal', '$window', '$timeout',
        'Catalog', 'CatalogEntry', 'authService', '$http', 'arachneSettings', 'Entity', '$location', 'messageService',
        function ($rootScope, $scope, $state, $stateParams, $uibModal, $window, $timeout,
                  Catalog, CatalogEntry, authService, $http, arachneSettings, Entity, $location, messages) {

            $rootScope.tinyFooter = true;
            $scope.entryMap = {};
            $scope.catalogId = $stateParams.id;
            $scope.childrenLimit = 20;
            $scope.showMoreThumbnailsCount = 0;
            $scope.editable = false;
            $scope.error = false;

            $scope.showThumbnails = false;
            $scope.cells = [];
            $scope.cells.length = 0;

            $scope.removeItems = false;
            $scope.itemsToRemove = [];

            if ($stateParams.view === 'map') $scope.map = true;

            $scope.treeOptions = {
                beforeDrop: function (event) {
                    var movedEntry = $scope.entryMap[event.source.nodeScope.$modelValue.id];
                    var tempEntry = angular.copy(movedEntry);
                    var newParentId;
                    if (event.dest.nodesScope.$parent.$modelValue) {
                        newParentId = event.dest.nodesScope.$parent.$modelValue.id;
                    } else {
                        newParentId = $scope.catalog.root.id;
                    }
                    tempEntry.parentId = newParentId;
                    tempEntry.indexParent = event.dest.index;
                    if (tempEntry.indexParent !== movedEntry.indexParent || tempEntry.parentId !== movedEntry.parentId) {
                        return CatalogEntry.update({id: movedEntry.id}, tempEntry).$promise.then(function () {
                            if (movedEntry.parentId !== newParentId) {
                                $scope.entryMap[movedEntry.parentId].totalChildren -= 1;
                                $scope.entryMap[newParentId].totalChildren += 1;
                            }
                            movedEntry.parentId = newParentId;
                            movedEntry.indexParent = getIndexParent(movedEntry);
                        }, function () {
                            messages.add('default');
                        });
                    } else {
                        return true;
                    }
                }
            };

            $scope.addChild = function (scope, entry) {

                if (!entry.children) entry.children = [];
                var editEntryModal = $uibModal.open({
                    templateUrl: 'app/catalog/edit-entry.html',
                    backdrop: 'static'
                });

                editEntryModal.close = function (newEntry, entity) {

                    var toAddPromises = [];
                    var entriesToAdd = [];

                    if(newEntry instanceof Array && entity === null) { //Multiple entries are added
                        //Iterate through the array of objects which should be added
                        for(var i = 0; i < newEntry.length; i++) {

                            if(newEntry[i].index === undefined) { //Array contains entity ids
                                var id = parseInt(newEntry[i]);
                                toAddPromises.push(new Promise(function(resolve, reject) {
                                    Entity.get({id: id}, function (entity) { //Try to find the entity. If found, add it to the catalog
                                        if (entity) {
                                            entriesToAdd.push({
                                                arachneEntityId: entity.entityId,
                                                label: entity.title,
                                                parentId: entry.id,
                                                indexParent: entry.children.length
                                            });
                                        }
                                        resolve();
                                    }, function () {
                                        messages.add('default');
                                        resolve();
                                    });
                                }));

                            } else { //Array contains entries with labels and entities attached to them
                                console.log("HA", newEntry);
                                var toAddItem = {};
                                if (newEntry[i].entity) {
                                    toAddItem.arachneEntityId = newEntry[i].entity.entityId;
                                } else {
                                    toAddItem.arachneEntityId = null;
                                }
                                // Use associated entity title as label if label is not set
                                if (!newEntry[i].label && newEntry[i].entity.title) {
                                    toAddItem.label = newEntry[i].entity.title;
                                } else {
                                    toAddItem.label = newEntry[i].label;
                                }
                                toAddItem.parentId = entry.id;
                                toAddItem.indexParent = entry.children.length;

                                entriesToAdd.push(toAddItem);
                                toAddPromises.push(true);

                            }

                        }
                    } else { //Only a singular entry is added

                        if (entity) {
                            newEntry.arachneEntityId = entity.entityId;
                        } else {
                            newEntry.arachneEntityId = null;
                        }

                        // Use associated entity title as label if label is not set
                        if (!newEntry.label && entity.title) {
                            newEntry.label = entity.title;
                        }

                        newEntry.parentId = entry.id;
                        newEntry.indexParent = entry.children.length;

                        entriesToAdd.push(newEntry);
                        toAddPromises.push(true);

                    }

                    Promise.all(toAddPromises).then(function() {
                        if(!entriesToAdd.length) {
                            return
                        }
                        CatalogEntry.save({}, entriesToAdd, function(result) {
                            for (var i = 0; i < result.length; i++) {
                                entry.children.push(result[i]);
                                entry.totalChildren += 1;
                                initialize(result[i]);
                                if (scope && scope.collapsed) {
                                    $scope.toggleNode(scope, entry);
                                }
                            }
                        }, function (err) {
                            console.error("Error when creating catalog entry!", err);
                            messages.add('default');
                        });
                        editEntryModal.dismiss();
                    });

                }
            };

            $scope.duplicateChild = function (scope, entry) {
                if (entry.children.length > 0) {
                    return;
                }
                var editableEntry = angular.copy(entry);
                var editEntryModal = $uibModal.open({
                    templateUrl: 'app/catalog/edit-entry.html',
                    controller: 'EditCatalogEntryController',
                    resolve: {
                        entry: function () {
                            return editableEntry
                        }
                    },
                    backdrop: 'static'
                });
                editEntryModal.close = function (editedEntry, entity) {
                    var newEntry = {};
                    angular.copy(editedEntry, newEntry);

                    if (entity) {
                        newEntry.arachneEntityId = entity.entityId;
                    } else {
                        newEntry.arachneEntityId = null;
                    }
                    // Use associated entity title as label if label is not set
                    if (!newEntry.label && entity.title) {
                        newEntry.label = entity.title;
                    }
                    newEntry.parentId = entry.id;
                    newEntry.indexParent = entry.children.length;

                    CatalogEntry.save({}, [newEntry], function (result) {
                        for (var i = 0; i < result.length; i++) {
                            entry.children.push(result[i]);
                            entry.totalChildren += 1;
                            initialize(result[i]);
                            if (scope && scope.collapsed) {
                                console.log("!!!!!");
                                $scope.toggleNode(scope, entry);
                            }
                        }
                    }, function (err) {
                        console.error("Error when creating catalog entry!", err);
                        messages.add('default');
{'fraction_non_alphanumeric': 0.09467766510797036, 'fraction_numerical': 0.0015236694162769925, 'mean_word_length': 0.9516046344714447, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5033698', 'n_tokens_mistral': 4284, 'n_tokens_neox': 3978, 'n_words': 845}