GuestBookStore.java

/*
 * Copyright © 2021-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.GuestBook;
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;

/**
 * The class <code>GuestBookStore</code> contains the repository for guest book
 * items.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
public class GuestBookStore extends AbstractIndexingStore<GuestBook> {

    /**
     * This is the constructor for the <code>GuestBookStore</code>.
     *
     * @param sessionFactory the session factory
     * @param indexingSession the indexing session
     */
    public GuestBookStore(SessionFactory sessionFactory,
        IndexingSession indexingSession) {

        super(sessionFactory, indexingSession);
    }

    /**
     * {@inheritDoc}
     *
     * @see org.ctan.site.stores.base.AbstractIndexingStore#indexType()
     */
    @Override
    protected IndexType indexType() {

        return IndexType.GUESTBOOK;
    }

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

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

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

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

}