LicenseStore.java

/*
 * Copyright © 2023-2025 The CTAN Team and individual licenses
 *
 * 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.License;
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>LicenseStore</code> contains the repository for licenses.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
public class LicenseStore extends AbstractIndexingStore<License> {

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

        super(sessionFactory, indexingSession);
    }

    /**
     * The method <code>findAllByKeyOrNameStartingWith</code> provides means to
     * retrieve licenses where the sort text is starting with a given string.
     * The comparison is done case-insensitive.
     *
     * @param s the initial segment
     * @return the list of licenses ordered by the sort text
     */
    public List<License> findAllByKeyOrNameStartingWith(String s) {

        var query = criteriaQuery();
        CriteriaBuilder cb = currentSession().getCriteriaBuilder();
        Root<License> license = query.from(License.class);
        if (s != null && !"".equals(s)) {
            query.where(cb.or(cb.like(license.get("key"), s),
                cb.like(license.get("name"), "%" + s)));
        }
        query.orderBy(cb.asc(license.get("key")));
        return list(query);
    }

    /**
     * The method <code>findAllByKeyStartingWith</code> provides means to
     * retrieve licenses where the sort text is starting with a given string.
     * The comparison is done case-insensitive.
     *
     * @param s the initial segment
     * @return the list of licenses ordered by the sort text
     */
    public List<License> findAllByKeyStartingWith(String s) {

        s = s.toLowerCase() + "%";
        var query = criteriaQuery();
        CriteriaBuilder cb = currentSession().getCriteriaBuilder();
        Root<License> license = query.from(License.class);
        if (s != null && !"".equals(s)) {
            query.where(cb.like(license.get("key"), s));
        }
        query.orderBy(cb.asc(license.get("key")));
        return list(query);
    }

    /**
     * The method <code>getByKey</code> provides means to find an license by its
     * key.
     *
     * @param key the key
     * @return the license or {@code null}
     */
    public License getByKey(String key) {

        var query = criteriaQuery();
        CriteriaBuilder cb = currentSession().getCriteriaBuilder();
        Root<License> license = query.from(License.class);
        query.where(cb.equal(license.get("key"), key));

        return uniqueResult(query);
    }

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

        return IndexType.LICENSES;
    }

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

        Root<License> root = query.from(License.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<License> list) {

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

}