6/09/2011

Categories in Liferay

If you want to add a category programmatically to some of your assets or you want to retrieve all objects that have been assigned a special category then you should read this blog entry. It helps you to filter out documents, tag Users or prevent communities from being discovered.


The basic knowledge you have to know is that liferay stores the information about "Which item has which category" in the following tables:


AssetCategory: Contains All Categories.
AssetEntries_AssetCategories: Contains the link between your objects (= asset entries) and the categories that have been assigned to them
AssetEntries: Asset Entries are special liferay representatives of every object you create in liferay. So for example Users, documents, pages etc etc.

If you want to retrieve all asset entries that have been assigned a certain cetagory, you will run into the problem, that you can´t access the table AssetEntries_AssetCategories by dynamic queries. But without dynamic queries you have to do a loooooot of retrieval work and manual filtering. I have pointed out a possible solution in this blog post : http://liferay-blogging.blogspot.com/2010/12/how-to-access-liferays-connection.html .



So, this is how you access the categories:


First, you need to know the entryId of the asset that belongs to your "liferay object". You can do this by using the standard classNameId / classPK way like you do it always in liferay. If you don´t know how to do it, leave me a comment.


Next, you get the categoryId from the AssetEntries_AssetCategories table, or you query your AssetEntry object with getCategories() which immediately returns all the categories you are interested in.


If you have the categoryId, you can access the table "AssetCategory".

If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.