package org.apereo.cas.config;

import lombok.extern.slf4j.Slf4j;
import org.apereo.cas.authentication.CoreAuthenticationUtils;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.discovery.CasServerProfileRegistrar;
import org.apereo.cas.services.ServicesManager;
import org.apereo.services.persondir.IPersonAttributeDao;
import org.pac4j.core.client.Clients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * This is {@link CasDiscoveryProfileConfiguration}.
 *
 * @author Misagh Moayyed
 * @since 5.2.0
 */
@Configuration("casDiscoveryProfileConfiguration")
@EnableConfigurationProperties(CasConfigurationProperties.class)
@Slf4j
public class CasDiscoveryProfileConfiguration {

    @Autowired
    @Qualifier("servicesManager")
    private ServicesManager servicesManager;
    
    @Autowired
    private CasConfigurationProperties casProperties;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired(required = false)
    @Qualifier("builtClients")
    private Clients builtClients;

    @Autowired
    @Qualifier("attributeRepository")
    private IPersonAttributeDao attributeRepository;

    @Bean
    public CasServerProfileRegistrar casServerProfileRegistrar() {
        return new CasServerProfileRegistrar(this.servicesManager, casProperties, this.builtClients, availableAttributes());
    }

    @Bean
    public Set<String> availableAttributes() {
        final LinkedHashSet<String> attributes = new LinkedHashSet<>(0);
        if (attributeRepository != null) {
            attributes.addAll(attributeRepository.getPossibleUserAttributeNames());
        }
        if (casProperties.getAuthn().getLdap() != null) {
            casProperties.getAuthn().getLdap().stream()
                    .forEach(ldap -> {
                        attributes.addAll(transformAttributes(ldap.getPrincipalAttributeList()));
                        attributes.addAll(transformAttributes(ldap.getAdditionalAttributes()));
                    });
        }
        if (casProperties.getAuthn().getJdbc() != null) {
            casProperties.getAuthn().getJdbc().getQuery().stream()
                    .forEach(jdbc -> attributes.addAll(transformAttributes(jdbc.getPrincipalAttributeList())));
        }
        return attributes;
    }

    private Set<String> transformAttributes(final List<String> attributes) {
        final Set<String> attributeSet = new LinkedHashSet<>();
        CoreAuthenticationUtils.transformPrincipalAttributesListIntoMultiMap(attributes)
                .values()
                .stream()
                .forEach(v -> attributeSet.add((String)v));
        return attributeSet;
    }
}

@@ -177,32 +177,202 @@
 tionProperties;%0A
+import org.apereo.cas.configuration.model.support.jdbc.JdbcAuthenticationProperties;%0Aimport org.apereo.cas.configuration.model.support.ldap.LdapAuthenticationProperties;%0A
 import org.apere
@@ -1385,20 +1385,16 @@
 anager;%0A
-    
 %0A    @Au
@@ -2072,55 +2072,8 @@
 0);%0A
-        if (attributeRepository != null) %7B%0A    
     
@@ -2144,32 +2144,33 @@
 teNames());%0A
+%0A
         
 %7D%0A        if
@@ -2153,38 +2153,77 @@
 );%0A%0A        
-%7D%0A        if (
+final List%3CLdapAuthenticationProperties%3E ldapProps = 
 casPropertie
@@ -2244,16 +2244,39 @@
 etLdap()
+;%0A        if (ldapProps
  != null
@@ -2295,42 +2295,17 @@
     
-casProperties.getAuthn().getLdap()
+ldapProps
 .str
@@ -2302,36 +2302,32 @@
 pProps.stream()%0A
-    
                 
@@ -2357,36 +2357,32 @@
                 
-   
- 
 attributes.addAl
@@ -2451,36 +2451,32 @@
                 
-    
 attributes.addAl
@@ -2527,28 +2527,24 @@
 ibutes()));%0A
-    
             
@@ -2569,20 +2569,63 @@
         
-if (
+final JdbcAuthenticationProperties jdbcProps = 
 casPrope
@@ -2650,16 +2650,39 @@
 etJdbc()
+;%0A        if (jdbcProps
  != null
@@ -2701,42 +2701,17 @@
     
-casProperties.getAuthn().getJdbc()
+jdbcProps
 .get
@@ -2719,36 +2719,32 @@
 uery().stream()%0A
-    
                 
@@ -3121,20 +3121,16 @@
         
-    
 .values(
@@ -3131,20 +3131,16 @@
 alues()%0A
-    
         
@@ -3157,36 +3157,32 @@
 m()%0A            
-  
-  
 .forEach(v -%3E at
@@ -3200,17 +3200,20 @@
 add(
-(
+v.to
 String
+(
 )
-v
 ));%0A

fix checkstyle