フォーラム

ホーム » Liferay Portal » English » 2. Using Liferay » General

構造的に表示 平面上に表示 ツリー上に表示
スレッド [ 前へ | 次へ ]
toggle
Norbert Bede
get_articles by vocabulary, categories, tags in VM
2009/10/25 1:56
答え

Norbert Bede

ランク: Junior Member

投稿: 38

参加年月日: 2009/04/19

最近の投稿

hi,

I want to publish articles in content display portlet by velocity engine (structures, templates). I want to to get from the database only those articles, which equal for a specific tag or category - or more of them.

LIke this (fake example)
#set ($document = $saxReaderUtil.readURL("http://@portal_url@/c/journal/get_articles?groupId=@group_id@&type=marketing&delta=1&approved=true&expired=false&orderBy=display-date&VOCABULARY=CEO"))

thanks
norbert
Sasha Privalov
RE: get_articles by vocabulary, categories, tags in VM
2009/10/26 9:02
答え

Sasha Privalov

ランク: Junior Member

投稿: 29

参加年月日: 2009/05/06

最近の投稿

I had the same problem and I solved it by writing my own servlet (installed as a "web" plugin) that works in a similar way to "get_articles" action, but allows searching by tags (and few other things that I needed).

And if writing a servlet is not an option for you, you may try to work it with out $serviceLocator.
I wrote a small example of CMS template that would do it, this one searches for articles tagged with "xxx" tag:

 1#set ($tagsService = $serviceLocator.findService("com.liferay.portlet.tags.service.TagsAssetLocalService"))
 2#set ($tagsEntryService = $serviceLocator.findService("com.liferay.portlet.tags.service.TagsEntryLocalService"))
 3#set ($journalArticleResourceService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleResourceLocalService"))
 4
 5#set ($longGroupId = $getterUtil.getLong($groupId))
 6
 7#set ($tags = ["xxx"])
 8#set ($entryIds = $tagsEntryService.getEntryIds($longGroupId,$tags.toArray($propsUtil.getArray(""))) )
 9
10#set ($notTags = [])
11#set ($notEntryIds = $tagsEntryService.getEntryIds($longGroupId,$notTags.toArray($propsUtil.getArray(""))) )
12
13#set ($classNameIds = [$portalUtil.getClassNameId("com.liferay.portlet.journal.model.JournalArticle")])
14#set ($classNameIdsArray =$arrayUtil.toArray($classNameIds.toArray($arrayUtil.toArray($entryIds))) )
15
16#set ($assets = $tagsService.getAssets($longGroupId,$classNameIdsArray,$entryIds,$notEntryIds,true,false,0,5) )
17
18#foreach ($asset in $assets)
19 $journalArticleResourceService.getArticleResource($asset.getClassPK())
20#end


Look at this thread for more info on serviceLocator: http://www.liferay.com/web/guest/community/forums/-/message_boards/message/979976
As it is explained there, if you have serviceLocator disabled for journal templates in your portal, there still is a workaround, you can replace the initial three lines of the code above with:
1#set ($tagsService = $portal.getClass().forName("com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil").getMethod("getService", null).invoke(null, null))
2#set ($tagsEntryService = $portal.getClass().forName("com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil").getMethod("getService", null).invoke(null, null))
3#set ($journalArticleResourceService = $portal.getClass().forName("com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil").getMethod("getService",null).invoke(null, null))


This template just gets the article resources, if you require to get some more information about each article, like title, you would need to use the JournalArticleLocalService aswell to retrieve the article object for given articleId.

PS. There are also some dirty hacks in my code, like using $propsUtil only to get some variable of String[] type - I don't know of any better method to do this.