InstallData3Resource.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.resources.catalogue;

import java.util.List;
import java.util.stream.Collectors;

import org.ctan.site.services.DateUtils;
import org.ctan.site.stores.InstallDataStore;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.dropwizard.hibernate.UnitOfWork;
import jakarta.annotation.security.PermitAll;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response.Status;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;

/**
 * The class <code>InstallData3Resource</code> contains the controller for the
 * install data resource.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Path("/3.0")
@Produces(MediaType.APPLICATION_JSON)
public class InstallData3Resource {

    /**
     * The class <code>InstallDataTo</code> contains the transport object for
     * the install entry in the installations list.
     */
    @Getter
    @Builder
    protected static class InstallTo {

        /**
         * The field <code>pkg</code> contains the package key.
         */
        private String pkg;

        /**
         * The field <code>date</code> contains the installation date on CTAN.
         */
        private String date;
    }

    /**
     * The field <code>store</code> contains the underlying repository.
     */
    private InstallDataStore store;

    /**
     * This is the constructor for the class <code>InstallData3Resource</code>.
     *
     * @param store the underlying store
     */
    @SuppressFBWarnings(value = {"CT_CONSTRUCTOR_THROW", "EI_EXPOSE_REP2"})
    public InstallData3Resource(@NonNull InstallDataStore store) {

        this.store = store;
    }

    /**
     * The method <code>getInstallDataByKey</code> contains the end-point for
     * /installs/key. It returns the install entries.
     *
     * @param key the key
     * @return the install entries
     */
    @GET
    @Path("/installs/{key}")
    @PermitAll
    @UnitOfWork(value = "siteDb")
    public List<InstallTo> getInstallDataByPkg(
        @NonNull @PathParam("key") String key) {

        var entries = store.findAllByPkg(key);
        if (entries == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        return entries.stream()
            .map(inst -> InstallTo.builder()
                .pkg(inst.getPkg())
                .date(DateUtils.formatDate(inst.getInstallationDate()))
                .build())
            .collect(Collectors.toList());
    }
}