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:
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!
I have been doing a DynamicQuery to get the JournalArticles of some categories like this:
private List<journalarticle> getJournalArticles(Long categoryId1, Long categoryId2) {
List<long> categoryIds = new ArrayList<long>();
if (categoryId1 != null) categoryIds.add(categoryId1);
if (categoryId2 != null) categoryIds.add(categoryId2);
DynamicQuery assetEntryAssetCategoryRelQuery = AssetEntryAssetCategoryRelLocalServiceUtil.dynamicQuery();
assetEntryAssetCategoryRelQuery.add(PropertyFactoryUtil.forName(Field.ASSET_CATEGORY_ID).in(categoryIds));
assetEntryAssetCategoryRelQuery.setProjection(ProjectionFactoryUtil.property("assetEntryId"));
List<long> assetEntryAssetCategoryRelIds = AssetEntryAssetCategoryRelLocalServiceUtil.dynamicQuery(assetEntryAssetCategoryRelQuery);
if (assetEntryAssetCategoryRelIds.isEmpty()) return Collections.emptyList();
DynamicQuery assetEntryQuery = AssetEntryLocalServiceUtil.dynamicQuery();
assetEntryQuery.add(PropertyFactoryUtil.forName(Field.CLASS_NAME_ID).eq(PortalUtil.getClassNameId(JournalArticle.class.getName())));
assetEntryQuery.add(PropertyFactoryUtil.forName("entryId").in(assetEntryAssetCategoryRelIds));
assetEntryQuery.setProjection(ProjectionFactoryUtil.property(Field.CLASS_PK));
List<long> assetEntryIds = AssetEntryLocalServiceUtil.dynamicQuery(assetEntryQuery);
if (assetEntryIds.isEmpty()) return Collections.emptyList();
DynamicQuery journalArticleQuery = JournalArticleLocalServiceUtil.dynamicQuery();
journalArticleQuery.add(PropertyFactoryUtil.forName(Field.STATUS).eq(WorkflowConstants.STATUS_APPROVED));
journalArticleQuery.add(PropertyFactoryUtil.forName("resourcePrimKey").in(assetEntryIds));
journalArticleQuery.addOrder(OrderFactoryUtil.desc("modifiedDate"));
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!