From dd31844d601ae5894ec8042f999233d6ac1c32e4 Mon Sep 17 00:00:00 2001 From: Guilherme Camacho Date: Thu, 16 Sep 2021 20:58:27 -0300 Subject: [PATCH] LPS-138913 Refactor to support search by keywords combined with pre-filters --- ...leFormVariationInfoCollectionProvider.java | 93 +++++++++++++++++-- 1 file changed, 84 insertions(+), 9 deletions(-) diff --git a/modules/apps/object/object-service/src/main/java/com/liferay/object/internal/info/collection/provider/ObjectEntrySingleFormVariationInfoCollectionProvider.java b/modules/apps/object/object-service/src/main/java/com/liferay/object/internal/info/collection/provider/ObjectEntrySingleFormVariationInfoCollectionProvider.java index 7f032b1d25f6ff..1c5d4a045e7071 100644 --- a/modules/apps/object/object-service/src/main/java/com/liferay/object/internal/info/collection/provider/ObjectEntrySingleFormVariationInfoCollectionProvider.java +++ b/modules/apps/object/object-service/src/main/java/com/liferay/object/internal/info/collection/provider/ObjectEntrySingleFormVariationInfoCollectionProvider.java @@ -39,12 +39,20 @@ import com.liferay.portal.kernel.dao.orm.QueryUtil; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.language.LanguageUtil; +import com.liferay.portal.kernel.search.BooleanClause; +import com.liferay.portal.kernel.search.BooleanClauseFactoryUtil; +import com.liferay.portal.kernel.search.BooleanClauseOccur; +import com.liferay.portal.kernel.search.BooleanQuery; import com.liferay.portal.kernel.search.Field; import com.liferay.portal.kernel.search.Hits; import com.liferay.portal.kernel.search.Indexer; import com.liferay.portal.kernel.search.IndexerRegistryUtil; +import com.liferay.portal.kernel.search.ParseException; import com.liferay.portal.kernel.search.QueryConfig; import com.liferay.portal.kernel.search.SearchContext; +import com.liferay.portal.kernel.search.generic.BooleanQueryImpl; +import com.liferay.portal.kernel.search.generic.NestedQuery; +import com.liferay.portal.kernel.search.generic.TermQueryImpl; import com.liferay.portal.kernel.service.ServiceContext; import com.liferay.portal.kernel.service.ServiceContextThreadLocal; import com.liferay.portal.kernel.util.GetterUtil; @@ -190,15 +198,7 @@ private SearchContext _buildSearchContext(CollectionQuery collectionQuery) searchContext.setAttribute( "objectDefinitionId", _objectDefinition.getObjectDefinitionId()); - Optional> configurationOptional = - collectionQuery.getConfigurationOptional(); - - Map configuration = configurationOptional.orElse( - Collections.emptyMap()); - - for (Map.Entry entry : configuration.entrySet()) { - searchContext.setAttribute(entry.getKey(), entry.getValue()); - } + searchContext.setBooleanClauses(_getBooleanClauses(collectionQuery)); ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext(); @@ -231,6 +231,69 @@ private SearchContext _buildSearchContext(CollectionQuery collectionQuery) return searchContext; } + private BooleanClause[] _getBooleanClauses(CollectionQuery collectionQuery) + throws ParseException { + + BooleanQuery booleanQuery = new BooleanQueryImpl(); + + Optional> configurationOptional = + collectionQuery.getConfigurationOptional(); + + Map configuration = configurationOptional.orElse( + Collections.emptyMap()); + + List objectFields = + _objectFieldLocalService.getObjectFields( + _objectDefinition.getObjectDefinitionId()); + + for (Map.Entry entry : configuration.entrySet()) { + String[] values = entry.getValue(); + + if ((values == null) || (values.length == 0) || + values[0].isEmpty()) { + + continue; + } + + ObjectField objectField = _getObjectField( + entry.getKey(), objectFields); + + if (objectField != null) { + BooleanQuery nestedBooleanQuery = new BooleanQueryImpl(); + + nestedBooleanQuery.add( + new TermQueryImpl( + "nestedFieldArray.fieldName", entry.getKey()), + BooleanClauseOccur.MUST); + + nestedBooleanQuery.add( + new TermQueryImpl( + _getField(objectField), entry.getValue()[0]), + BooleanClauseOccur.MUST); + + booleanQuery.add( + new NestedQuery("nestedFieldArray", nestedBooleanQuery), + BooleanClauseOccur.MUST); + } + } + + return new BooleanClause[] { + BooleanClauseFactoryUtil.create( + booleanQuery, BooleanClauseOccur.MUST.getName()) + }; + } + + private String _getField(ObjectField objectField) { + if (Objects.equals(objectField.getType(), "Boolean")) { + return "nestedFieldArray.value_boolean"; + } + else if (Objects.equals(objectField.getType(), "String")) { + return "nestedFieldArray.value_text"; + } + + return ""; + } + private long _getGroupId() throws PortalException { ObjectScopeProvider objectScopeProvider = _objectScopeProviderRegistry.getObjectScopeProvider( @@ -246,6 +309,18 @@ private long _getGroupId() throws PortalException { return objectScopeProvider.getGroupId(serviceContext.getRequest()); } + private ObjectField _getObjectField( + String name, List objectFields) { + + for (ObjectField objectField : objectFields) { + if (Objects.equals(name, objectField.getName())) { + return objectField; + } + } + + return null; + } + private List _getOptions( ObjectField objectField) {