/* 
    ETRI IntraPMS Project
    전자결재  문서 Class

    
*/


package com.etri.groupware.eda;

import java.util.*;


public class ApprovalDocument {
	
	//문서의 내용 관련 Attribute	
	private String docID;			//문서번호
	private String initDay;			//작성일
	private String initPersonID;		//작성자
	private String conditionCode;		//결재 상황
	private String title;			//문서제목
	private String comment;			//문서 내용
	private String listID;			
		
	//문서함에서 보여주는 Attrubute	
	private String endDay = "00/00/00";	//결재 완료일
	private String returnPerson;		//반려자
	private String currentPerson;		//현 결재자
	private int currentPosition;		//현 결재 위치(결재자의 Index)
	
	//첨부 파일과 결재 경로 저장을 위한 	Attribute
	private List approvalCourse = new ArrayList();	//결재 경로 저장 List
	private List attatchFile = new ArrayList();	//첨부파일을 위한 List
	
	public ApprovalDocument(){
		super();
	}
	
	public ApprovalDocument( String docID , String initDay , String initPersonID ,
	 String conditionCode , String title , String comment){
	 	this.docID = docID;
	 	this.initDay = initDay;
	 	this.initPersonID = initPersonID;
	 	this.conditionCode = conditionCode;
	 	this.title = title;
	 	this.comment = comment;
	}

	public ApprovalDocument( String docID , String initDay , String initPersonID ,
	 String conditionCode , String title , String comment, String listID){
	 	this.docID = docID;
	 	this.initDay = initDay;
	 	this.initPersonID = initPersonID;
	 	this.conditionCode = conditionCode;
	 	this.title = title;
	 	this.comment = comment;
	 	this.listID = listID;
	}

	
	public ApprovalDocument( String docID , String initDay , String initPersonID ,
	 String conditionCode , String title , String comment , ApprovalCourse[] approvalCourse ){
	 	
	 	this.docID = docID;
	 	this.initDay = initDay;
	 	this.initPersonID = initPersonID;
	 	this.conditionCode = conditionCode;
	 	this.title = title;
	 	this.comment = comment;
	 	for(int i = 0 ; i < approvalCourse.length ; i++)
	 		this.approvalCourse.add( approvalCourse[i]);
	 }

