CrudPkg3Resource.java
/*
* Copyright © 2025-2026 The CTAN Team and individual pkgs
*
* This file is distributed under the 3-clause BSD license.
* See file LICENSE for details.
*/
package org.ctan.site.resources.admin;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.ctan.site.domain.catalogue.AuthorRef;
import org.ctan.site.domain.catalogue.Pkg;
import org.ctan.site.stores.PkgStore;
import org.ctan.site.stores.base.GeneralPage;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.dropwizard.hibernate.UnitOfWork;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import lombok.NonNull;
/**
* The class <code>CrudPkg3Resource</code> contains the CRUD controller for the
* pkg resource.
*
* @pkg <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
@Path("/3.0/admin")
@Produces(MediaType.APPLICATION_JSON)
// TODO @RolesAllowed("ADMIN")
public class CrudPkg3Resource {
/**
* The field <code>store</code> contains the underlying store.
*/
private PkgStore store;
/**
* This is the constructor for the class <code>CrudPkg3Resource</code>.
*
* @param store the underlying store
*/
@SuppressFBWarnings(value = {"CT_CONSTRUCTOR_THROW", "EI_EXPOSE_REP2"})
public CrudPkg3Resource(@NonNull PkgStore store) {
this.store = store;
}
/**
* The method <code>create</code> provides an end-point to create a pkg.
*
* @param pkg the pkg object to store
* @return the updated entity
*/
@POST
@Path("/pkg")
@UnitOfWork(value = "siteDb")
public Pkg create(@NonNull Pkg pkg) {
return store.save(pkg);
}
/**
* The method <code>get</code> provides means to retrieve a pkg.
*
* @param key the account
* @return {@code true} iff the pkg exists
*/
@GET
@Path("/pkg/{key}")
@UnitOfWork(value = "siteDb")
public Map<String, Object> get(@NonNull @PathParam("key") String key) {
Map<String, Object> map = new HashMap<>();
var pkg = store.getByKey(key);
map.put("key", pkg.getKey());
map.put("name", pkg.getName());
map.put("versionNumber", pkg.getVersionNumber());
map.put("versionDate", pkg.getVersionDate());
map.put("authors", pkg.getAuthors().stream()
.map((AuthorRef it) -> List.of(it.getAuthor().getKey(),
it.getAuthor().toString(), it.isActive()))
.collect(Collectors.toList()));
map.put("path", pkg.getCtanPath());
map.put("home", pkg.getHome().split(","));
map.put("announce", pkg.getAnnounce().split(","));
map.put("bugs", pkg.getBugs().split(","));
map.put("repository", pkg.getRepository().split(","));
map.put("development", pkg.getDevelopment().split(","));
map.put("support", pkg.getSupport().split(","));
map.put("texlive", pkg.getTexliveLocation());
map.put("miktex", pkg.getMiktexLocation());
map.put("tlContrib", pkg.getTlContribLocation());
map.put("topics", pkg.getTopics().stream()
.map(it -> it.getKey())
.collect(Collectors.toList()));
return map;
}
/**
* The method <code>list</code> provides means to retrieve a page of
* packages.
*
* @param q the name pattern
* @param page the page
* @param size the page size
* @param orderBy the order
* @param asc the indicator for ascending
* @return the page for the pkg or {@code null}
*/
@GET
@Path("/pkgs")
@UnitOfWork(value = "siteDb")
public GeneralPage list(@QueryParam("q") String q,
@QueryParam("page") @DefaultValue("1") int page,
@QueryParam("size") @DefaultValue("256") int size,
@QueryParam("order") String orderBy,
@QueryParam("asc") boolean asc) {
GeneralPage pg = store.list(q, page - 1, size, orderBy, asc);
pg.setList(pg.getList().stream()
.map((Map<String, Object> it) -> Map.of("key", it.get("key"),
"name", it.get("name"),
"versionNumber", it.get("versionNumber"),
"versionDate", it.get("versionDate")))
.collect(Collectors.toList()));
return pg;
}
/**
* The method <code>remove</code> provides an end-point to remove a pkg.
*
* @param key the account
* @return {@code true} iff the pkg has existed
*/
@DELETE
@Path("/pkg/{key}")
@UnitOfWork(value = "siteDb")
public boolean remove(@NonNull @PathParam("key") String key) {
var pkg = store.getByKey(key);
return pkg == null ? false : store.remove(pkg);
}
/**
* The method <code>set</code> provides means to set a single attribute of
* an pkg.
*
* @param pkg the pkg
* @param map the mapping containing key and value
* @return {@code true} iff the setting has succeeded
*/
@PUT
@Path("/pkg/{pkg}")
@UnitOfWork(value = "siteDb")
public boolean set(@NonNull @PathParam("pkg") String pkg,
@NonNull Map<String, String> map) {
return store.set(pkg,
map.get("key"),
map.get("value")) != null;
}
}