JsonAuthorResource.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.api;

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

import org.apache.commons.lang3.StringUtils;
import org.ctan.site.domain.Gender;
import org.ctan.site.stores.AuthorStore;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.dropwizard.hibernate.UnitOfWork;
import jakarta.annotation.security.PermitAll;
import jakarta.ws.rs.DefaultValue;
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.Builder.Default;
import lombok.Getter;
import lombok.NonNull;

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

    /**
     * The class <code>AuthorSummaryTo</code> contains the transport object for
     * the author resource in the summary list.
     */
    @Getter
    @Builder
    protected static class AuthorSummaryTo {

        private String familyname;

        private String givenname;

        private String von;

        private String junior;

        private String pseudonym;

        private String key;

        private String gender;

        @Default
        private Boolean died = Boolean.FALSE;
    }

    /**
     * The class <code>AuthorTo</code> contains the transport object for the
     * author resource.
     */
    @Getter
    @Builder
    @SuppressFBWarnings(value = "EI_EXPOSE_REP")
    protected static class AuthorTo {

        private String familyname;

        private String givenname;

        private String von;

        private String junior;

        private String pseudonym;

        private String key;

        private String title;

        @Default
        private Boolean female = Boolean.FALSE;

        @Default
        private Boolean died = Boolean.FALSE;

        private String[] packages;
    }

    /**
     * The class <code>PkgSummaryTo</code> contains the transport object for the
     * package of the author resource.
     */
    @Getter
    @Builder
    protected static class PkgSummaryTo {

        private String name;

        private String key;

        private String summary;

        @Default
        private Boolean active = Boolean.TRUE;
    }

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

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

        this.store = store;
    }

    /**
     * The method <code>getAuthorByKey</code> provides means to retrieve an
     * author.
     *
     * @param vers the version
     * @param id the key of the author
     * @param ref the indicator whether or not to return the references to the
     *     packages authored by the author
     * @return an author or {@code null}
     */
    @GET
    @Path("/json/{vers}/author/{id}")
    @PermitAll
    @UnitOfWork(value = "siteDb")
    public AuthorTo getAuthorByKey(
        @NonNull @PathParam("vers") String vers,
        @NonNull @PathParam("id") String id,
        @QueryParam("ref") @DefaultValue("false") Boolean ref) {

        var author = store.getByKey(id);
        if (author == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        String[] packages = null;
        if (ref) {
            packages = author.getRefs().stream()
                .map(r -> r.getPkg().getKey())
                .collect(Collectors.toList())
                .toArray(new String[]{});
        }
        var pseudonym = author.getPseudonym();
        var hasPseudonym = StringUtils.isNotEmpty(pseudonym);
        return switch (vers) {
            case "1.0", "1.1" -> AuthorTo.builder()
                .givenname(hasPseudonym
                    ? pseudonym
                    : author.getGivenname())
                .familyname(hasPseudonym
                    ? null
                    : author.getFamilyname())
                .key(author.getKey())
                .died(null)
                .female(null)
                .packages(packages)
                .build();
            case "1.2", "1.3", "2.0", "2.1" -> AuthorTo.builder()
                .givenname(hasPseudonym
                    ? pseudonym
                    : author.getGivenname())
                .familyname(hasPseudonym
                    ? null
                    : author.getFamilyname())
                .von(author.getVon())
                .junior(author.getJunior())
                .title(author.getTitle())
                .key(author.getKey())
                .died(author.getDied())
                .female(author.getGender() == Gender.F)
                .packages(packages)
                .build();
            default -> throw new WebApplicationException(Status.NOT_FOUND);
        };
    }

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

        var stream = store.findAllByKeyStartingWith(key != null ? key : "")
            .stream();
        return switch (vers) {
            case "1.0", "1.1" -> stream
                .map(a -> {
                    var pseudonym = a.getPseudonym();
                    var hasPseudonym = StringUtils.isNotEmpty(pseudonym);
                    return AuthorSummaryTo.builder()
                        .key(a.getKey())
                        .givenname(hasPseudonym
                            ? null
                            : a.getGivenname())
                        .familyname(hasPseudonym
                            ? pseudonym
                            : a.getFamilyname())
                        .build();
                }).collect(Collectors.toList());
            case "1.2" -> stream
                .map(author -> {
                    var pseudonym = author.getPseudonym();
                    var hasPseudonym = StringUtils.isNotEmpty(pseudonym);
                    return AuthorSummaryTo.builder()
                        .key(author.getKey())
                        .givenname(hasPseudonym
                            ? null
                            : author.getGivenname())
                        .familyname(hasPseudonym
                            ? pseudonym
                            : author.getFamilyname())
                        .gender(author.getGender().getValue())
                        .build();
                }).collect(Collectors.toList());
            case "1.3", "2.0", "2.1" -> stream
                .map(a -> {
                    var pseudonym = a.getPseudonym();
                    var hasPseudonym = StringUtils.isNotEmpty(pseudonym);
                    return AuthorSummaryTo.builder()
                        .key(a.getKey())
                        .givenname(hasPseudonym
                            ? null
                            : a.getGivenname())
                        .familyname(
                            hasPseudonym
                                ? pseudonym
                                : a.getFamilyname())
                        .von(hasPseudonym ? null : a.getVon())
                        .junior(hasPseudonym ? null : a.getJunior())
                        .pseudonym(pseudonym)
                        .died(a.getDied())
                        .gender(a.getGender().getValue())
                        .build();
                }).collect(Collectors.toList());
            default -> throw new WebApplicationException(Status.NOT_FOUND);
        };
    }
}