Add new finders in history and message history

cusax-fix
Damian Minkov 19 years ago
parent e1987ea9bf
commit 8583bf5e44

@ -303,6 +303,191 @@ public synchronized QueryResultSet findByPeriod(Date startDate, Date endDate,
return find(startDate, endDate, keywords, field, caseSensitive);
}
/**
* Returns the supplied number of recent messages after the given date
*
* @param date messages after date
* @param count messages count
* @return QueryResultSet the found records
* @throws RuntimeException
*/
public QueryResultSet getFirstRecordsAfter(Date date, int count) throws
RuntimeException
{
TreeSet result = new TreeSet(new HistoryRecordComparator());
Vector filelist =
filterFilesByDate(this.historyImpl.getFileList(), date, null);
int leftCount = count;
int currentFile = 0;
while(leftCount > 0 && currentFile < filelist.size())
{
Document doc = this.historyImpl.
getDocumentForFile( (String) filelist.get(currentFile));
if(doc == null)
{
currentFile++;
continue;
}
NodeList nodes = doc.getElementsByTagName("record");
Node node;
for (int i = 0; i < nodes.getLength() && leftCount > 0; i++)
{
node = nodes.item(i);
NodeList propertyNodes = node.getChildNodes();
String ts = node.getAttributes().getNamedItem("timestamp")
.getNodeValue();
Date timestamp = new Date(Long.parseLong(ts));
if(!isInPeriod(timestamp, date, null))
continue;
ArrayList nameVals = new ArrayList();
boolean isRecordOK = true;
int len = propertyNodes.getLength();
for (int j = 0; j < len; j++)
{
Node propertyNode = propertyNodes.item(j);
if (propertyNode.getNodeType() == Node.ELEMENT_NODE)
{
// Get nested TEXT node's value
Node nodeValue = propertyNode.getFirstChild();
if(nodeValue != null)
{
nameVals.add(propertyNode.getNodeName());
nameVals.add(nodeValue.getNodeValue());
}
else
isRecordOK = false;
}
}
// if we found a broken record - just skip it
if(!isRecordOK)
continue;
String[] propertyNames = new String[nameVals.size() / 2];
String[] propertyValues = new String[propertyNames.length];
for (int j = 0; j < propertyNames.length; j++)
{
propertyNames[j] = (String) nameVals.get(j * 2);
propertyValues[j] = (String) nameVals.get(j * 2 + 1);
}
HistoryRecord record = new HistoryRecord(propertyNames,
propertyValues, timestamp);
result.add(record);
leftCount--;
}
currentFile++;
}
return new OrderedQueryResultSet(result);
}
/**
* Returns the supplied number of recent messages before the given date
*
* @param date messages before date
* @param count messages count
* @return QueryResultSet the found records
* @throws RuntimeException
*/
public QueryResultSet getLastRecordsBefore(Date date, int count) throws
RuntimeException
{
// the files are supposed to be ordered from oldest to newest
Vector filelist =
filterFilesByDate(this.historyImpl.getFileList(), null, date);
TreeSet result = new TreeSet(new HistoryRecordComparator());
int leftCount = count;
int currentFile = filelist.size() - 1;
while(leftCount > 0 && currentFile >= 0)
{
Document doc = this.historyImpl.
getDocumentForFile( (String) filelist.get(currentFile));
if(doc == null)
{
currentFile--;
continue;
}
NodeList nodes = doc.getElementsByTagName("record");
Node node;
for (int i = 0; i < nodes.getLength() && leftCount > 0; i++)
{
node = nodes.item(i);
NodeList propertyNodes = node.getChildNodes();
String ts = node.getAttributes().getNamedItem("timestamp")
.getNodeValue();
Date timestamp = new Date(Long.parseLong(ts));
if(!isInPeriod(timestamp, null, date))
continue;
ArrayList nameVals = new ArrayList();
boolean isRecordOK = true;
int len = propertyNodes.getLength();
for (int j = 0; j < len; j++)
{
Node propertyNode = propertyNodes.item(j);
if (propertyNode.getNodeType() == Node.ELEMENT_NODE)
{
// Get nested TEXT node's value
Node nodeValue = propertyNode.getFirstChild();
if(nodeValue != null)
{
nameVals.add(propertyNode.getNodeName());
nameVals.add(nodeValue.getNodeValue());
}
else
isRecordOK = false;
}
}
// if we found a broken record - just skip it
if(!isRecordOK)
continue;
String[] propertyNames = new String[nameVals.size() / 2];
String[] propertyValues = new String[propertyNames.length];
for (int j = 0; j < propertyNames.length; j++)
{
propertyNames[j] = (String) nameVals.get(j * 2);
propertyValues[j] = (String) nameVals.get(j * 2 + 1);
}
HistoryRecord record = new HistoryRecord(propertyNames,
propertyValues, timestamp);
result.add(record);
leftCount--;
}
currentFile--;
}
return new OrderedQueryResultSet(result);
}
private QueryResultSet find(
Date startDate, Date endDate,
String[] keywords, String field, boolean caseSensitive)

