TopicStore.java
/*
* Copyright © 2021-2025 The CTAN Team and individual topics
*
* This file is distributed under the 3-clause BSD license.
* See file LICENSE for details.
*/
package org.ctan.site.stores;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.ctan.site.domain.catalogue.Topic;
import org.ctan.site.services.search.base.IndexType;
import org.ctan.site.services.search.base.IndexingSession;
import org.ctan.site.stores.base.AbstractIndexingStore;
import org.hibernate.SessionFactory;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import lombok.NonNull;
/**
* The class <code>TopicStore</code> contains the repository for topics.
*
* @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
public class TopicStore extends AbstractIndexingStore<Topic> {
/**
* This is the constructor for the <code>TopicStore</code>.
*
* @param sessionFactory the session factory
* @param indexingSession the indexing session
*/
public TopicStore(SessionFactory sessionFactory,
IndexingSession indexingSession) {
super(sessionFactory, indexingSession);
}
/**
* The method <code>findAllByNameStartingWith</code> provides means to
* retrieve topics where the sort text is starting with a given string. The
* comparison is done case-insensitive.
*
* @param pattern the initial segment
* @return the list of topics ordered by the sort text
*/
public List<Topic> findAllByKeyStartingWith(@NonNull String pattern) {
var query = criteriaQuery();
CriteriaBuilder cb = currentSession().getCriteriaBuilder();
Root<Topic> topic = query.from(Topic.class);
query.where(cb.like(topic.get("key"), pattern.toLowerCase() + "%"))
.orderBy(cb.asc(topic.get("key")));
return list(query);
}
/**
* The method <code>getByKey</code> provides means to find an topic by its
* key.
*
* @param key the (non-null) key
* @return the topic or {@code null}
*/
public Topic getByKey(@NonNull String key) {
var query = criteriaQuery();
CriteriaBuilder cb = currentSession().getCriteriaBuilder();
Root<Topic> root = query.from(Topic.class);
query.where(cb.equal(root.get("key"), key));
return uniqueResult(query);
}
/**
* {@inheritDoc}
*
* @see org.ctan.site.stores.base.AbstractIndexingStore#indexType()
*/
@Override
protected IndexType indexType() {
return IndexType.TOPICS;
}
/**
* {@inheritDoc}
*
* @see org.ctan.site.stores.base.AbstractStore#listQuery(java.lang.String,
* jakarta.persistence.criteria.CriteriaBuilder,
* jakarta.persistence.criteria.CriteriaQuery)
*/
@Override
protected Root<Topic> listQuery(String term, CriteriaBuilder cb,
CriteriaQuery<Topic> query) {
Root<Topic> root = query.from(Topic.class);
if (term != null && !term.isBlank()) {
var t = "%" + term.toLowerCase() + "%";
query.where(cb.like(cb.lower(root.get("key")), t));
}
return root;
}
/**
* {@inheritDoc}
*
* @see org.ctan.site.stores.base.AbstractStore#map(java.util.List)
*/
@Override
protected List<Map<String, Object>> map(List<Topic> list) {
return list.stream()
.map(it -> it.toMap())
.collect(Collectors.toList());
}
}