From f6f74c6f4ff6042a88938135d73eaf65c9d23e1f Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Wed, 22 Feb 2017 17:04:45 +0100 Subject: [PATCH] TextInfo table (unfinished). --- .../midpoint/repo/sql/SearchTest.java | 10 +- .../repo/sql/data/common/RObject.java | 19 +++ .../repo/sql/data/common/RObjectTextInfo.java | 132 ++++++++++++++++++ .../query2/resolution/ItemPathResolver.java | 41 ++++++ 4 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/data/common/RObjectTextInfo.java diff --git a/repo/repo-sql-impl-test/src/test/java/com/evolveum/midpoint/repo/sql/SearchTest.java b/repo/repo-sql-impl-test/src/test/java/com/evolveum/midpoint/repo/sql/SearchTest.java index 2a7adeef39b..b5e6a1c5c40 100644 --- a/repo/repo-sql-impl-test/src/test/java/com/evolveum/midpoint/repo/sql/SearchTest.java +++ b/repo/repo-sql-impl-test/src/test/java/com/evolveum/midpoint/repo/sql/SearchTest.java @@ -628,7 +628,15 @@ public void caseInsensitiveSearchTest() throws Exception { 1); } - @SuppressWarnings("SameParameterValue") + @Test + public void fullTextSearch() throws Exception { + assertObjectsFound(QueryBuilder.queryFor(UserType.class, prismContext) + .fullText("test userx00003") + .build(), + 1); + } + + @SuppressWarnings("SameParameterValue") private void assertObjectsFound(ObjectQuery query, int expectedCount) throws Exception { assertObjectsFoundBySearch(query, expectedCount); assertObjectsFoundByCount(query, expectedCount); diff --git a/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/data/common/RObject.java b/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/data/common/RObject.java index 8e74065b7d8..294caf48718 100644 --- a/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/data/common/RObject.java +++ b/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/data/common/RObject.java @@ -186,6 +186,8 @@ public abstract class RObject implements Metadata polys; private Set booleans; + private Set textInfoItems; + @Id @GeneratedValue(generator = "ObjectOidGenerator") @GenericGenerator(name = "ObjectOidGenerator", strategy = "com.evolveum.midpoint.repo.sql.util.ObjectOidGenerator") @@ -558,6 +560,20 @@ public void setBooleans(Set booleans) { this.booleans = booleans; } + @NotQueryable + @OneToMany(mappedBy = "owner", orphanRemoval = true) + @Cascade({org.hibernate.annotations.CascadeType.ALL}) + public Set getTextInfoItems() { + if (textInfoItems == null) { + textInfoItems = new HashSet<>(); + } + return textInfoItems; + } + + public void setTextInfoItems(Set textInfoItems) { + this.textInfoItems = textInfoItems; + } + @Override public boolean equals(Object o) { if (this == o) @@ -600,6 +616,7 @@ public boolean equals(Object o) { if (booleans != null ? !booleans.equals(rObject.booleans) : rObject.booleans != null) return false; if (booleansCount != null ? !booleansCount.equals(rObject.booleansCount) : rObject.booleansCount != null) return false; + if (textInfoItems != null ? !textInfoItems.equals(rObject.textInfoItems) : rObject.textInfoItems != null) return false; return true; } @@ -674,6 +691,8 @@ public static void copyFromJAXB(ObjectType jaxb, RObject< if (jaxb.getExtension() != null) { copyFromJAXB(jaxb.getExtension().asPrismContainerValue(), repo, prismContext, RObjectExtensionType.EXTENSION); } + + repo.getTextInfoItems().addAll(RObjectTextInfo.createSet(jaxb, repo)); } @Deprecated 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 new file mode 100644 index 00000000000..38f7a990b03 --- /dev/null +++ b/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/data/common/RObjectTextInfo.java @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2010-2013 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.repo.sql.data.common; + +import com.evolveum.midpoint.repo.sql.query2.definition.NotQueryable; +import com.evolveum.midpoint.repo.sql.util.RUtil; +import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType; +import org.hibernate.annotations.ForeignKey; +import org.jetbrains.annotations.NotNull; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +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; + +/** + * @author mederly + */ +@Entity +@Table(name = TABLE_NAME, indexes = { + @Index(name = "iTextInfoOid", columnList = COLUMN_OWNER_OID)}) +public class RObjectTextInfo implements Serializable { + + public static final String TABLE_NAME = "m_object_text_info"; + public static final String COLUMN_OWNER_OID = "owner_oid"; + + private Integer id; + + private RObject owner; + private String ownerOid; + + private String text; + + public RObjectTextInfo() { + } + + @Id + @GeneratedValue + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @ForeignKey(name = "fk_reference_owner") + @MapsId("owner") + @ManyToOne(fetch = FetchType.LAZY) + @NotQueryable + public RObject getOwner() { + return owner; + } + + @Id + @Column(name = COLUMN_OWNER_OID, length = RUtil.COLUMN_LENGTH_OID) + @NotQueryable + public String getOwnerOid() { + if (ownerOid == null && owner != null) { + ownerOid = owner.getOid(); + } + return ownerOid; + } + + public void setOwner(RObject owner) { + this.owner = owner; + } + + public void setOwnerOid(String ownerOid) { + this.ownerOid = ownerOid; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + RObjectTextInfo that = (RObjectTextInfo) o; + return Objects.equals(owner, that.owner) && + Objects.equals(ownerOid, that.ownerOid) && + Objects.equals(id, that.id) && + Objects.equals(text, that.text); + } + + @Override + public int hashCode() { + return Objects.hash(owner, ownerOid, id, text); + } + + public static Set createSet(@NotNull ObjectType object, @NotNull RObject repo) { + Set rv = new HashSet<>(); + if (object.getName() != null) { + rv.add(create(repo, object.getName().getOrig())); + } + if (object.getDescription() != null) { + rv.add(create(repo, object.getDescription())); + } + return rv; + } + + private static RObjectTextInfo create(RObject repo, String text) { + RObjectTextInfo rv = new RObjectTextInfo(); + rv.setOwner(repo); + rv.setText(text); + return rv; + } +} diff --git a/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/query2/resolution/ItemPathResolver.java b/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/query2/resolution/ItemPathResolver.java index aceaec90341..158fec48ad4 100644 --- a/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/query2/resolution/ItemPathResolver.java +++ b/repo/repo-sql-impl/src/main/java/com/evolveum/midpoint/repo/sql/query2/resolution/ItemPathResolver.java @@ -128,6 +128,47 @@ String addJoin(JpaLinkDefinition joinedItemDefinition, String currentHqlPath) th return joinedItemAlias; } +// String addTextInfoJoin(String currentHqlPath) throws QueryException { +// RootHibernateQuery hibernateQuery = context.getHibernateQuery(); +// String joinedItemJpaName = "textInfoItems"; +// String joinedItemFullPath = currentHqlPath + "." + joinedItemJpaName; +// String joinedItemAlias; +// if (!joinedItemDefinition.isMultivalued()) { +// /* +// * Let's check if we were already here i.e. if we had already created this join. +// * This is to avoid useless creation of redundant joins for singleton items. +// * +// * But how can we be sure that item is singleton if we look only at the last segment (which is single-valued)? +// * Imagine we are creating join for single-valued entity of u.abc.(...).xyz.ent where +// * ent is single-valued and not embedded (so we are to create something like "left join u.abc.(...).xyz.ent e"). +// * Then "u" is certainly a single value: either the root object, or some value pointed to by Exists restriction. +// * Also, abc, ..., xyz are surely single-valued: otherwise they would be connected by a join. So, +// * u.abc.(...).xyz.ent is a singleton. +// * +// * Look at it in other way: if e.g. xyz was multivalued, then we would have something like: +// * left join u.abc.(...).uvw.xyz x +// * left join x.ent e +// * And, therefore we would not be looking for u.abc.(...).xyz.ent. +// */ +// +// JoinSpecification existingJoin = hibernateQuery.getPrimaryEntity().findJoinFor(joinedItemFullPath); +// if (existingJoin != null) { +// // but let's check the condition as well +// String existingAlias = existingJoin.getAlias(); +// // we have to create condition for existing alias, to be matched to existing condition +// Condition conditionForExistingAlias = createJoinCondition(existingAlias, joinedItemDefinition, hibernateQuery); +// if (ObjectUtils.equals(conditionForExistingAlias, existingJoin.getCondition())) { +// LOGGER.trace("Reusing alias '{}' for joined path '{}'", existingAlias, joinedItemFullPath); +// return existingAlias; +// } +// } +// } +// joinedItemAlias = hibernateQuery.createAlias(joinedItemDefinition); +// Condition condition = createJoinCondition(joinedItemAlias, joinedItemDefinition, hibernateQuery); +// hibernateQuery.getPrimaryEntity().addJoin(new JoinSpecification(joinedItemAlias, joinedItemFullPath, condition)); +// return joinedItemAlias; +// } + private Condition createJoinCondition(String joinedItemAlias, JpaLinkDefinition joinedItemDefinition, RootHibernateQuery hibernateQuery) throws QueryException { Condition condition = null; if (joinedItemDefinition instanceof JpaAnyPropertyLinkDefinition) {