Search in sources :

Example 71 with ResultSet

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

the class ArchiveIndexer method updateIndex.

/**
     * Updates the search index with all new conversation data since the last index update.
     */
public void updateIndex() {
    // Immediately return if the service has been stopped.
    if (stopped) {
        return;
    }
    // Do nothing if archiving is disabled.
    if (!conversationManager.isArchivingEnabled()) {
        return;
    }
    // If we're currently rebuilding the index, return.
    if (rebuildInProgress) {
        return;
    }
    writerLock.lock();
    IndexModifier writer = null;
    try {
        writer = new IndexModifier(directory, new StandardAnalyzer(), false);
        List<Long> conversationIDs = new ArrayList<Long>();
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(NEW_CONVERSATIONS);
            pstmt.setLong(1, lastModified);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                conversationIDs.add(rs.getLong(1));
            }
        } catch (SQLException sqle) {
            Log.error(sqle.getMessage(), sqle);
        } finally {
            DbConnectionManager.closeConnection(rs, pstmt, con);
        }
        // updated since then.
        for (long conversationID : conversationIDs) {
            writer.deleteDocuments(new Term("conversationID", Long.toString(conversationID)));
        }
        // Load meta-data for each conversation.
        Map<Long, Boolean> externalMetaData = new HashMap<Long, Boolean>();
        for (long conversationID : conversationIDs) {
            try {
                con = DbConnectionManager.getConnection();
                pstmt = con.prepareStatement(CONVERSATION_METADATA);
                pstmt.setLong(1, conversationID);
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    externalMetaData.put(conversationID, rs.getInt(1) == 1);
                }
            } catch (SQLException sqle) {
                Log.error(sqle.getMessage(), sqle);
            } finally {
                DbConnectionManager.closeConnection(rs, pstmt, con);
            }
        }
        // Now index all the new conversations.
        long newestDate = indexConversations(conversationIDs, externalMetaData, writer, false);
        writer.optimize();
        // Done indexing so store a last modified date.
        if (newestDate != -1) {
            lastModified = newestDate;
            indexProperties.setProperty("lastModified", Long.toString(lastModified));
        }
    } catch (IOException ioe) {
        Log.error(ioe.getMessage(), ioe);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                Log.error(e.getMessage(), e);
            }
        }
        writerLock.unlock();
    }
}
Also used : SQLException(java.sql.SQLException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) IndexModifier(org.apache.lucene.index.IndexModifier) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ResultSet(java.sql.ResultSet)

Example 72 with ResultSet

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

the class Conversation method loadFromDb.

private void loadFromDb() throws NotFoundException {
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(LOAD_CONVERSATION);
        pstmt.setLong(1, conversationID);
        rs = pstmt.executeQuery();
        if (!rs.next()) {
            throw new NotFoundException("Conversation not found: " + conversationID);
        }
        this.room = rs.getString(1) == null ? null : new JID(rs.getString(1));
        this.external = rs.getInt(2) == 1;
        this.startDate = new Date(rs.getLong(3));
        this.lastActivity = new Date(rs.getLong(4));
        this.messageCount = rs.getInt(5);
        rs.close();
        pstmt.close();
        this.participants = new ConcurrentHashMap<String, UserParticipations>();
        pstmt = con.prepareStatement(LOAD_PARTICIPANTS);
        pstmt.setLong(1, conversationID);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            // Rebuild full JID of participant
            String baredJID = rs.getString(1);
            String resource = rs.getString(2);
            JID fullJID = new JID("".equals(resource) ? baredJID : baredJID + "/" + resource);
            // Rebuild joined and left time
            ConversationParticipation participation = new ConversationParticipation(new Date(rs.getLong(4)), rs.getString(3));
            if (rs.getLong(5) > 0) {
                participation.participationEnded(new Date(rs.getLong(5)));
            }
            // Store participation data
            UserParticipations userParticipations = participants.get(fullJID.toString());
            if (userParticipations == null) {
                userParticipations = new UserParticipations(room != null);
                participants.put(fullJID.toString(), userParticipations);
            }
            userParticipations.addParticipation(participation);
        }
    } catch (SQLException sqle) {
        Log.error(sqle.getMessage(), sqle);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
}
Also used : JID(org.xmpp.packet.JID) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) NotFoundException(org.jivesoftware.util.NotFoundException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) PreparedStatement(java.sql.PreparedStatement) Date(java.util.Date)

Example 73 with ResultSet

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

the class Conversation method getMessages.

