⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatsettingsmanager.java

📁 openfire 服务器源码下载
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * $RCSfile$
 * $Revision: 23995 $
 * $Date: 2005-11-21 13:48:54 -0800 (Mon, 21 Nov 2005) $
 *
 * Copyright (C) 1999-2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
 */

package org.jivesoftware.openfire.fastpath.settings.chat;

import org.jivesoftware.xmpp.workgroup.AgentSession;
import org.jivesoftware.xmpp.workgroup.Workgroup;
import org.jivesoftware.xmpp.workgroup.event.WorkgroupEventDispatcher;
import org.jivesoftware.xmpp.workgroup.event.WorkgroupEventListener;
import org.dom4j.Element;
import org.jivesoftware.database.DbConnectionManager;
import org.xmpp.component.ComponentManagerFactory;
import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

/**
 * Utility class for doing all Database operations related to the ChatSettings.
 * You would use this class to retrieve, update or insert new settings.
 */
public class ChatSettingsManager implements WorkgroupEventListener {

    private static final String GET_SETTINGS =
            "SELECT * FROM fpChatSetting WHERE workgroupNode=?";
    private static final String INSERT_CHAT_SETTING =
            "INSERT INTO fpChatSetting VALUES(?,?,?,?,?,?,?)";
    private static final String UPDATE_CHAT_SETTING =
            "UPDATE fpChatSetting SET value=? WHERE name=? AND workgroupNode=?";
    private static final String DELETE_CHAT_SETTINGS =
            "DELETE FROM fpChatSetting WHERE workgroupNode=?";
    private static final String DELETE_SINGLE_CHAT_SETTING = 
            "DELETE FROM fpChatSetting WHERE name=? AND workgroupNode=?";

    private static ChatSettingsManager singleton = new ChatSettingsManager();

    /**
     * Map for caching settings. This will map by workgroup node.
     */
    private final Map<String, ChatSettings> cachedSettings = new HashMap<String, ChatSettings>();

    /**
     * Returns the singleton instance of <CODE>ChatSettingsManager</CODE>,
     * creating it if necessary.
     * <p/>
     *
     * @return the singleton instance of <Code>ChatSettingsManager</CODE>
     */
    public static ChatSettingsManager getInstance() {
        return singleton;
    }

    private ChatSettingsManager() {
        // Private constructor for singleton design.
        WorkgroupEventDispatcher.addListener(this);
    }

    public static void shutdown() {
        WorkgroupEventDispatcher.removeListener(singleton);
        singleton = null;
    }

