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 comments:

  1. Nice Article!!!

    ReplyDelete
  2. 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?

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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

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

    ReplyDelete
  6. 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 !!!!!

    ReplyDelete

Your Comments on this post

Re:(4) Do you want a million for the New Year?

To unsubscribe, send us an email with the subject 'unsubscribe' qiWv qiWv Do you want a million for the New Year? qiWv qiWv ...