public static String renderSnippet(final ReadableWaveletData wavelet,
final int maxSnippetLength) {
final StringBuilder sb = new StringBuilder();
Set<String> docsIds = wavelet.getDocumentIds();
long newestLmt = -1;
ReadableBlipData newestBlip = null;
for (String docId : docsIds) {
ReadableBlipData blip = wavelet.getDocument(docId);
long currentLmt = blip.getLastModifiedTime();
if (currentLmt > newestLmt) {
newestLmt = currentLmt;
newestBlip = blip;
}
}
if (newestBlip == null) {
// Render whatever data we have and hope its good enough
sb.append(collateTextForWavelet(wavelet));
} else {
DocOp docOp = newestBlip.getContent().asOperation();
sb.append(collateTextForOps(Lists.newArrayList(docOp)));
sb.append(" ");
docOp.apply(InitializationCursorAdapter.adapt(new DocInitializationCursor() {
@Override
public void annotationBoundary(AnnotationBoundaryMap map) {
}
@Override
public void characters(String chars) {
// No chars in the conversation manifest
}
@Override
public void elementEnd() {
}
@Override
public void elementStart(String type, Attributes attrs) {
if (sb.length() >= maxSnippetLength) {
return;
}
if (DocumentConstants.BLIP.equals(type)) {
String blipId = attrs.get(DocumentConstants.BLIP_ID);
if (blipId != null) {
ReadableBlipData document = wavelet.getDocument(blipId);
if (document == null) {
// We see this when a blip has been deleted
return;
}
sb.append(collateTextForDocuments(Arrays.asList(document)));