BrowseProjectController.java

package info.textgrid.rep.browseproject;

import java.io.IOException;
import java.util.Comparator;
import java.util.List;

import info.textgrid.namespaces.middleware.tgsearch.portal.Project;
import info.textgrid.rep.i18n.I18N;
import info.textgrid.rep.i18n.LocaleResolver;
import info.textgrid.rep.service.TgrepConfigurationService;
import info.textgrid.rep.service.TgsearchClientService;
import io.quarkus.qute.Template;
import io.quarkus.qute.TemplateInstance;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/projects")
public class BrowseProjectController {

  @Inject
  private TgsearchClientService tgsearchClient;

  @Inject
  private TgrepConfigurationService tgrepConfig;

  @Inject
  LocaleResolver localeResolver;

  @Inject
  Template browseprojects;

  @GET
  @Produces(MediaType.TEXT_HTML)
  public TemplateInstance projects() throws IOException {

    I18N i18n = localeResolver.getI18N();

    List<Project> projects = this.tgsearchClient.getProjects().getProjects();

    // sort by existance of portalconfig. object count otherwise.
    // list is pre-sorted by object count from tgsearch, so we do not sort all by count in a first comparision step
    projects.sort(new ProjectComparator());

    return browseprojects
        .data("projects", projects)
        .data("language", localeResolver.getLanguage())
        .data("config", tgrepConfig)
        .data("i18n", i18n.getTranslationMap())
        .data("localeResolver", localeResolver);
  }

  /**
   * Comparator, which prefers Projects with a portalconfig (portalconfig not null)
   * and sorts by object count otherwise
   */
  public static class ProjectComparator implements Comparator<Project> {

    @Override
    public int compare(Project p1, Project p2) {

      Object value1 = p1.getPortalconfig();
      Object value2 = p2.getPortalconfig();

      if (value1 == null && value2 == null) {
        return 0;
      } else if(value1 == null) {
        return 1;
      } else if (value2 == null) {
        return -1;
      } else {
        return p1.getCount() >= p2.getCount() ? -1 : 1;
      }
    }
  }

}