/*
 * $Id: EditNewsAction.java,v 1.12 2006/10/04 03:15:02 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: EditNewsAction.java,v $
 * Revision 1.12  2006/10/04 03:15:02  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.11  2006/05/24 17:59:33  marbobo
 * + Toolbar des Html-Editors kann optional angezeigt oder verborgen werden
 *
 * Revision 1.10  2005/10/17 12:57:05  jonyp
 * bug fix: if news is edited, update creators inforamtion
 *
 * Revision 1.9  2005/02/20 16:56:23  marbobo
 * + Beim Speichern der Entities in die Datenbank wird sichergestellt
 *    dass Unicode-Zeichen in Kodierterform (als &#xxxxx;) gespeichert werden
 *
 * Revision 1.8  2005/02/14 14:01:47  bigmac
 * Applikationsweites CSS konfigurierbar gemacht + Refactorings
 *
 * Revision 1.7  2004/09/16 13:10:55  marbobo
 * + Auf VKCRequestProcessor umgestellt
 *
 * Revision 1.6  2004/07/26 01:34:51  marbobo
 * + Initiale Version
 *
 */
package vkc.administration.action;

import java.sql.Timestamp;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vkc.administration.form.EditNewsForm;
import vkc.db.entities.VKCNews;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCNewsSerializer;
import vkc.exception.DBException;
import vkc.util.DateUtils;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;

public class EditNewsAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/administration/editNews.jsp");

	private ActionForward INFO_FORWARD = new ActionForward("/templates/${template.name}/common/InfoPage.jsp");

	public ActionForward execute(ActionMapping mapping, 
								 ActionForm f,
								 HttpServletRequest request, 
								 HttpServletResponse response)
	throws Exception {
		EditNewsForm form = (EditNewsForm) f;

		VKCUser user = form.getUser();

		if (!user.hasAdminPrivileges()) {
			request.setAttribute(WebShare.INFO_KEY, "vkc.info.access_denied");

			return INFO_FORWARD;
		}

		if (request.getParameter("action.save.news") != null) {
			return handleSaveNewsAction(form, request);
		}

		if (request.getParameter("action.back") != null) {
			return new ActionForward("/newsList.do");
		}

		if (request.getParameter("action.toggle.tinymcetoolbar") != null) {
			return DEFAULT_FORWARD;
		}
		
		return handleInitializeAction(form, request);
	}

	private ActionForward handleInitializeAction(EditNewsForm form, HttpServletRequest request) {
		form.synchronizeForm(request);
		
		return DEFAULT_FORWARD;
	}

	private ActionForward handleSaveNewsAction(EditNewsForm form, HttpServletRequest request) throws DBException {
		VKCUser user = form.getUser();
		VKCNews news = form.getNews();

		news.setHeadline(VKCStringUtils.filterUnicodeChars(form.getHeadline()));
		
		String newsText = VKCStringUtils.filterUnicodeChars(form.getNewsText());
		
		if (!form.isTinyMCECompatible()) {
			newsText = VKCStringUtils.decodeXmlEntities(newsText);
			newsText = VKCStringUtils.encodeNamedXmlEntities(newsText);
		}
		
		news.setNewsText(newsText);
		news.setInfoUrl(form.getInfoUrl());

		if (DateUtils.parseDate(form.getValidFrom()) != null) {
			news.setValidFrom(new Timestamp(DateUtils.parseDate(form.getValidFrom()).getTime()));
		} else {
			news.setValidFrom(null);
		}

		if (DateUtils.parseDate(form.getValidUntil()) != null) {
			Calendar c = Calendar.getInstance();
			c.setTime(DateUtils.parseDate(form.getValidUntil()));
			c.set(Calendar.HOUR_OF_DAY, 23);
			c.set(Calendar.MINUTE, 59);
			c.set(Calendar.SECOND, 59);
			news.setValidUntil(new Timestamp(c.getTimeInMillis()));
		} else {
			news.setValidUntil(null);
		}

		news.setCreatorId(user.getUserId());
		news.setCreated(new Timestamp(System.currentTimeMillis()));

		VKCNewsSerializer.saveNews(news);

		return new ActionForward("/newsList.do");
	}
}