/*
 * $Id: EditNewsForm.java,v 1.7 2006/05/31 14:55:01 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: EditNewsForm.java,v $
 * Revision 1.7  2006/05/31 14:55:01  marbobo
 * + Bugfix
 *
 * Revision 1.6  2006/05/31 14:00:32  marbobo
 * + Zeilenumbrüche bei Beschreibungsfelder anzeigen
 *
 * Revision 1.5  2006/05/24 17:58:51  marbobo
 * + Toolbar des Html-Editors kann optional angezeigt oder verborgen werden
 *
 * Revision 1.4  2004/11/10 15:02:48  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.3  2004/07/26 01:34:51  marbobo
 * + Initiale Version
 *
 */
package vkc.administration.form;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.swing.text.BadLocationException;

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

import vkc.db.entities.VKCNews;
import vkc.db.serializer.VKCNewsSerializer;
import vkc.misc.VKCLogManager;
import vkc.util.DateUtils;
import vkc.util.StringUtils;
import vkc.util.VKCStringUtils;
import vkc.view.form.BasicActionForm;

public class EditNewsForm extends BasicActionForm {
	private Integer newsId = null;

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

	private VKCNews news = null;

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

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

		news = null;
	}

	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

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

			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 void synchronizeForm(HttpServletRequest request) {
		try {
			setNews(VKCNewsSerializer.retrieveNews(getNewsId()));

			if (init(request)) {
				initializeForm(request);
			}

		} catch (Exception e) {
			VKCLogManager.errorLog(this, "", e.getMessage(), e);
		}
	}

	private boolean init(HttpServletRequest request) {
		return (request.getParameter("initialize") != null) == true;
	}

	private void initializeForm(HttpServletRequest request) throws BadLocationException {
		setHeadline(getNews().getHeadline());

		String text = getNews().getNewsText();

		if (!isTinyMCECompatible()) {
			text = VKCStringUtils.filterUnicodeChars(text);
			text = VKCStringUtils.htmlFormatedText(text);
			text = VKCStringUtils.decodeXmlEntities(text);
			text = VKCStringUtils.decodeNamedXmlEntitiesNotInSwing(text);
			text = VKCStringUtils.encodeNewLine(text);
			text = VKCStringUtils.removeHtmlTags(text);
			text = VKCStringUtils.decodeNewLine(text);
		} else {
			text = VKCStringUtils.convertNewLineInHTML(text);
		}

		setNewsText(text);
		setInfoUrl(getNews().getInfoUrl());

		if (getNews().getValidFrom() != null) {
			Date from = new Date(getNews().getValidFrom().getTime());
			setValidFrom(DateUtils.getDD_MM_YYYY(from));
		} else {
			setValidFrom("");
		}

		if (getNews().getValidUntil() != null) {
			Date until = new Date(getNews().getValidUntil().getTime());
			setValidUntil(DateUtils.getDD_MM_YYYY(until));
		} else {
			setValidUntil("");
		}
	}

	public String getHeadline() {
		return headline;
	}

	public String getInfoUrl() {
		return infoUrl;
	}

	public VKCNews getNews() {
		return news;
	}

	public Integer getNewsId() {
		return newsId;
	}

	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 setNews(VKCNews news1) {
		news = news1;
	}

	public void setNewsId(Integer newsId1) {
		newsId = newsId1;
	}

	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);
	}
}