Determine latest version of Web Content in Liferay

Liferay Logo

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());
}

Comments

3 responses to “Determine latest version of Web Content in Liferay”

  1. András Avatar
    András

    IT was a really really great atricle, thank you!

  2. Hany Bonyadi Avatar
    Hany Bonyadi

    Very very good.

  3. mojtaba safaeian Avatar
    mojtaba safaeian

    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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.