/*
 * $Id: VKCCatProjectStep.java,v 1.20 2004/06/27 17:16:55 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCCatProjectStep.java,v $
 * Revision 1.20  2004/06/27 17:16:55  marbobo
 * + Quellcode maintenance
 *
 * Revision 1.19  2004/06/14 17:43:21  marbobo
 * Kleine Korrektur (nochmals)
 *
 * Revision 1.17  2004/04/06 23:54:41  marbobo
 * + toNewProjectStepInfo()
 *
 * Revision 1.16  2004/03/11 20:17:20  bigmac
 * Anpassungen an VKCUserSerializer
 *
 * Revision 1.15  2004/03/09 21:10:32  marbobo
 * +isStepRunning() isStepProcessed() isStepChecked()
 *
 * Revision 1.14  2004/03/07 22:31:00  marbobo
 * Aufgeräumt und neue getter-methoden
 *
 * Revision 1.13  2004/03/05 02:28:21  marbobo
 * +isProcessed() +isChecked() +isActivated()
 *
 * Revision 1.12  2004/03/03 23:18:47  marbobo
 * +getProcessor()
 * +getProcessedTSString()
 *
 * Revision 1.11  2004/02/10 15:17:57  jonyp
 * new get/setter methods
 *
 * Revision 1.10  2004/01/27 10:47:39  bigmac
 * +toString()
 *
 * Revision 1.9  2004/01/16 11:53:47  jonyp
 * + getDeadlineAsString
 *
 * Revision 1.8  2004/01/16 08:15:21  jonyp
 * corrections
 *
 * Revision 1.7  2004/01/14 15:32:13  jonyp
 * *** empty log message ***
 *
 * Revision 1.6  2004/01/14 10:36:31  jonyp
 * new field deadline
 *
 * Revision 1.5  2004/01/13 10:03:58  jonyp
 * initialize object
 *
 * Revision 1.4  2004/01/13 10:01:18  jonyp
 * initialize object
 *
 * Revision 1.3  2004/01/12 15:01:26  jonyp
 * layout changes
 *
 * Revision 1.2  2004/01/12 14:59:40  jonyp
 * processedTS flag
 *
 * Revision 1.1  2004/01/12 14:51:16  jonyp
 * first strike
 *
 */

package vkc.db.entities;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

import vkc.db.serializer.VKCUserSerializer;
import vkc.exception.DBException;
import vkc.util.DateUtils;
import vkc.view.bean.NewProjectStepInfo;
import vkc.view.bean.UserInfo;


/**
 * Klasse stellt einen CategoryProjectStep im VKC-System dar
 *
 * @author jonyP
 */
public class VKCCatProjectStep implements Serializable {

	private Integer catProjStepId = new Integer(-1);
	private Integer categoryId = new Integer(-1);
	private Integer step = new Integer(0);
	private boolean enabled = true;
	private String activity = "";
	private Timestamp startDate;
	private Timestamp endDate;
	private Integer processorId;
	private Timestamp processedTS;
	private Timestamp checkedTS;
	private Integer checkerId;
    
    public VKCCatProjectStep() {
    }
    
    public VKCCatProjectStep(Integer step, String activity, Timestamp startDate, Timestamp endDate) {
        this.step = step;
        this.enabled = true;
        this.activity = activity;
        this.startDate = startDate;
        this.endDate = endDate;
    }
    
	public String toString() {
		String pid = ( getProcessorId() !=null ) ? getProcessorId().toString() : "null";
		String pts = ( getProcessedTS() !=null ) ? getProcessedTSString() : "null";
		String cid = ( getCheckerId() !=null ) ? getCheckerId().toString() : "null";
		String cts = ( getCheckedTS() !=null ) ? getCheckedTSString() : "null";
		return "ActivityID     : " + getCatProjStepId() +
			   "\nActivity     : " + getActivity() +
			   "\nStep         : " + getStep() +
		       "\nEnabled      : " + isEnabled() + 
		       "\nCategoryId   : " + getCategoryId() + 
		       "\nStartDate    : " + getStartDate() +
		       "\nEndDate      : " + getEndDate() +
		       "\nProcessorId  : " + pid +
		       "\nProcessedTS  : " + pts +
		       "\nCheckerId    : " + cid +
		       "\nCheckedTS    : " + cts;
	}
	
	public void setCatProjStepId(Integer integer) {
		catProjStepId = integer;
	}

