/*
 * $Id: VKCGroup.java,v 1.13 2005/02/15 09:26:47 bigmac Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCGroup.java,v $
 * Revision 1.13  2005/02/15 09:26:47  bigmac
 * Refactorings
 *
 * Revision 1.12  2004/10/22 12:13:18  marbobo
 * + Datumstypen auf Timestamp umgestellt
 *
 * Revision 1.11  2004/06/27 17:16:55  marbobo
 * + Quellcode maintenance
 *
 * Revision 1.10  2004/03/18 01:21:37  marbobo
 * In der equals()-Methode wird nun die GroupId verglichen
 *
 * Revision 1.9  2004/03/14 18:01:26  marbobo
 * -setGroupUserIds() entfernt
 *
 * Revision 1.8  2004/03/13 14:48:58  marbobo
 * EditGroupAction und EditGroupForm auf Request-Scope umgestellt
 *
 * Revision 1.7  2003/12/18 18:49:54  bigmac
 * Überflüssige Imports gelöscht
 *
 * Revision 1.6  2003/11/28 15:04:10  jonyp
 * changes due to new schema
 *
 * Revision 1.5  2003/11/11 15:00:34  jonyp
 * deleted creategroups field
 *
 * Revision 1.4  2003/10/23 21:35:00  marbobo
 * Aufgeräumt
 *
 * Revision 1.3  2003/10/05 01:37:58  marbobo
 * +accountValid bei Gruppen
 * +isGroupValid()
 *
 * Revision 1.2  2003/08/18 21:14:56  marbobo
 * import angepasst
 *
 * Revision 1.1  2003/08/02 16:57:12  bigmac
 * *** empty log message ***
 *
 */
package vkc.db.entities;

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

import vkc.db.serializer.VKCGroupSerializer;
import vkc.exception.DBException;
import vkc.security.Accessor;
import vkc.util.ObjectCloner;
import vkc.util.StringUtils;

/**
 * Diese Klasse repräsentiert die Gruppen des VKC-Systems
 *
 */
public final class VKCGroup implements Accessor, Cloneable, Serializable {
	private Boolean accountValid = Boolean.TRUE; 
    private Boolean adminPrivileges = Boolean.FALSE;    

    private Integer creatorId;
	private Integer groupId = null;
	private Integer lastChangerId;

    private String description;
    private String name;

    private Timestamp created = new Timestamp( System.currentTimeMillis() );
	private Timestamp lastChanged = new Timestamp( System.currentTimeMillis() );
    private Timestamp validFrom;
    private Timestamp validUntil;

    /**
     * Creates a new VKCGroup object.
     */
    public VKCGroup() {
    }

    /**
     * Creates a new VKCGroup object.
     *
     * @param _name DOCUMENT ME!
     * @param _description DOCUMENT ME!
     * @param _creatorId DOCUMENT ME!
     * @param _lastChangerId DOCUMENT ME!
     */
    public VKCGroup( String _name, String _description, Integer _creatorId, Integer _lastChangerId ) {
        name = _name;
        description = _description;
        creatorId = _creatorId;
        lastChangerId = _lastChangerId;
    }

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

    public Boolean getAdminPrivileges() {
        return adminPrivileges;
    }    

    public void setCreated( Timestamp t ) {
        created = t;
    }

    public Timestamp getCreated() {
        return created;
    }

    public void setCreatorId( Integer i ) {
        creatorId = i;
    }

    public Integer getCreatorId() {
        return creatorId;
    }

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

    public String getDescription() {
        return description;
    }

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

    public Integer getGroupId() {
        return groupId;
    }

    public void setLastChanged( Timestamp t ) {
        lastChanged = t;
    }

    public Timestamp getLastChanged() {
        return lastChanged;
    }

    public void setLastChangerId( Integer i ) {
        lastChangerId = i;
    }

    public Integer getLastChangerId() {
        return lastChangerId;
    }

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

    public String getName() {
        return name;
    }

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

    public Timestamp getValidFrom() {
        return validFrom;
    }

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

    public Timestamp getValidUntil() {
        return validUntil;
    }

    public Integer getAccessorId() {
        return getGroupId();
    }

    public String getAccessorName() {
        return getName();
    }

    public boolean equals( Object o ) {
		if ( !( o instanceof VKCGroup ) ) {
	    	return false;
		}

		VKCGroup group = (VKCGroup)o;

		return getGroupId().equals( group.getGroupId() );
    }

    public int hashCode(){
		return getName().hashCode();
    }

    public void printGroup() {
        System.out.println( "name = " + getName() );
        System.out.println( "description = " + getDescription() );
    }

    public VKCGroup cloneGroup() throws java.lang.Exception {
        VKCGroup cloneGroup = (VKCGroup) ObjectCloner.deepCopy( this );

        return cloneGroup;
    }

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

	public Boolean getAccountValid() {
		return accountValid;
	}

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

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

				return Boolean.FALSE;
			} else if ( getValidFrom() == null && getValidUntil() != null ) {
				if ( getValidUntil().after( today ) ) {
					return getAccountValid();			
				} 

				return Boolean.FALSE;
			} else if ( getValidFrom() != null && getValidUntil() != null ) {
				if ( getValidFrom().before( today ) && getValidUntil().after( today ) ) {
					return getAccountValid();			
				} 

				return Boolean.FALSE;
			} else{
				return getAccountValid();
			}
		} 

		return getAccountValid();
	}

	public Set getGroupUserIds() throws DBException {
		return VKCGroupSerializer.getGroupUserIds( getGroupId() );
	}
}