16 February, 2011

Article by tag name

As Now we are moving with liferay 6 we would have lot many requirements
to find the articles by tag name.So in liferay-6 i found little bit
change to find the articles according to tags in comparison with liferay 5.2.X.
So here below i have put the code snippet to find articles by tag for liferay 6.
Hope it will be usefull !!!



public List findArticleByTag(ThemeDisplay themeDisplay , String tagName) throws PortalException, SystemException{
  AssetTag assetTagObj = AssetTagLocalServiceUtil.getTag(themeDisplay.getScopeGroupId(), tagName);  
  long tagid = assetTagObj.getTagId(); 
  AssetEntryQuery assetEntryQuery = new AssetEntryQuery(); 
  long[] anyTagIds = {tagid};  
  assetEntryQuery.setAnyTagIds(anyTagIds);  
  List assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery);  
  List journalArticleList =new ArrayList();  
  for(AssetEntry ae : assetEntryList)
  {  
     JournalArticleResource journalArticleResourceObj = JournalArticleResourceLocalServiceUtil.getJournalArticleResource(ae.getClassPK()); 
     JournalArticle journalArticleObj = JournalArticleLocalServiceUtil.getArticle(themeDisplay.getScopeGroupId(),journalArticleResourceObj.getArticleId());  
     journalArticleList.add(journalArticleObj); 
  } 
  return journalArticleList;  
 }

Rate Me:

6 Discussion:

Anonymous said...

Nice Article!!!

Anonymous said...

Good one. A doubt. I see this line List assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery);

This will bring all the asset irrespective of whether it is blog, mb,wiki and then in the snippet below you are trying to use the primary key to get journal article, what if the assetentry is not a journal article at all, will it not give error?

jignesh vachhani said...

yes right this is for asset entry only but if you want for other then might be you can find Query object for that as well.

pageraz said...

Thankx for this example

for the one who talked about getting just the Journal Article
you can add this line
assetEntryQuery.setClassName(JournalArticle.class.getName());

so it will limit the result just for the JournalArticle entries
try to explore the assetEntryQuery methods you will see a lot of query options

lirone75 said...

I don't know/understand why some methods are called getTag(...) and some other getAssetTag(...). What could be the reason ?

jignesh vachhani said...

It's just for getting Tag names it doesn't matter how will you get Tag name using other functions.
So hope you got my answer !!!!!