package org.gluu.oxtrust.ldap.service;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.gluu.oxtrust.model.GluuSAMLTrustRelationship;
import org.gluu.persist.exception.mapping.EntryPersistenceException;
import org.slf4j.Logger;
import org.xdi.config.oxtrust.AppConfiguration;

/**
 * Perform Shibboleth startup time initialization.
 * 
 * @author Yuriy Movchan
 * @version 0.1, 05/04/2017
 */
@ApplicationScoped
@Named
public class ShibbolethInitializer {
	
	@Inject
	private Logger log;

	@Inject
	private AppConfiguration appConfiguration;
	
	@Inject
	private ApplianceService applianceService;
	
	@Inject
	private TrustService trustService;
	
	@Inject
	private Shibboleth3ConfService shibboleth3ConfService;
	
	public boolean createShibbolethConfiguration() {
		boolean createConfig = appConfiguration.isConfigGeneration();
		log.info("IDP config generation is set to " + createConfig);
		
		if (createConfig) {

			String gluuSPInum;
			GluuSAMLTrustRelationship gluuSP;

			try {
				gluuSPInum = applianceService.getAppliance().getGluuSPTR();

				// log.info("########## gluuSPInum = " + gluuSPInum);

				gluuSP = new GluuSAMLTrustRelationship();
				gluuSP.setDn(trustService.getDnForTrustRelationShip(gluuSPInum));

			} catch (EntryPersistenceException ex) {
				log.error("Failed to determine SP inum", ex);
				return false;
			}

			// log.info("########## gluuSP.getDn() = " + gluuSP.getDn());

			boolean servicesNeedRestarting = false;
			if (gluuSPInum == null || ! trustService.containsTrustRelationship(gluuSP)) {

				log.info("No trust relationships exist in LDAP. Adding gluuSP");
//				GluuAppliance appliance = applianceService.getAppliance();
//				appliance.setGluuSPTR(null);
//				applianceService.updateAppliance(appliance);
				shibboleth3ConfService.addGluuSP();
				servicesNeedRestarting = true;
			}

			gluuSP = trustService.getRelationshipByInum(applianceService.getAppliance().getGluuSPTR());

			List<GluuSAMLTrustRelationship> trustRelationships = trustService.getAllActiveTrustRelationships();

			/*
			if (trustRelationships != null && !trustRelationships.isEmpty()) {
				for (GluuSAMLTrustRelationship gluuSAMLTrustRelationship : trustRelationships) {
					log.info("########## gluuSAMLTrustRelationship.getDn() = " + gluuSAMLTrustRelationship.getDn());
				}
			}
			*/

			String shibbolethVersion = appConfiguration.getShibbolethVersion();
			log.info("########## shibbolethVersion = " + shibbolethVersion);

			shibboleth3ConfService.generateMetadataFiles(gluuSP);
			shibboleth3ConfService.generateConfigurationFiles(trustRelationships);

			shibboleth3ConfService.removeUnusedCredentials();
			shibboleth3ConfService.removeUnusedMetadata();

			if (servicesNeedRestarting) {
				applianceService.restartServices();
			}
		}

		return true;
	}

}

@@ -1540,16 +1540,18 @@
  false;%0A
+//
 %09%09%09if (g
@@ -1623,17 +1623,21 @@
 uSP)) %7B%0A
-%0A
+//%0A//
 %09%09%09%09log.
@@ -1848,16 +1848,18 @@
 iance);%0A
+//
 %09%09%09%09shib
@@ -1886,24 +1886,26 @@
 ddGluuSP();%0A
+//
 %09%09%09%09services
@@ -1927,16 +1927,18 @@
 = true;%0A
+//
 %09%09%09%7D%0A%0A%09%09

Remove default Gluu SP SAML Trust Relationship setup #404