/*
 * $Id: GroupInfo.java,v 1.10 2004/11/05 01:57:40 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: GroupInfo.java,v $
 * Revision 1.10  2004/11/05 01:57:40  marbobo
 * + isGroupValid() optimiert
 *
 * Revision 1.9  2004/07/26 11:34:04  jonyp
 * added countMembers
 *
 * Revision 1.8  2004/07/20 08:51:21  jonyp
 * added copyright info + cvs log
 *
 *
 */

package vkc.view.bean;

import java.sql.Timestamp;

import vkc.db.serializer.VKCUserSerializer;
import vkc.exception.DBException;
import vkc.util.DateUtils;
import vkc.util.StringUtils;

public final class GroupInfo {
	private boolean accountValid = false;
	private boolean adminPrivileges = false;

	private int countMembers = 0;

	private Integer creatorId;
	private Integer groupId;
	
	private String description = "";
	private String name = "";

	private Timestamp created;
	private Timestamp validFrom;
	private Timestamp validUntil;

	public GroupInfo() {
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public Integer getGroupId() {
		return groupId;
	}

	public void setName( String s ) {
		name = StringUtils.normalize( s );
	}

	public void setDescription( String s ) {
		description = StringUtils.normalize( s );
	}

	public void setGroupId( Integer i ) {
		groupId = i;
	}

	public boolean isAccountValid() {
		return accountValid;
	}

	public void setAccountValid( boolean b ) {
		accountValid = b;
	}

	public Timestamp getValidFrom() {
		return validFrom;
	}

	public void setValidFrom( Timestamp t ) {
		validFrom = t;
	}

	public Timestamp getValidUntil() {
		return validUntil;
	}

	public void setValidUntil( Timestamp t ) {
		validUntil = t;
	}

	public boolean isGroupValid(){
		Timestamp today = new Timestamp( System.currentTimeMillis() );

		if ( isAccountValid() ) {
			if ( getValidFrom() != null && getValidUntil() == null ) {
				if ( getValidFrom().before( today ) ) {
					return isAccountValid();			
				} 

				return false;
			} else if ( getValidFrom() == null && getValidUntil() != null ) {
				if ( getValidUntil().after( today ) ) {
					return isAccountValid();			
				} 

				return false;
			} else if ( getValidFrom() != null && getValidUntil() != null ) {
				if ( getValidFrom().before( today ) && getValidUntil().after( today ) ) {
					return isAccountValid();			
				} 

				return false;
			} else{
				return isAccountValid();
			}
		} 

		return isAccountValid();
	}

	public Integer getCreatorId() {
		return creatorId;
	}

	public void setCreatorId( Integer i ) {
		creatorId = i;
	}
	
	public UserInfo getCreator() throws DBException {
		if ( getCreatorId() != null ) {
			return VKCUserSerializer.retrieveUserInfo( getCreatorId() );
		} 

		return null;
	}
	
	public Timestamp getCreated() {
		return created;
	}

	public void setCreated( Timestamp t ) {
		created = t;
	}
	
	public String getCreatedStr() {
		if ( getCreated() != null ) {
			return DateUtils.getDD_MM_YYYY( getCreated() );
		} 

		return "";
	}

	public boolean isAdminPrivileges() {
		return adminPrivileges;
	}

	public void setAdminPrivileges( boolean b ) {
		adminPrivileges = b;
	}

	public boolean equals( Object object ) {
		if ( object == null || object instanceof GroupInfo == false ) {
			return false;
		} 

		GroupInfo info = (GroupInfo)object;
		
		return getGroupId().equals( info.getGroupId() );
	}

	public int getCountMembers() {
		return countMembers;
	}

	public void setCountMembers( int i ) {
		countMembers = i;
	}
}