Search in sources :

Example 76 with ResultSet

use of java.sql.ResultSet in project Openfire by igniterealtime.

the class JdbcPersistenceManager method countConversations.

private int countConversations(Date startDate, Date endDate, String ownerJid, String withJid, String whereClause) {
    StringBuilder querySB;
    querySB = new StringBuilder(COUNT_CONVERSATIONS);
    if (whereClause != null && whereClause.length() != 0) {
        querySB.append(" WHERE ").append(whereClause);
    }
    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();
        if (rs.next()) {
            return rs.getInt(1);
        } else {
            return 0;
        }
    } catch (SQLException sqle) {
        Log.error("Error counting conversations", sqle);
        return 0;
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) XmppResultSet(com.reucon.openfire.plugin.archive.xep0059.XmppResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 77 with ResultSet

use of java.sql.ResultSet in project Openfire by igniterealtime.

the class JdbcPersistenceManager method getConversation.

private Conversation getConversation(Long conversationId, String ownerJid, String withJid, Date start) {
    Conversation conversation = null;
    StringBuilder querySB;
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    querySB = new StringBuilder(SELECT_CONVERSATIONS);
    querySB.append(" WHERE ");
    if (conversationId != null) {
        querySB.append(CONVERSATION_ID).append(" = ? ");
    } else {
        querySB.append(CONVERSATION_OWNER_JID).append(" = ?");
        if (withJid != null) {
            querySB.append(" AND ");
            querySB.append(CONVERSATION_WITH_JID).append(" = ? ");
        }
        if (start != null) {
            querySB.append(" AND ");
            querySB.append(CONVERSATION_START_TIME).append(" = ? ");
        }
    }
    querySB.append(SELECT_CONVERSATIONS_GROUP_BY);
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(querySB.toString());
        int i = 1;
        if (conversationId != null) {
            pstmt.setLong(1, conversationId);
        } else {
            pstmt.setString(i++, ownerJid);
            if (withJid != null) {
                pstmt.setString(i++, withJid);
            }
            if (start != null) {
                pstmt.setLong(i++, dateToMillis(start));
            }
        }
        rs = pstmt.executeQuery();
        Log.debug("getConversation: SELECT_CONVERSATIONS: " + pstmt.toString());
        if (rs.next()) {
            conversation = extractConversation(rs);
        } else {
            return null;
        }
        rs.close();
        pstmt.close();
        pstmt = con.prepareStatement(SELECT_PARTICIPANTS_BY_CONVERSATION);
        pstmt.setLong(1, conversation.getId());
        rs = pstmt.executeQuery();
        Log.debug("getConversation: SELECT_PARTICIPANTS_BY_CONVERSATION: " + pstmt.toString());
        while (rs.next()) {
            for (Participant participant : extractParticipant(rs)) {
                conversation.addParticipant(participant);
            }
        }
        rs.close();
        pstmt.close();
        pstmt = con.prepareStatement(SELECT_MESSAGES_BY_CONVERSATION);
        pstmt.setLong(1, conversation.getId());
        pstmt.setString(2, conversation.getOwnerJid());
        rs = pstmt.executeQuery();
        Log.debug("getConversation: SELECT_MESSAGES_BY_CONVERSATION: " + pstmt.toString());
        while (rs.next()) {
            ArchivedMessage message;
            message = extractMessage(rs);
            message.setConversation(conversation);
            conversation.addMessage(message);
        }
    } catch (SQLException sqle) {
        Log.error("Error selecting conversation", sqle);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    return conversation;
}
Also used : Participant(com.reucon.openfire.plugin.archive.model.Participant) ArchivedMessage(com.reucon.openfire.plugin.archive.model.ArchivedMessage) 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 78 with ResultSet

use of java.sql.ResultSet in project Openfire by igniterealtime.

the class MucMamPersistenceManager method findMessages.

@Override
public Collection<ArchivedMessage> findMessages(Date startDate, Date endDate, String owner, String with, XmppResultSet xmppResultSet) {
    JID mucRoom = new JID(owner);
    MultiUserChatManager manager = XMPPServer.getInstance().getMultiUserChatManager();
    MultiUserChatService service = manager.getMultiUserChatService(mucRoom);
    MUCRoom room = service.getChatRoom(mucRoom.getNode());
    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    // If logging isn't enabled, do nothing.
    if (!room.isLogEnabled())
        return null;
    List<ArchivedMessage> msgs = new LinkedList<>();
    if (startDate == null) {
        startDate = new Date(0L);
    }
    if (endDate == null) {
        endDate = new Date();
    }
    int max = xmppResultSet.getMax();
    // TODO: Suppress this, since we don't yet have requestor information for access control.
    with = null;
    try {
        connection = DbConnectionManager.getConnection();
        StringBuilder sql = new StringBuilder(LOAD_HISTORY);
        if (with != null) {
            sql.append(WHERE_SENDER);
        }
        if (xmppResultSet.getAfter() != null) {
            sql.append(WHERE_AFTER);
        }
        if (xmppResultSet.getBefore() != null) {
            sql.append(WHERE_BEFORE);
        }
        sql.append(ORDER_BY);
        pstmt = connection.prepareStatement(sql.toString());
        pstmt.setString(1, StringUtils.dateToMillis(startDate));
        pstmt.setString(2, StringUtils.dateToMillis(endDate));
        pstmt.setLong(3, room.getID());
        int pos = 3;
        if (with != null) {
            pstmt.setString(++pos, with);
        }
        if (xmppResultSet.getAfter() != null) {
            pstmt.setLong(++pos, xmppResultSet.getAfter());
        }
        if (xmppResultSet.getBefore() != null) {
            pstmt.setLong(++pos, xmppResultSet.getBefore());
        }
        rs = pstmt.executeQuery();
        while (rs.next()) {
            String senderJID = rs.getString(1);
            String nickname = rs.getString(2);
            Date sentDate = new Date(Long.parseLong(rs.getString(3).trim()));
            String subject = rs.getString(4);
            String body = rs.getString(5);
            String stanza = rs.getString(6);
            long id = rs.getLong(7);
            if (stanza == null) {
                Message message = new Message();
                message.setType(Message.Type.groupchat);
                message.setSubject(subject);
                message.setBody(body);
                // Set the sender of the message
                if (nickname != null && nickname.trim().length() > 0) {
                    JID roomJID = room.getRole().getRoleAddress();
                    // Recreate the sender address based on the nickname and room's JID
                    message.setFrom(new JID(roomJID.getNode(), roomJID.getDomain(), nickname, true));
                } else {
                    // Set the room as the sender of the message
                    message.setFrom(room.getRole().getRoleAddress());
                }
                stanza = message.toString();
            }
            ArchivedMessage archivedMessage = new ArchivedMessage(sentDate, ArchivedMessage.Direction.from, null, null);
            archivedMessage.setStanza(stanza);
            archivedMessage.setId(id);
            msgs.add(archivedMessage);
        }
    } catch (SQLException e) {
        Log.error("SQL failure during MAM-MUC: ", e);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, connection);
    }
    // TODO - Not great, really should be done by suitable LIMIT stuff.
    // Would need to reverse ordering in some cases and then reverse results.
    boolean complete = true;
    xmppResultSet.setCount(msgs.size());
    while (msgs.size() > max) {
        msgs.remove(msgs.size() - 1);
        complete = false;
    }
    xmppResultSet.setComplete(complete);
    if (msgs.size() > 0) {
        xmppResultSet.setFirst(msgs.get(0).getId());
        if (msgs.size() > 1) {
            xmppResultSet.setLast(msgs.get(msgs.size() - 1).getId());
        }
    }
    return msgs;
}
Also used : JID(org.xmpp.packet.JID) MultiUserChatManager(org.jivesoftware.openfire.muc.MultiUserChatManager) ArchivedMessage(com.reucon.openfire.plugin.archive.model.ArchivedMessage) Message(org.xmpp.packet.Message) SQLException(java.sql.SQLException) Connection(java.sql.Connection) MultiUserChatService(org.jivesoftware.openfire.muc.MultiUserChatService) PreparedStatement(java.sql.PreparedStatement) LinkedList(java.util.LinkedList) Date(java.util.Date) MUCRoom(org.jivesoftware.openfire.muc.MUCRoom) ArchivedMessage(com.reucon.openfire.plugin.archive.model.ArchivedMessage) ResultSet(java.sql.ResultSet) XmppResultSet(com.reucon.openfire.plugin.archive.xep0059.XmppResultSet)

