// Path: src/main/java/com/epam/reportportal/auth/integration/converter/SamlConverter.java
// public class SamlConverter {
// 
// 	public static final BiConsumer<UpdateAuthRQ, Integration> UPDATE_FROM_REQUEST = (request, integration) -> {
// 		integration.setEnabled(request.getEnabled());
// 		integration.setName(IDP_NAME.getRequiredParameter(request));
// 		ParameterUtils.setSamlParameters(request, integration);
// 	};
// 
// 	public final static Function<Integration, SamlResource> TO_RESOURCE = integration -> {
// 		SamlResource resource = new SamlResource();
// 		resource.setId(integration.getId());
// 		resource.setIdentityProviderName(integration.getName());
// 		resource.setEnabled(integration.isEnabled());
// 
// 		EMAIL_ATTRIBUTE.getParameter(integration).ifPresent(resource::setEmailAttribute);
// 		FIRST_NAME_ATTRIBUTE.getParameter(integration).ifPresent(resource::setFirstNameAttribute);
// 		LAST_NAME_ATTRIBUTE.getParameter(integration).ifPresent(resource::setLastNameAttribute);
// 		FULL_NAME_ATTRIBUTE.getParameter(integration).ifPresent(resource::setFullNameAttribute);
// 		IDP_ALIAS.getParameter(integration).ifPresent(resource::setIdentityProviderAlias);
// 		IDP_METADATA_URL.getParameter(integration).ifPresent(resource::setIdentityProviderMetadataUrl);
// 		IDP_URL.getParameter(integration).ifPresent(resource::setIdentityProviderUrl);
// 		IDP_NAME_ID.getParameter(integration).ifPresent(resource::setIdentityProviderNameId);
// 		final IntegrationType integrationType = integration.getType();
// 		ofNullable(integrationType.getDetails()).flatMap(typeDetails -> Optional.ofNullable(typeDetails.getDetails()))
// 				.flatMap(BASE_PATH::getParameter)
// 				.ifPresent(resource::setCallbackUrl);
// 		return resource;
// 	};
// 
// 	public final static Function<List<Integration>, List<ExternalIdentityProviderConfiguration>> TO_EXTERNAL_PROVIDER_CONFIG = integrations -> {
// 		List<ExternalIdentityProviderConfiguration> externalProviders = integrations.stream()
// 				.map(integration -> new ExternalIdentityProviderConfiguration().setAlias(IDP_ALIAS.getParameter(integration).get())
// 						.setMetadata(IDP_METADATA_URL.getRequiredParameter(integration))
// 						.setLinktext(integration.getName())
// 						.setNameId(NameId.fromUrn(IDP_NAME_ID.getParameter(integration).get())))
// 				.collect(Collectors.toList());
// 		IntStream.range(0, externalProviders.size()).forEach(value -> externalProviders.get(value).setAssertionConsumerServiceIndex(value));
// 		return externalProviders;
// 	};
// 
// 	public final static Function<List<Integration>, SamlProvidersResource> TO_PROVIDERS_RESOURCE = integrations -> {
// 		if (CollectionUtils.isEmpty(integrations)) {
// 			SamlProvidersResource emptyResource = new SamlProvidersResource();
// 			emptyResource.setProviders(Collections.emptyList());
// 			return emptyResource;
// 		}
// 		SamlProvidersResource resource = new SamlProvidersResource();
// 		resource.setProviders(integrations.stream().map(TO_RESOURCE).collect(Collectors.toList()));
// 		return resource;
// 	};
// 
// }


import com.epam.reportportal.auth.integration.converter.SamlConverter;
import com.epam.ta.reportportal.dao.IntegrationRepository;
import com.epam.ta.reportportal.entity.integration.Integration;
import com.epam.ta.reportportal.entity.integration.IntegrationType;
import org.springframework.context.ApplicationListener;
import org.springframework.security.saml.provider.SamlServerConfiguration;
import org.springframework.security.saml.provider.service.config.LocalServiceProviderConfiguration;
import org.springframework.stereotype.Component;
import java.util.List;
/*
 * Copyright 2019 EPAM Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.epam.reportportal.auth.event;



/**
 * Handles SAML settings changes event and reload configuration of IDP in service provider configuration
 *
 * @author Yevgeniy Svalukhin
 */
@Component
public class SamlProvidersReloadEventHandler implements ApplicationListener<SamlProvidersReloadEvent> {

	private final IntegrationRepository integrationRepository;
	private final SamlServerConfiguration samlConfiguration;

	public SamlProvidersReloadEventHandler(IntegrationRepository integrationRepository, SamlServerConfiguration spConfiguration) {
		this.integrationRepository = integrationRepository;
		this.samlConfiguration = spConfiguration;
	}

