throw new RuntimeException(errorString.toString()); // (handled below)
}
// 4) Add the results to the original data
SharePojo savedQueryShare = SharePojo.fromDb(DbManager.getSocial().getShare().findOne(
new BasicDBObject(SharePojo._id_, savedQuery._parentShare.get_id())), SharePojo.class);
if (null != savedQueryShare) {
DocumentQueueControlPojo toModify = DocumentQueueControlPojo.fromApi(savedQueryShare.getShare(), DocumentQueueControlPojo.class);
List<BasicDBObject> docs = (List<BasicDBObject>) rp.getData();
if ((null != docs) && !docs.isEmpty()) {
if (null == toModify.getQueueList()) {
toModify.setQueueList(new ArrayList<ObjectId>(docs.size()));
}
ObjectId ignoreBeforeId = toModify.getLastDocIdInserted();
ObjectId maxDocId = toModify.getLastDocIdInserted();
//DEBUG
//System.out.println("before, num docs=" + toModify.getQueueList().size() + " adding " + docs.size() + " from " + ignoreBeforeId);
// Some alerting preamble
StringBuffer alertText = null;
StringBuffer alertTitle = null;
String rootUrl = new PropertiesManager().getURLRoot().replace("/api/", "");
int maxDocsToAdd = 10; // (default)
boolean alert = false;
if ((null != toModify.getQueryInfo().getAlert()) && (null != toModify.getQueryInfo().getAlert().getEmailAddresses())
&& !toModify.getQueryInfo().getAlert().getEmailAddresses().isEmpty())
{
alert = true;
alertText = new StringBuffer();
if (null != toModify.getQueryInfo().getAlert().getMaxDocsToInclude()) {
maxDocsToAdd = toModify.getQueryInfo().getAlert().getMaxDocsToInclude();
if (maxDocsToAdd < 0) {
maxDocsToAdd = Integer.MAX_VALUE;
}
}
createAlertPreamble(alertText, toModify.getQueryInfo().getQuery(), savedQuery._parentShare.get_id(), rootUrl);
}//TESTED
// Add new docs...
int numDocsAdded = 0;
for (BasicDBObject doc: docs) {
ObjectId docId = doc.getObjectId(DocumentPojo._id_);
if (null != docId) {
if (null != ignoreBeforeId) {
if (docId.compareTo(ignoreBeforeId) <= 0) { // ie docId <= ignoreBeforeId
continue;
}
}//(end check if this doc has already been seen)
toModify.getQueueList().add(0, docId);
//Alerting
if (alert) {
// (this fn checks if the max number of docs have been added):
createAlertDocSummary(alertText, numDocsAdded, maxDocsToAdd, doc, rootUrl);
numDocsAdded++;
}
if (null == maxDocId) {
maxDocId = docId;
}
else if (maxDocId.compareTo(docId) < 0) { // ie maxDocId < docId
maxDocId = docId;
}
}//TESTED (test5)
}//(end loop over new docs)
// More alerting
if (alert && (numDocsAdded > 0)) {
alertTitle = new StringBuffer("IKANOW: Queue \"").append(toModify.getQueueName()).append("\" has ").append(numDocsAdded).append(" new");
if (numDocsAdded == 1) {
alertTitle.append(" document.");
}
else {
alertTitle.append(" documents.");
}
// (terminate the doc list)
if (maxDocsToAdd > 0) {
alertText.append("</ol>");
alertText.append("\n");
}
String to = (Arrays.toString(toModify.getQueryInfo().getAlert().getEmailAddresses().toArray()).replaceAll("[\\[\\]]", "")).replace(',', ';');
try {
new SendMail(null, to, alertTitle.toString(), alertText.toString()).send("text/html");
}
catch (Exception e) {
//DEBUG
//e.printStackTrace();
}
}//TESTED
// Remove old docs...
int maxDocs = query.output.docs.numReturn;
if (null != toModify.getMaxDocs()) { // override
maxDocs = toModify.getMaxDocs();
}
if (toModify.getQueueList().size() > maxDocs) {
toModify.setQueueList(toModify.getQueueList().subList(0, maxDocs));
}//TESTED (test2.2)
//DEBUG
//System.out.println("after, num docs=" + toModify.getQueueList().size() + " at " + maxDocId);
// Update share info:
toModify.setLastDocIdInserted(maxDocId);
// We've modified the share so update it:
savedQueryShare.setShare(toModify.toApi());
savedQueryShare.setModified(new Date());
DbManager.getSocial().getShare().save(savedQueryShare.toDb());
}//(end found some docs)
}//(end found share)