Search in sources :

Example 6 with Conversation

use of com.reucon.openfire.plugin.archive.model.Conversation in project Openfire by igniterealtime.

the class JdbcPersistenceManager method findConversations.

public Collection<Conversation> findConversations(Date startDate, Date endDate, String ownerJid, String withJid, XmppResultSet xmppResultSet) {
    final TreeMap<Long, Conversation> conversations;
    final StringBuilder querySB;
    final StringBuilder whereSB;
    final StringBuilder limitSB;
    conversations = new TreeMap<Long, Conversation>();
    querySB = new StringBuilder(SELECT_CONVERSATIONS);
    whereSB = new StringBuilder();
    limitSB = new StringBuilder();
    startDate = getAuditedStartDate(startDate);
    if (startDate != null) {
        appendWhere(whereSB, CONVERSATION_START_TIME, " >= ?");
    }
    if (endDate != null) {
        appendWhere(whereSB, CONVERSATION_END_TIME, " <= ?");
    }
    if (ownerJid != null) {
        appendWhere(whereSB, CONVERSATION_OWNER_JID, " = ?");
    }
    if (withJid != null) {
        appendWhere(whereSB, CONVERSATION_WITH_JID, " = ?");
    }
    if (xmppResultSet != null) {
        Integer firstIndex = null;
        int max = xmppResultSet.getMax() != null ? xmppResultSet.getMax() : DEFAULT_MAX;
        xmppResultSet.setCount(countConversations(startDate, endDate, ownerJid, withJid, whereSB.toString()));
        if (xmppResultSet.getIndex() != null) {
            firstIndex = xmppResultSet.getIndex();
        } else if (xmppResultSet.getAfter() != null) {
            firstIndex = countConversationsBefore(startDate, endDate, ownerJid, withJid, xmppResultSet.getAfter(), whereSB.toString());
            firstIndex += 1;
        } else if (xmppResultSet.getBefore() != null) {
            firstIndex = countConversationsBefore(startDate, endDate, ownerJid, withJid, xmppResultSet.getBefore(), whereSB.toString());
            firstIndex -= max;
            if (firstIndex < 0) {
                firstIndex = 0;
            }
        }
        firstIndex = firstIndex != null ? firstIndex : 0;
        if (DbConnectionManager.getDatabaseType() == DbConnectionManager.DatabaseType.sqlserver) {
            limitSB.append(" BETWEEN ").append(firstIndex + 1);
            limitSB.append(" AND ").append(firstIndex + max);
        } else {
            limitSB.append(" LIMIT ").append(max);
            limitSB.append(" OFFSET ").append(firstIndex);
        }
        xmppResultSet.setFirstIndex(firstIndex);
    }
    if (whereSB.length() != 0) {
        querySB.append(" WHERE ").append(whereSB);
    }
    querySB.append(SELECT_CONVERSATIONS_GROUP_BY);
    if (DbConnectionManager.getDatabaseType() == DbConnectionManager.DatabaseType.sqlserver) {
        querySB.insert(0, "SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY " + CONVERSATION_ID + ") AS RowNum FROM ( ");
        querySB.append(") ofConversation ) t2 WHERE RowNum");
    } else {
        querySB.append(" ORDER BY ").append(CONVERSATION_ID);
    }
    querySB.append(limitSB);
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(querySB.toString());
        bindConversationParameters(startDate, endDate, ownerJid, withJid, pstmt);
        rs = pstmt.executeQuery();
        Log.debug("findConversations: SELECT_CONVERSATIONS: " + pstmt.toString());
        while (rs.next()) {
            Conversation conv = extractConversation(rs);
            conversations.put(conv.getId(), conv);
        }
    } catch (SQLException sqle) {
        Log.error("Error selecting conversations", sqle);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    if (xmppResultSet != null && conversations.size() > 0) {
        xmppResultSet.setFirst(conversations.firstKey());
        xmppResultSet.setLast(conversations.lastKey());
    }
    return conversations.values();
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) XmppResultSet(com.reucon.openfire.plugin.archive.xep0059.XmppResultSet) ResultSet(java.sql.ResultSet) Conversation(com.reucon.openfire.plugin.archive.model.Conversation) PreparedStatement(java.sql.PreparedStatement)

Example 7 with Conversation

use of com.reucon.openfire.plugin.archive.model.Conversation in project Openfire by igniterealtime.

the class JdbcPersistenceManager method getConversations.

public List<Conversation> getConversations(Collection<Long> conversationIds) {
    final List<Conversation> conversations;
    final StringBuilder querySB;
    conversations = new ArrayList<Conversation>();
    if (conversationIds.isEmpty()) {
        return conversations;
    }
    querySB = new StringBuilder(SELECT_CONVERSATIONS);
    querySB.append(" WHERE ");
    querySB.append(CONVERSATION_ID);
    querySB.append(" IN ( ");
    for (int i = 0; i < conversationIds.size(); i++) {
        if (i == 0) {
            querySB.append("?");
        } else {
            querySB.append(",?");
        }
    }
    querySB.append(" )");
    querySB.append(SELECT_CONVERSATIONS_GROUP_BY);
    querySB.append(" ORDER BY ").append(CONVERSATION_END_TIME);
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(querySB.toString());
        int i = 0;
        for (Long id : conversationIds) {
            pstmt.setLong(++i, id);
        }
        rs = pstmt.executeQuery();
        while (rs.next()) {
            conversations.add(extractConversation(rs));
        }
    } catch (SQLException sqle) {
        Log.error("Error selecting conversations", sqle);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    return conversations;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) XmppResultSet(com.reucon.openfire.plugin.archive.xep0059.XmppResultSet) ResultSet(java.sql.ResultSet) Conversation(com.reucon.openfire.plugin.archive.model.Conversation) PreparedStatement(java.sql.PreparedStatement)