/**
	 * Returns the archived messages in the conversation. If message archiving is not enabled, this method will always return an empty collection.
	 * This method will only return messages that have already been batch-archived to the database; in other words, it does not provide a real-time
	 * view of new messages.
	 * 
	 * @return the archived messages in the conversation.
	 */
public List<ArchivedMessage> getMessages() {
    if (room == null && !conversationManager.isMessageArchivingEnabled()) {
        return Collections.emptyList();
    } else if (room != null && !conversationManager.isRoomArchivingEnabled()) {
        return Collections.emptyList();
    }
    List<ArchivedMessage> messages = new ArrayList<ArchivedMessage>();
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(LOAD_MESSAGES);
        pstmt.setLong(1, getConversationID());
        rs = pstmt.executeQuery();
        while (rs.next()) {
            JID fromJID = new JID(rs.getString(1));
            String fromJIDResource = rs.getString(2);
            if (fromJIDResource != null && !"".equals(fromJIDResource)) {
                fromJID = new JID(rs.getString(1) + "/" + fromJIDResource);
            }
            JID toJID = new JID(rs.getString(3));
            String toJIDResource = rs.getString(4);
            if (toJIDResource != null && !"".equals(toJIDResource)) {
                toJID = new JID(rs.getString(3) + "/" + toJIDResource);
            }
            Date date = new Date(rs.getLong(5));
            String body = DbConnectionManager.getLargeTextField(rs, 6);
            messages.add(new ArchivedMessage(conversationID, fromJID, toJID, date, body, false));
        }
    } catch (SQLException sqle) {
        Log.error(sqle.getMessage(), sqle);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    // Add messages of users joining or leaving the group chat conversation
    if (room != null) {
        for (Map.Entry<String, UserParticipations> entry : participants.entrySet()) {
            JID user = new JID(entry.getKey());
            boolean anonymous = false;
            String name;
            try {
                name = UserNameManager.getUserName(user);
            } catch (UserNotFoundException e) {
                name = user.toBareJID();
                anonymous = true;
            }
            for (ConversationParticipation participation : entry.getValue().getParticipations()) {
                if (participation.getJoined() == null) {
                    Log.warn("Found muc participant with no join date in conversation: " + conversationID);
                    continue;
                }
                JID jid = new JID(room + "/" + participation.getNickname());
                String joinBody;
                String leftBody;
                if (anonymous) {
                    joinBody = LocaleUtils.getLocalizedString("muc.conversation.joined.anonymous", MonitoringConstants.NAME, Arrays.asList(participation.getNickname()));
                    leftBody = LocaleUtils.getLocalizedString("muc.conversation.left.anonymous", MonitoringConstants.NAME, Arrays.asList(participation.getNickname()));
                } else {
                    joinBody = LocaleUtils.getLocalizedString("muc.conversation.joined", MonitoringConstants.NAME, Arrays.asList(participation.getNickname(), name));
                    leftBody = LocaleUtils.getLocalizedString("muc.conversation.left", MonitoringConstants.NAME, Arrays.asList(participation.getNickname(), name));
                }
                messages.add(new ArchivedMessage(conversationID, user, jid, participation.getJoined(), joinBody, true));
                if (participation.getLeft() != null) {
                    messages.add(new ArchivedMessage(conversationID, user, jid, participation.getLeft(), leftBody, true));
                }
            }
        }
        // Sort messages by sent date
        Collections.sort(messages, new Comparator<ArchivedMessage>() {

            public int compare(ArchivedMessage o1, ArchivedMessage o2) {
                return o1.getSentDate().compareTo(o2.getSentDate());
            }
        });
    }
    return messages;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) JID(org.xmpp.packet.JID) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Date(java.util.Date) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 74 with ResultSet

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

the class ConversationManager method getArchivedConversationCount.

/**
	 * Returns the total number of conversations that have been archived to the database. The archived conversation may only be the meta-data, or it
	 * might include messages as well if message archiving is turned on.
	 *
	 * @return the total number of archived conversations.
	 */
public int getArchivedConversationCount() {
    int conversationCount = 0;
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(CONVERSATION_COUNT);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            conversationCount = rs.getInt(1);
        }
    } catch (SQLException sqle) {
        Log.error(sqle.getMessage(), sqle);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    return conversationCount;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 75 with ResultSet

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

the class JdbcPersistenceManager method countMessages.

private Integer countMessages(Date startDate, Date endDate, String ownerJid, String withJid, String whereClause) {
    StringBuilder querySB;
    querySB = new StringBuilder(COUNT_MESSAGES);
    if (whereClause != null && whereClause.length() != 0) {
        querySB.append(" AND ").append(whereClause);
    }
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(querySB.toString());
        bindMessageParameters(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)

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