	public ApprovalDocument( String docID , String initDay , String initPersonID ,
	 String conditionCode , String title , String comment , ApprovalCourse[] course , AttatchFile[] file){
	 	
	 	this.docID = docID;
	 	this.initDay = initDay;
	 	this.initPersonID = initPersonID;
	 	this.conditionCode = conditionCode;
	 	this.title = title;
	 	this.comment = comment;
	 	for(int i = 0 ; i < course.length ; i++)
	 		this.approvalCourse.add( course[i]);
	 		
	 	for(int i = 0 ; i < file.length ; i++)
	 		this.attatchFile.add( file[i]);
	 }

/*----------------------------------------------------------------- */
/**
 [메소드이름]: addapprovalCourse<br>
 [기능]: 문서의 결재 경로 List에 결재 경로를 추가한다.<br>
 [파라미터]: ApprovalCourse approvalCourse<br>
 [호출자] : this<br>
 */
/*--------------------------------------------------------------- */
	 public void addapprovalCourse( ApprovalCourse approvalCourse ){
	 	this.approvalCourse.add( approvalCourse );
	 }

/*----------------------------------------------------------------- */
/**
 [메소드이름]: addattatchFile<br>
 [기능]: 문서의 첨부 파일 List에 첨부 파일을 추가한다.<br>
 [파라미터]: AttatchFile attatchFile<br>
 [호출자] : this<br>
 */
/*--------------------------------------------------------------- */
	 public void addattatchFile( AttatchFile attatchFile ){
	 	this.attatchFile.add( attatchFile );
	 }
	
/*----------------------------------------------------------------- */
/**
 [메소드이름]: indexOfapprovalCourse<br>
 [기능]: 지정 결재자의 결재 경로상에서 위치를 찾는다.<br>
 [파라미터]: String person<br>
 [호출자] : this<br>
 */
/*--------------------------------------------------------------- */	
	 public int indexOfapprovalCourse( String person ){
	 	ApprovalCourse[] course = getapprovalCourse();
	 	int i;
	 	for(i = 1 ; i < approvalCourse.size() ; i++)
	 		if( (((ApprovalCourse)approvalCourse.get(i)).getpersonID()).equals(person) &&
	 		    (((ApprovalCourse)approvalCourse.get(i)).getconditionCode()).equals(ApprovalCourse.NOTCOMPLETE))
	 			break;
	 	if( i == approvalCourse.size() )
	 		return -1 ;
	 	else 
	 		return i;
	 }
	 
/*----------------------------------------------------------------- */
/**
 [메소드이름]: removeapprovalCourse<br>
 [기능]: 문서에 저장되어 있는 결재 경로 중 지정 위치의 결재 경로를 삭제한다.<br>
 [파라미터]: int i<br>
 [호출자] : this<br>
 */
/*--------------------------------------------------------------- */
	 public void removeapprovalCourse( int i ){
	 	this.approvalCourse.remove( i );
	 }

/*----------------------------------------------------------------- */
/**
 [메소드이름]: removeattatchFile<br>
 [기능]: 문서에 저장되어 있는 첨부파일 중 지정 위치의 첨부파일을 삭제한다.<br>
 [파라미터]: int i<br>
 [호출자] : this<br>
 */
/*--------------------------------------------------------------- */
	 public void removeattatchFile( int i ){
	 	this.attatchFile.remove( i );
	 }

/*----------------------------------------------------------------- */
/**
 [메소드이름]: sizeOfApprovalCourse<br>
 [기능]: 문서에 저장되어 있는 결재 경로의 길이를 리턴한다.<br>
 [파라미터]: 없음<br>
 [호출자] : this<br>
 */
/*--------------------------------------------------------------- */
	 public int sizeOfApprovalCourse(){
	 	return this.approvalCourse.size();
	 }

/*----------------------------------------------------------------- */
/**
 [메소드이름]: sizeOfAttatchFile<br>
 [기능]: 문서에 저장되어 있는 첨부 파일의 개수를 리턴한다.<br>
 [파라미터]: 없음<br>
 [호출자] : this<br>
 */
/*--------------------------------------------------------------- */
	 public int sizeOfAttatchFile(){
	 	return this.attatchFile.size();
	 }
	 
	 //Attribute get method
	 public String getdocID(){
	 	return docID;
	 }
	 
	 public String getinitDay(){
	 	return initDay;
	 }
	 
	 public String getinitPersonID(){
	 	return initPersonID;
	 }
	 
	 public String getconditionCode(){
	 	return conditionCode;
	 }
	 
	 public String gettitle(){
	 	return title;
	 }
	 
	 public String getcomment(){
	 	return comment;
	 }

	 public String getlistID(){
	 	return listID;
	 }
	 
	 public ApprovalCourse getapprovalCourse( int i ){
	 	return ((ApprovalCourse)this.approvalCourse.get(i));
	 }
	 
	 public ApprovalCourse[] getapprovalCourse(){
	  	ApprovalCourse[] result = new ApprovalCourse[ approvalCourse.size()];
	 	for( int i = 0 ; i<result.length ; i++ )
	 		result[i] = (ApprovalCourse)approvalCourse.get(i);
	 	return result;
	 }

	 public AttatchFile getattatchFile( int i ){
	 	return ((AttatchFile)this.attatchFile.get(i));
	 }
	 
	 public AttatchFile[] getattatchFile(){
	 	AttatchFile[] result = new AttatchFile[ attatchFile.size()];
	 	for( int i = 0 ; i<result.length ; i++ )
	 		result[i] = (AttatchFile)attatchFile.get(i);
	 	return result;
	 }
	 
	 public String getendDay(){
	 	return this.endDay;
	 }
	 
	 public String getreturnPerson(){
	 	return this.returnPerson;
	 }
	 
	 public String getcurrentPerson(){
	 	return this.currentPerson;
	 }	
	 
	 public int getcurrentPosition(){
	 	return this.currentPosition;
	 }

	 //Attribute set mehod
	 public void setdocID( String docID ){
	 	this.docID = docID;
	 }
	 
	 public void setinitDay( String initDay ){
	 	this.initDay = initDay;
	 }
	 
	 public void setinitPersonID( String initPersonID ){
	 	this.initPersonID = initPersonID;
	 }
	 
