list of Vocabularies and Categories

Olga S, modified 8 Years ago. Junior Member Posts: 26 Join Date: 4/17/17 Recent Posts
Hello!
I need to get the list of Vocabularies and categories which belong to those vocabularies in my Portlet (I need to display them in drop down).
Is there a neat way to do that?
I tried solution for 6.2 (I am using Liferay 7) but it's not working https://www.kerstner.at/2012/11/search-categories-by-name-or-vocabulary-in-liferay-6/

Thanks!
thumbnail
Danielle Ardon, modified 8 Years ago. Junior Member Posts: 37 Join Date: 6/6/16 Recent Posts
Hi Olga,

So if I understand correctly you need to be able to retrieve all the categories from a vocabulary by name?
So lets say you have a vocabulary "Animals" and you want all the animal categories in that to display in a dropdown?

Liferay 7.0 (and 6.2 too) has a method already that allows you to search for a vocabulary by name:
https://docs.liferay.com/portal/7.0/javadocs/portal-kernel/com/liferay/asset/kernel/service/AssetVocabularyLocalService.html
(In 7.0 you should just use the localservice instead of the util classes).

So that would be something like:
@Component(immediate = true, service = CategoryRetriever.class)
public class CategoryRetrieverImpl implements CategoryRetriever {

	private static final Log LOG = LogFactoryUtil.getLog(CategoryRetrieverImpl.class);

	@Reference
	private AssetCategoryLocalService assetCategoryService;

	@Reference
	private AssetVocabularyLocalService assetVocabularyLocalService;

    @Reference
    private DefaultValue defaultValue;
    AssetCategory assetCategory;
    
	@Override
	public List<assetcategories> getCategories( String vocabularyName, long groupId) {
		List<assetcategory> assetCategories = new ArrayList&lt;&gt;();
		try {
			AssetVocabulary vocabulary = assetVocabularyLocalService.getGroupVocabulary(groupId,
					vocabularyName);
			if(Validator.isNotNull(vocabulary)){
                        assetCategories =  vocabulary.getCategories();
                  }
		catch (PortalException e) {
			LOG.error(String.format("Retrieving vocabulary %s failed",vocabularyName),e);
		}
 return assetCategories;
}</assetcategory></assetcategories>



I don't really see a reason to use dynamicquery for something that is already a standard api method. If you need to search categories by name then you can still use dynamicquery, it is still supported in 7.0.
Olga S, modified 8 Years ago. Junior Member Posts: 26 Join Date: 4/17/17 Recent Posts
Hello Danielle!
Thanks so much for your reply!
I will try this code.
I am still confused about one thing.
All Vocabularies are retrieved based on some group, however I created my vocabularies and categories out of group context(I did'n assign any vocab to any group explicitly). Which group do I need to use in getGroupVocabularies() to retrieve vocabs?


Thanks!
thumbnail
Danielle Ardon, modified 8 Years ago. Junior Member Posts: 37 Join Date: 6/6/16 Recent Posts
Ahh well the group in this case means the site, so if you created the vocabulary for a specific site, you should use the groupId for that site.
But if you created the vocabulary in the global site/scope then you need the global group Id. You can get this global group Id in several ways.
If you have acces to a request object you can get it via the theme display:

ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute( WebKeys.THEME_DISPLAY);
And then themeDisplay.getCompany.getGroupId();


Or if you don't have access to the request object:
Override
    public Company getDefaultCompany() {
        Company defaultCompany = null;
        String webId = PropsUtil.get("company.default.web.id");
        try {
            defaultCompany = companyService.getCompanyByWebId(webId);
        } catch (PortalException e) {
            LOG.error(String.format("Error while retrieving default company, error is %s", e.getMessage()));
        }
        return defaultCompany;
    }

    @Override
    public long getGlobalGroupId() {
        Company defaultCompany = getDefaultCompany();
        long groupId = 0;
        try {
            groupId = defaultCompany.getGroupId();
        } catch (PortalException e) {
            LOG.error("Error while retrieving global groupId", e);
        }
        return groupId;
    }
Olga S, modified 8 Years ago. Junior Member Posts: 26 Join Date: 4/17/17 Recent Posts
Hello Danielle!
Thanks,
it worked!