Message Boards

Liferay 7.2 - How to DynamicQuery two categories

Fabio Carvalho, modified 4 Years ago.

Liferay 7.2 - How to DynamicQuery two categories

Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
Hello,

I have been doing a DynamicQuery to get the JournalArticles of some categories like this:
private List<journalarticle> getJournalArticles(Long categoryId1, Long categoryId2) {
&nbsp; &nbsp; List<long> categoryIds = new ArrayList<long>();
&nbsp; &nbsp; if (categoryId1 != null) categoryIds.add(categoryId1);
&nbsp; &nbsp; if (categoryId2 != null) categoryIds.add(categoryId2);

&nbsp; &nbsp; DynamicQuery assetEntryAssetCategoryRelQuery = AssetEntryAssetCategoryRelLocalServiceUtil.dynamicQuery();
&nbsp; &nbsp; assetEntryAssetCategoryRelQuery.add(PropertyFactoryUtil.forName(Field.ASSET_CATEGORY_ID).in(categoryIds));
&nbsp; &nbsp; assetEntryAssetCategoryRelQuery.setProjection(ProjectionFactoryUtil.property("assetEntryId"));

&nbsp; &nbsp; List<long> assetEntryAssetCategoryRelIds = AssetEntryAssetCategoryRelLocalServiceUtil.dynamicQuery(assetEntryAssetCategoryRelQuery);
&nbsp; &nbsp; if (assetEntryAssetCategoryRelIds.isEmpty()) return Collections.emptyList();
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp; &nbsp; DynamicQuery assetEntryQuery = AssetEntryLocalServiceUtil.dynamicQuery();
&nbsp; &nbsp; assetEntryQuery.add(PropertyFactoryUtil.forName(Field.CLASS_NAME_ID).eq(PortalUtil.getClassNameId(JournalArticle.class.getName())));
&nbsp; &nbsp; assetEntryQuery.add(PropertyFactoryUtil.forName("entryId").in(assetEntryAssetCategoryRelIds));
&nbsp; &nbsp; assetEntryQuery.setProjection(ProjectionFactoryUtil.property(Field.CLASS_PK));
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp; &nbsp; List<long> assetEntryIds = AssetEntryLocalServiceUtil.dynamicQuery(assetEntryQuery);&nbsp;
&nbsp; &nbsp; if (assetEntryIds.isEmpty()) return Collections.emptyList();
&nbsp; &nbsp;
&nbsp; &nbsp; DynamicQuery journalArticleQuery = JournalArticleLocalServiceUtil.dynamicQuery();
&nbsp; &nbsp; journalArticleQuery.add(PropertyFactoryUtil.forName(Field.STATUS).eq(WorkflowConstants.STATUS_APPROVED));
&nbsp; &nbsp;&nbsp;journalArticleQuery.add(PropertyFactoryUtil.forName("resourcePrimKey").in(assetEntryIds));
&nbsp; &nbsp; journalArticleQuery.addOrder(OrderFactoryUtil.desc("modifiedDate"));

&nbsp;   List<journalarticle> journalArticles = JournalArticleLocalServiceUtil.dynamicQuery(journalArticleQuery);
    return journalArticles.isEmpty() ? Collections.emptyList() : journalArticles;
}</journalarticle></long></long></long></long></journalarticle>

Ok, so this is returning the JournalArticles with the categoryId1 and also the JournalArticles of the categoryId2. But what I really want is, in case the categoryId1 and categoryId2 are both not null, I want to query all the JournalArticles that have both categories!

Lets suppose that the categoryId1 is 10001 and categoryId2 is 10002. I want to get all the JournalArticle that have both categories (10001 and 10002). I am not sure on how to make this query. Can anyone help me with this?

Thanks in advance!