BotRegistrationService.java

/*
 * Copyright (c) 2002-2019, Mairie de Paris
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice
 *     and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice
 *     and the following disclaimer in the documentation and/or other materials
 *     provided with the distribution.
 *
 *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * License 1.0
 */
package fr.paris.lutece.plugins.botpress.service;

import fr.paris.lutece.plugins.botpress.business.BPBot;
import fr.paris.lutece.plugins.botpress.business.BPBotHome;
import fr.paris.lutece.plugins.chatbot.service.BotService;
import fr.paris.lutece.plugins.chatbot.service.bot.ChatBot;
import fr.paris.lutece.portal.service.i18n.I18nService;
import fr.paris.lutece.util.ReferenceList;
import java.util.Locale;

/**
 * BotRegistrationService
 */
public class BotRegistrationService
{

    private static final String MESSAGE_STATUS_ENABLED = "botpress.bot_status.enabled";
    private static final String MESSAGE_STATUS_DISABLED = "botpress.bot_status.disabled";
    private static final String MESSAGE_MODE_STANDARD = "botpress.mode.standard";
    private static final String MESSAGE_MODE_STANDALONE = "botpress.mode.standalone";

    private static final int STATUS_ENABLED = 1;
    private static final int STATUS_DISABLED = 0;
    private static final int MODE_STANDARD = 0;
    private static final int MODE_STANDALONE = 1;

    private static ReferenceList _listStatus;

    /** private constructor */
    private BotRegistrationService()
    {
    }
    
    /**
     * Register all bots
     */
    public static void registerAllBots( )
    {
        for ( BPBot bot : BPBotHome.getBPBotsList( ) )
        {
            ChatBot chatbot = new BotInstance( bot );
            BotService.register( chatbot );
        }
    }

    /**
     * Returns the list of available bot status
     *
     * @param locale
     *            The locale
     * @return The list
     */
    public static synchronized ReferenceList getBotsStatusList( Locale locale )
    {
        if ( _listStatus == null )
        {
            _listStatus = new ReferenceList( );
            _listStatus.addItem( STATUS_DISABLED, I18nService.getLocalizedString( MESSAGE_STATUS_DISABLED, locale ) );
            _listStatus.addItem( STATUS_ENABLED, I18nService.getLocalizedString( MESSAGE_STATUS_ENABLED, locale ) );
        }
        return _listStatus;
    }

    /**
     * Register the bot
     * 
     * @param botInstance
     *            The bot instance
     * @param nStatus
     *            The status
     */
    public static void register( BotInstance botInstance, int nStatus )
    {
        if ( nStatus == STATUS_ENABLED )
        {
            BotService.register( botInstance );
        }
    }

    /**
     * Un register the bot
     * 
     * @param strBotKey
     *            The bot key
     */
    public static void unregister( String strBotKey )
    {
        BotService.unregister( strBotKey );
    }

    /**
     * Return the list of mode
     * @param locale The locale to translate mode
     * @return The list
     */
    public static ReferenceList getModes( Locale locale )
    {
        ReferenceList list = new ReferenceList( );

        list.addItem( MODE_STANDARD, I18nService.getLocalizedString( MESSAGE_MODE_STANDARD, locale ) );
        list.addItem( MODE_STANDALONE, I18nService.getLocalizedString( MESSAGE_MODE_STANDALONE, locale ) );

        return list;
    }
}