Tag is an important tool in Liferay. Using Asset Tag we can organize content in the portal. We can assign Tag while creating new asset or in the existing assets in Liferay Portal. After that we can search assets by associated Tag. In this article we will try to explore Liferay Asset Tag.
1. What are the assets available in Liferay where we can assign a Tag?
Ans:
By default below are the assets available in Liferay Portal where we can add a Tag
i) Web Content
ii) Documents and Media
iii) Blogs
iv) Message Boards
v) Wiki Page
vi) Bookmarks
The above 6 items are called Assets in Liferay Portal. However we can crate our custom asset implementing the Asset Framework in Liferay.
2. How to create Tag and where its stored?
Ans:
Tags are created in the Control Panel of the Portal. To create new Tag we can navigate like this
Control Panel→Manage Liferay→Content
After creating the Tag, the Tags are stored in assettag Table in Liferay Database. Below is the table details
3. Whats assetCount column in the assetTag table?
Ans:
Initially when we create a new Tag the value of assetCount will be zero. But with time the value get changes. Consider we are creating one web content and assigned one tag with the content. Therefore the value of assetCount will be incremented by one.
For example if the value of assetCount is 5 then we should understand that we have assigned the tag for 5 times.
4. Programmatically find assetCount by Tag name.
Ans:
By using Liferay API we can find out the value of assetCount. Here is a sample program
ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY); String tagName = "news"; try { AssetTag assetTag = AssetTagLocalServiceUtil.getTag(themeDisplay.getScopeGroupId(), tagName); int assetCount = assetTag.getAssetCount(); System.out.println("Tage name: "+tagName); System.out.println("Number of associated assets: "+assetCount); } catch (PortalException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); }
5. Programmatically find all the assets by tag name
Ans:
Below is the sample program to fetch all the assets by a given tag name. In the below code we have considered the tag name as “news”
ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest .getAttribute(WebKeys.THEME_DISPLAY); String tagName = "news"; try { AssetTag assetTag = AssetTagLocalServiceUtil.getTag(themeDisplay.getScopeGroupId(), tagName); AssetEntryQuery assetEntryQuery = new AssetEntryQuery(); long[] tagIds = { assetTag.getTagId() }; assetEntryQuery.setAnyTagIds(tagIds); List<AssetEntry> assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery); for (AssetEntry assetEntry : assetEntryList) { //Here all the logic will go } } catch (PortalException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); }
6. For a given tag get all the journal article associated with it
Ans:
Considering that our Tag Name is news. Below is the code to retrieve all the web content by the Tag news.
ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest .getAttribute(WebKeys.THEME_DISPLAY); String tagName = "news"; try { AssetTag assetTag = AssetTagLocalServiceUtil.getTag(themeDisplay.getScopeGroupId(), tagName); AssetEntryQuery assetEntryQuery = new AssetEntryQuery(); long[] tagIds = { assetTag.getTagId() }; assetEntryQuery.setAnyTagIds(tagIds); assetEntryQuery.setClassName(JournalArticle.class.getName()); List<AssetEntry> assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery); for (AssetEntry assetEntry : assetEntryList) { JournalArticleResource journalArticleResource = JournalArticleResourceLocalServiceUtil.getJournalArticleResource(assetEntry.getClassPK()); JournalArticle journalArticle = JournalArticleLocalServiceUtil.getArticle(journalArticleResource.getGroupId(),journalArticleResource.getArticleId()); System.out.println("Journal Article Title: "+journalArticle.getTitle(themeDisplay.getLocale())); } } catch (PortalException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); }
7. Find all the book marks entry by a Tag
Ans:
Below is the sample code to retrieve all the Book Marks Entry by a given tag news.
ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY); String tagName = "news"; try { AssetTag assetTag = AssetTagLocalServiceUtil.getTag(themeDisplay.getScopeGroupId(), tagName); AssetEntryQuery assetEntryQuery = new AssetEntryQuery(); long[] tagIds = { assetTag.getTagId() }; assetEntryQuery.setAnyTagIds(tagIds); assetEntryQuery.setClassName(BookmarksEntry.class.getName()); List<AssetEntry> assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery); for (AssetEntry assetEntry : assetEntryList) { BookmarksEntry bookmarksEntry = BookmarksEntryLocalServiceUtil.getBookmarksEntry(assetEntry.getClassPK()); //Other logic can go here using bookmarksEntry object } } catch (PortalException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); }
8. Get all the Wiki Pages by a Tag
Ans:
Below is the sample code to retrieve all the Wiki Pages by Tag “sports”
ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY); String tagName = "sports"; try { AssetTag assetTag = AssetTagLocalServiceUtil.getTag(themeDisplay.getScopeGroupId(), tagName); AssetEntryQuery assetEntryQuery = new AssetEntryQuery(); long[] tagIds = { assetTag.getTagId() }; assetEntryQuery.setAnyTagIds(tagIds); assetEntryQuery.setClassName(WikiPage.class.getName()); List<AssetEntry> assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery); for (AssetEntry assetEntry : assetEntryList) { WikiPage wikiPage = WikiPageLocalServiceUtil.getPage(assetEntry.getClassPK()); //Other logic can go here using wikiPage object System.out.println("Wiki Page Title::"+wikiPage.getTitle()); } } catch (PortalException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); }
9. Similarly for Message Board,Blogs and Documents
As explained above below is a sample program to retrieve Message Board, Blogs and Documents by a Tag “news”
ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY); String tagName = "news"; try { AssetTag assetTag = AssetTagLocalServiceUtil.getTag(themeDisplay.getScopeGroupId(), tagName); AssetEntryQuery assetEntryQuery = new AssetEntryQuery(); long[] tagIds = { assetTag.getTagId() }; assetEntryQuery.setAnyTagIds(tagIds); /** * For message board */ assetEntryQuery.setClassName(MBMessage.class.getName()); List<AssetEntry> assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery); for (AssetEntry assetEntry : assetEntryList) { MBMessage mbMessage = MBMessageLocalServiceUtil.getMBMessage(assetEntry.getClassPK()); System.out.println("Messageboard Title::"+mbMessage.getSubject()); } /** * For blogs */ assetEntryQuery.setClassName(BlogsEntry.class.getName()); assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery); for (AssetEntry assetEntry : assetEntryList) { BlogsEntry BlogsEntry = BlogsEntryLocalServiceUtil.getBlogsEntry(assetEntry.getClassPK()); System.out.println("Blog Title::"+BlogsEntry.getTitle()); } /** * For Documents and media */ assetEntryQuery.setClassName(DLFileEntry.class.getName()); assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery); for (AssetEntry assetEntry : assetEntryList) { DLFileEntry dLFileEntry = DLFileEntryLocalServiceUtil.getDLFileEntry(assetEntry.getClassPK()); System.out.println("File Title::"+dLFileEntry.getTitle()); } } catch (PortalException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); }
10. Database tables associated with Asset Tag
When we assign a tag to an Asset few tables will be updated to hold the association i.e., relation between the Asset and Tag. Below is the rough flow with respect to database tables
Table assettag : The column assetCount of the table will be update
Table assetentries_assettags : A new row will be inserted in this table
Table assettagstats : The column assetCount of the table will be updated
And More…Navigation from assetentry table to assettag….
how to create and assign tag to jaournal article programmatically??