package net.ihe.xcpd.resp.action;

import ihe.iti.xcpd._2009.ValidateAssertion;

import java.io.Serializable;

import javax.ejb.Remove;
import javax.ejb.Stateful;

import net.ihe.gazelle.simulator.common.model.ApplicationConfiguration;
import net.ihe.xcpd.resp.tools.ElementString;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;
import org.w3c.dom.Element;


/**
 * @author abderrazek boufahja
 *
 */
@Stateful
@Name("assertionValidatorManager")
@Scope(ScopeType.SESSION)
public class AssertionValidatorManager implements AssertionValidatorManagerLocal,Serializable {

	
	/**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    @Logger
	private static Log log;
    
//~ Attribute ///////////////////////////////////////////////////////////////////////
    
    private String stsUrl;
    
    private String stsPort;
    
    private String stsServiceName;
    
    private String assertionBody;
    
    private boolean resultValidation = false;
    
    //~ getters and setters /////////////////////////////////////////////////////////////
    
    public void setResultValidation(boolean resultValidation) {
        this.resultValidation = resultValidation;
    }

    public boolean isResultValidation() {
        return resultValidation;
    }

    public void setAssertionBody(String assertionBody) {
        this.assertionBody = assertionBody;
    }

    public String getAssertionBody() {
        return assertionBody;
    }

    public void setStsServiceName(String stsServiceName) {
        this.stsServiceName = stsServiceName;
    }

    public String getStsServiceName() {
        if (stsServiceName == null){
            stsServiceName = ApplicationConfiguration.getValueOfVariable("sts_service_name");
        }
        return stsServiceName;
    }

    public void setStsPort(String stsPort) {
        this.stsPort = stsPort;
    }

    public String getStsPort() {
        if (stsPort == null){
            stsPort = ApplicationConfiguration.getValueOfVariable("sts_port");
        }
        return stsPort;
    }

    public void setStsUrl(String stsUrl) {
        this.stsUrl = stsUrl;
    }

    public String getStsUrl() {
        if (stsUrl == null){
            stsUrl = ApplicationConfiguration.getValueOfVariable("sts_url");
        }
        return stsUrl;
    }

    // methods /////////////////////////////////////////////////////////////////////////
    
    public void validateAssertion(){
        if (this.assertionBody==null || this.stsPort == null || this.stsServiceName==null || this.stsUrl== null){
            FacesMessages.instance().add("You have an empty field.");
            return;
        }
        if (this.assertionBody.equals("") || this.stsPort.equals("") || this.stsServiceName.equals("") || this.stsUrl.equals("")){
            FacesMessages.instance().add("You have an empty field.");
            return;
        }
        Element assertion = null;
        try {
            assertion = ElementString.string2Element0(this.assertionBody);
        } catch (Exception e) {
            FacesMessages.instance().add("The assertion is not a valid XML.");
            e.printStackTrace();
            return;
            
        }
        try {
            this.resultValidation = ValidateAssertion.validate(assertion, this.stsUrl, this.stsServiceName, this.stsPort);
        } catch (Exception e) {
            FacesMessages.instance().add("You have a problem on the declaration of a field. Review the service trust url.");
            e.printStackTrace();
            return;
        }
    }
    
    

	@Destroy
	@Remove
	public void destroy()
	{
		log.info("destroy");
	}


}
