Search Categories by Name or Vocabulary in Liferay 6+

When using categories (AssetCategory) for assets such as web content (i.e. JournalArticles) in Liferay one of the use cases you will most probably come across is to search for matching entries by name. Furthermore, as categories are made up of sets of vocabularies (AssetVocabulary) you might also want to search for categories matching certain vocabularies. The following code snippets demonstrates a solution based on DynamicQueries.

Search Category by Name

The first example shows how to search for a category named “Politics”:

package at.kerstner.portlet;

import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portlet.asset.model.AssetCategory;
import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;

import java.util.ArrayList;
import java.util.List;

public class DummyPortlet {

	public List getCategories() {

		List categories = new ArrayList();

		DynamicQuery query = DynamicQueryFactoryUtil.forClass(
				AssetCategory.class).add(
				PropertyFactoryUtil.forName("name").eq("Politics"));
		try {
			categories = AssetCategoryLocalServiceUtil
					.dynamicQuery(query, 0, 1); // we only want to first one
		} catch (SystemException e) {
                   // handle exception...
		}

		return categories;
	}
}

Search Category by Vocabulary

The second code snippet demonstrates how to search for categories matching the vocabulary “My Vocabulary”:

package at.kerstner.portlet;

import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portlet.asset.model.AssetCategory;
import com.liferay.portlet.asset.model.AssetVocabulary;
import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;

import java.util.ArrayList;
import java.util.List;

public class DummyPortlet {

	@SuppressWarnings("unchecked")
	public List getCategories() {

		List vocabularies = new ArrayList();
		List categories = new ArrayList();

		DynamicQuery queryVocabularies = DynamicQueryFactoryUtil.forClass(
				AssetVocabulary.class).add(
				PropertyFactoryUtil.forName("name").eq("My Vocabulary")); // first search for vocabulary
		try {
			vocabularies = AssetVocabularyLocalServiceUtil.dynamicQuery(
					queryVocabularies, 0, 1);

			if (vocabularies.size() < 1) {
				return categories;
			}

			DynamicQuery queryCategories = DynamicQueryFactoryUtil.forClass(
					AssetCategory.class).add(
					PropertyFactoryUtil.forName("vocabularyId").eq(
							vocabularies.get(0).getVocabularyId())); // then get all categories matching the vocabulary

			categories = AssetVocabularyLocalServiceUtil.dynamicQuery(
					queryCategories, 0, 100); // let's get some to show

		} catch (SystemException e) {
                   // handle exception...
		}

		return categories;
	}
}

The above code works for Liferay 6 and above.

You may also like...

5 Responses

  1. Gubernath says:

    This is exactly the thing I’ve been looking for! Superb and thank you!

  2. Pankaj says:

    Thanks a lot! It helped me! 🙂

  3. Karen says:

    Lovely! Thanks!

  4. fer says:

    Excellent !!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *