StopwordStore.java

/*
 * Copyright © 2024-2025 The CTAN Team and individual authors
 *
 * 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.site.Stopword;
import org.ctan.site.stores.base.AbstractStore;
import org.hibernate.SessionFactory;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;

/**
 * The class <code>StopwordStore</code> contains the repository for guest book
 * stop words.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
public class StopwordStore extends AbstractStore<Stopword> {

    /**
     * This is the constructor for the <code>StopwordStore</code>.
     *
     * @param sessionFactory the session factory
     */
    public StopwordStore(SessionFactory sessionFactory) {

        super(sessionFactory);
    }

    /**
     * {@inheritDoc}
     *
     * @see org.ctan.site.stores.base.AbstractStore#listQuery(java.lang.String,
     *     jakarta.persistence.criteria.CriteriaBuilder,
     *     jakarta.persistence.criteria.CriteriaQuery)
     */
    @Override
    protected Root<Stopword> listQuery(String term, CriteriaBuilder cb,
        CriteriaQuery<Stopword> query) {

        Root<Stopword> root = query.from(Stopword.class);
        if (term != null && !term.isBlank()) {
            var t = "%" + term.toLowerCase() + "%";
            query.where(cb.like(cb.lower(root.get("stopword")), t));
        }
        return root;
    }

    /**
     * {@inheritDoc}
     *
     * @see org.ctan.site.stores.base.AbstractStore#map(java.util.List)
     */
    @Override
    protected List<Map<String, Object>> map(List<Stopword> list) {

        return list.stream()
            .map(it -> it.toMap())
            .collect(Collectors.toList());
    }

}