package com.etri.groupware;


import com.etri.groupware.organization.*;
import com.etri.groupware.authentication.* ;
import com.etri.groupware.bbs.*;
import com.etri.groupware.mail.*;
import com.etri.groupware.eda.*;

import java.util.*;


public class GroupwareController implements GroupwareControllerInterface{

     private static String receiveMailServerIP = null;
     private static String sendMailServerIP = null;	

     private static GroupwareControllerInterface gc = null;
     private static GroupwareDBManagerFactoryInterface gdmf = null;
     
     private /*static*/ OrganizationManagementControllerInterface omc = null;
     private /*static*/ AuthenticationControllerInterface ac = null;
     private /*static*/ BBSControllerInterface bc = null;
     private /*static*/ EDAControllerInterface ec = null;
     private MailControllerInterface mc = null;
     
     

     


     private GroupwareController() { }
            
     public static GroupwareControllerInterface init(Properties props) throws Exception{

     	if ( gc == null ) {
     		
     		if ( props == null ) {
     			throw new Exception("Property Setting needed !!!");
     		}
     	
           
           // OrganizationDBManagerInterface 를 구현한 클래스 이름 세팅
           String OrganizationDBManagerClassName = props.getProperty("OrganizationDBManagerClassName");
           if(OrganizationDBManagerClassName != null){
           	GroupwareOptions.OrganizationDBManagerClassName = OrganizationDBManagerClassName;
           }       
           
           // AuthenticationDBManagerInterface 를 구현한 클래스 이름 세팅
           String AuthenticationDBManagerClassName = props.getProperty("AuthenticationDBManagerClassName");
           if(AuthenticationDBManagerClassName != null){
           	GroupwareOptions.AuthenticationDBManagerClassName = AuthenticationDBManagerClassName;
           }
           
           // BBSDBManagerInterface 를 구현한 클래스 이름 세팅
           String BBSDBManagerClassName = props.getProperty("BBSDBManagerClassName");
           if(BBSDBManagerClassName != null){
           	 GroupwareOptions.BBSDBManagerClassName = BBSDBManagerClassName;
           }
           
           // EDADBManagerInterface 를 구현한 클래스 이름 세팅
           String EDADBManagerClassName = props.getProperty("EDADBManagerClassName");
           if(EDADBManagerClassName != null){
           	GroupwareOptions.EDADBManagerClassName = EDADBManagerClassName;  
           }
           
           // Mail Server 세팅
           receiveMailServerIP = props.getProperty("ReceiveMailServerIP");
           if(receiveMailServerIP != null){
           	GroupwareOptions.ReceiveMailServerIP = receiveMailServerIP;  
           }
           
           sendMailServerIP = props.getProperty("SendMailServerIP");
           if(sendMailServerIP != null){
           	GroupwareOptions.SendMailServerIP = sendMailServerIP;  
           }
           
               	
           gdmf = getGroupwareDBManagerFactory();
               	
           gc =  new GroupwareController();
     
     	}
     	
        return gc;
     }
     
       
     
     
     public OrganizationManagementControllerInterface createOrganizationManagementController() throws Exception{
 	
     	if(omc == null){
         	
         	OrganizationDBManagerInterface odmi = gdmf.createOrganizationDBManager();
         	
         	
         	omc = OrganizationManagementController.init(odmi);
        }
        
        return omc;
     }

     public AuthenticationControllerInterface createAuthenticationController() throws Exception {
     	 	
     	if(ac == null){
         	
         	AuthenticationDBManagerInterface admi = gdmf.createAuthenticationDBManager();
         	
         	
         	ac = AuthenticationController.init(admi);
        }
        
        return ac;
     }
               
     public  BBSControllerInterface  createBBSController() throws Exception{
        
        if(bc == null){
        	BBSDBManagerInterface bdmi = gdmf.createBBSDBManager();
        
                bc = BBSController.init(bdmi);
        }           
         
        return bc;
    }
    
    
    public EDAControllerInterface createEDAController() throws Exception{
        
        if(ec == null){
        	EDADBManagerInterface edmi = gdmf.createEDADBManager();
        	
        	ec = EDAController.init(edmi);
        }
        
        return ec;
    }
     
 
    public MailControllerInterface createMailController() throws Exception{
    	Properties props = new Properties();   
    	  			
	props.put("ReceiveMailServerIP",  GroupwareOptions.ReceiveMailServerIP);
	props.put("SendMailServerIP", GroupwareOptions.SendMailServerIP );
				
	mc = MailController.init(props);	
        	       	
                
        return mc;
    }
    
    
    private static GroupwareDBManagerFactoryInterface getGroupwareDBManagerFactory() throws Exception{
        
        return new GroupwareDBManagerFactory();
    }       	
}        