Example 8 with Conversation

use of com.reucon.openfire.plugin.archive.model.Conversation in project Openfire by igniterealtime.

the class JdbcPersistenceManager method getActiveConversations.

public Collection<Conversation> getActiveConversations(int conversationTimeout) {
    final Collection<Conversation> conversations;
    final long now = System.currentTimeMillis();
    conversations = new ArrayList<Conversation>();
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(isOracleDB() ? SELECT_ACTIVE_CONVERSATIONS_ORACLE : SELECT_ACTIVE_CONVERSATIONS);
        pstmt.setLong(1, now - conversationTimeout * 60L * 1000L);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            conversations.add(extractConversation(rs));
        }
    } catch (SQLException sqle) {
        Log.error("Error selecting conversations", sqle);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    return conversations;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) XmppResultSet(com.reucon.openfire.plugin.archive.xep0059.XmppResultSet) ResultSet(java.sql.ResultSet) Conversation(com.reucon.openfire.plugin.archive.model.Conversation) PreparedStatement(java.sql.PreparedStatement)

Example 9 with Conversation

use of com.reucon.openfire.plugin.archive.model.Conversation in project Openfire by igniterealtime.

the class IQRetrieveHandler method handleIQ.

public IQ handleIQ(IQ packet) throws UnauthorizedException {
    final IQ reply = IQ.createResultIQ(packet);
    final RetrieveRequest retrieveRequest = new RetrieveRequest(packet.getChildElement());
    // inclusive
    int fromIndex;
    // exclusive
    int toIndex;
    int max;
    final Conversation conversation = retrieve(packet.getFrom(), retrieveRequest);
    if (conversation == null) {
        return error(packet, PacketError.Condition.item_not_found);
    }
    final Element chatElement = reply.setChildElement("chat", NAMESPACE);
    chatElement.addAttribute("with", conversation.getWithJid());
    chatElement.addAttribute("start", XmppDateUtil.formatDate(conversation.getStart()));
    max = conversation.getMessages().size();
    fromIndex = 0;
    toIndex = max > 0 ? max : 0;
    final XmppResultSet resultSet = retrieveRequest.getResultSet();
    if (resultSet != null) {
        if (resultSet.getMax() != null && resultSet.getMax() <= max) {
            max = resultSet.getMax();
            toIndex = fromIndex + max;
        }
        if (resultSet.getIndex() != null) {
            fromIndex = resultSet.getIndex();
            toIndex = fromIndex + max;
        } else if (resultSet.getAfter() != null) {
            fromIndex = resultSet.getAfter().intValue() + 1;
            toIndex = fromIndex + max;
        } else if (resultSet.getBefore() != null) {
            if (resultSet.getBefore() != Long.MAX_VALUE)
                toIndex = resultSet.getBefore().intValue();
            else
                toIndex = conversation.getMessages().size();
            fromIndex = toIndex - max;
        }
    }
    fromIndex = fromIndex < 0 ? 0 : fromIndex;
    toIndex = toIndex > conversation.getMessages().size() ? conversation.getMessages().size() : toIndex;
    toIndex = toIndex < fromIndex ? fromIndex : toIndex;
    final List<ArchivedMessage> messages = conversation.getMessages().subList(fromIndex, toIndex);
    for (int i = 0; i < messages.size(); i++) {
        if (i == 0) {
            addMessageElement(chatElement, conversation, messages.get(i), null);
        } else {
            addMessageElement(chatElement, conversation, messages.get(i), messages.get(i - 1));
        }
    }
    if (resultSet != null) {
        if (messages.size() > 0) {
            resultSet.setFirst((long) fromIndex);
            resultSet.setFirstIndex(fromIndex);
            resultSet.setLast((long) toIndex - 1);
        }
        resultSet.setCount(conversation.getMessages().size());
        chatElement.add(resultSet.createResultElement());
    }
    return reply;
}
Also used : ArchivedMessage(com.reucon.openfire.plugin.archive.model.ArchivedMessage) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) Conversation(com.reucon.openfire.plugin.archive.model.Conversation) XmppResultSet(com.reucon.openfire.plugin.archive.xep0059.XmppResultSet)

Aggregations

Conversation (com.reucon.openfire.plugin.archive.model.Conversation)9 XmppResultSet (com.reucon.openfire.plugin.archive.xep0059.XmppResultSet)6 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 ArchivedMessage (com.reucon.openfire.plugin.archive.model.ArchivedMessage)3 Participant (com.reucon.openfire.plugin.archive.model.Participant)2 Element (org.dom4j.Element)2 IQ (org.xmpp.packet.IQ)2 JID (org.xmpp.packet.JID)2 Date (java.util.Date)1 XMPPServer (org.jivesoftware.openfire.XMPPServer)1