	public Integer getCatProjStepId() {
		return catProjStepId;
	}

	public void setCategoryId(Integer integer) {
		categoryId = integer;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setActivity(String string) {
		activity = string;
	}

	public String getActivity() {
		return activity;
	}

	public void setStep(Integer integer) {
		step = integer;
	}

	public Integer getStep() {
		return step;
	}

	public void setStartDate(Timestamp timestamp) {
		startDate = timestamp;
	}

	public Timestamp getStartDate() {
		return startDate;
	}

	public String getStartDateString() {
		if ( startDate != null ) {
			return DateUtils.getDD_MM_YYYY( new Date( startDate.getTime() ) );
		} 

		return null;
	}
	
	public void setEndDate(Timestamp timestamp) {
		endDate = timestamp;
	}

	public Timestamp getEndDate() {
		return endDate;
	}

	public String getEndDateString() {
		if ( endDate != null ) {
			return DateUtils.getDD_MM_YYYY( new Date( endDate.getTime() ) );
		} 

		return null;
	}
	
	public void setProcessorId(Integer integer) {
		processorId = integer;
	}

	public Integer getProcessorId() {
		return processorId;
	}

	public void setProcessedTS(Timestamp timestamp) {
		processedTS = timestamp;
	}

	public Timestamp getProcessedTS() {
		return processedTS;
	}

	public UserInfo getProcessor() throws DBException {
		if ( processorId != null ) {
			return VKCUserSerializer.retrieveUserInfo( processorId );
		} 

		return null;
	}
	
	public String getProcessedTSString() {
		if ( processedTS != null ) {
			return DateUtils.getDD_MM_YYYY( new Date( processedTS.getTime() ) );
		} 

		return null;
	}
	
	public boolean isProcessed() {
		return processorId != null && processedTS != null;
	}
	
	public void setCheckerId(Integer integer) {
		checkerId = integer;
	}

	public Integer getCheckerId() {
		return checkerId;
	}

	public void setCheckedTS(Timestamp timestamp) {
		checkedTS = timestamp;
	}

	public Timestamp getCheckedTS() {
		return checkedTS;
	}

	public UserInfo getChecker() throws DBException {
		if ( checkerId != null ) {
			return VKCUserSerializer.retrieveUserInfo( checkerId );
		} 

		return null;
	}
	
	public String getCheckedTSString() {
		if ( checkedTS != null ) {
			return DateUtils.getDD_MM_YYYY( new Date( checkedTS.getTime() ) );
		} 

		return null;
	}
	
	public boolean isChecked() {
		return checkerId != null && checkedTS != null;
	}
	
	public void setEnabled(boolean b) {
		enabled = b;
	}
	
	public boolean isEnabled() {
		return enabled;
	}

	public boolean isActivated() {
		if ( enabled ) {
			Date now = new Date( System.currentTimeMillis() );
			if ( startDate.before( now ) && endDate.after( now ) ) {
				return true;
			} 

			return false;
		} 

		return false;
	}
	
	public boolean isStepRunning() {
		return !isProcessed() && !isChecked() && isEnabled();
	}
	
	public boolean isStepProcessed() {
		return isProcessed() && !isChecked() && isEnabled();
	}

	public boolean isStepChecked() {
		return isChecked() && isEnabled();
	}
	
	public NewProjectStepInfo toNewProjectStepInfo() {
		NewProjectStepInfo step = new NewProjectStepInfo();
		step.setCatProjStepId( getCatProjStepId() );
		step.setCategoryId( getCategoryId() );
		step.setActivity( getActivity() );
		step.setStep( getStep() );
		if ( getStartDate() != null ) {
			step.setStartDate( DateUtils.getDD_MM_YYYY( getStartDate() ) );
		}
		if ( getEndDate() != null ) {
			step.setEndDate( DateUtils.getDD_MM_YYYY( getEndDate() ) );
		}
		step.setEnabled( isEnabled() );
		step.setProcessorId( getProcessorId() );
		if ( getProcessedTS() != null ) {
			step.setProcessedTS( DateUtils.getDD_MM_YYYY( getProcessedTS() ) );
		}
		step.setCheckerId( getCheckerId() );
		if ( getCheckedTS() != null ) {
			step.setCheckedTS( DateUtils.getDD_MM_YYYY( getCheckedTS() ) );
		}
		return step;
	}
}