@ -294,6 +294,105 @@ public Collection findLast(MetaContact contact, int count)
return resultAsList.subList(startIndex, resultAsList.size());
}
/**
* Returns the supplied number of recent messages after the given date
* exchanged by all the contacts in the supplied metacontact
*
* @param contact MetaContact
* @param date messages after date
* @param count messages count
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
public Collection getFirstMessagesAfter(MetaContact contact, Date date,
int count) throws RuntimeException
{
TreeSet result = new TreeSet(new MessageEventComparator());
Iterator iter = contact.getContacts();
while (iter.hasNext())
{
Contact item = (Contact) iter.next();
try
{
History history = this.getHistory(null, item);
HistoryReader reader = history.getReader();
Iterator recs = reader.getFirstRecordsAfter(date, count);
while (recs.hasNext())
{
result.add(
convertHistoryRecordToMessageEvent(
(HistoryRecord)recs.next(),
item));
}
} catch (IOException e)
{
logger.error("Could not read history", e);
}
}
LinkedList resultAsList = new LinkedList(result);
int startIndex = resultAsList.size() - count;
if(startIndex < 0)
startIndex = 0;
return resultAsList.subList(startIndex, resultAsList.size());
}
/**
* Returns the supplied number of recent messages before the given date
* exchanged by all the contacts in the supplied metacontact
*
* @param contact MetaContact
* @param date messages before date
* @param count messages count
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
public Collection getLastMessagesBefore(MetaContact contact, Date date,
int count) throws RuntimeException
{
TreeSet result = new TreeSet(new MessageEventComparator());
Iterator iter = contact.getContacts();
while (iter.hasNext())
{
Contact item = (Contact) iter.next();
try
{
History history = this.getHistory(null, item);
HistoryReader reader = history.getReader();
Iterator recs = reader.getLastRecordsBefore(date, count);
while (recs.hasNext())
{
result.add(
convertHistoryRecordToMessageEvent(
(HistoryRecord)recs.next(),
item));
}
} catch (IOException e)
{
logger.error("Could not read history", e);
}
}
LinkedList resultAsList = new LinkedList(result);
int startIndex = resultAsList.size() - count;
if(startIndex < 0)
startIndex = 0;
return resultAsList.subList(startIndex, resultAsList.size());
}
/**
* Returns the history by specified local and remote contact
* if one of them is null the default is used

@ -27,7 +27,7 @@ public interface HistoryReader {
* Thrown if an exception occurs during the execution of the
* query, such as internal IO error.
*/
QueryResultSet findByStartDate(Date startDate) throws RuntimeException;
public QueryResultSet findByStartDate(Date startDate) throws RuntimeException;
/**
* Searches the history for all records with timestamp before
@ -39,7 +39,7 @@ public interface HistoryReader {
* Thrown if an exception occurs during the execution of the
* query, such as internal IO error.
*/
QueryResultSet findByEndDate(Date endDate) throws RuntimeException;
public QueryResultSet findByEndDate(Date endDate) throws RuntimeException;
/**
* Searches the history for all records with timestamp between
@ -52,7 +52,7 @@ public interface HistoryReader {
* Thrown if an exception occurs during the execution of the
* query, such as internal IO error.
*/
QueryResultSet findByPeriod(Date startDate, Date endDate)
public QueryResultSet findByPeriod(Date startDate, Date endDate)
throws RuntimeException;
/**
@ -65,7 +65,7 @@ QueryResultSet findByPeriod(Date startDate, Date endDate)
* Thrown if an exception occurs during the execution of the
* query, such as internal IO error.
*/
QueryResultSet findByKeyword(String keyword, String field) throws RuntimeException;
public QueryResultSet findByKeyword(String keyword, String field) throws RuntimeException;
/**
* Searches the history for all records containing the <tt>keyword</tt>.
@ -78,7 +78,7 @@ QueryResultSet findByPeriod(Date startDate, Date endDate)
* Thrown if an exception occurs during the execution of the
* query, such as internal IO error.
*/
QueryResultSet findByKeyword(String keyword, String field, boolean caseSensitive)
public QueryResultSet findByKeyword(String keyword, String field, boolean caseSensitive)
throws RuntimeException;
/**
@ -91,7 +91,7 @@ QueryResultSet findByKeyword(String keyword, String field, boolean caseSensitive
* Thrown if an exception occurs during the execution of the
* query, such as internal IO error.
*/
QueryResultSet findByKeywords(String[] keywords, String field) throws RuntimeException;
public QueryResultSet findByKeywords(String[] keywords, String field) throws RuntimeException;
/**
* Searches the history for all records containing all <tt>keywords</tt>.
@ -104,7 +104,7 @@ QueryResultSet findByKeyword(String keyword, String field, boolean caseSensitive
* Thrown if an exception occurs during the execution of the
* query, such as internal IO error.
*/
QueryResultSet findByKeywords(String[] keywords, String field, boolean caseSensitive)
public QueryResultSet findByKeywords(String[] keywords, String field, boolean caseSensitive)
throws RuntimeException;
/**
@ -120,7 +120,7 @@ QueryResultSet findByKeywords(String[] keywords, String field, boolean caseSensi
* Thrown if an exception occurs during the execution of the
* query, such as internal IO error.
*/
QueryResultSet findByPeriod(Date startDate, Date endDate,
public QueryResultSet findByPeriod(Date startDate, Date endDate,
String[] keywords, String field)
throws UnsupportedOperationException;
@ -138,7 +138,7 @@ QueryResultSet findByPeriod(Date startDate, Date endDate,
* Thrown if an exception occurs during the execution of the
* query, such as internal IO error.
*/
QueryResultSet findByPeriod(Date startDate, Date endDate,
public QueryResultSet findByPeriod(Date startDate, Date endDate,
String[] keywords, String field, boolean caseSensitive)
throws UnsupportedOperationException;
@ -151,18 +151,37 @@ QueryResultSet findByPeriod(Date startDate, Date endDate,
*/
QueryResultSet findLast(int count) throws RuntimeException;
/**
* Returns the supplied number of recent messages after the given date
*
* @param date messages after date
* @param count messages count
* @return QueryResultSet the found records
* @throws RuntimeException
*/
public QueryResultSet getFirstRecordsAfter(Date date, int count) throws RuntimeException;
/**
* Returns the supplied number of recent messages before the given date
*
* @param date messages before date
* @param count messages count
* @return QueryResultSet the found records
* @throws RuntimeException
*/
public QueryResultSet getLastRecordsBefore(Date date, int count) throws RuntimeException;
/**
* Adding progress listener for monitoring progress of search process
*
* @param listener HistorySearchProgressListener
*/
void addSearchProgressListener(HistorySearchProgressListener listener);
public void addSearchProgressListener(HistorySearchProgressListener listener);
/**
* Removing progress listener
*
* @param listener HistorySearchProgressListener
*/
void removeSearchProgressListener(HistorySearchProgressListener listener);
public void removeSearchProgressListener(HistorySearchProgressListener listener);
}

@ -28,7 +28,7 @@ public interface MessageHistoryService
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
Collection findByStartDate(MetaContact contact, Date startDate)
public Collection findByStartDate(MetaContact contact, Date startDate)
throws RuntimeException;
/**
@ -40,7 +40,7 @@ Collection findByStartDate(MetaContact contact, Date startDate)
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
Collection findByEndDate(MetaContact contact, Date endDate)
public Collection findByEndDate(MetaContact contact, Date endDate)
throws RuntimeException;
/**
@ -53,7 +53,7 @@ Collection findByEndDate(MetaContact contact, Date endDate)
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
Collection findByPeriod(MetaContact contact, Date startDate, Date endDate)
public Collection findByPeriod(MetaContact contact, Date startDate, Date endDate)
throws RuntimeException;
/**
@ -68,7 +68,7 @@ Collection findByPeriod(MetaContact contact, Date startDate, Date endDate)
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
Collection findByPeriod(MetaContact contact, Date startDate, Date endDate, String[] keywords)
public Collection findByPeriod(MetaContact contact, Date startDate, Date endDate, String[] keywords)
throws RuntimeException;
/**
@ -84,7 +84,7 @@ Collection findByPeriod(MetaContact contact, Date startDate, Date endDate, Strin
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
Collection findByPeriod(MetaContact contact, Date startDate, Date endDate,
public Collection findByPeriod(MetaContact contact, Date startDate, Date endDate,
String[] keywords, boolean caseSensitive)
throws RuntimeException;
@ -97,7 +97,7 @@ Collection findByPeriod(MetaContact contact, Date startDate, Date endDate,
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
Collection findByKeyword(MetaContact contact, String keyword)
public Collection findByKeyword(MetaContact contact, String keyword)
throws RuntimeException;
/**
@ -122,7 +122,7 @@ Collection findByKeyword(MetaContact contact, String keyword, boolean caseSensit
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
Collection findByKeywords(MetaContact contact, String[] keywords)
public Collection findByKeywords(MetaContact contact, String[] keywords)
throws RuntimeException;
/**
@ -135,7 +135,7 @@ Collection findByKeywords(MetaContact contact, String[] keywords)
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
Collection findByKeywords(MetaContact contact, String[] keywords, boolean caseSensitive)
public Collection findByKeywords(MetaContact contact, String[] keywords, boolean caseSensitive)
throws RuntimeException;
/**
@ -147,7 +147,33 @@ Collection findByKeywords(MetaContact contact, String[] keywords, boolean caseSe
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
Collection findLast(MetaContact contact, int count)
public Collection findLast(MetaContact contact, int count)
throws RuntimeException;
/**
* Returns the supplied number of recent messages after the given date
* exchanged by all the contacts in the supplied metacontact
*
* @param contact MetaContact
* @param date messages after date
* @param count messages count
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
public Collection getFirstMessagesAfter(MetaContact contact, Date date, int count)
throws RuntimeException;
/**
* Returns the supplied number of recent messages before the given date
* exchanged by all the contacts in the supplied metacontact
*
* @param contact MetaContact
* @param date messages before date
* @param count messages count
* @return Collection of MessageReceivedEvents or MessageDeliveredEvents
* @throws RuntimeException
*/
public Collection getLastMessagesBefore(MetaContact contact, Date date, int count)
throws RuntimeException;
/**
@ -155,13 +181,12 @@ Collection findLast(MetaContact contact, int count)
*
* @param listener HistorySearchProgressListener
*/
void addSearchProgressListener(MessageHistorySearchProgressListener listener);
public void addSearchProgressListener(MessageHistorySearchProgressListener listener);
/**
* Removing progress listener
*
* @param listener HistorySearchProgressListener
*/
void removeSearchProgressListener(MessageHistorySearchProgressListener listener);
public void removeSearchProgressListener(MessageHistorySearchProgressListener listener);
}

@ -337,6 +337,35 @@ public void readRecords()
assertTrue("Message no found",
msgs.contains(messagesToSend[4].getContent()));
/**
* Must return exactly the 3 messages after controlDate1
*/
rs = msgHistoryService.getFirstMessagesAfter(testMetaContact, controlDate1, 3);
assertTrue("Nothing found 9", !rs.isEmpty());
msgs = getMessages(rs);
assertEquals("Messages must be 3", msgs.size(), 3);
assertTrue("Message no found",
msgs.contains(messagesToSend[1].getContent()));
assertTrue("Message no found",
msgs.contains(messagesToSend[2].getContent()));
assertTrue("Message no found",
msgs.contains(messagesToSend[3].getContent()));
/**
* Must return exactly the 3 messages before controlDate2
*/
rs = msgHistoryService.getLastMessagesBefore(testMetaContact, controlDate2, 3);
assertTrue("Nothing found 10", !rs.isEmpty());
msgs = getMessages(rs);
assertEquals("Messages must be 3", msgs.size(), 3);
assertTrue("Message no found",
msgs.contains(messagesToSend[0].getContent()));
assertTrue("Message no found",
msgs.contains(messagesToSend[1].getContent()));
assertTrue("Message no found",
msgs.contains(messagesToSend[2].getContent()));
}
/**

Loading…
Cancel
Save