/*
 * $Id: UserDocumentViewSettingsAction.java,v 1.8 2008/02/12 12:13:24 korowa Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: UserDocumentViewSettingsAction.java,v $
 * Revision 1.8  2008/02/12 12:13:24  korowa
 * bugfixing: unter umständen verursachte die Übergabe des Requests bei '"Zurück" Action ein Aufruf von Funktion in vorheriger Form, was zu Fehlern führte
 *
 * Revision 1.7  2006/10/04 03:14:28  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.6  2005/09/04 18:00:41  marbobo
 * + Neues Metadaten-Attribute (Erstellungsdatum) für Dokumente hinzugefügt
 *
 * Revision 1.5  2005/02/14 14:01:49  bigmac
 * Applikationsweites CSS konfigurierbar gemacht + Refactorings
 *
 * Revision 1.4  2004/11/09 14:35:40  marbobo
 * + Umstellung von authorId auf publisherId
 * + Hinzufügen von AuthorName für Dokumente
 * + Nötwendige Änderungen betreffend AuthorName und PublisherId
 *
 * Revision 1.3  2004/11/02 21:42:28  marbobo
 * + Einige Funktionen für Gast-Benutzer gesperrt
 * + WebShare.LOGIN_PAGE mit WebShare.getLoginPage() ersetzt
 *
 * Revision 1.2  2004/05/10 20:16:14  marbobo
 * + "Komplett Ansicht", "Standard Ansicht" und "Benutzer Ansicht"
 *
 * Revision 1.1  2004/04/29 22:31:48  marbobo
 * Initiale Version
 *
 */
package vkc.profile.action;

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.db.serializer.VKCDocumentViewFieldSerializer;
import vkc.exception.DBException;
import vkc.profile.form.UserDocumentViewSettingsForm;
import vkc.view.WebShare;
import vkc.view.bean.DocumentViewSettings;

/**
 * Action-Klasse zum Verwalten von Dokumentansicht-Einstellungen
 *
 * @author marbobo
 * @version $Revision: 1.8 $
 */
public final class UserDocumentViewSettingsAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/userProfile/userDocumentViewSettings.jsp");

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

	/**
	 * Hier werden die entsprechenden Aktionen eingeleitet
	 */
	public synchronized ActionForward execute( ActionMapping mapping, 
	        								   ActionForm f, 
	        								   HttpServletRequest request, 
	        								   HttpServletResponse response )
	throws Exception {
		UserDocumentViewSettingsForm form = (UserDocumentViewSettingsForm)f;

		if (form.getUser().isGuestUser()) {
			request.setAttribute(WebShare.INFO_KEY, "vkc.info.access_denied");
			
			return INFO_FORWARD;
		}

		if (request.getParameter("action.back") != null) {
			return new ActionForward("/userProfile.do?initialize=true", true);
		} else if (request.getParameter("action.save") != null) {
			return handleSaveDocumentViewSettingsAction(form, request);
		} else if (request.getParameter("action.change.documentviewtype") != null) {
			return handleChangeDocumentViewTypeAction(form, request);
		} else {
			return handleInitializeAction(form, request);
		}
	}

	private ActionForward handleChangeDocumentViewTypeAction(UserDocumentViewSettingsForm form, HttpServletRequest request) throws DBException {
		DocumentViewSettings settings = new DocumentViewSettings();

		if (form.isFullViewType()) {
			settings = settings.getFullviewDocumentViewSettings();			
		} else if (form.isDefaultViewType()) {
			settings = settings.getDefaultviewDocumentViewSettings();			
		} else if (form.isUserViewType()) {
			settings = VKCDocumentViewFieldSerializer.retrieveDocumentViewSettings(form.getUser().getUserId());
		}

		form.setFields(settings);
		
		return DEFAULT_FORWARD;
	}

	private ActionForward handleSaveDocumentViewSettingsAction(UserDocumentViewSettingsForm form, HttpServletRequest request) throws DBException {
		DocumentViewSettings settings = form.getSettings();
		
		if (form.isFullViewType()) {
			settings = settings.getFullviewDocumentViewSettings();			
			form.setFileName(settings.isFileName());
			form.setFileFormat(settings.isFileFormat());
			form.setFileSize(settings.isFileSize());
			form.setClassType(settings.isClassType());
			form.setStatusType(settings.isStatusType());
			form.setVersionNo(settings.isVersionNo());
			form.setVersionLabel(settings.isVersionLabel());
			form.setVersionDescr(settings.isVersionDescr());
			form.setExpires(settings.isExpires());
			form.setPublished(settings.isPublished());
			form.setAuthor(settings.isAuthor());
			form.setCreationDate(settings.isCreationDate());
			form.setPublisher(settings.isPublisher());
			form.setAccessCount(settings.isAccessCount());
			form.setLastAccessed(settings.isLastAccessed());
			form.setKeywords(settings.isKeywords());
			form.setDescription(settings.isDescription());
			form.setAdditionalFormats(settings.isAdditionalFormats());
			form.setDynaAttributes(settings.isDynaAttributes());
			form.setAddAttributes(settings.isAddAttributes());
		} else if (form.isDefaultViewType()) {
			settings = settings.getDefaultviewDocumentViewSettings();			
			form.setFileName(settings.isFileName());
			form.setFileFormat(settings.isFileFormat());
			form.setFileSize(settings.isFileSize());
			form.setClassType(settings.isClassType());
			form.setStatusType(settings.isStatusType());
			form.setVersionNo(settings.isVersionNo());
			form.setVersionLabel(settings.isVersionLabel());
			form.setVersionDescr(settings.isVersionDescr());
			form.setExpires(settings.isExpires());
			form.setPublished(settings.isPublished());
			form.setAuthor(settings.isAuthor());
			form.setCreationDate(settings.isCreationDate());
			form.setPublisher(settings.isPublisher());
			form.setAccessCount(settings.isAccessCount());
			form.setLastAccessed(settings.isLastAccessed());
			form.setKeywords(settings.isKeywords());
			form.setDescription(settings.isDescription());
			form.setAdditionalFormats(settings.isAdditionalFormats());
			form.setDynaAttributes(settings.isDynaAttributes());
			form.setAddAttributes(settings.isAddAttributes());
		} else if (form.isUserViewType()) {
			settings.setFileName(form.isFileName());
			settings.setFileFormat(form.isFileFormat());
			settings.setFileSize(form.isFileSize());
			settings.setClassType(form.isClassType());
			settings.setStatusType(form.isStatusType());
			settings.setVersionNo(form.isVersionNo());
			settings.setVersionLabel(form.isVersionLabel());
			settings.setVersionDescr(form.isVersionDescr());
			settings.setExpires(form.isExpires());
			settings.setPublished(form.isPublished());
			settings.setAuthor(form.isAuthor());
			settings.setCreationDate(form.isCreationDate());
			settings.setPublisher(form.isPublisher());
			settings.setAccessCount(form.isAccessCount());
			settings.setLastAccessed(form.isLastAccessed());
			settings.setKeywords(form.isKeywords());
			settings.setDescription(form.isDescription());
			settings.setAdditionalFormats(form.isAdditionalFormats());
			settings.setDynaAttributes(form.isDynaAttributes());
			settings.setAddAttributes(form.isAddAttributes());
		}

		VKCDocumentViewFieldSerializer.saveDocumentViewSettings(settings, form.getUser().getUserId());
		
		return DEFAULT_FORWARD;
	}

	private ActionForward handleInitializeAction(UserDocumentViewSettingsForm form, HttpServletRequest request) {
		return DEFAULT_FORWARD;
	}
}