Determine latest version of Web Content in Liferay

Most of the time when using Liferay’s Web Content (i.e. JournalArticle) you will want to determine the latest version to be displayed to your users. The following code snippet shows a simple solution to do so:

List<JournalArticle> articles = JournalArticleLocalServiceUtil.getStructureArticles(GROUPID, STRUCTUREID);
ListIterator<JournalArticle> it = articles.listIterator();
List<String> checkedArticleIds = new ArrayList<String>();

while (it.hasNext()) {
  JournalArticle article = it.next();

  if (checkedArticleIds.contains(article.getArticleId())) {
    continue; // previous article version already checked
  }

  JournalArticle articleLastVersion = JournalArticleLocalServiceUtil.getLatestArticle(GROUPID, article.getArticleId());

  checkedArticleIds.add(article.getArticleId());

  System.out.println("Added articleId " + article.getArticleId() + " with version " + article.getVersion());
}

You may also like...

3 Responses

  1. András says:

    IT was a really really great atricle, thank you!

  2. Hany Bonyadi says:

    Very very good.

  3. mojtaba safaeian says:

    But this is not optimised way to do this, what if we have tens of thousands of articles, you must offer a way to query database for latest article versions not to retrieve all articles and then select the latest IDs.

    thank you

Leave a Reply

Your email address will not be published. Required fields are marked *