JsonTopicResource.java

/*
 * Copyright © 2024-2025 The CTAN Team and individual Topics
 *
 * This file is distributed under the 3-clause BSD license.
 * See file LICENSE for details.
 */
package org.ctan.site.resources.catalogue.api;

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

import org.ctan.site.stores.TopicStore;

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.QueryParam;
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>Topic3Resource</code> contains the controller for the Topic
 * resource.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public class JsonTopicResource {

    /**
     * The class <code>TopicTo</code> contains the transport object for the
     * Topic resource in the summary list.
     */
    @Getter
    @Builder
    @SuppressFBWarnings(value = "EI_EXPOSE_REP")
    protected static class TopicTo {

        private String key;

        private String details;

        private String[] packages;
    }

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

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

        this.store = store;
    }

    /**
     * The method <code>getTopicByKey</code> provides means to retrieve an
     * Topic.
     *
     * @param vers the version
     * @param id the key of the topic
     * @param ref the indicator whether or not to return the references to the
     *     packages tagged with the topic
     * @return a topic or {@code null}
     */
    @GET
    @Path("/json/{vers}/topic/{id}")
    @PermitAll
    @UnitOfWork(value = "siteDb")
    public TopicTo getTopicByKey(
        @NonNull @PathParam("vers") String vers,
        @NonNull @PathParam("id") String id,
        @QueryParam("ref") Boolean ref) {

        switch (vers) {
            case "1.0", "1.1", "1.2", "1.3", "2.0", "2.1":
                break;
            default:
                throw new WebApplicationException(Status.NOT_FOUND);
        }
        var topic = store.getByKey(id);
        if (topic == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        var result = TopicTo.builder()
            .key(topic.getKey())
            .details(topic.getDetails("en"));
        if (ref) {
            result.packages(topic.getPackages()
                .stream()
                .map(p -> p.getKey())
                .collect(Collectors.toList())
                .toArray(new String[]{}));
        }
        return result.build();
    }

    /**
     * The method <code>getTopics</code> provides means to retrieve a list of
     * topics starting with a given pattern.
     *
     * @param vers the version number
     * @param key the key
     * @return a list of matching Topic summaries
     */
    @GET
    @Path("/json/{vers}/topics")
    @PermitAll
    @UnitOfWork(value = "siteDb")
    public List<TopicTo> getTopics(
        @NonNull @PathParam("vers") String vers,
        @QueryParam("key") String key) {

        switch (vers) {
            case "1.0", "1.1", "1.2", "1.3", "2.0", "2.1":
                break;
            default:
                throw new WebApplicationException(Status.NOT_FOUND);
        }
        return store.findAllByKeyStartingWith(key != null ? key : "")
            .stream()
            .map(a -> {
                return TopicTo.builder()
                    .key(a.getKey())
                    .details(a.getDetails("en"))
                    .build();
            }).collect(Collectors.toList());
    }
}