	 public void setconditionCode( String conditionCode ){
	 	this.conditionCode = conditionCode;
	 }
	 
	 public void settitle( String title ){
	 	this.title = title;
	 }
	 
	 public void setcomment( String comment ){
	 	this.comment = comment;
	 }

	 public void setlistID( String listID ){
	 	this.listID = listID;
	 }
	 
	 public void setapprovalCourse( int i , ApprovalCourse approvalCourse ){
	 	this.approvalCourse.set( i , approvalCourse );
	 }
	 
	 public void setapprovalCourse( ApprovalCourse[] approvalCourse){
	 	this.approvalCourse.clear();
	 	for( int i=0 ; i < approvalCourse.length ; i++ )
	 		this.approvalCourse.add( approvalCourse[i] );
	 }

	 public void setattatchFile( int i , AttatchFile attatchFile ){
	 	this.attatchFile.set( i , attatchFile );
	 }
	 
	 public void setattatchFile( AttatchFile[] attatchFile ){
	 	this.attatchFile.clear();
	 	for( int i = 0 ; i < attatchFile.length ; i++ )
	 		this.attatchFile.add ( attatchFile[i] );
	 }

	 public void setendDay(){
	 	int endPosition;
	 	ApprovalCourse course= new ApprovalCourse();
	 	for( endPosition = 1 ; endPosition < approvalCourse.size() ; endPosition ++ ){
	 		course = ( ApprovalCourse ) approvalCourse.get( endPosition );
	 		if( course.gettype() != ApprovalCourse.SUBMITAPPROVAL )
	 			break;
	 	}
		this.endDay = ((ApprovalCourse)approvalCourse.get( endPosition - 1)).getworkDate();
	 }
	 
	 public void setendDay( String endDay ){
	 	this.endDay = endDay;
	 }

	 public void setreturnPerson(){
		ApprovalCourse course = new ApprovalCourse();
		int rejectPosition;
		for( rejectPosition = 1 ; rejectPosition < approvalCourse.size() ;rejectPosition++){
			course = (ApprovalCourse) approvalCourse.get(rejectPosition);
				if((course.getconditionCode()).equals( ApprovalCourse.REJECT ))
					break;
			}
		this.returnPerson = course.getpersonID() ;
	 }
	 
	 public void setreturnPerson( String returnPerson ){
	 	this.returnPerson = returnPerson;
	 }
	 
	 public void setcurrentPerson(){
	 	int currentPosition;
	 	
	 	for( currentPosition = 1 ; currentPosition < approvalCourse.size() ; currentPosition++ ){
	 		System.out.println( currentPosition +"번째 결재자 : " + ((ApprovalCourse) approvalCourse.get(currentPosition)).getpersonID() + " , 타입 : " + ((ApprovalCourse)approvalCourse.get(currentPosition)).gettype() );
	 		if((((ApprovalCourse)approvalCourse.get(currentPosition)).getconditionCode()).equals(ApprovalCourse.NOTCOMPLETE))
				break;
		}
		System.out.println("결재 경로 길이:" + approvalCourse.size());
		System.out.println("현재 결재자 위치 :"+ currentPosition );
		if( currentPosition == approvalCourse.size() ){
			this.currentPerson = ((ApprovalCourse)approvalCourse.get( currentPosition - 1)).getpersonID();
			System.out.println("결재 완료");
			setendDay();
			System.out.println( "완료일" + getendDay() );
		}else if( ((ApprovalCourse)approvalCourse.get( currentPosition )).gettype() == ApprovalCourse.SUBMITINFORM ){
			this.currentPerson = ((ApprovalCourse)approvalCourse.get( currentPosition - 1)).getpersonID();
			System.out.println("결재 완료 그리고 통보자 있음");
			setendDay();
			System.out.println( "완료일" + getendDay() );
		}
		else{    	
			this.currentPerson = ((ApprovalCourse)approvalCourse.get(currentPosition)).getpersonID();
			this.currentPosition = currentPosition;
			System.out.println( "현 결재자 " + this.currentPerson);
			System.out.println( "현 결재 위치 " + this.currentPosition);
		}
		
	 }

	 public void setcurrentPerson( String currentPerson ){
	 	this.currentPerson = currentPerson;
	 }
	 
}	 
