diff --git a/src/net/java/sip/communicator/impl/msghistory/MessageHistoryServiceImpl.java b/src/net/java/sip/communicator/impl/msghistory/MessageHistoryServiceImpl.java index 4a3a77d09..7d6bc08ac 100644 --- a/src/net/java/sip/communicator/impl/msghistory/MessageHistoryServiceImpl.java +++ b/src/net/java/sip/communicator/impl/msghistory/MessageHistoryServiceImpl.java @@ -61,10 +61,10 @@ public HistoryService getHistoryService() { return historyService; } - public QueryResultSet findByStartDate(MetaContact contact, Date startDate) + public Collection findByStartDate(MetaContact contact, Date startDate) throws RuntimeException { - Vector result = new Vector(); + LinkedList result = new LinkedList(); Iterator iter = contact.getContacts(); while (iter.hasNext()) @@ -78,7 +78,8 @@ public QueryResultSet findByStartDate(MetaContact contact, Date startDate) Iterator recs = history.getReader().findByStartDate(startDate); while (recs.hasNext()) { - result.add(recs.next()); + HistoryRecord hr = (HistoryRecord)recs.next(); + result.add(convertHistoryRecordToMessageEvent(hr, item)); } } catch (IOException e) { @@ -86,13 +87,13 @@ public QueryResultSet findByStartDate(MetaContact contact, Date startDate) } } - return new DefaultQueryResultSet(result); + return result; } - public QueryResultSet findByEndDate(MetaContact contact, Date endDate) + public Collection findByEndDate(MetaContact contact, Date endDate) throws RuntimeException { - Vector result = new Vector(); + LinkedList result = new LinkedList(); Iterator iter = contact.getContacts(); while (iter.hasNext()) @@ -106,7 +107,7 @@ public QueryResultSet findByEndDate(MetaContact contact, Date endDate) Iterator recs = history.getReader().findByEndDate(endDate); while (recs.hasNext()) { - result.add(recs.next()); + result.add(convertHistoryRecordToMessageEvent((HistoryRecord)recs.next(), item)); } } catch (IOException e) { @@ -114,13 +115,13 @@ public QueryResultSet findByEndDate(MetaContact contact, Date endDate) } } - return new DefaultQueryResultSet(result); + return result; } - public QueryResultSet findByPeriod(MetaContact contact, Date startDate, Date endDate) + public Collection findByPeriod(MetaContact contact, Date startDate, Date endDate) throws RuntimeException { - Vector result = new Vector(); + LinkedList result = new LinkedList(); Iterator iter = contact.getContacts(); while (iter.hasNext()) @@ -134,7 +135,11 @@ public QueryResultSet findByPeriod(MetaContact contact, Date startDate, Date end Iterator recs = history.getReader().findByPeriod(startDate, endDate); while (recs.hasNext()) { - result.add(recs.next()); + result.add( + convertHistoryRecordToMessageEvent( + (HistoryRecord)recs.next(), + item)); + } } catch (IOException e) { @@ -142,15 +147,15 @@ public QueryResultSet findByPeriod(MetaContact contact, Date startDate, Date end } } - return new DefaultQueryResultSet(result); + return result; } - public QueryResultSet findByPeriod(MetaContact contact, + public Collection findByPeriod(MetaContact contact, Date startDate, Date endDate, String[] keywords) throws UnsupportedOperationException { - Vector result = new Vector(); + LinkedList result = new LinkedList(); Iterator iter = contact.getContacts(); while (iter.hasNext()) @@ -165,7 +170,10 @@ public QueryResultSet findByPeriod(MetaContact contact, findByPeriod(startDate, endDate, keywords, SEARCH_FIELD); while (recs.hasNext()) { - result.add(recs.next()); + result.add( + convertHistoryRecordToMessageEvent( + (HistoryRecord)recs.next(), + item)); } } catch (IOException e) { @@ -173,13 +181,13 @@ public QueryResultSet findByPeriod(MetaContact contact, } } - return new DefaultQueryResultSet(result); + return result; } - public QueryResultSet findByKeyword(MetaContact contact, String keyword) + public Collection findByKeyword(MetaContact contact, String keyword) throws RuntimeException { - Vector result = new Vector(); + LinkedList result = new LinkedList(); Iterator iter = contact.getContacts(); while (iter.hasNext()) @@ -194,7 +202,11 @@ public QueryResultSet findByKeyword(MetaContact contact, String keyword) findByKeyword(keyword, SEARCH_FIELD); while (recs.hasNext()) { - result.add(recs.next()); + result.add( + convertHistoryRecordToMessageEvent( + (HistoryRecord)recs.next(), + item)); + } } catch (IOException e) { @@ -202,13 +214,13 @@ public QueryResultSet findByKeyword(MetaContact contact, String keyword) } } - return new DefaultQueryResultSet(result); + return result; } - public QueryResultSet findByKeywords(MetaContact contact, String[] keywords) + public Collection findByKeywords(MetaContact contact, String[] keywords) throws RuntimeException { - Vector result = new Vector(); + LinkedList result = new LinkedList(); Iterator iter = contact.getContacts(); while (iter.hasNext()) @@ -223,7 +235,11 @@ public QueryResultSet findByKeywords(MetaContact contact, String[] keywords) findByKeywords(keywords, SEARCH_FIELD); while (recs.hasNext()) { - result.add(recs.next()); + result.add( + convertHistoryRecordToMessageEvent( + (HistoryRecord)recs.next(), + item)); + } } catch (IOException e) { @@ -231,13 +247,13 @@ public QueryResultSet findByKeywords(MetaContact contact, String[] keywords) } } - return new DefaultQueryResultSet(result); + return result; } - public QueryResultSet findLast(MetaContact contact, int count) + public Collection findLast(MetaContact contact, int count) throws RuntimeException { - List result = new ArrayList(); + List result = new LinkedList(); // too stupid but there is no such metod in the history service // to be implemented @@ -257,7 +273,11 @@ public QueryResultSet findLast(MetaContact contact, int count) Iterator recs = history.getReader().findByStartDate(startDate); while (recs.hasNext()) { - result.add(recs.next()); + result.add( + convertHistoryRecordToMessageEvent( + (HistoryRecord)recs.next(), + item)); + } } catch (IOException e) { @@ -270,7 +290,7 @@ public QueryResultSet findLast(MetaContact contact, int count) result = result.subList(result.size() - count, result.size()); } - return new DefaultQueryResultSet(new Vector(result)); + return new LinkedList(result); } private History getHistory(Contact localContact, Contact remoteContact) @@ -295,6 +315,23 @@ private History getHistory(Contact localContact, Contact remoteContact) return retVal; } + private Object convertHistoryRecordToMessageEvent(HistoryRecord hr, Contact contact) + { + MessageImpl msg = new MessageImpl(hr); + if(msg.isOutgoing) + { + return new MessageDeliveredEvent( + new MessageImpl(hr), + contact, + hr.getTimestamp()); + } + else + return new MessageReceivedEvent( + new MessageImpl(hr), + contact, + hr.getTimestamp()); + } + /** * starts the service. Check the current registerd protocol providers * which supports BasicIM and adds message listener to them @@ -508,4 +545,98 @@ private void handleProviderAdded( } } + /** + * Simple message implementation. + */ + private class MessageImpl + implements Message + { + private String textContent = null; + private String contentType = null; + private String contentEncoding = null; + private String messageUID = null; + private String subject = null; + + private boolean isOutgoing = false; + + MessageImpl(HistoryRecord hr) + { + // History structure + // 0 - dir + // 1 - msg_CDATA + // 2 - msgTyp + // 3 - enc + // 4- uid + // 5 - sub + + for (int i = 0; i < hr.getPropertyNames().length; i++) + { + String propName = hr.getPropertyNames()[i]; + if(propName.equals("msg") || propName.equals("msg_CDATA")) + textContent = hr.getPropertyValues()[i]; + else if(propName.equals("msgTyp")) + contentType = hr.getPropertyValues()[i]; + else if(propName.equals("enc")) + contentEncoding = hr.getPropertyValues()[i]; + else if(propName.equals("uid")) + messageUID = hr.getPropertyValues()[i]; + else if(propName.equals("sub")) + subject = hr.getPropertyValues()[i]; + else if(propName.equals("dir")) + if(hr.getPropertyValues()[i].equals("in")) + isOutgoing = false; + else if(hr.getPropertyValues()[i].equals("out")) + isOutgoing = true; + } + } + + public MessageImpl(String content, + String contentType, + String contentEncoding, + String subject, + String messageUID) + { + this.textContent = content; + this.contentType = contentType; + this.contentEncoding = contentEncoding; + this.subject = subject; + this.messageUID = messageUID; + } + + public String getContent() + { + return textContent; + } + + public String getContentType() + { + return contentType; + } + + public String getEncoding() + { + return contentEncoding; + } + + public String getMessageUID() + { + return messageUID; + } + + public byte[] getRawData() + { + return getContent().getBytes(); + } + + public int getSize() + { + return getContent().length(); + } + + public String getSubject() + { + return subject; + } + } + } diff --git a/src/net/java/sip/communicator/service/msghistory/MessageHistoryService.java b/src/net/java/sip/communicator/service/msghistory/MessageHistoryService.java index 1f12e60d1..f07960a17 100644 --- a/src/net/java/sip/communicator/service/msghistory/MessageHistoryService.java +++ b/src/net/java/sip/communicator/service/msghistory/MessageHistoryService.java @@ -17,18 +17,18 @@ */ public interface MessageHistoryService { - QueryResultSet findByStartDate(MetaContact contact, Date startDate) + Collection findByStartDate(MetaContact contact, Date startDate) throws RuntimeException; - QueryResultSet findByEndDate(MetaContact contact, Date endDate) + Collection findByEndDate(MetaContact contact, Date endDate) throws RuntimeException; - QueryResultSet findByPeriod(MetaContact contact, Date startDate, Date endDate) + Collection findByPeriod(MetaContact contact, Date startDate, Date endDate) throws RuntimeException; - QueryResultSet findByPeriod(MetaContact contact, Date startDate, Date endDate, String[] keywords) + Collection findByPeriod(MetaContact contact, Date startDate, Date endDate, String[] keywords) throws UnsupportedOperationException; - QueryResultSet findByKeyword(MetaContact contact, String keyword) + Collection findByKeyword(MetaContact contact, String keyword) throws RuntimeException; - QueryResultSet findByKeywords(MetaContact contact, String[] keywords) + Collection findByKeywords(MetaContact contact, String[] keywords) throws RuntimeException; - QueryResultSet findLast(MetaContact contact, int count) + Collection findLast(MetaContact contact, int count) throws RuntimeException; } diff --git a/test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryService.java b/test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryService.java index bd1d61d27..c8c8e6ed4 100644 --- a/test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryService.java +++ b/test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryService.java @@ -17,6 +17,7 @@ import net.java.sip.communicator.service.msghistory.*; import net.java.sip.communicator.service.protocol.*; import net.java.sip.communicator.util.*; +import net.java.sip.communicator.service.protocol.event.*; /** * Tests message history. @@ -222,9 +223,9 @@ public void readRecords() /** * This matches all written messages, they are minimum 5 */ - QueryResultSet rs = msgHistoryService.findByKeyword(testMetaContact, "test"); + Collection rs = msgHistoryService.findByKeyword(testMetaContact, "test"); - assertTrue("Nothing found findByKeyword ", rs.hasNext()); + assertTrue("Nothing found findByKeyword ", !rs.isEmpty()); Vector msgs = getMessages(rs); @@ -236,7 +237,7 @@ public void readRecords() */ rs = msgHistoryService.findByEndDate(testMetaContact, controlDate2); - assertTrue("Nothing found findByEndDate", rs.hasNext()); + assertTrue("Nothing found findByEndDate", !rs.isEmpty()); msgs = getMessages(rs); @@ -249,7 +250,7 @@ public void readRecords() testMetaContact, new String[]{"test", "word2"}); - assertTrue("Nothing found findByKeywords", rs.hasNext()); + assertTrue("Nothing found findByKeywords", !rs.isEmpty()); msgs = getMessages(rs); assertTrue("Messages too few - findByKeywords", msgs.size() >= 1); @@ -260,7 +261,7 @@ public void readRecords() testMetaContact, new String[]{"test1", "word2"}); - assertFalse("Something found findByKeywords", rs.hasNext()); + assertFalse("Something found findByKeywords", !rs.isEmpty()); /** * must find 2 messages @@ -268,9 +269,10 @@ public void readRecords() rs = msgHistoryService.findByPeriod( testMetaContact, controlDate1, controlDate2); - assertTrue("Nothing found findByPeriod", rs.hasNext()); + assertTrue("Nothing found findByPeriod", !rs.isEmpty()); msgs = getMessages(rs); + assertEquals("Messages must be 2", msgs.size(), 2); assertTrue("Message no found", @@ -284,7 +286,7 @@ public void readRecords() rs = msgHistoryService.findByPeriod( testMetaContact, controlDate1, controlDate2, new String[]{"word2"}); - assertTrue("Nothing found findByPeriod", rs.hasNext()); + assertTrue("Nothing found findByPeriod", !rs.isEmpty()); msgs = getMessages(rs); @@ -297,7 +299,7 @@ public void readRecords() */ rs = msgHistoryService.findByStartDate(testMetaContact, controlDate2); - assertTrue("Nothing found findByStartDate", rs.hasNext()); + assertTrue("Nothing found findByStartDate", !rs.isEmpty()); msgs = getMessages(rs); assertEquals("Messages must be 2", msgs.size(), 2); assertTrue("Message no found", @@ -310,7 +312,7 @@ public void readRecords() */ rs = msgHistoryService.findLast(testMetaContact, 3); - assertTrue("Nothing found 8", rs.hasNext()); + assertTrue("Nothing found 8", !rs.isEmpty()); msgs = getMessages(rs); assertEquals("Messages must be 3", msgs.size(), 3); assertTrue("Message no found", @@ -331,25 +333,24 @@ public void testPurgeLocalContactListCopy() metaClService.purgeLocallyStoredContactListCopy(); } - private Vector getMessages(QueryResultSet rs) + private Vector getMessages(Collection rs) { Vector result = new Vector(); - while (rs.hasNext()) + Iterator iter = rs.iterator(); + while (iter.hasNext()) { - HistoryRecord hr = (HistoryRecord)rs.next(); - for (int i = 0; i < hr.getPropertyNames().length; i++) - { - if(hr.getPropertyNames()[i].equals("msg")) - { - result.add(hr.getPropertyValues()[i]); - break; - } - } + Object item = (Object) iter.next(); + if(item instanceof MessageDeliveredEvent) + result.add(((MessageDeliveredEvent)item).getSourceMessage().getContent()); + else + if(item instanceof MessageReceivedEvent) + result.add(((MessageReceivedEvent)item).getSourceMessage().getContent()); } return result; } + private void dumpResult(QueryResultSet rs) { while (rs.hasNext())