	@Override
	public void onApplicationEvent(SamlProvidersReloadEvent event) {
		final IntegrationType integrationType = event.getIntegrationType();
		final List<Integration> integrations = integrationRepository.findAllGlobalByType(integrationType);

		LocalServiceProviderConfiguration serviceProvider = samlConfiguration.getServiceProvider();

		serviceProvider.getProviders().clear();
// Path: src/main/java/com/epam/reportportal/auth/integration/converter/SamlConverter.java
// public class SamlConverter {
// 
// 	public static final BiConsumer<UpdateAuthRQ, Integration> UPDATE_FROM_REQUEST = (request, integration) -> {
// 		integration.setEnabled(request.getEnabled());
// 		integration.setName(IDP_NAME.getRequiredParameter(request));
// 		ParameterUtils.setSamlParameters(request, integration);
// 	};
// 
// 	public final static Function<Integration, SamlResource> TO_RESOURCE = integration -> {
// 		SamlResource resource = new SamlResource();
// 		resource.setId(integration.getId());
// 		resource.setIdentityProviderName(integration.getName());
// 		resource.setEnabled(integration.isEnabled());
// 
// 		EMAIL_ATTRIBUTE.getParameter(integration).ifPresent(resource::setEmailAttribute);
// 		FIRST_NAME_ATTRIBUTE.getParameter(integration).ifPresent(resource::setFirstNameAttribute);
// 		LAST_NAME_ATTRIBUTE.getParameter(integration).ifPresent(resource::setLastNameAttribute);
// 		FULL_NAME_ATTRIBUTE.getParameter(integration).ifPresent(resource::setFullNameAttribute);
// 		IDP_ALIAS.getParameter(integration).ifPresent(resource::setIdentityProviderAlias);
// 		IDP_METADATA_URL.getParameter(integration).ifPresent(resource::setIdentityProviderMetadataUrl);
// 		IDP_URL.getParameter(integration).ifPresent(resource::setIdentityProviderUrl);
// 		IDP_NAME_ID.getParameter(integration).ifPresent(resource::setIdentityProviderNameId);
// 		final IntegrationType integrationType = integration.getType();
// 		ofNullable(integrationType.getDetails()).flatMap(typeDetails -> Optional.ofNullable(typeDetails.getDetails()))
// 				.flatMap(BASE_PATH::getParameter)
// 				.ifPresent(resource::setCallbackUrl);
// 		return resource;
// 	};
// 
// 	public final static Function<List<Integration>, List<ExternalIdentityProviderConfiguration>> TO_EXTERNAL_PROVIDER_CONFIG = integrations -> {
// 		List<ExternalIdentityProviderConfiguration> externalProviders = integrations.stream()
// 				.map(integration -> new ExternalIdentityProviderConfiguration().setAlias(IDP_ALIAS.getParameter(integration).get())
// 						.setMetadata(IDP_METADATA_URL.getRequiredParameter(integration))
// 						.setLinktext(integration.getName())
// 						.setNameId(NameId.fromUrn(IDP_NAME_ID.getParameter(integration).get())))
// 				.collect(Collectors.toList());
// 		IntStream.range(0, externalProviders.size()).forEach(value -> externalProviders.get(value).setAssertionConsumerServiceIndex(value));
// 		return externalProviders;
// 	};
// 
// 	public final static Function<List<Integration>, SamlProvidersResource> TO_PROVIDERS_RESOURCE = integrations -> {
// 		if (CollectionUtils.isEmpty(integrations)) {
// 			SamlProvidersResource emptyResource = new SamlProvidersResource();
// 			emptyResource.setProviders(Collections.emptyList());
// 			return emptyResource;
// 		}
// 		SamlProvidersResource resource = new SamlProvidersResource();
// 		resource.setProviders(integrations.stream().map(TO_RESOURCE).collect(Collectors.toList()));
// 		return resource;
// 	};
// 
// }


// Path: src/main/java/com/epam/reportportal/auth/event/SamlProvidersReloadEventHandler.java
import com.epam.reportportal.auth.integration.converter.SamlConverter;
import com.epam.ta.reportportal.dao.IntegrationRepository;
import com.epam.ta.reportportal.entity.integration.Integration;
import com.epam.ta.reportportal.entity.integration.IntegrationType;
import org.springframework.context.ApplicationListener;
import org.springframework.security.saml.provider.SamlServerConfiguration;
import org.springframework.security.saml.provider.service.config.LocalServiceProviderConfiguration;
import org.springframework.stereotype.Component;
import java.util.List;

/*
 * Copyright 2019 EPAM Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.epam.reportportal.auth.event;



/**
 * Handles SAML settings changes event and reload configuration of IDP in service provider configuration
 *
 * @author Yevgeniy Svalukhin
 */
@Component
public class SamlProvidersReloadEventHandler implements ApplicationListener<SamlProvidersReloadEvent> {

	private final IntegrationRepository integrationRepository;
	private final SamlServerConfiguration samlConfiguration;

	public SamlProvidersReloadEventHandler(IntegrationRepository integrationRepository, SamlServerConfiguration spConfiguration) {
		this.integrationRepository = integrationRepository;
		this.samlConfiguration = spConfiguration;
	}

	@Override
	public void onApplicationEvent(SamlProvidersReloadEvent event) {
		final IntegrationType integrationType = event.getIntegrationType();
		final List<Integration> integrations = integrationRepository.findAllGlobalByType(integrationType);

		LocalServiceProviderConfiguration serviceProvider = samlConfiguration.getServiceProvider();

		serviceProvider.getProviders().clear();
		serviceProvider.getProviders().addAll(SamlConverter.TO_EXTERNAL_PROVIDER_CONFIG.apply(integrations));