
/*-------------------------------------------------------------------------*/
/**
 [Project ]: 프레임웍 기반 그룹웨어 시제품 개발
 
 [Date] : 1999. 9. 3
 [Source file]: com/etri/groupware/mail/MailController.java
 [Vesion] : 1.0
 */
/*--------------------------------------------------------------------------*/

package com.etri.groupware.mail;

import java.util.*;
import javax.mail.*;
import com.etri.groupware.*;


public class MailController implements MailControllerInterface {
	
	
    public static String receiveMailServerIP =null;
    public static String sendMailServerIP = null;

    private ReceiveMailServerManagerInterface mailServerManager = null;
    private MailSenderInterface mailSender = null;
    private Properties props = null;
	
    private static MailControllerInterface mc = null;
    
    /**
       @roseuid 37C1DB58012E
     */
    public static MailControllerInterface init( Properties props) throws Exception  {
    	
    	if (mc == null ) {
    	
    		mc = (MailControllerInterface) new MailController(props);    	
    		
    	}
    	
    	return mc;    
    }
    
/*
	public static MailControllerInterface init() throws Exception  {
    	
    	if (mc == null ) {
    		mc = (MailControllerInterface) new MailController();
    	}
    	
    	return mc;    
    }

   */ 
    /**
       @roseuid 37C1DF20019E
     */
    protected MailController(Properties props)  throws Exception {


    	    	
    	this.props = props;
    	
    	if ( props != null ) {
    		
    		 MailController.receiveMailServerIP = props.getProperty("ReceiveMailServerIP");    		
    		 MailController.sendMailServerIP = props.getProperty("SendMailServerIP");    		 
    	}
    	
    	if ( MailController.sendMailServerIP != null ){
    		mailSender = (MailSenderInterface) new MailSender("smtp", MailController.sendMailServerIP);    		
    	} 
    	else {
    		mailSender = (MailSenderInterface) new MailSender();    		

    	}
    	
    	// Protocol에 관하여 Setting 할 수 있도록 변형 필요.
    	// 날짜 설정( 60일 이전 메일 지움(default) )
    	//   	
    	
    }
	
	protected MailController()  throws Exception {
		
		if ( MailController.sendMailServerIP != null ){
    		mailSender = (MailSenderInterface) new MailSender("smtp", MailController.sendMailServerIP);    		
    	} 
    	else {
    		mailSender = (MailSenderInterface) new MailSender();    		
    	}

		
    	// Default 설정 필요.
		
		// Protocol에 관하여 Setting 할 수 있도록 변형 필요.
    	// 날짜 설정( 60일 이전 메일 지움(default) )
    	
    }
	
	public void connectMailServer(String id, String password) throws Exception{
				
		mailServerManager = createReceiveMailServerManager();  		
		mailServerManager.connect(MailController.receiveMailServerIP, id, password);
	}

		
	public void connectMailServer(String host, String id, String password) throws Exception{
				
		mailServerManager = createReceiveMailServerManager();  		
		mailServerManager.connect(host, id, password);
	}
	
	public void disconnectMailServer() throws Exception{
		
		mailServerManager.disconnect();
	}
 
    
    /**
       @roseuid 37C1DB9E0310
     */
    public ResultStatus sendMail( MailInfo mi) throws Exception {
    	return mailSender.sendMail(mi);
    }
    
    
    public ResultStatus sendMail( MailInfo mi, String id, String password ) throws Exception {
    	return mailSender.sendMail(mi, id, password);
    }
    /**
       @roseuid 37C1F0650083
     */
    public Message[] receiveMail() throws Exception {
    	return mailServerManager.receiveMail();
    }
    
    /**
       @roseuid 37B464D202E8
     */
    public Message[] receiveMail( String srcFolder) throws Exception {
    	return mailServerManager.receiveMail(srcFolder);
    }
    
    /**
       @roseuid 37C1E0F0039A
     */
    public Message[] receiveMail( String srcFolder,  int fromIndex,  int endIndex) throws Exception {
    	return mailServerManager.receiveMail( srcFolder, fromIndex, endIndex);
    }
    
    /**
       @roseuid 37B464D202EF
     */
    public ResultStatus moveMail( String srcFolder,  String dstFolder,  int mailNum) throws Exception {
    	return mailServerManager.moveMail(srcFolder, dstFolder, mailNum);
    }
    
    /**
       @roseuid 37CEDDB4034F
     */
    public ResultStatus moveMail( String srcFolder,  String dstFolder,  int fromIndex,  int endIndex) throws Exception {
    	return mailServerManager.moveMail(srcFolder, dstFolder, fromIndex, endIndex);
    }
    
    /**
       @roseuid 37B464D202F5
     */
    public ResultStatus addMail( String srcFolder,  Message msg) {
    	return null;
    }
    
    /**
       @roseuid 37B464D202FD
     */
    public ResultStatus removeMail( String srcFolder,  int num) throws Exception {
    	return mailServerManager.removeMail(srcFolder, num);
    }
    
    /**
       @roseuid 37CEE2AF0212
     */
    public ResultStatus removeMail( String srcFolder,  int fromIndex,  int endIndex) throws Exception  {
    	return mailServerManager.removeMail(srcFolder, fromIndex, endIndex);
    }
    
    /**
       @roseuid 37B464D20309
     */
    public ResultStatus copyMail( String srcFolder,  String dstFolder,  int mailNum) throws Exception {
    	return mailServerManager.copyMail(srcFolder, dstFolder, mailNum);
    }
    
    /**
       @roseuid 37CEDD59018C
     */
    public ResultStatus copyMail( String srcFolder,  String dstFolder,  int fromIndex,  int endIndex) throws Exception {
    	return mailServerManager.copyMail(srcFolder, dstFolder,  fromIndex, endIndex);

    }
    
    /**
       @roseuid 37B464D2030B
     */
    public Message[] searchMail( String srcFolder,  SearchData sd) throws Exception  {
    	
    	return mailServerManager.searchMail(srcFolder, sd);

    }
    
    /**
       @roseuid 37CEDDF6023C
     */
    public  ReceiveMailServerManagerInterface createReceiveMailServerManager(String host, String id, String password) throws Exception  {
    	
    	if (mailServerManager  == null ) {
    		mailServerManager  = new ReceiveMailServerManager(host,  id, password);
    	}
    	
    	return mailServerManager; 
    }
	
	protected  ReceiveMailServerManagerInterface createReceiveMailServerManager() throws Exception  {
		if (mailServerManager  == null) {
			mailServerManager  = new ReceiveMailServerManager();
		}
		
    	return mailServerManager;
    }

	
	public ReceiveMailServerManagerInterface getReceiveMailSender(){
		return mailServerManager;
	}
    
    /**
       @roseuid 37CEDE060393
     */
    public MailSenderInterface createMailSender(String protocol, String host) {
    	
    	if (mailSender == null) {
    		 mailSender = (MailSenderInterface) new MailSender(protocol, host);
    	}
    	
    	 return mailSender;
    	
    }
	
	public MailSenderInterface getMailSender() {
    	
    	 return mailSender; 
    	
    }
}