Getting journal articles by category: Liferay Port

2019-08-23 06:08发布

I am trying to write a simple Liferay portlet in Python. The portlet will show a list of categories and when clicked will show a list of Web Content articles (journal articles) of a certain structure.

I am able to get the list of categories but cannot find a way using the liferay api to get a list of articles by category?

I have searched allover but it seems to me the method should be on this page:

http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portlet/journal/service/JournalArticleLocalServiceUtil.html

3条回答
淡お忘
2楼-- · 2019-08-23 06:48

The proposed solution is good yet needs one extra piece. It will return all Assets - web-content-articles are a subset of assets. For example, you will get documents (which have been categorized the same way) as well. To refine your search, add a className, classNameid, or classTypeId to the AssetEntryQuery (in addition to the category id). Alternatively, within the for-loop you could pick out the web-content, ignore the others.

查看更多
手持菜刀,她持情操
3楼-- · 2019-08-23 07:05

Thanks, AssetEntryQuery was the solution:

from com.liferay.portlet.asset.service.persistence import AssetEntryQuery
from com.liferay.portlet.asset.service import AssetEntryServiceUtil

aq = AssetEntryQuery()
aq.setAllCategoryIds([442492])
articles = AssetEntryServiceUtil.getEntries(aq)

for a in articles:
  out.write(str(a.title))
  out.write(str(a))
查看更多
在下西门庆
4楼-- · 2019-08-23 07:07

It is a Java implementation but really easy to convert into python.

<%
String languageId = LanguageUtil.getLanguageId( renderRequest );
List<JournalArticle> journalArticleList = new ArrayList<JournalArticle>();

AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
assetEntryQuery.setAnyCategoryIds(new long[] { 12704 }); //category Id
assetEntryQuery.setOrderByCol1("modifiedDate");
assetEntryQuery.setEnd(5);
List<AssetEntry> assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery);
for (AssetEntry ae : assetEntryList) {
    JournalArticleResource journalArticleResource = JournalArticleResourceLocalServiceUtil.getJournalArticleResource(ae.getClassPK());
    JournalArticle journalArticle = JournalArticleLocalServiceUtil.getLatestArticle(journalArticleResource.getResourcePrimKey());


    JournalContentUtil.clearCache();
    String content = JournalContentUtil.getContent(journalArticleResource.getGroupId(), journalArticle.getArticleId(), "view", languageId, themeDisplay);

    out.println("<br>"+journalArticle.getTitle(languageId)+"<br>");
    out.println(content);

}
%>
查看更多
登录 后发表回答