From f3b30d3146458301289cd1539da107acc70c2149 Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Mon, 27 Feb 2017 15:10:01 +0100 Subject: [PATCH] Another helper method for full-text search. (cherry picked from commit 54f0e44) --- .../util/FullTextSearchConfigurationUtil.java | 78 +++++++++++++++++++ .../util/SystemConfigurationTypeUtil.java | 4 - .../repo/sql/data/common/RObjectTextInfo.java | 40 +--------- 3 files changed, 81 insertions(+), 41 deletions(-) create mode 100644 infra/schema/src/main/java/com/evolveum/midpoint/schema/util/FullTextSearchConfigurationUtil.java diff --git a/infra/schema/src/main/java/com/evolveum/midpoint/schema/util/FullTextSearchConfigurationUtil.java b/infra/schema/src/main/java/com/evolveum/midpoint/schema/util/FullTextSearchConfigurationUtil.java new file mode 100644 index 00000000000..d90f27a2a99 --- /dev/null +++ b/infra/schema/src/main/java/com/evolveum/midpoint/schema/util/FullTextSearchConfigurationUtil.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2010-2017 Evolveum + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.evolveum.midpoint.schema.util; + +import com.evolveum.midpoint.prism.path.ItemPath; +import com.evolveum.midpoint.schema.constants.ObjectTypes; +import com.evolveum.midpoint.util.QNameUtil; +import com.evolveum.midpoint.xml.ns._public.common.common_3.FullTextSearchConfigurationType; +import com.evolveum.midpoint.xml.ns._public.common.common_3.FullTextSearchIndexedItemsConfigurationType; +import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType; +import com.evolveum.prism.xml.ns._public.types_3.ItemPathType; +import org.jetbrains.annotations.NotNull; + +import javax.xml.namespace.QName; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * @author mederly + */ +public class FullTextSearchConfigurationUtil { + + public static boolean isEnabled(FullTextSearchConfigurationType config) { + return config != null && !config.getIndexed().isEmpty() && !Boolean.FALSE.equals(config.isEnabled()); + } + + public static boolean isEnabledFor(FullTextSearchConfigurationType config, Class clazz) { + return isEnabled(config) && !getFullTextSearchItemPaths(config, clazz).isEmpty(); + } + + @NotNull + public static Set getFullTextSearchItemPaths(@NotNull FullTextSearchConfigurationType config, Class clazz) { + List types = + ObjectTypes.getObjectType(clazz).thisAndSupertypes().stream() + .map(ot -> ot.getTypeQName()) + .collect(Collectors.toList()); + Set paths = new HashSet<>(); + for (FullTextSearchIndexedItemsConfigurationType indexed : config.getIndexed()) { + if (isApplicable(indexed, types)) { + for (ItemPathType itemPathType : indexed.getItem()) { + ItemPath path = itemPathType.getItemPath(); + if (!ItemPath.isNullOrEmpty(path) && !ItemPath.containsEquivalent(paths, path)) { + paths.add(path); + } + } + } + } + return paths; + } + + private static boolean isApplicable(FullTextSearchIndexedItemsConfigurationType indexed, List types) { + if (indexed.getObjectType().isEmpty()) { + return true; + } + for (QName type : types) { + if (QNameUtil.matchAny(type, indexed.getObjectType())) { + return true; + } + } + return false; + } +} diff --git a/infra/schema/src/main/java/com/evolveum/midpoint/schema/util/SystemConfigurationTypeUtil.java b/infra/schema/src/main/java/com/evolveum/midpoint/schema/util/SystemConfigurationTypeUtil.java index 0eddd130630..758a7914b51 100644 --- a/infra/schema/src/main/java/com/evolveum/midpoint/schema/util/SystemConfigurationTypeUtil.java +++ b/infra/schema/src/main/java/com/evolveum/midpoint/schema/util/SystemConfigurationTypeUtil.java @@ -17,7 +17,6 @@ package com.evolveum.midpoint.schema.util; import com.evolveum.midpoint.prism.PrismObject; -import com.evolveum.midpoint.xml.ns._public.common.common_3.FullTextSearchConfigurationType; import com.evolveum.midpoint.xml.ns._public.common.common_3.InternalsConfigurationType; import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType; @@ -54,7 +53,4 @@ public static Integer getMaxModelClicks(PrismObject sys return sysconfigObject.asObjectable().getInternals().getMaxModelClicks(); } - public static boolean isFullTextSearchEnabled(FullTextSearchConfigurationType config) { - return config != null && !config.getIndexed().isEmpty() && !Boolean.FALSE.equals(config.isEnabled()); - } } diff --git a/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/data/common/RObjectTextInfo.java b/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/data/common/RObjectTextInfo.java index 07d00e05f04..14abe60f141 100644 --- a/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/data/common/RObjectTextInfo.java +++ b/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/data/common/RObjectTextInfo.java @@ -26,25 +26,19 @@ import com.evolveum.midpoint.repo.sql.data.common.id.RObjectTextInfoId; import com.evolveum.midpoint.repo.sql.query2.definition.NotQueryable; import com.evolveum.midpoint.repo.sql.util.RUtil; -import com.evolveum.midpoint.schema.constants.ObjectTypes; -import com.evolveum.midpoint.schema.util.SystemConfigurationTypeUtil; -import com.evolveum.midpoint.util.QNameUtil; +import com.evolveum.midpoint.schema.util.FullTextSearchConfigurationUtil; import com.evolveum.midpoint.util.logging.Trace; import com.evolveum.midpoint.util.logging.TraceManager; import com.evolveum.midpoint.xml.ns._public.common.common_3.*; -import com.evolveum.prism.xml.ns._public.types_3.ItemPathType; import org.apache.commons.lang3.StringUtils; import org.hibernate.annotations.ForeignKey; import org.jetbrains.annotations.NotNull; import javax.persistence.*; -import javax.xml.namespace.QName; import java.io.Serializable; import java.util.*; import java.util.Objects; -import java.util.stream.Collectors; -import static com.evolveum.midpoint.repo.sql.data.common.RObjectTextInfo.COLUMN_OWNER_OID; import static com.evolveum.midpoint.repo.sql.data.common.RObjectTextInfo.TABLE_NAME; /** @@ -132,26 +126,10 @@ public static Set createItemsSet(@NotNul @NotNull RepositoryContext repositoryContext) { FullTextSearchConfigurationType config = repositoryContext.repositoryService.getFullTextSearchConfiguration(); - if (!SystemConfigurationTypeUtil.isFullTextSearchEnabled(config)) { + if (!FullTextSearchConfigurationUtil.isEnabled(config)) { return Collections.emptySet(); } - List types = - ObjectTypes.getObjectType(object.getClass()).thisAndSupertypes().stream() - .map(ot -> ot.getTypeQName()) - .collect(Collectors.toList()); - Set paths = new HashSet<>(); - for (FullTextSearchIndexedItemsConfigurationType indexed : config.getIndexed()) { - if (isApplicable(indexed, types)) { - for (ItemPathType itemPathType : indexed.getItem()) { - ItemPath path = itemPathType.getItemPath(); - if (path.isEmpty()) { - LOGGER.debug("Empty path in full time index configuration; skipping."); - } else if (!ItemPath.containsEquivalent(paths, path)) { - paths.add(path); - } - } - } - } + Set paths = FullTextSearchConfigurationUtil.getFullTextSearchItemPaths(config, object.getClass()); List values = new ArrayList<>(); for (ItemPath path : paths) { @@ -189,18 +167,6 @@ public static Set createItemsSet(@NotNul return createItemsSet(repo, allWords); } - private static boolean isApplicable(FullTextSearchIndexedItemsConfigurationType indexed, List types) { - if (indexed.getObjectType().isEmpty()) { - return true; - } - for (QName type : types) { - if (QNameUtil.matchAny(type, indexed.getObjectType())) { - return true; - } - } - return false; - } - private static Set createItemsSet(RObject repo, List allWords) { Set rv = new HashSet<>(); StringBuilder sb = new StringBuilder();