/*
 * $Id: NewGroupForm.java,v 1.13 2005/02/20 23:34:15 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: NewGroupForm.java,v $
 * Revision 1.13  2005/02/20 23:34:15  marbobo
 * + Bugfix
 *
 * Revision 1.12  2004/11/10 15:02:48  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.11  2004/08/07 00:34:43  marbobo
 * + Bugfix für Zeitangaben und Refactorings
 *
 * Revision 1.10  2004/07/20 08:09:56  jonyp
 * added copyright info + cvs log
 *
 */

package vkc.administration.form;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import vkc.db.serializer.VKCGroupSerializer;
import vkc.exception.DBException;
import vkc.misc.VKCLogManager;
import vkc.util.DateUtils;
import vkc.util.StringUtils;
import vkc.util.VKCStringUtils;
import vkc.view.form.BasicActionForm;

public final class NewGroupForm extends BasicActionForm {
	private boolean adminprivileges	= false;
	private boolean creategroups    = false;
	private boolean groupActive   	= false;

	private String  description		= "";
	private String  name       		= "";
	private String  validFrom		= "";
	private String  validUntil		= "";

	public void reset( ActionMapping mapping, HttpServletRequest request ) {
	    super.reset( mapping, request );

	    adminprivileges	= false;
		creategroups    = false;
		groupActive   	= false;

		description		= "";
		name       		= "";
		validFrom		= "";
		validUntil		= "";
	}

	/**
	 * Validiert die Eingabefelder und gibt gegebenfalls die Fehler zurück
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public ActionErrors validate( ActionMapping mapping, HttpServletRequest request ) {
		ActionErrors errors = new ActionErrors();

		setAdminprivileges( request.getParameter( "adminprivileges" ) != null );
		setGroupActive( request.getParameter( "groupActive" ) != null );
		
		if ( request.getParameter( "action.create.group" ) != null ) {
			if ( isEmpty( getName() ) ) {
				errors.add( "groupname", new ActionError( "vkc.administration.error.groupname.required" ) );
			}
	    
			if ( isGroupActive() ) {
				Date f = DateUtils.parseDate( getValidFrom() );
				Date u = DateUtils.parseDate( getValidUntil() );
	
				if ( !isEmpty( getValidFrom() ) ) {
					if ( f == null ) {
						errors.add( "validFrom", new ActionError( "error.date" ) );
					}
				}
	
				if ( !isEmpty( getValidUntil() ) ) {
					if ( u == null ) {
						errors.add( "validUntil", new ActionError( "error.date" ) );
					}
				}
	
				if ( f != null && u != null && f.after( u ) ) {
					errors.add( "date", new ActionError( "error.date.range" ) );
				}
			}
	
			try {
				if ( VKCGroupSerializer.isGroupExistent( VKCStringUtils.filterUnicodeChars( getName() ) ) ) {
					errors.add( "groupname.exist", new ActionError( "vkc.administration.error.groupname.exist" ) );
				}
			} catch ( DBException e ) {
				VKCLogManager.errorLog( this, "", e.getMessage(), e );
			}
		}

		return errors;
	}

	public boolean isAdminprivileges() {
		return adminprivileges;
	}

	public boolean isCreategroups() {
		return creategroups;
	}

	public String getDescription() {
		return description;
	}

	public boolean isGroupActive() {
		return groupActive;
	}

	public String getName() {
		return name;
	}

	public String getValidFrom() {
		return validFrom;
	}

	public String getValidUntil() {
		return validUntil;
	}

	public void setAdminprivileges( boolean b ) {
		adminprivileges = b;
	}

	public void setCreategroups( boolean b ) {
		creategroups = b;
	}

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

	public void setGroupActive( boolean b ) {
		groupActive = b;
	}

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

	public void setValidFrom( String s ) {
		validFrom = StringUtils.normalize( s );
	}

	public void setValidUntil( String s ) {
		validUntil = StringUtils.normalize( s );
	}
}
