
package org.gluu.oxtrust.action;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.Instance;
import javax.faces.application.FacesMessage;
import javax.inject.Inject;
import javax.inject.Named;

import org.gluu.jsf2.message.FacesMessages;
import org.gluu.jsf2.service.ConversationService;
import org.gluu.oxtrust.config.ConfigurationFactory;
import org.gluu.oxtrust.ldap.service.AppInitializer;
import org.gluu.oxtrust.ldap.service.AttributeService;
import org.gluu.oxtrust.ldap.service.CASService;
import org.gluu.oxtrust.ldap.service.Shibboleth3ConfService;
import org.gluu.oxtrust.ldap.service.TrustService;
import org.gluu.oxtrust.model.GluuSAMLTrustRelationship;
import org.gluu.oxtrust.util.OxTrustConstants;
import org.gluu.site.ldap.persistence.LdapEntryManager;
import org.gluu.site.ldap.persistence.exception.LdapMappingException;
import org.slf4j.Logger;
import org.xdi.config.oxtrust.AppConfiguration;
import org.xdi.config.oxtrust.AttributeResolverConfiguration;
import org.xdi.config.oxtrust.CacheRefreshConfiguration;
import org.xdi.config.oxtrust.LdapOxTrustConfiguration;
import org.xdi.model.GluuAttribute;
import org.xdi.service.security.Secure;
import org.xdi.util.StringHelper;

@ConversationScoped
@Named("attributeResolverAction")
@Secure("#{permissionService.hasPermission('trust', 'access')}")
public class AttributeResolverAction implements Serializable {

	private static final long serialVersionUID = -9125609238796284572L;
	
	@Inject
	private Logger log;

	@Inject
	private AttributeService attributeService;

	@Inject
	private FacesMessages facesMessages;
	
	@Inject
	private ConversationService conversationService;
	
	@Inject
	private AppConfiguration applicationConfiguration;

    @Inject
    private LdapEntryManager ldapEntryManager;
	
	@Inject
	private TrustService trustService;
	
	@Inject
	private Shibboleth3ConfService shibboleth3ConfService;

	@Inject
	private ConfigurationFactory configurationFactory;
	
	private List<GluuAttribute> attributes;
	
	private String attributeName;
	private String nameIdType;
	private boolean enable;
	private String base;

	private boolean initialized;
	
	public boolean isEnable() {
		return enable;
	}
	public void setEnable(boolean enable) {
		this.enable = enable;
	}
	
	public String getBase() {
		return base;
	}

	public void setBase(String base) {
		this.base = base;
	}
	
	public List<GluuAttribute> getAttributes() {
		return attributes;
	}
	public void setAttributes(List<GluuAttribute> attributes) {
		this.attributes = attributes;
	}
	public String getAttributeName() {
		return attributeName;
	}
	public void setAttributeName(String attributeName) {
		this.attributeName = attributeName;
	}
	
	public String getNameIdType() {
		return nameIdType;
	}
	public void setNameIdType(String nameIdType) {
		this.nameIdType = nameIdType;
	}
	public String init() {
		if (initialized) {
			return OxTrustConstants.RESULT_SUCCESS;
		}

		this.attributes = attributeService.getAllAttributes();

		final LdapOxTrustConfiguration conf = configurationFactory.loadConfigurationFromLdap("oxTrustConfAttributeResolver");
		AttributeResolverConfiguration attributeResolverConfiguration = conf.getAttributeResolverConfig();
		if (attributeResolverConfiguration != null) {
			this.base = attributeResolverConfiguration.getBase();
			this.nameIdType = attributeResolverConfiguration.getNameIdType();
			this.enable = attributeResolverConfiguration.isEnabled();
			
			String attributeName = attributeResolverConfiguration.getAttributeName();
			GluuAttribute foundAttribute = attributeService.getAttributeByName(attributeName, this.attributes);
			if (foundAttribute != null) {
				this.attributeName = foundAttribute.getName();
			}
		}

		this.initialized = true;

		return OxTrustConstants.RESULT_SUCCESS;
	}

	public String saveCustomAttributetoResolveImpl(){
		String outcome = saveCustomAttributetoResolve();
		
		if (OxTrustConstants.RESULT_SUCCESS.equals(outcome)) {
			facesMessages.add(FacesMessage.SEVERITY_INFO, "NameId configuration updated successfully");
		} else if (OxTrustConstants.RESULT_FAILURE.equals(outcome)) {
			facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to update NameId configuration");
		}
		
		return outcome;
	}

	public String saveCustomAttributetoResolve(){
		AttributeResolverConfiguration attributeResolverConfiguration = new AttributeResolverConfiguration();
		attributeResolverConfiguration.setBase(base);
		attributeResolverConfiguration.setAttributeName(attributeName);
		attributeResolverConfiguration.setNameIdType(nameIdType);
		attributeResolverConfiguration.setEnabled(enable);
		try {
			final LdapOxTrustConfiguration conf = configurationFactory.loadConfigurationFromLdap();
			conf.setAttributeResolverConfig(attributeResolverConfiguration);
			conf.setRevision(conf.getRevision() + 1);
			ldapEntryManager.merge(conf);
		} catch (Exception ex) {
			log.error("Failed to save Attribute Resolver configuration configuration", ex);
			return OxTrustConstants.RESULT_FAILURE;
		}

		if (!enable) {
			return OxTrustConstants.RESULT_FAILURE;
		}

		
		boolean updateShib3Configuration = applicationConfiguration.isConfigGeneration(); 
		if (updateShib3Configuration) {    
			List<GluuSAMLTrustRelationship> trustRelationships = trustService.getAllActiveTrustRelationships();    
			if (!shibboleth3ConfService.generateConfigurationFiles(trustRelationships)) {
				log.error("Failed to update Shibboleth v3 configuration");
				facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to update Shibboleth v3 configuration");			}
		}

		facesMessages.add(FacesMessage.SEVERITY_INFO, "Saml NameId configuration updated successfully.");
		return OxTrustConstants.RESULT_SUCCESS;
	}
	
	public String cancel(){
		facesMessages.add(FacesMessage.SEVERITY_INFO, "Saml NameId configuration not updated");
		conversationService.endConversation();

		return OxTrustConstants.RESULT_SUCCESS;
	}
	
}

@@ -137,49 +137,8 @@
 ed;%0A
-import javax.enterprise.inject.Instance;%0A
 impo
@@ -178,16 +178,16 @@
 essage;%0A
+
 import j
@@ -385,61 +385,8 @@
 ry;%0A
-import org.gluu.oxtrust.ldap.service.AppInitializer;%0A
 impo
@@ -440,57 +440,8 @@
 ce;%0A
-import org.gluu.oxtrust.ldap.service.CASService;%0A
 impo
@@ -712,78 +712,8 @@
 er;%0A
-import org.gluu.site.ldap.persistence.exception.LdapMappingException;%0A
 impo
@@ -847,65 +847,8 @@
 on;%0A
-import org.xdi.config.oxtrust.CacheRefreshConfiguration;%0A
 impo
@@ -935,16 +935,16 @@
 ribute;%0A
+
 import o
@@ -978,42 +978,8 @@
 ure;
-%0Aimport org.xdi.util.StringHelper;
 %0A%0A@C

Optimize imports