<commit_before>'use strict';

function EntityManager() {
    this.__init__();
}

EntityManager.prototype = {

    // The user entities.
    entities        : null,

    // POST calls.
    post_delete_entity        : null,
    post_save_entity          : null,
    post_load_user_entities   : null,

    // State booleans.
    user_entities_loaded   : null,
    loading                : null,

    // TODO : Refactor
    delete_all_children_of_entity_that_do_not_have_other_parents: function(parent_entity) {
        for (var c = parent_entity.children.length; c--;) {

            var child_entity = parent_entity.children[c];

            // No matter what the parent reference is getting removed for all children entity.
            child_entity.remove_parent(parent_entity);

            // If the child entity had no other parents then remove it as well.
            // TODO : Eventually also check for any outside references. Perhaps don't enable delete if there are external references.
            if (child_entity.number_of_parents() === 0) {
                this.delete_entity(child_entity);
            }
        }
    },

    entity_deleted_response: function(data) {
        // FOR_DEV_START
        if (data === SERVER_REPLY_GENERIC_YES) {
            l('Entity deleted!');
        } else {
            l('Entity did not get deleted : {|' + data + '|}');
        }
        // FOR_DEV_END
    },

    delete_entity: function(entity) {
        var entity_to_delete = null;
        var index_to_splice = this._get_index_of_entity(entity);
        if (index_to_splice !== NOT_FOUND) {
            entity_to_delete = this.entities[index_to_splice];
        } else {
            raise_exception('Entity to delete was not found!');
        }

        if (entity_to_delete !== null) {
            // TODO : Delete all child entities from the ENTITY_MANAGER if they don't have other parent entities.
            this.delete_all_children_of_entity_that_do_not_have_other_parents(entity_to_delete);

            // TODO : Make sure the server does the same deletion steps that the client does.
            var data = {};
            data[ENTITY_PROPERTY_USERNAME] = ENTITY_OWNER.get_username();
            data[ENTITY_PROPERTY_PASSWORD] = ENTITY_OWNER.get_password();
            data[ENTITY_DEFAULT_PROPERTY_RELATIVE_ID] = entity_to_delete.get_relative_id();
            this.post_delete_entity.perform_post(data, this.entity_deleted_response.bind(this));
        }

        if (index_to_splice !== NOT_FOUND) {
            // Re-calculate the index to splice off as child entities may have been removed thus changing the list order/indexes.
            this.entities.splice(this._get_index_of_entity(entity), 1);
        }
    },

    __init__: function() {
        this.entities = [];
        this.user_entities_loaded = false;
        this.loading = false;

        this.post_delete_entity        = new PostHelper(POST_URL_DELETE_ENTITY);
        this.post_save_entity          = new PostHelper(POST_URL_SAVE_ENTITY);
        this.post_load_user_entities   = new PostHelper(POST_URL_GET_USER_ENTITIES);
    },

    /*        __        __          __      __       ___
        |    /  \  /\  |  \ | |\ | / _`    |  \  /\   |   /\     .
        |___ \__/ /~~\ |__/ | | \| \__>    |__/ /~~\  |  /~~\    .*/

    all_user_entities_loaded: function(data) {
        // FOR_DEV_START
        //l('Got the following data for user entities');
        //l(data);
        //l(typeof(data));
        // FOR_DEV_END
        if (is_string(data)) {
            //l('Data is a string!');
            if (data === '{}') {
                this.user_entities_loaded = true;
                this.all_data_loaded();
                return;
            }

            data = JSON.parse(data);
        }

        for (var entity_id_num in data) {
            if (data.hasOwnProperty(entity_id_num)) {
                MANAGER_ENTITY.add_user_entity_from_entity_data(data[entity_id_num]);
            }
        }

        this.user_entities_loaded = true;
        ENTITY_OWNER.set_owner_entity(this.get_owner_entity());
        this.all_data_loaded();
    },

    set_all_entities_to_not_needing_to_be_saved: function() {
        for (var e = 0; e < this.entities.length; e++) {
            this.entities[e].needs_to_be_saved = false;
        }
    },

    all_data_loaded: function() {
        this.link_entities();
        this.set_all_entities_to_not_needing_to_be_saved();
        this.loading = false;
    },

    load_data: function() {
        this.loading = true;
        var data = {};
        data[ENTITY_PROPERTY_USERNAME] = ENTITY_OWNER.get_username();
        data[ENTITY_PROPERTY_PASSWORD] = ENTITY_OWNER.get_password();<commit_msg>general updating 02<commit_after>'use strict';

function EntityManager() {
    this.__init__();
}

EntityManager.prototype = {

    // The user entities.
    entities        : null,

    // POST calls.
    post_delete_entity        : null,
    post_save_entity          : null,
    post_load_user_entities   : null,

    // State booleans.
    user_entities_loaded   : null,
    loading                : null,

    // TODO : Refactor
    delete_all_children_of_entity_that_do_not_have_other_parents: function(parent_entity) {
        for (var c = parent_entity.children.length; c--;) {

            var child_entity = parent_entity.children[c];

            // No matter what the parent reference is getting removed for all children entity.
            child_entity.remove_parent(parent_entity);

            // If the child entity had no other parents then remove it as well.
            // TODO : Eventually also check for any outside references. Perhaps don't enable delete if there are external references.
            if (child_entity.number_of_parents() === 0) {
                this.delete_entity(child_entity);
            }
        }
    },

    entity_deleted_response: function(data) {
        // FOR_DEV_START
        if (data === SERVER_REPLY_GENERIC_YES) {
            l('Entity deleted!');
        } else {
            l('Entity did not get deleted : {|' + data + '|}');
        }
        // FOR_DEV_END
    },

    delete_entity: function(entity) {
        var entity_to_delete = null;
        var index_to_splice = this._get_index_of_entity(entity);
        if (index_to_splice !== NOT_FOUND) {
            entity_to_delete = this.entities[index_to_splice];
        } else {
            raise_exception('Entity to delete was not found!');
        }

        if (entity_to_delete !== null) {
            // TODO : Delete all child entities from the ENTITY_MANAGER if they don't have other parent entities.
            this.delete_all_children_of_entity_that_do_not_have_other_parents(entity_to_delete);

            // TODO : Make sure the server does the same deletion steps that the client does.
            var data = {};
            data[ENTITY_PROPERTY_USERNAME] = ENTITY_OWNER.get_username();
            data[ENTITY_PROPERTY_PASSWORD] = <PASSWORD>();
            data[ENTITY_DEFAULT_PROPERTY_RELATIVE_ID] = entity_to_delete.get_relative_id();
            this.post_delete_entity.perform_post(data, this.entity_deleted_response.bind(this));
        }

        if (index_to_splice !== NOT_FOUND) {
            // Re-calculate the index to splice off as child entities may have been removed thus changing the list order/indexes.
            this.entities.splice(this._get_index_of_entity(entity), 1);
        }
    },

    __init__: function() {
        this.entities = [];
        this.user_entities_loaded = false;
        this.loading = false;

        this.post_delete_entity        = new PostHelper(POST_URL_DELETE_ENTITY);
        this.post_save_entity          = new PostHelper(POST_URL_SAVE_ENTITY);
        this.post_load_user_entities   = new PostHelper(POST_URL_GET_USER_ENTITIES);
    },

    /*        __        __          __      __       ___
        |    /  \  /\  |  \ | |\ | / _`    |  \  /\   |   /\     .
        |___ \__/ /~~\ |__/ | | \| \__>    |__/ /~~\  |  /~~\    .*/

    all_user_entities_loaded: function(data) {
        // FOR_DEV_START
        //l('Got the following data for user entities');
        //l(data);
        //l(typeof(data));
        // FOR_DEV_END
        if (is_string(data)) {
            //l('Data is a string!');
            if (data === '{}') {
                this.user_entities_loaded = true;
                this.all_data_loaded();
                return;
            }

            data = JSON.parse(data);
        }

        data = eval(data);

        for (var entity_id_num in data) {
            if (data.hasOwnProperty(entity_id_num)) {
                MANAGER_ENTITY.add_user_entity_from_entity_data(data[entity_id_num]);
            }
        }

        this.user_entities_loaded = true;
        ENTITY_OWNER.set_owner_entity(this.get_owner_entity());
        this.all_data_loaded();
    },

    set_all_entities_to_not_needing_to_be_saved: function() {
        for (var e = 0; e < this.entities.length; e++) {
            this.entities[e].needs_to_be_saved = false;
        }
    },

    all_data_loaded: function() {
        this.link_entities();
        this.set_all_entities_to_not_needing_to_be_saved();
        this.loading = false;
    },

    load_data: function() {
        this.loading = true;
        var data = {};
        data[ENTITY_PROPERTY_USERNAME] = ENTITY_OWNER.get_username();
        data[ENTITY_PROPERTY_PASSWORD] = ENTITY_OWNER.get_password();
{'fraction_non_alphanumeric': 0.1037467018469657, 'fraction_numerical': 0.0008443271767810027, 'mean_word_length': 2.0082539682539684, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1467487', 'n_tokens_mistral': 2906, 'n_tokens_neox': 2851, 'n_words': 541}