From 60ee56eb394c225c57eba61cadb8c3a9c52c72a4 Mon Sep 17 00:00:00 2001 From: jensp Date: Sat, 17 Dec 2016 16:39:10 +0000 Subject: [PATCH] CCM NG: Basic integration for Hibernate Search git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4487 8810af33-2d31-482b-a856-94f89814c4df --- ccm-core/pom.xml | 2 +- .../java/org/libreccm/core/CcmObject.java | 6 ++ .../org/libreccm/modules/CcmIntegrator.java | 12 +++ .../org/libreccm/search/SearchManager.java | 81 +++++++++++++++++++ pom.xml | 4 +- 5 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 ccm-core/src/main/java/org/libreccm/search/SearchManager.java diff --git a/ccm-core/pom.xml b/ccm-core/pom.xml index 9d8e89c3d..5ef0ee9e8 100644 --- a/ccm-core/pom.xml +++ b/ccm-core/pom.xml @@ -64,7 +64,7 @@ org.hibernate - hibernate-search + hibernate-search-orm provided diff --git a/ccm-core/src/main/java/org/libreccm/core/CcmObject.java b/ccm-core/src/main/java/org/libreccm/core/CcmObject.java index dacc10caf..0a6ea76f4 100644 --- a/ccm-core/src/main/java/org/libreccm/core/CcmObject.java +++ b/ccm-core/src/main/java/org/libreccm/core/CcmObject.java @@ -41,6 +41,7 @@ import javax.validation.constraints.NotNull; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; + import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; @@ -50,6 +51,8 @@ import java.util.Objects; import static org.libreccm.core.CoreConstants.CORE_XML_NS; import static org.libreccm.core.CoreConstants.DB_SCHEMA; +import org.hibernate.search.annotations.Field; + /** * Root class of all entities in LibreCCM which need categorisation and * permission services. @@ -92,6 +95,7 @@ public class CcmObject implements Identifiable, Serializable { @Id @Column(name = "OBJECT_ID") @GeneratedValue(strategy = GenerationType.AUTO) + @Field @XmlElement(name = "object-id", namespace = CORE_XML_NS) private long objectId; @@ -100,6 +104,7 @@ public class CcmObject implements Identifiable, Serializable { */ @Column(name = "UUID", unique = true) @NotNull + @Field @XmlElement(name = "uuid") private String uuid; @@ -108,6 +113,7 @@ public class CcmObject implements Identifiable, Serializable { */ @Column(name = "DISPLAY_NAME") @Audited + @Field @XmlElement(name = "display-name", namespace = CORE_XML_NS) private String displayName; diff --git a/ccm-core/src/main/java/org/libreccm/modules/CcmIntegrator.java b/ccm-core/src/main/java/org/libreccm/modules/CcmIntegrator.java index ddd33b7d6..d749aec1f 100644 --- a/ccm-core/src/main/java/org/libreccm/modules/CcmIntegrator.java +++ b/ccm-core/src/main/java/org/libreccm/modules/CcmIntegrator.java @@ -453,6 +453,18 @@ public class CcmIntegrator implements Integrator { } } + /** + * A helper method for getting a setting from the configuration database. + * We can't use JPA/Hibernate in this class because the JPA subsystem is + * not initialised when this class runs. Therefore we have the fallback to + * JDBC here. + * + * @param connection Connection to the database. + * @param settingClass Setting class used to represent the setting. + * @param settingName The name of the setting to retrieve. + * @return The value of the setting. + * @throws SQLException + */ private Optional getSetting(final Connection connection, final String settingClass, final String settingName) diff --git a/ccm-core/src/main/java/org/libreccm/search/SearchManager.java b/ccm-core/src/main/java/org/libreccm/search/SearchManager.java new file mode 100644 index 000000000..57703bafc --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/search/SearchManager.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2016 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.search; + +import org.apache.lucene.search.Query; +import org.hibernate.search.MassIndexer; +import org.hibernate.search.SearchFactory; +import org.hibernate.search.jpa.FullTextEntityManager; +import org.hibernate.search.jpa.Search; +import org.hibernate.search.query.dsl.QueryBuilder; +import org.libreccm.core.CoreConstants; +import org.libreccm.security.AuthorizationRequired; +import org.libreccm.security.RequiresPrivilege; + +import java.util.List; +import java.util.concurrent.Future; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.transaction.Transactional; + +/** + * + * @author Jens Pelzetter + */ +@ApplicationScoped +public class SearchManager { + + @Inject + private EntityManager entityManager; + + private FullTextEntityManager fullTextEntityManager; + + public SearchManager() { + + } + + @PostConstruct + private void init() { + fullTextEntityManager = Search.getFullTextEntityManager(entityManager); + } + + @AuthorizationRequired + @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) + public Future rebuildIndex() { + final MassIndexer indexer = fullTextEntityManager.createIndexer(); + return indexer.start(); + } + + public QueryBuilder createQueryBuilder(final Class entityClass) { + final SearchFactory searchFactory = fullTextEntityManager.getSearchFactory(); + return searchFactory.buildQueryBuilder().forEntity(entityClass).get(); + } + + @Transactional(Transactional.TxType.REQUIRED) + public List executeQuery(final Query query) { + final javax.persistence.Query jpaQuery = fullTextEntityManager + .createFullTextQuery(query); + + return jpaQuery.getResultList(); + } + +} diff --git a/pom.xml b/pom.xml index ba4b17f8f..2cb25c3b6 100644 --- a/pom.xml +++ b/pom.xml @@ -276,8 +276,8 @@ --> org.hibernate - hibernate-search - 5.5.4.Final + hibernate-search-orm + 5.5.5.Final