/*
 * Copyright 2009 IHE International (http://www.ihe.net)
 *
 * 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 net.ihe.xcpd.resp.action;

import java.io.Serializable;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;

import net.ihe.gazelle.simulator.common.model.ApplicationConfiguration;

import org.jboss.seam.Component;
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.annotations.security.Restrict;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

/**
 * @author Abderrazek Boufahja > INRIA Rennes IHE development Project
 *
 */
@Stateful
@Name("configurationManager")
@Scope(ScopeType.SESSION)
public class ConfigurationManager implements ConfigurationManagerLocal, Serializable{

   
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
  //~ Statics variables and Class initializer ////////////////////////////////////////

    /** Logger */
    @Logger
    private static Log log;

    //~ Attribute ///////////////////////////////////////////////////////////////////////
    
    private String gazelleBin;
    
    private String xcpdRequestSchema;
    
    private String xcpdResponseSchema;
    
    private String pdsServerName;
    
    private String pdsServerPort;
    
    private String process;
    
    private String stsUrl;
    
    private String stsPort;
    
    private String stsServiceName;
    
    private String isxua;
    
    private String homeCommunityID;
    
    private String systemRootID;
    
    private String schematronValidator;
    
    //~ getters and setters /////////////////////////////////////////////////////////////
     
    public void setSchematronValidator(String schematronValidator) {
        this.schematronValidator = schematronValidator;
    }

    public String getSchematronValidator() {
        return schematronValidator;
    }

    public void setSystemRootID(String systemRootID) {
        this.systemRootID = systemRootID;
    }

    public String getSystemRootID() {
        return systemRootID;
    }

    public void setHomeCommunityID(String homeCommunityID) {
        this.homeCommunityID = homeCommunityID;
    }

    public String getHomeCommunityID() {
        return homeCommunityID;
    }

    public void setIsxua(String isxua) {
        this.isxua = isxua;
    }

    public String getIsxua() {
        if (isxua == null){
            isxua = ApplicationConfiguration.getValueOfVariable("is_xua");
        }
        return isxua;
    }
    
    public void setStsServiceName(String stsServiceName) {
        this.stsServiceName = stsServiceName;
    }

    public String getStsServiceName() {
        return stsServiceName;
    }

    public void setStsPort(String stsPort) {
        this.stsPort = stsPort;
    }

    public String getStsPort() {
        return stsPort;
    }

    public void setStsUrl(String stsUrl) {
        this.stsUrl = stsUrl;
    }

    public String getStsUrl() {
        return stsUrl;
    }

    public void setProcess(String process) {
        this.process = process;
    }

    public String getProcess() {
        return process;
    }

    public void setPdsServerPort(String pdsServerPort) {
        this.pdsServerPort = pdsServerPort;
    }

    public String getPdsServerPort() {
        return pdsServerPort;
    }

    public void setPdsServerName(String pdsServerName) {
        this.pdsServerName = pdsServerName;
    }

    public String getPdsServerName() {
        return pdsServerName;
    }

    public void setXcpdResponseSchema(String xcpdResponseSchema) {
        this.xcpdResponseSchema = xcpdResponseSchema;
    }

    public String getXcpdResponseSchema() {
        if (xcpdResponseSchema == null){
            xcpdResponseSchema = ApplicationConfiguration.getValueOfVariable("xcpd_response_schema");
        }
        return xcpdResponseSchema;
    }

    public void setXcpdRequestSchema(String xcpdRequestSchema) {
        this.xcpdRequestSchema = xcpdRequestSchema;
    }

    public String getXcpdRequestSchema() {
        if (xcpdRequestSchema == null){
            xcpdRequestSchema = ApplicationConfiguration.getValueOfVariable("xcpd_request_schema");
        }
        return xcpdRequestSchema;
    }

    public void setGazelleBin(String gazelleBin) {
        this.gazelleBin = gazelleBin;
    }

    public String getGazelleBin() {
        if (gazelleBin == null){
            gazelleBin = ApplicationConfiguration.getValueOfVariable("gazelle_bin");
        }
        return gazelleBin;
    }

    public static void setLog(Log log) {
        ConfigurationManager.log = log;
    }

    public static Log getLog() {
        return log;
    }

