package org.apereo.cas.ticket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.OrderComparator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * This is {@link DefaultTicketCatalog}.
 *
 * @author Misagh Moayyed
 * @since 5.1.0
 */
public class DefaultTicketCatalog implements TicketCatalog {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTicketCatalog.class);
    
    private final Map<String, TicketDefinition> ticketMetadataMap = new HashMap<>();

    public DefaultTicketCatalog() {
    }

    @Override
    public TicketDefinition find(final String ticketId) {
        return ticketMetadataMap.values().stream().filter(md -> ticketId.startsWith(md.getPrefix())).findFirst().orElse(null);
    }

    @Override
    public TicketDefinition find(final Ticket ticket) {
        LOGGER.debug("Locating ticket definition for ticket [{}]", ticket);
        return find(ticket.getPrefix());
    }

    @Override
    public void register(final TicketDefinition ticketDefinition) {
        LOGGER.debug("Registering/Updating ticket definition [{}]", ticketDefinition);
        ticketMetadataMap.put(ticketDefinition.getPrefix(), ticketDefinition);
    }

    @Override
    public void update(final TicketDefinition metadata) {
        register(metadata);
    }

    @Override
    public boolean contains(final String ticketId) {
        LOGGER.debug("Locating ticket definition for [{}]", ticketId);
        return ticketMetadataMap.containsKey(ticketId);
    }

    @Override
    public Collection<TicketDefinition> findAll() {
        final List list = new ArrayList<>(ticketMetadataMap.values());
        OrderComparator.sort(list);
        LOGGER.debug("Located all registered and known sorted ticket definitions [{}]", list);
        return list;
    }

    @Override
    public Collection<TicketDefinition> find(final Class<Ticket> ticketClass) {
        final List list = ticketMetadataMap.values()
                .stream()
                .filter(t -> t.getImplementationClass().isInstance(ticketClass))
                .collect(Collectors.toList());
        OrderComparator.sort(list);
        LOGGER.debug("Located all registered and known sorted ticket definitions [{}] that match [{}]", list, ticketClass);
        return list;
    }
}

@@ -550,13 +550,9 @@
 s);%0A
-    
 %0A
+
     
@@ -760,125 +760,452 @@
     
-return ticketMetadataMap.values().stream().filter(md -%3E ticketId.startsWith(md.getPrefix())).findFirst().orElse(null)
+final TicketDefinition defn = ticketMetadataMap.values()%0A                .stream()%0A                .filter(md -%3E ticketId.startsWith(md.getPrefix()))%0A                .findFirst()%0A                .orElse(null);%0A        if (defn == null) %7B%0A            LOGGER.error(%22Ticket definition for %5B%7B%7D%5D cannot be found in the ticket catalog which only contains %5B%7B%7D%5D%22,%0A                    ticketId, ticketMetadataMap.keySet());%0A        %7D%0A        return defn
 ;%0A  

add extra logging to the catalog