Example 79 with ResultSet

use of java.sql.ResultSet in project Openfire by igniterealtime.

the class MUCPersistenceManager method loadHistory.

private static void loadHistory(Long serviceID, Map<Long, LocalMUCRoom> rooms) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
        connection = DbConnectionManager.getConnection();
        statement = connection.prepareStatement(LOAD_ALL_HISTORY);
        // Reload the history, using "muc.history.reload.limit" (days) if present
        long from = 0;
        String reloadLimit = JiveGlobals.getProperty(MUC_HISTORY_RELOAD_LIMIT);
        if (reloadLimit != null) {
            // if the property is defined, but not numeric, default to 2 (days)
            int reloadLimitDays = JiveGlobals.getIntProperty(MUC_HISTORY_RELOAD_LIMIT, 2);
            Log.warn("MUC history reload limit set to " + reloadLimitDays + " days");
            from = System.currentTimeMillis() - (BigInteger.valueOf(86400000).multiply(BigInteger.valueOf(reloadLimitDays))).longValue();
        }
        statement.setLong(1, serviceID);
        statement.setString(2, StringUtils.dateToMillis(new Date(from)));
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            try {
                LocalMUCRoom room = rooms.get(resultSet.getLong(1));
                // Skip to the next position if the room does not exist or if history is disabled
                if (room == null || !room.isLogEnabled()) {
                    continue;
                }
                String senderJID = resultSet.getString(2);
                String nickname = resultSet.getString(3);
                Date sentDate = new Date(Long.parseLong(resultSet.getString(4).trim()));
                String subject = resultSet.getString(5);
                String body = resultSet.getString(6);
                String stanza = resultSet.getString(7);
                room.getRoomHistory().addOldMessage(senderJID, nickname, sentDate, subject, body, stanza);
            } catch (SQLException e) {
                Log.warn("A database exception prevented the history for one particular MUC room to be loaded from the database.", e);
            }
        }
    } finally {
        DbConnectionManager.closeConnection(resultSet, statement, connection);
    }
    // don't have in their histories the last room subject
    for (MUCRoom loadedRoom : rooms.values()) {
        if (!loadedRoom.getRoomHistory().hasChangedSubject() && loadedRoom.getSubject() != null && loadedRoom.getSubject().length() > 0) {
            loadedRoom.getRoomHistory().addOldMessage(loadedRoom.getRole().getRoleAddress().toString(), null, loadedRoom.getModificationDate(), loadedRoom.getSubject(), null, null);
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 80 with ResultSet

use of java.sql.ResultSet in project Openfire by igniterealtime.

the class User method loadProperties.

private void loadProperties() {
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(LOAD_PROPERTIES);
        pstmt.setString(1, username);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            properties.put(rs.getString(1), rs.getString(2));
        }
    } catch (SQLException sqle) {
        Log.error(sqle.getMessage(), sqle);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

ResultSet (java.sql.ResultSet)15888 PreparedStatement (java.sql.PreparedStatement)9451 SQLException (java.sql.SQLException)6466 Connection (java.sql.Connection)6445 Statement (java.sql.Statement)4619 Test (org.junit.Test)3647 ArrayList (java.util.ArrayList)2376 Properties (java.util.Properties)1233 HashMap (java.util.HashMap)639 ResultSetMetaData (java.sql.ResultSetMetaData)620 CallableStatement (java.sql.CallableStatement)580 DatabaseMetaData (java.sql.DatabaseMetaData)499 IOException (java.io.IOException)438 List (java.util.List)433 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)414 Timestamp (java.sql.Timestamp)363 Map (java.util.Map)359 BigDecimal (java.math.BigDecimal)350 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)292 HashSet (java.util.HashSet)247