/*
 *  Copyright 2015 Anyware Services
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * The Thesaurus DAO.
 */
 Ext.define('Ametys.plugins.thesaurus.ThesaurusDAO', {
    singleton: true,
    
    constructor: function(config)
    {
        /**
         * @callable
         * @member Ametys.plugins.thesaurus.ThesaurusDAO
         * @method createThesaurus 
         * Create a new thesaurus
         * This calls the method 'createThesaurus' of the server DAO 'org.ametys.plugins.thesaurus.ThesaurusDAO'.
         * @param {Object[]} parameters The parameters to transmit to the server method
         * @param {String} parameters.label The name of thesaurus to create
         * @param {Function} callback The function to call when the java process is over. Use options.scope for the scope.
         * @param {Boolean} callback.success True if the creation successed.   
         * @param {Object} callback.id The id of created thesaurus. Null on error (please note that when an error occured, the callback may not be called depending on the value of errorMessage).
         * @param {Object} callback.label The label of created thesaurus.              
         * @param {Object} [options] Advanced options for the call.
         * @param {Boolean/String/Object} [options.errorMessage] Display an error message. See Ametys.data.ServerComm#callMethod errorMessage.
         * @param {Boolean/String/Object} [options.waitMessage] Display a waiting message. See Ametys.data.ServerComm#callMethod waitMessage.
         * @param {Number} [options.scope] This parameter is the scope used to call the callback. Moreover is the given class is a mixin of Ametys.data.ServerCaller, its methods Ametys.data.ServerCaller#beforeServerCall and Ametys.data.ServerCaller#afterServerCall will be used so see their documentation to look for additional options (such a refreshing on Ametys.ribbon.element.ui.ButtonController#beforeServerCall).
         * @param {Number} [options.priority] The message priority. See Ametys.data.ServerComm#callMethod for more information on the priority. PRIORITY_SYNCHRONOUS cannot be used here.
         * @param {String} [options.cancelCode] Cancel similar unachieved read operations. See Ametys.data.ServerComm#callMethod cancelCode.
         * @param {Object} [options.arguments] Additional arguments set in the callback.arguments parameter.                  
         * @param {Boolean} [options.ignoreCallbackOnError] If the server throws an exception, should the callback beeing called with a null parameter. See Ametys.data.ServerComm#callMethod ignoreOnError.
         */
        this.addCallables(
            {
                role: "org.ametys.plugins.thesaurus.ThesaurusDAO",
                methodName: "createThesaurus",
                callback: {
                    handler: this._createThesaurusCb,
                    scope: this
                },
                waitMessage: true,
                errorMessage: {
                    msg: "{{i18n PLUGINS_THESAURUS_DAO_CREATE_THESAURUS_ERROR}}",
                    category: Ext.getClassName(this)
                }
            }
        );
        
        /**
         * @callable
         * @member Ametys.plugins.thesaurus.ThesaurusDAO
         * @method updateThesaurus 
         * Rename a thesaurus
         * This calls the method 'updateThesaurus' of the server DAO 'org.ametys.plugins.thesaurus.ThesaurusDAO'.
         * @param {Object[]} parameters The parameters to transmit to the server method
         * @param {String} parameters.id The id of thesaurus to renamed
         * @param {String} parameters.label The new name of thesaurus
         * @param {Function} callback The function to call when the java process is over. Use options.scope for the scope. 
         * @param {Object} callback.id The id of updated thesaurus. Null on error (please note that when an error occured, the callback may not be called depending on the value of errorMessage).
         * @param {Object} callback.label The label of updated thesaurus.              
         * @param {Object} [options] Advanced options for the call.
         * @param {Boolean/String/Object} [options.errorMessage] Display an error message. See Ametys.data.ServerComm#callMethod errorMessage.
         * @param {Boolean/String/Object} [options.waitMessage] Display a waiting message. See Ametys.data.ServerComm#callMethod waitMessage.
         * @param {Number} [options.scope] This parameter is the scope used to call the callback. Moreover is the given class is a mixin of Ametys.data.ServerCaller, its methods Ametys.data.ServerCaller#beforeServerCall and Ametys.data.ServerCaller#afterServerCall will be used so see their documentation to look for additional options (such a refreshing on Ametys.ribbon.element.ui.ButtonController#beforeServerCall).
         * @param {Number} [options.priority] The message priority. See Ametys.data.ServerComm#callMethod for more information on the priority. PRIORITY_SYNCHRONOUS cannot be used here.
         * @param {String} [options.cancelCode] Cancel similar unachieved read operations. See Ametys.data.ServerComm#callMethod cancelCode.
         * @param {Object} [options.arguments] Additional arguments set in the callback.arguments parameter.                  
         * @param {Boolean} [options.ignoreCallbackOnError] If the server throws an exception, should the callback beeing called with a null parameter. See Ametys.data.ServerComm#callMethod ignoreOnError.
         */
        this.addCallables(
            {
                role: "org.ametys.plugins.thesaurus.ThesaurusDAO",
                methodName: "updateThesaurus",
                callback: {
                    handler: this._updateThesaurusCb,
                    scope: this
                },
                waitMessage: true,
                errorMessage: {
                    msg: "{{i18n PLUGINS_THESAURUS_DAO_UPDATE_THESAURUS_ERROR}}",
                    category: Ext.getClassName(this)
                }
            }
        );
        
        /**
         * @callable
         * @member Ametys.plugins.thesaurus.ThesaurusDAO
         * @method deleteThesaurus 
         * Delete a thesaurus
         * This calls the method 'deleteThesaurus' of the server DAO 'org.ametys.plugins.thesaurus.ThesaurusDAO'.
         * @param {Object[]} parameters The parameters to transmit to the server method
         * @param {String} parameters.id The id of thesaurus to delete
         * @param {Ametys.message.MessageTarget} parameters.target The message target corresponding to the thesaurus to delete. It is not transmitted to the server, but is needed for sending a deletion message on the bus.
         * @param {Function} callback The function to call when the java process is over. Use options.scope for the scope. 
         * @param {Object} callback.id The id of deleted thesaurus. Null on error (please note that when an error occured, the callback may not be called depending on the value of errorMessage).
         * @param {Boolean} callback.hasReferences true if thesaurus is still referenced.            
         * @param {Object} [options] Advanced options for the call.
         * @param {Boolean/String/Object} [options.errorMessage] Display an error message. See Ametys.data.ServerComm#callMethod errorMessage.
         * @param {Boolean/String/Object} [options.waitMessage] Display a waiting message. See Ametys.data.ServerComm#callMethod waitMessage.
         * @param {Number} [options.scope] This parameter is the scope used to call the callback. Moreover is the given class is a mixin of Ametys.data.ServerCaller, its methods Ametys.data.ServerCaller#beforeServerCall and Ametys.data.ServerCaller#afterServerCall will be used so see their documentation to look for additional options (such a refreshing on Ametys.ribbon.element.ui.ButtonController#beforeServerCall).
         * @param {Number} [options.priority] The message priority. See Ametys.data.ServerComm#callMethod for more information on the priority. PRIORITY_SYNCHRONOUS cannot be used here.
         * @param {String} [options.cancelCode] Cancel similar unachieved read operations. See Ametys.data.ServerComm#callMethod cancelCode.
         * @param {Object} [options.arguments] Additional arguments set in the callback.arguments parameter.                  
         * @param {Boolean} [options.ignoreCallbackOnError] If the server throws an exception, should the callback beeing called with a null parameter. See Ametys.data.ServerComm#callMethod ignoreOnError.
         */
        this.addCallables(
            {
                role: "org.ametys.plugins.thesaurus.ThesaurusDAO",
                methodName: "deleteThesaurus",
                localParamsIndex: 1,
                callback: {
                    handler: this._deleteThesaurusCb,
                    scope: this
                },
                waitMessage: true,
                errorMessage: {
                    msg: "{{i18n PLUGINS_THESAURUS_DAO_DELETE_THESAURUS_ERROR}}",
                    category: Ext.getClassName(this)
                }
            }
        );
        
        /**
         * @callable
         * @member Ametys.plugins.thesaurus.ThesaurusDAO
         * @method createMicrothesaurus 
         * Create a new microthesaurus
         * This calls the method 'createMicrothesaurus' of the server DAO 'org.ametys.plugins.thesaurus.ThesaurusDAO'.
         * @param {Object[]} parameters The parameters to transmit to the server method
         * @param {String} parameters.label The name of microthesaurus to create
         * @param {String} parameters.id The id of parent thesaurus
         * @param {Function} callback The function to call when the java process is over. Use options.scope for the scope. 
         *  @param {Boolean} callback.success True if the creation successed.   
         * @param {Object} callback.id The id of created microthesaurus. Null on error (please note that when an error occured, the callback may not be called depending on the value of errorMessage).
         * @param {Object} callback.label The label of created microthesaurus. 
         * @param {Object} callback.thesaurusId The id of parent thesaurus           
         * @param {Object} [options] Advanced options for the call.
         * @param {Boolean/String/Object} [options.errorMessage] Display an error message. See Ametys.data.ServerComm#callMethod errorMessage.
         * @param {Boolean/String/Object} [options.waitMessage] Display a waiting message. See Ametys.data.ServerComm#callMethod waitMessage.
         * @param {Number} [options.scope] This parameter is the scope used to call the callback. Moreover is the given class is a mixin of Ametys.data.ServerCaller, its methods Ametys.data.ServerCaller#beforeServerCall and Ametys.data.ServerCaller#afterServerCall will be used so see their documentation to look for additional options (such a refreshing on Ametys.ribbon.element.ui.ButtonController#beforeServerCall).
         * @param {Number} [options.priority] The message priority. See Ametys.data.ServerComm#callMethod for more information on the priority. PRIORITY_SYNCHRONOUS cannot be used here.
         * @param {String} [options.cancelCode] Cancel similar unachieved read operations. See Ametys.data.ServerComm#callMethod cancelCode.
         * @param {Object} [options.arguments] Additional arguments set in the callback.arguments parameter.                  
         * @param {Boolean} [options.ignoreCallbackOnError] If the server throws an exception, should the callback beeing called with a null parameter. See Ametys.data.ServerComm#callMethod ignoreOnError.
         */
        this.addCallables(
            {
                role: "org.ametys.plugins.thesaurus.ThesaurusDAO",
                methodName: "createMicrothesaurus",
                callback: {
                    handler: this._createMicrothesaurusCb,
                    scope: this
                },
                waitMessage: true,
                errorMessage: {
                    msg: "{{i18n PLUGINS_THESAURUS_DAO_CREATE_MICROTHESAURUS_ERROR}}",
                    category: Ext.getClassName(this)
                }
            }
        );
        
        /**
         * @callable
         * @member Ametys.plugins.thesaurus.ThesaurusDAO
         * @method updateMicrothesaurus 
         * Rename a microthesaurus
         * This calls the method 'updateMicrothesaurus' of the server DAO 'org.ametys.plugins.thesaurus.ThesaurusDAO'.
         * @param {Object[]} parameters The parameters to transmit to the server method
         * @param {String} parameters.id The id of microthesaurus
         * @param {String} parameters.label The name of microthesaurus to create
         * @param {Function} callback The function to call when the java process is over. Use options.scope for the scope. 
         * @param {Object} callback.id The id of update microthesaurus. Null on error (please note that when an error occured, the callback may not be called depending on the value of errorMessage).
         * @param {Object} callback.label The label of created microthesaurus. 
         * @param {Object} callback.thesaurusId The id of parent thesaurus           
         * @param {Object} [options] Advanced options for the call.
         * @param {Boolean/String/Object} [options.errorMessage] Display an error message. See Ametys.data.ServerComm#callMethod errorMessage.
         * @param {Boolean/String/Object} [options.waitMessage] Display a waiting message. See Ametys.data.ServerComm#callMethod waitMessage.
         * @param {Number} [options.scope] This parameter is the scope used to call the callback. Moreover is the given class is a mixin of Ametys.data.ServerCaller, its methods Ametys.data.ServerCaller#beforeServerCall and Ametys.data.ServerCaller#afterServerCall will be used so see their documentation to look for additional options (such a refreshing on Ametys.ribbon.element.ui.ButtonController#beforeServerCall).
         * @param {Number} [options.priority] The message priority. See Ametys.data.ServerComm#callMethod for more information on the priority. PRIORITY_SYNCHRONOUS cannot be used here.
         * @param {String} [options.cancelCode] Cancel similar unachieved read operations. See Ametys.data.ServerComm#callMethod cancelCode.
         * @param {Object} [options.arguments] Additional arguments set in the callback.arguments parameter.                  
         * @param {Boolean} [options.ignoreCallbackOnError] If the server throws an exception, should the callback beeing called with a null parameter. See Ametys.data.ServerComm#callMethod ignoreOnError.
         */
        this.addCallables(
            {
                role: "org.ametys.plugins.thesaurus.ThesaurusDAO",
                methodName: "updateMicrothesaurus",
                callback: {
                    handler: this._updateMicrothesaurusCb,
                    scope: this
                },
                waitMessage: true,
                errorMessage: {
                    msg: "{{i18n PLUGINS_THESAURUS_DAO_UPDATE_MICROTHESAURUS_ERROR}}",
                    category: Ext.getClassName(this)
                }
            }
        );
        
        /**
         * @callable
         * @member Ametys.plugins.thesaurus.ThesaurusDAO
         * @method deleteMicrothesaurus 
         * Delete a microthesaurus
         * This calls the method 'deleteMicrothesaurus' of the server DAO 'org.ametys.plugins.thesaurus.ThesaurusDAO'.
         * @param {Object[]} parameters The parameters to transmit to the server method
         * @param {String} parameters.id The id of microthesaurus to delete
         * @param {Function} callback The function to call when the java process is over. Use options.scope for the scope. 
         * @param {Object} [options] Advanced options for the call.
         * @param {Boolean/String/Object} [options.errorMessage] Display an error message. See Ametys.data.ServerComm#callMethod errorMessage.
         * @param {Boolean/String/Object} [options.waitMessage] Display a waiting message. See Ametys.data.ServerComm#callMethod waitMessage.
         * @param {Number} [options.scope] This parameter is the scope used to call the callback. Moreover is the given class is a mixin of Ametys.data.ServerCaller, its methods Ametys.data.ServerCaller#beforeServerCall and Ametys.data.ServerCaller#afterServerCall will be used so see their documentation to look for additional options (such a refreshing on Ametys.ribbon.element.ui.ButtonController#beforeServerCall).
         * @param {Number} [options.priority] The message priority. See Ametys.data.ServerComm#callMethod for more information on the priority. PRIORITY_SYNCHRONOUS cannot be used here.
         * @param {String} [options.cancelCode] Cancel similar unachieved read operations. See Ametys.data.ServerComm#callMethod cancelCode.
         * @param {Object} [options.arguments] Additional arguments set in the callback.arguments parameter.                  
         * @param {Boolean} [options.ignoreCallbackOnError] If the server throws an exception, should the callback beeing called with a null parameter. See Ametys.data.ServerComm#callMethod ignoreOnError.
         */
        this.addCallables(
            {
                role: "org.ametys.plugins.thesaurus.ThesaurusDAO",
                methodName: "deleteMicrothesaurus",
                callback: {
                    handler: this._deleteMicrothesaurusCb,
                    scope: this
                },
                waitMessage: true,
                errorMessage: {
                    msg: "{{i18n PLUGINS_THESAURUS_DAO_DELETE_MICROTHESAURUS_ERROR}}",
                    category: Ext.getClassName(this)
                },
                localParamsIndex: 1
            }
        );
        
        /**
         * @callable
         * @member Ametys.plugins.thesaurus.ThesaurusDAO
         * @method getThesaurusLabel 
         * Get the label of a thesaurus
         * This calls the method 'getThesaurusLabel' of the server DAO 'org.ametys.plugins.thesaurus.ThesaurusDAO'.
         * @param {Object[]} parameters The parameters to transmit to the server method
         * @param {String} parameters.id The id of thesaurus
         * @param {Function} callback The function to call when the java process is over. Use options.scope for the scope.
         * @param {Object} callback.label The label of created microthesaurus.  
         * @param {Object} [options] Advanced options for the call.
         * @param {Boolean/String/Object} [options.errorMessage] Display an error message. See Ametys.data.ServerComm#callMethod errorMessage.
         * @param {Boolean/String/Object} [options.waitMessage] Display a waiting message. See Ametys.data.ServerComm#callMethod waitMessage.
         * @param {Number} [options.scope] This parameter is the scope used to call the callback. Moreover is the given class is a mixin of Ametys.data.ServerCaller, its methods Ametys.data.ServerCaller#beforeServerCall and Ametys.data.ServerCaller#afterServerCall will be used so see their documentation to look for additional options (such a refreshing on Ametys.ribbon.element.ui.ButtonController#beforeServerCall).
         * @param {Number} [options.priority] The message priority. See Ametys.data.ServerComm#callMethod for more information on the priority. PRIORITY_SYNCHRONOUS cannot be used here.
         * @param {String} [options.cancelCode] Cancel similar unachieved read operations. See Ametys.data.ServerComm#callMethod cancelCode.
         * @param {Object} [options.arguments] Additional arguments set in the callback.arguments parameter.                  
         * @param {Boolean} [options.ignoreCallbackOnError] If the server throws an exception, should the callback beeing called with a null parameter. See Ametys.data.ServerComm#callMethod ignoreOnError.
         */
        this.addCallables(
            {
                role: "org.ametys.plugins.thesaurus.ThesaurusDAO",
                methodName: "getThesaurusLabel",
                errorMessage: {
                    msg: "{{i18n PLUGINS_THESAURUS_UITOOL_THESAURUS_NOT_FOUND_ERROR}}",
                    category: Ext.getClassName(this)
                }
            }
        );
        
        /**
         * @callable
         * @member Ametys.plugins.thesaurus.ThesaurusDAO
         * @method getMicrothesaurii 
         * Get the microthesaurii of a thesaurus
         * This calls the method 'getMicrothesaurii' of the server DAO 'org.ametys.plugins.thesaurus.ThesaurusDAO'.
         * @param {Object[]} parameters The parameters to transmit to the server method
         * @param {String} parameters.id The id of thesaurus
         * @param {Function} callback The function to call when the java process is over. Use options.scope for the scope.
         * @param {String[]} callback.microthesaurii The id of microthesaurii.
         * @param {Object} [options] Advanced options for the call.
         * @param {Boolean/String/Object} [options.errorMessage] Display an error message. See Ametys.data.ServerComm#callMethod errorMessage.
         * @param {Boolean/String/Object} [options.waitMessage] Display a waiting message. See Ametys.data.ServerComm#callMethod waitMessage.
         * @param {Number} [options.scope] This parameter is the scope used to call the callback. Moreover is the given class is a mixin of Ametys.data.ServerCaller, its methods Ametys.data.ServerCaller#beforeServerCall and Ametys.data.ServerCaller#afterServerCall will be used so see their documentation to look for additional options (such a refreshing on Ametys.ribbon.element.ui.ButtonController#beforeServerCall).
         * @param {Number} [options.priority] The message priority. See Ametys.data.ServerComm#callMethod for more information on the priority. PRIORITY_SYNCHRONOUS cannot be used here.
         * @param {String} [options.cancelCode] Cancel similar unachieved read operations. See Ametys.data.ServerComm#callMethod cancelCode.
         * @param {Object} [options.arguments] Additional arguments set in the callback.arguments parameter.                  
         * @param {Boolean} [options.ignoreCallbackOnError] If the server throws an exception, should the callback beeing called with a null parameter. See Ametys.data.ServerComm#callMethod ignoreOnError.
         */
        this.addCallables(
            {
                role: "org.ametys.plugins.thesaurus.ThesaurusDAO",
                methodName: "getMicrothesaurii",
                waitMessage: false,
                errorMessage: {
                    msg: "{{i18n PLUGINS_THESAURUS_GET_MICROTHESAURII_ERROR}}",
                    category: Ext.getClassName(this)
                }
            }
        );
        
    },
    
    /**
     * Callback function called after {@link #createThesaurus} has been processed.
     * @param {Object} response The response provided by the {@link Ametys.data.ServerComm}
     * @private
     */
    _createThesaurusCb: function(response)
    {
        if (response.success)
        {
            Ext.create("Ametys.message.Message", {
                type:  Ametys.message.Message.CREATED,
	            targets: {
	                id: Ametys.message.MessageTarget.THESAURUS,
	                parameters: {
	                    id: response.id,
	                    label: response.label
	                }
	            }
            });
        }
        else if (response.alreadyExist)
        {
            Ametys.Msg.show({
                title: "{{i18n PLUGINS_THESAURUS_CREATE_THESAURUS_LABEL}}",
                msg: Ext.String.format("{{i18n PLUGINS_THESAURUS_DAO_UPDATE_THESAURUS_ALREADY_EXISTS_ERROR}}", response.label),
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
        }
    },
    
    /**
     * Callback function called after {@link #updateThesaurus} has been processed.
     * @param {Object} response The response provided by the {@link Ametys.data.ServerComm}
     * @private
     */
    _updateThesaurusCb: function(response)
    {
    	if (response.success)
        {
            Ext.create("Ametys.message.Message", {
                type:  Ametys.message.Message.MODIFIED,
                targets: {
                    id: Ametys.message.MessageTarget.THESAURUS,
                    parameters: {
                        id: response.id,
                        label: response.label
                    }
                }
            });
            
            if (response.hasMicrothesaurii)
            {
                // Restart the application
                Ametys.plugins.thesaurus.ThesaurusActions.restart();
            }
        }
        else if (response.alreadyExist)
        {
            Ametys.Msg.show({
                title: "{{i18n PLUGINS_THESAURUS_RENAME_THESAURUS_LABEL}}",
                msg: Ext.String.format("{{i18n PLUGINS_THESAURUS_DAO_UPDATE_THESAURUS_ALREADY_EXISTS_ERROR}}", response.label),
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
        }
    },
    
    /**
     * Callback function called after {@link #deleteThesaurus} has been processed.
     * @param {Object} response The response provided by the {@link Ametys.data.ServerComm}
     * @param {Array} args The callback arguments
     * @param {Array} params The parameters
     * @private
     */
    _deleteThesaurusCb: function(response, args, params)
    {
        if (response.success)
        {
            // Close tools
            var thesaurusTool = Ametys.tool.ToolsManager.getTool('uitool-thesaurus$' + response.id);
            if (thesaurusTool!= null)
            {
                Ametys.tool.ToolsManager.removeTool(thesaurusTool);
            }
                    
            var deletedMicrothesaurii = response['deleted-microthesaurii'];
            if (deletedMicrothesaurii && deletedMicrothesaurii.length > 0)
            {
                // Close search tools
                Ext.Array.each(deletedMicrothesaurii, function (microthesaurus)
                {
                    var searchTool = Ametys.tool.ToolsManager.getTool('uitool-microthesaurus$reference-table-search-ui.' + microthesaurus.id);
	                if (searchTool!= null)
	                {
	                    Ametys.tool.ToolsManager.removeTool(searchTool);
	                }
                });
            }
            
            // Restart the application
            Ametys.plugins.thesaurus.ThesaurusActions.restart();
        }
        else if (response['undeletable-microthesaurus'])
        {
            var undeletableMT = response['undeletable-microthesaurus'];
            
            var errorMsg = response.hasReferencingContents ? Ext.String.format("{{i18n PLUGINS_THESAURUS_DAO_DELETE_THESAURUS_MICROTHESAURUS_REFERENCES_ERROR}}", undeletableMT.label)
                : Ext.String.format("{{i18n PLUGINS_THESAURUS_DAO_DELETE_THESAURUS_UNDELETABLE_MICROTHESAURUS_ERROR}}", undeletableMT.label);
                
            Ametys.Msg.show({
                title: "{{i18n PLUGINS_THESAURUS_DAO_DELETE_THESAURUS_TITLE}}",
                msg: errorMsg,
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
        }
        else if (response['undeleted-microthesaurus'])
        {
            var undeletedMT = response['undeleted-microthesaurus'];
            Ametys.Msg.show({
                title: "{{i18n PLUGINS_THESAURUS_DAO_DELETE_THESAURUS_TITLE}}",
                msg: Ext.String.format("{{i18n PLUGINS_THESAURUS_DAO_DELETE_THESAURUS_MICROTHESAURUS_ERROR}}", undeletedMT.label),
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
        }
        else
        {
            Ametys.Msg.show({
                title: "{{i18n PLUGINS_THESAURUS_DAO_DELETE_THESAURUS_TITLE}}",
                msg: "{{i18n PLUGINS_THESAURUS_DAO_DELETE_THESAURUS_ERROR}}",
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
        }
    },
    
    /**
     * Callback function called after {@link #createMicrothesaurus} has been processed.
     * @param {Object} response The response provided by the {@link Ametys.data.ServerComm}
     * @private
     */
    _createMicrothesaurusCb: function(response)
    {
        if (response.success)
        {
            Ext.create("Ametys.message.Message", {
                type:  Ametys.message.Message.CREATED,
                targets: {
                    id: Ametys.message.MessageTarget.MICROTHESAURUS,
                    parameters: {
                        id: response.id,
                        thesaurusId: response.thesaurusId
                    }
                }
            });
            
            // Restart application
            Ametys.plugins.thesaurus.ThesaurusActions.restart();
        }
    },
    
    /**
     * Callback function called after {@link #updateMicrothesaurus} has been processed.
     * @param {Object} response The response provided by the {@link Ametys.data.ServerComm}
     * @private
     */
    _updateMicrothesaurusCb: function(response)
    {
        if (response.success)
        {
            Ext.create("Ametys.message.Message", {
                type:  Ametys.message.Message.MODIFIED,
                targets: {
                    id: Ametys.message.MessageTarget.MICROTHESAURUS,
                    parameters: {
                        id: response.id,
                        thesaurusId: response.thesaurusId
                    }
                }
            });
            
            // Restart application
            Ametys.plugins.thesaurus.ThesaurusActions.restart();
        }
    },
    
    /**
     * Callback function called after {@link #deleteMicrothesaurus} has been processed.
     * @param {Object} response The response provided by the {@link Ametys.data.ServerComm}
     * @param {Array} args The callback arguments
     * @param {Array} params The parameters
     * @private
     */
    _deleteMicrothesaurusCb: function(response, args, params)
    {
        if (response.success)
        {
	        Ext.create("Ametys.message.Message", {
	            type:  Ametys.message.Message.DELETED,
	            targets: params[1]
	        });
	        
	        var searchTool = Ametys.tool.ToolsManager.getTool('uitool-microthesaurus$reference-table-search-ui.' + response.id);
	        if (searchTool!= null)
	        {
	            Ametys.tool.ToolsManager.removeTool(searchTool);
	        }
            
            // Restart application
	        Ametys.plugins.thesaurus.ThesaurusActions.restart();
        }
        else if (response.hasReferencingContents)
        {
            Ametys.Msg.show({
                title: "{{i18n PLUGINS_THESAURUS_DAO_DELETE_MICROTHESAURUS_TITLE}}",
                msg: "{{i18n PLUGINS_THESAURUS_DAO_DELETE_MICROTHESAURUS_REFERENCES_ERROR}}",
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
        }
        else
        {
            Ametys.Msg.show({
                title: "{{i18n PLUGINS_THESAURUS_DAO_DELETE_MICROTHESAURUS_TITLE}}",
                msg: "{{i18n PLUGINS_THESAURUS_DAO_DELETE_THESAURUS_ERROR}}",
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
        }
    },
    
    /**
     * Callback function called after {@link #getThesaurusLabel} has been processed.
     * @param {String} label The thesaurus' label
     * @param {Array} args The callback arguments
     * @param {Array} params The parameters
     * @private
     */
    _getThesaurusLabelCb: function (label, args, params)
    {
        if (Ext.isFunction(params[1]))
        {
            params[1](label);
        }
    },
    
    /**
     * Callback function called after {@link #getMicrothesaurii} has been processed.
     * @param {String[]} microthesaurii The thesaurus' microthesaurii
     * @param {Array} args The callback arguments
     * @param {Array} params The parameters
     * @private
     */
    _getMicrothesauriiCb: function (microthesaurii, args, params)
    {
        if (Ext.isFunction(params[1]))
        {
            params[1](microthesaurii);
        }
    }
 });