/*
 * $Id: CreateNewsForm.java,v 1.2 2004/07/26 01:34:23 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: CreateNewsForm.java,v $
 * Revision 1.2  2004/07/26 01:34:23  marbobo
 * + Komplett neu Überarbeitet
 *
 * Revision 1.1  2004/06/22 13:59:47  vchris
 * In Package vkc.administration verlagert
 *
 * Revision 1.2  2004/06/22 12:08:28  vchris
 * *** empty log message ***
 *
 * Revision 1.1  2004/05/14 11:43:58  vchris
 * News: initiale Version
 *
 * 
 * Created on 12.05.2004
 */
package vkc.administration.form;

import java.net.MalformedURLException;
import java.net.URL;
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.util.DateUtils;
import vkc.util.StringUtils;
import vkc.view.form.BasicActionForm;

public class CreateNewsForm extends BasicActionForm {

	private String headline		= "";
	private String newsText		= "";
	private String infoUrl		= "";
	private String validFrom    = "";
	private String validUntil   = "";

    public void reset( ActionMapping mapping, HttpServletRequest request ) {
	    super.reset( mapping, request );
		headline 	= "";
		newsText 	= "";
		infoUrl	 	= "";
		validFrom   = "";
		validUntil  = "";
	}

    public ActionErrors validate( ActionMapping mapping, HttpServletRequest request ) {

        ActionErrors errors = new ActionErrors();

		if ( request.getParameter("action.save.news") != null ) {

			if ( isEmpty( getHeadline() ) ) {
				errors.add( "headline", new ActionError( "vkc.administration.news.errors.headline" ) );
			}
				
			if ( isEmpty( getNewsText() ) ) {
				errors.add( "newstext", new ActionError( "vkc.administration.news.errors.newstext" ) );
			}
	
            if ( !isEmpty( getInfoUrl() ) ) {
                try {
                    new URL( getInfoUrl() );
                } catch (MalformedURLException e) {
    				errors.add( "url", new ActionError( "vkc.administration.news.errors.url" ) );
                }
            }
			
			Date f = DateUtils.parseDate( getValidFrom() );
			Date u = DateUtils.parseDate( getValidUntil() );

			if ( !isEmpty( getValidFrom() ) ) {
	            if ( f == null ) {
					errors.add( "validfrom", new ActionError( "vkc.administration.news.errors.validfrom" ) );
	            }
			}

			if ( !isEmpty( getValidUntil() ) ) {
				if ( u == null ) {
					errors.add( "validuntil", new ActionError( "vkc.administration.news.errors.validuntil" ) );
				}
			}

            if ( f != null && u != null && f.after( u ) ) {
                errors.add( "daterange", new ActionError( "error.date.range" ) );
            }

		}

		return errors;
    }

    public String getHeadline() {
		return headline;
	}

	public String getInfoUrl() {
		return infoUrl;
	}

	public String getNewsText() {
		return newsText;
	}

	public String getValidFrom() {
		return validFrom;
	}

	public String getValidUntil() {
		return validUntil;
	}

	public void setHeadline( String s ) {
		headline = StringUtils.normalize( s );
	}

	public void setInfoUrl( String s ) {
		infoUrl = StringUtils.normalize( s );
	}

	public void setNewsText( String s ) {
		newsText = StringUtils.normalize( s );
	}

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

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