diff --git a/source/java/org/alfresco/repo/discussion/DiscussionServiceImpl.java b/source/java/org/alfresco/repo/discussion/DiscussionServiceImpl.java index dab100cf7c..0cdb935bc6 100644 --- a/source/java/org/alfresco/repo/discussion/DiscussionServiceImpl.java +++ b/source/java/org/alfresco/repo/discussion/DiscussionServiceImpl.java @@ -477,7 +477,8 @@ public class DiscussionServiceImpl implements DiscussionService } @Override - public PostInfo getPrimaryPost(TopicInfo topic) { + public PostInfo getPrimaryPost(TopicInfo topic) + { // First up, see if there is a post with the same name as the topic // (That's the normal Share case) PostInfo post = getPost(topic, topic.getSystemName()); @@ -499,6 +500,41 @@ public class DiscussionServiceImpl implements DiscussionService String postName = children.get(0).getQName().getLocalName(); return buildPost(postNodeRef, topic, postName, null); } + + @Override + public PostInfo getMostRecentPost(TopicInfo topic) + { + // Do a listing at the Node level, ordered by created date + // to get the most recent nodes + NodeBackedEntity node = null; + int skip = 0; + int pageSize = 10000; + while(node == null) + { + PagingRequest paging = new PagingRequest(skip, pageSize); + paging.setRequestTotalCountMax(skip+pageSize+10); + CannedQueryResults results = + listEntries(topic.getNodeRef(), ForumModel.TYPE_POST, null, paging); + + // Move to the next page if we're not at the end + if(results.hasMoreItems()) + { + skip += pageSize; + continue; + } + // Bail if the topic lacks posts + if(results.getPage().size() == 0) + { + // No posts in the topic + return null; + } + // Grab the last node + node = results.getPage().get(results.getPage().size()-1); + } + + // Wrap and return + return buildPost(node.getNodeRef(), topic, node.getName(), null); + } @Override diff --git a/source/java/org/alfresco/repo/discussion/DiscussionServiceImplTest.java b/source/java/org/alfresco/repo/discussion/DiscussionServiceImplTest.java index ceccf4dc7a..470d003b4c 100644 --- a/source/java/org/alfresco/repo/discussion/DiscussionServiceImplTest.java +++ b/source/java/org/alfresco/repo/discussion/DiscussionServiceImplTest.java @@ -246,6 +246,8 @@ public class DiscussionServiceImplTest // The topic has no primary post assertEquals(null, DISCUSSION_SERVICE.getPrimaryPost(topic)); + // Which means no recent post + assertEquals(null, DISCUSSION_SERVICE.getMostRecentPost(topic)); // Get with an arbitrary name gives nothing @@ -288,6 +290,10 @@ public class DiscussionServiceImplTest // Topic will now have a primary post assertNotNull(DISCUSSION_SERVICE.getPrimaryPost(topic)); assertEquals(post.getNodeRef(), DISCUSSION_SERVICE.getPrimaryPost(topic).getNodeRef()); + + // The new post will be the most recent one + assertNotNull(DISCUSSION_SERVICE.getMostRecentPost(topic)); + assertEquals(post.getNodeRef(), DISCUSSION_SERVICE.getMostRecentPost(topic).getNodeRef()); // Topic will now have one post listed @@ -355,6 +361,15 @@ public class DiscussionServiceImplTest assertNotNull(objects); assertEquals(topic.getNodeRef(), objects.getFirst().getNodeRef()); assertEquals(reply1.getNodeRef(), objects.getSecond().getNodeRef()); + + + // The primary post will be unchanged + assertNotNull(DISCUSSION_SERVICE.getPrimaryPost(topic)); + assertEquals(post.getNodeRef(), DISCUSSION_SERVICE.getPrimaryPost(topic).getNodeRef()); + + // But the most recent will be the newest reply + assertNotNull(DISCUSSION_SERVICE.getMostRecentPost(topic)); + assertEquals(reply2.getNodeRef(), DISCUSSION_SERVICE.getMostRecentPost(topic).getNodeRef()); // Check the overall count now diff --git a/source/java/org/alfresco/service/cmr/discussion/DiscussionService.java b/source/java/org/alfresco/service/cmr/discussion/DiscussionService.java index 63cb12e1a2..fb281d777f 100644 --- a/source/java/org/alfresco/service/cmr/discussion/DiscussionService.java +++ b/source/java/org/alfresco/service/cmr/discussion/DiscussionService.java @@ -124,6 +124,17 @@ public interface DiscussionService { @NotAuditable PostInfo getPrimaryPost(TopicInfo topic); + /** + * Retrieves the newest (most recent) Post in a topic, be that + * the Primary Post or a Reply. + * This is typically used when identifying if a topic has had + * new posts added to it since the user last saw it. + * Note that this works on Created Date, and not Modified/Updated, + * so edits to an existing post will not change this. + */ + @NotAuditable + PostInfo getMostRecentPost(TopicInfo topic); + /** * Retrieves an existing {@link TopicInfo} from the repository, * which is within a site