    /**
     * Retrieves the settings for a given workgroup.
     *
     * @param workgroup the owning workgroup of the settings.
     * @return the ChatSettings object mapped to the workgroup.
     */
    private ChatSettings getChatSettingsFromDb(Workgroup workgroup) {
        final ChatSettings chatSettings = new ChatSettings(workgroup);
        cachedSettings.put(workgroup.getJID().getNode(), chatSettings);

        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(GET_SETTINGS);

            String workgroupName = workgroup.getJID().getNode();

            pstmt.setString(1, workgroupName);
            ResultSet result = pstmt.executeQuery();
            if (result.next()) {
                do {
                    String wg = result.getString("workgroupNode");
                    int type = result.getInt("type");
                    String label = result.getString("label");
                    String description = result.getString("description");
                    String name = result.getString("name");
                    String value = result.getString("value");
                    String defaultValue = result.getString("defaultValue");

                    ChatSetting setting = new ChatSetting(name);
                    setting.setWorkgroupNode(workgroupName);
                    setting.setType(type);

                    setting.setValue(value);
                    setting.setDefaultValue(defaultValue);
                    setting.setLabel(label);
                    setting.setDescription(description);
                    chatSettings.addChatSetting(setting);
                }
                while (result.next());
            }
            else {
                // If no settings were found in the DB then try creating the default settings for
                // this workgroup
                ChatSettingsCreator.getInstance().createDefaultSettings(workgroup.getJID());
            }
        }
        catch (Exception ex) {
            cachedSettings.remove(workgroup.getJID().getNode());
            ComponentManagerFactory.getComponentManager().getLog().error(ex);
        }
        finally {
            DbConnectionManager.closeConnection(pstmt, con);
        }
        return chatSettings;
    }

    /**
     * Retrieves the ChatSettings mapped to a particular workgroup.
     *
     * @param workgroup the owning workgroup of the settings.
     * @return the ChatSettings found.
     */
    public ChatSettings getChatSettings(Workgroup workgroup) {
        String workgroupNode = workgroup.getJID().getNode();
        ChatSettings chatSettings = cachedSettings.get(workgroupNode);
        if (chatSettings == null) {
            synchronized (workgroupNode.intern()) {
                chatSettings = cachedSettings.get(workgroupNode);
                if (chatSettings == null) {
                    chatSettings = getChatSettingsFromDb(workgroup);
                }
            }
        }
        return chatSettings;
    }

    /**
     * Adds a new ChatSetting, persisting to the database and cache objects.
     *
     * @param settings the <code>ChatSetting</code> to add.
     */
    public void addChatSetting(ChatSetting settings) {
        Connection con;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            try {
                pstmt = con.prepareStatement(INSERT_CHAT_SETTING);
                pstmt.setString(1, settings.getWorkgroupNode());
                pstmt.setInt(2, settings.getType().getType());
                pstmt.setString(3, settings.getLabel());
                pstmt.setString(4, settings.getDescription());
                pstmt.setString(5, settings.getKey().toString());

                DbConnectionManager.setLargeTextField(pstmt, 6, settings.getValue());
                pstmt.setString(7, settings.getDefaultValue());
                pstmt.executeUpdate();
            }
            catch (Exception ex) {
                ComponentManagerFactory.getComponentManager().getLog().error(ex);
            }
            finally {
                DbConnectionManager.closeConnection(pstmt, con);
            }
        }
        catch (Exception ex) {
            ComponentManagerFactory.getComponentManager().getLog().error(ex);
        }

        // Add to cache
        ChatSettings chatSettings = cachedSettings.get(settings.getWorkgroupNode());
        if (chatSettings != null) {
            chatSettings.addChatSetting(settings);
        }
    }

    /**
     * Update a WebChatSetting in the Database.
     *
     * @param settings the <code>WebChatSetting</code> to update.
     */
    public void updateChatSetting(ChatSetting settings) {
        Connection con;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            try {
                pstmt = con.prepareStatement(UPDATE_CHAT_SETTING);
                DbConnectionManager.setLargeTextField(pstmt, 1, settings.getValue());
                pstmt.setString(2, settings.getKey().toString());
                pstmt.setString(3, settings.getWorkgroupNode());
                pstmt.executeUpdate();
            }
            catch (Exception ex) {
                ComponentManagerFactory.getComponentManager().getLog().error(ex);
            }
            finally {
                DbConnectionManager.closeConnection(pstmt, con);
            }
        }
        catch (Exception ex) {
            ComponentManagerFactory.getComponentManager().getLog().error(ex);
        }

        // Add to cache
        ChatSettings chatSettings = cachedSettings.get(settings.getWorkgroupNode());
        if (chatSettings != null) {
            chatSettings.addChatSetting(settings);
        }
    }

    /**
     * Removes a <code>WebChatSetting</code> from the database.
     *
     * @param setting the WebChatSetting to remove.
     */
    public void removeChatSetting(ChatSetting setting) {
        Connection con;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            try {
                pstmt = con.prepareStatement(DELETE_SINGLE_CHAT_SETTING);
                pstmt.setString(1, setting.getKey().toString());
                pstmt.setString(2, setting.getWorkgroupNode());
                pstmt.executeUpdate();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -