Setting up the most Popular Journal Articles in your Personalized Community in Liferay Portal

Packt Publishing

Personal community is a dynamic feature of Liferay portal. By default, the personal community is a portal-wide setting that will affect all of the users. It would be nice to have more features in the personal community such as showing the most popular journal articles. This article by Jonas Yuan will address how to set up the most popular journal articles in you personalized community and view the counter for other assets.

In a web site, we will have a lot of journal articles (that is, web content) for a given article type. For example, for the article type Article Content, we will have articles talking about product family. We may want to know how many times the end users read each article. Meanwhile, it would be nice if we could show the most popular articles (for example, TOP 10 articles) for this given article type.

As shown in the following screenshot, a journal article My EDI Product I is shown via a portlet Ext Web Content Display. Rating and comments on this article are also exhibited. At the same time, the medium-size image, polls, and related content of this article are listed, too. A view counter of this article is especially displayed under the ratings. Moreover, the most popular articles are exhibited with article title and number of views under related content. All these articles belong to the article type article-content. That is, the article in the current portlet Ext Web Content Display has the most popular articles only for the article type article-content.

Of course, you can customize the portlet Web Content Display directly through changing JSP files. For demo purposes, we will implement the view counter in the portlet Ext Web Content Display. Meanwhile, we will implement the mostly popular articles via VM services and article templates. In addition, we will analyze the view counter for other assets such as Image Gallery images, Document Library documents, Wiki articles, Blog entries, Message Boards threads, and so on.

Adding a view counter in the Web Content Display portlet

First of all, let's add a view counter in the Ext Web Content Display portlet. As the function of view counter for assets (including journal articles) is provided in the model TagsAssetModel of the com.liferay.portlet.tags.model package in the /portal/portal-service/src folder, we could use this feature in this portlet directly. To do so, use the following steps:

Create a folder journal_content in the folder /ext/ext-web/docroot/html/portlet/.

Copy the JSP file view.jsp in the folder /portal/portal-web/docroot/html/portlet/ to the folder /ext/ext-web/docroot/html/portlet/journal_content and open it.

Add the line <%@ page import="com.liferay.portlet.tags.model.TagsAsset" %> after the line <%@ include file="/html/portlet/journal_content/init.jsp" %>, and check the following lines:

The code above shows a way to increase the view counter via the TagsAssetLocalServiceUtil.incrementViewCounter method. This method takes two parameters className and classPK as inputs. For the current journal article, the two parameters are JournalArticle.class.getName() and articleDisplay.getResourcePrimKey(). Then, this code shows a way to display view counted through the TagsAssetLocalServiceUtil.getAsset method. Similarly, this method also takes two parameters, className and classPK, as inputs. This approach would be useful for other assets, as the className parameter could be Image Gallery, Document Library, Wiki, Blogs, Message Boards, Bookmark, and so on.

Setting up VM service

We can set up the VM service to exhibit the most popular articles. We can also add the getMostPopularArticles method in the custom velocity tool ExtVelocityToolUtil.

To do so, first add the following method in the ExtVelocityToolService interface:

The preceding code shows a way to get the most popular articles by company ID, group ID, article type, and limited articles to be returned. DynamicQuery API allows us to leverage the existing mapping definitions through access to the Hibernate session. For example, DynamicQuery dq0 selects the journal articles by companyID, groupId, and type; DynamicQuery query selects tagsassets by classPK, which exists in DynamicQuery dq0; and tagsassets are ordered by viewCount as well.

Finally, add the following method to register the above method in ExtVelocityToolUtil:

The code above shows a generic approach to get TOP 10 articles for any article types. Of course, you can extend this approach to find TOP 10 assets. This can include Image Gallery images, Document Library documents, Wiki articles, Blog entries, Message Boards threads, Bookmark entries, slideshow, videos, games, video queue, video list, playlist, and so on. You may practice these TOP 10 assets feature.

Building article template for the most popular journal articles

We have added view counter on journal articles. We have already built VM service for the most popular articles too. Now let's build an article template for them.

Setting up the default article type

As mentioned earlier, there is a set of types of journal articles, for example, announcements, blogs, general, news, press-release, updates, article-tout, article-content, and so on. In real case, only some of these types will require view counter, for example article-content. Let's configure the default article type for mostly popular articles. We can add the following line at the end of portal-ext.properties.

ext.most_popular_articles.article_type=article-content

The code above shows that the default article type for most_popular_articles is article-content.

Setting up the article template

Let's create a template named MOST_POPULAR_ARTICLES via the portlet Web Content with the following content:

The code above shows how to use the extVelocityToolUtil.getMostPopularArticles method to get the most popular articles via companyId, groupId, article_type and the number of articles (10). Then it shows the most popular articles with title and view count.

Putting all article templates together

Finally, we need to update the ARTICLE_CONTENT template via the portlet Web Content in order to merge the POLL, RELATED_CONTENT, and MOST_POPULAR_ARTICLES templates. Thus, we will have one integrated view for polls, related articles, and the most popular articles. To do so, add the following line after the line #parse("$journalTemplatesPath/RELATED_Content") in the ARTICLE_CONTENT template:

#parse ("$journalTemplatesPath/MOST_POPULAR_ARTICLES")

Cool! You have built dynamic articles with polls, related content, and the most popular articles successfully. If you created an article with the Article Content type and tags, the polls, related content, and most popular articles (TOP 10) will come out altogether.

Having a handle on view counter for assets

Assets could be journal articles, Wiki articles, blog entries, Message Boards threads, Image Gallery images, Document Library documents, bookmark entries, and so on. Although the function of view counter for these assets is provided in the TagsAssetModel model at the com.liferay.portlet.tags.model package in the folder /portal/portal-service/src, Message Boards threads and bookmark entries still use their own models. At the same time, Image Gallery images and Document Library documents do not use the TagsAssetModel model for view counter yet. Therefore, it would be nice to look at view counter for these assets in detail.

Using journal article tokens

We have added the view counter feature in JSP file successfully. Here we have one more option: adding the views counter through the journal articles token. You can just add a @view_counter@ token to either the content of journal article or the output of article template used. This token is automatically translated to the logic of view counter increment. To find out the logic, locate the Java file ViewCounterTransformerListener.java at the com.liferay.portlet.journal.util package in the folder /portal/portal-impl/src and open it. Check out the following lines:

The code above shows the same logic as that of the journal article JSP file that was mentioned earlier. Because it is AJAX-based, even if the page is cached somewhere in a proxy, you will get the correct number of views counted.

For example, when editing the text of the article named My EDI Product I, you can simply add @view_counter@ anywhere in the text and save it. That's it. Besides the @view_counter@ token, there is a set of tokens you can use at runtime, translating to their applicable runtime value at processing time. The following is a complete list of tokens and their runtime values:

Get view count on Wiki articles

The view count of Wiki articles is provided by default via the TagsAssetModel model at the com.liferay.portlet.tags.model package in the folder /portal/portal-service/src. You can find it out from view.jsp in the folder /portal/portal-web/docroot/html/portlet/wiki as follows:

The code above shows an increasing view counter of the current Wiki article via the TagsAssetLocalServiceUtil service. Then it shows the views counted for the current Wiki article.

Getting views count on blog entries

Similarly, the view count of blog entries is provided by default via the TagsAssetModel model too. You can find it from the view_entry.jsp and view_entry_content.jsp file in the folder /portal/portal-web/docroot/html/portlet/blogs as follows:

The code above first shows an increasing view counter of the current blog entries via the TagsAssetLocalServiceUtil service in the view_entry.jsp file. Then it shows the views counted for the current blog entry in view_entry_content.jsp.

Getting views on Message Boards threads

Message Board threads have their own model to manage the views counted. You can find related methods—locate the Java file MBThreadModel.java at the com.liferay.portlet.messageboards.model package in the /portal/portal-service/src folder and open it. Check the following lines:

public int getViewCount();public void setViewCount(int viewCount);

The code above shows get and set methods for view counts.

Then you can check the method which is used to increase views count. Locate the Java file MBMessageLocalServiceImpl.java in the com.liferay.portlet.messageboards.service.impl package in the /portal/portal-impl/src folder and open it. Check the following lines:

The code above reuses the setter to increase the views counted when displaying Message Boards message.

Setting up view counter on the Image Gallery images

Although the function of the view count for Image Gallery images is provided in the TagsAssetModel model, the feature of view counter is not provided in the portal by default. Of course, we can set up view counter on the Image Gallery images simply by using the following steps:

Create a package com.liferay.portal.servlet in the folder /ext/ext-impl/src.

Copy the Java file ImageServlet.java from the package com.liferay.portal.servlet in the folder /portal/portal-impl/src to the package com.liferay.portal.servlet in the folder /ext/ext-impl/src.

Locate the Java file ImageServlet.java at the package com.liferay.portal.servlet in the /ext/ext-impl/src folder and open it.

Add the following lines after the line image = ImageLocalServiceUtil.getImage(imageId); in the getImage method :

The code above shows a way to set up a view count for the Image Gallery images. When getting an image via either imageId or uuid and groupId, it uses the TagsAssetLocalServiceUtil service to increase view counter for the current image

Setting up view counter on Document Library documents

Similar to view counter of the Image Gallery images, the feature of view counter for Document Library documents is not provided in the portal by default. But we can set it up easily by using the following steps:

Create a package com.liferay.portlet.documentlibrary.action in the folder /ext/ext-impl/src.

Copy the Java file GetFileAction.java from the package com.liferay.portlet.documentlibrary.action in the /portal/portal-impl/src folder to the package com.liferay.portlet.documentlibrary.action in the /ext/ext-impl/src folder.

Locate the Java file GetFileAction.java at the package com.liferay.portlet.documentlibrary.action in the /ext/ext-impl/src folder and open it.

Add the following lines after the line String contentType = MimeTypesUtil.getContentType(fileName); in the GetFile method and save it:

The code above shows a way to increase view counter for Document Library documents. When called, the getFile method uses the TagsAssetLocalServiceUtil service to increase the view counter for the current document.

Getting visits on bookmark entries

Similar to Message Boards threads, bookmark entries have their own model to manage views counter. You can find related methods in the Java file BookmarksEntryModel.java in the package com.liferay.portlet.bookmarks.model in the folder /portal/portal-service/src. Open it and then check the following lines:

public int getVisits();public void setVisits(int visits);

The code above shows the get and set methods for visits. Then check the method to increase visits: locate the Java file BookmarksEntryLocalServiceImpl.java in the package com.liferay.portlet.bookmarks.service.impl in the /portal/portal-impl/src folder and open it; check the following lines:

As shown in the code above, it reuses the set method to increase visits (as explained earlier) when a Bookmark entry is opened.

Summary

A web site has many journal articles for a given article. We may also want to know how many times the end users read each article. This article showed us how to set up the most popular journal articles and view counter for assets.

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.