PkgAliasStore.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 org.ctan.site.domain.catalogue.Pkg;
import org.ctan.site.domain.catalogue.PkgAlias;
import org.hibernate.SessionFactory;

import io.dropwizard.hibernate.AbstractDAO;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Root;

/**
 * The class <code>PkgAliasStore</code> contains the repository for package
 * aliases.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
public class PkgAliasStore extends AbstractDAO<PkgAlias> {

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

        super(sessionFactory);
    }

    /**
     * The method <code>drop</code> deletes the given package from the database
     * and the search index.
     *
     * @param pkg the package
     */
    public void drop(Pkg pkg) {

        currentSession().remove(pkg);
    }

    // /**
    // * The method <code>findAll</code> collects all packages.
    // *
    // * @return a list of packages
    // */
    // public List<Pkg> findAll() {
    //
    // CriteriaBuilder cb = currentSession().getCriteriaBuilder();
    // var query = criteriaQuery();
    // Root<Pkg> p = query.from(Pkg.class);
    // query.orderBy(cb.asc(p.get("key")));
    // return list(query);
    // }

    // /**
    // * The method <code>findAllByKeyStartingWith</code> collects all packages
    // * where the key has a given prefix.
    // *
    // * @param s the prefix or {@code null}
    // * @return the list of matching packages
    // */
    // public List<Pkg> findAllByKeyStartingWith(String s) {
    //
    // var query = criteriaQuery();
    // CriteriaBuilder cb = currentSession().getCriteriaBuilder();
    // Root<Pkg> p = query.from(Pkg.class);
    // if (s != null && !"".equals(s)) {
    // query.where(cb.like(p.get("key"), s.toLowerCase() + "%"));
    // }
    // query.orderBy(cb.asc(p.get("key")));
    // return list(query);
    // }

    // /**
    // * The method <code>findAllByNameStartingWith</code> collects all packages
    // * where the name has a given prefix.
    // *
    // * @param s the prefix or {@code null}
    // * @return the list of matching packages
    // */
    // public List<Pkg> findAllByNameStartingWith(String s) {
    //
    // var query = criteriaQuery();
    // CriteriaBuilder cb = currentSession().getCriteriaBuilder();
    // Root<Pkg> p = query.from(Pkg.class);
    // if (s != null && !"".equals(s)) {
    // query.where(cb.like(p.get("name"), s.toLowerCase() + "%"));
    // }
    // query.orderBy(cb.asc(p.get("key")));
    // return list(query);
    // }

    /**
     * The method <code>getByKey</code> provides means to retrieve a package by
     * its key.
     *
     * @param id the id
     * @return the package or {@code null}
     */
    public PkgAlias getByKey(String id) {

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

        return uniqueResult(query);
    }

    /**
     * The method <code>insert</code> provides means to save a package.
     *
     * @param alias the package
     * @return the package
     */
    public PkgAlias insert(PkgAlias alias) {

        return persist(alias);
    }

}