    // methods /////////////////////////////////////////////////////////////////////////

    @Restrict("#{s:hasPermission('ConfigurationManager', 'initializeVariables', null)}")
    public void initializeVariables(){
        xcpdResponseSchema = ApplicationConfiguration.getValueOfVariable("xcpd_response_schema");
        xcpdRequestSchema = ApplicationConfiguration.getValueOfVariable("xcpd_request_schema");
        gazelleBin = ApplicationConfiguration.getValueOfVariable("gazelle_bin");
        pdsServerName = ApplicationConfiguration.getValueOfVariable("pds_server_name");
        pdsServerPort = ApplicationConfiguration.getValueOfVariable("pds_server_port");
        process = ApplicationConfiguration.getValueOfVariable("process");
        stsUrl = ApplicationConfiguration.getValueOfVariable("sts_url");
        stsServiceName = ApplicationConfiguration.getValueOfVariable("sts_service_name");
        stsPort = ApplicationConfiguration.getValueOfVariable("sts_port");
        isxua = ApplicationConfiguration.getValueOfVariable("is_xua");
        homeCommunityID = ApplicationConfiguration.getValueOfVariable("homeCommunityID");
        systemRootID = ApplicationConfiguration.getValueOfVariable("system_id_root");
        schematronValidator = ApplicationConfiguration.getValueOfVariable("schematron_validator");
    }
    
    @Restrict("#{s:hasPermission('ConfigurationManager', 'saveVariables', null)}")
    public void saveVariables(){
        EntityManager entityManager=(EntityManager)Component.getInstance("entityManager");
        ApplicationConfiguration ac;
        if (gazelleBin != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("gazelle_bin");
            if (ac != null){
                ac.setValue(gazelleBin);
                ac = entityManager.merge(ac);
            }
        }
        if (xcpdResponseSchema != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("xcpd_response_schema");
            if (ac != null){
                ac.setValue(xcpdResponseSchema);
                ac = entityManager.merge(ac);
            }
        }
        if (xcpdRequestSchema != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("xcpd_request_schema");
            if (ac != null){
                ac.setValue(xcpdRequestSchema);
                ac = entityManager.merge(ac);
            }
        }
        if (pdsServerName != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("pds_server_name");
            if (ac != null){
                ac.setValue(pdsServerName);
                ac = entityManager.merge(ac);
            }
        }
        if (pdsServerPort != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("pds_server_port");
            if (ac != null){
                ac.setValue(pdsServerPort);
                ac = entityManager.merge(ac);
            }
        }
        if (process != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("process");
            if (ac != null){
                ac.setValue(process);
                ac = entityManager.merge(ac);
            }
        }
        if (stsUrl != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("sts_url");
            if (ac != null){
                ac.setValue(stsUrl);
                ac = entityManager.merge(ac);
            }
        }
        if (stsServiceName != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("sts_service_name");
            if (ac != null){
                ac.setValue(stsServiceName);
                ac = entityManager.merge(ac);
            }
        }
        if (stsPort != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("sts_port");
            if (ac != null){
                ac.setValue(stsPort);
                ac = entityManager.merge(ac);
            }
        }
        if (isxua != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("is_xua");
            if (ac != null){
                ac.setValue(isxua);
                ac = entityManager.merge(ac);
            }
        }
        
        if (homeCommunityID != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("homeCommunityID");
            if (ac != null){
                ac.setValue(homeCommunityID);
                ac = entityManager.merge(ac);
            }
        }
        
        if (systemRootID != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("system_id_root");
            if (ac != null){
                ac.setValue(systemRootID);
                ac = entityManager.merge(ac);
            }
        }
        
        if (schematronValidator != null){
            ac = ApplicationConfiguration.getApplicationConfigurationByVariable("schematron_validator");
            if (ac != null){
                ac.setValue(schematronValidator);
                ac = entityManager.merge(ac);
            }
        }
        
        entityManager.flush();
        FacesMessages.instance().add("Modifications was saved.");
    }

    // destroy method ///////////////////////////////////////////////////////////////////
    
    @Destroy
    @Remove
    public void destroy()
    {
        log.info("destroy");
    }
}
