/*
 * $Id: UserDocumentViewSettingsForm.java,v 1.5 2006/09/17 21:19:01 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: UserDocumentViewSettingsForm.java,v $
 * Revision 1.5  2006/09/17 21:19:01  marbobo
 * + virtowebView-Einträge und Methoden wegen Migration entfernt (nicht mehr nötig)
 *
 * Revision 1.4  2005/09/04 18:00:41  marbobo
 * + Neues Metadaten-Attribute (Erstellungsdatum) für Dokumente hinzugefügt
 *
 * Revision 1.3  2004/11/09 14:35:41  marbobo
 * + Umstellung von authorId auf publisherId
 * + Hinzufügen von AuthorName für Dokumente
 * + Nötwendige Änderungen betreffend AuthorName und PublisherId
 *
 * 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.form;


import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

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

import vkc.db.serializer.VKCDocumentViewFieldSerializer;
import vkc.exception.DBException;
import vkc.view.bean.DocumentViewSettings;
import vkc.view.form.DynaForm;

/**
 * Form bean zum Ändern des Dokumentansicht
 *
 * @author marbobo
 */
public final class UserDocumentViewSettingsForm extends DynaForm {
	private HashMap defaultFields = null;
	private DocumentViewSettings settings = null; 
	private String documentViewType = "defaultview";
	private boolean fileName = false;
	private boolean fileFormat = false;
	private boolean fileSize = false;
	private boolean classType = false;
	private boolean statusType = false;
	private boolean versionNo = false;
	private boolean versionLabel = false;
	private boolean versionDescr = false;
	private boolean expires = false;
	private boolean published = false;
	private boolean author = false;
	private boolean creationDate = false;
	private boolean publisher = false;
	private boolean accessCount = false;
	private boolean lastAccessed = false;
	private boolean keywords = false;
	private boolean description = false;
	private boolean additionalFormats = false;
	private boolean dynaAttributes = false;
	private boolean addAttributes = false; 

	/**
	 * Reset der Eingabefelder auf Defaultwerte
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		super.reset(mapping, request);
		
		defaultFields = null;
		settings = null;
		documentViewType = "defaultview";
	}

	/**
	 * Validierung der Eingabefelder
	 *
	 * @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();
        
		setFileName(request.getParameter("fileName") != null);
		setFileFormat(request.getParameter("fileFormat") != null);
		setFileSize(request.getParameter("fileSize") != null);
		setClassType(request.getParameter("classType") != null);
		setStatusType(request.getParameter("statusType") != null);
		setVersionNo(request.getParameter("versionNo") != null);
		setVersionLabel(request.getParameter("versionLabel") != null);
		setVersionDescr(request.getParameter("versionDescr") != null);
		setExpires(request.getParameter("expires") != null);
		setPublished(request.getParameter("published") != null);
		setAuthor(request.getParameter("author") != null);
		setCreationDate(request.getParameter("creationDate") != null);
		setPublisher(request.getParameter("publisher") != null);
		setAccessCount(request.getParameter("accessCount") != null);
		setLastAccessed(request.getParameter("lastAccessed") != null);
		setKeywords(request.getParameter("keywords") != null);
		setDescription(request.getParameter("description") != null);
		setAdditionalFormats(request.getParameter("additionalFormats") != null);
		setDynaAttributes(request.getParameter("dynaAttributes") != null);
		setAddAttributes(request.getParameter("addAttributes") != null);
		
		initializeForm(request);
		
		return errors;
	}

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

	private void initializeForm(HttpServletRequest request) {
		if (init(request)) {
			DocumentViewSettings settings = new DocumentViewSettings();

			try {
				settings = getSettings();
				
				if (settings.isDefaultViewSetting()) {
					setDocumentViewType("defaultview");
					settings = settings.getDefaultviewDocumentViewSettings();
				} else {
					setDocumentViewType("userview");
				}
				
				setFields(settings);
			} catch (DBException e) {
				setDocumentViewType("userview");
				setFields(settings);
			}
		}
	}

	public void setFields(DocumentViewSettings settings) {
		setFileName(settings.isFileName());
		setFileFormat(settings.isFileFormat());
		setFileSize(settings.isFileSize());
		setClassType(settings.isClassType());
		setStatusType(settings.isStatusType());
		setVersionNo(settings.isVersionNo());
		setVersionLabel(settings.isVersionLabel());
		setVersionDescr(settings.isVersionDescr());
		setExpires(settings.isExpires());
		setPublished(settings.isPublished());
		setAuthor(settings.isAuthor());
		setCreationDate(settings.isCreationDate());
		setPublisher(settings.isPublisher());
		setAccessCount(settings.isAccessCount());
		setLastAccessed(settings.isLastAccessed());
		setKeywords(settings.isKeywords());
		setDescription(settings.isDescription());
		setAdditionalFormats(settings.isAdditionalFormats());
		setDynaAttributes(settings.isDynaAttributes());
		setAddAttributes(settings.isAddAttributes());
	}

	public DocumentViewSettings getSettings() throws DBException {
		if (settings == null) {
			settings = VKCDocumentViewFieldSerializer.retrieveDocumentViewSettings(getUser().getUserId());
		}

		return settings;
	}

	public void setSettings(DocumentViewSettings s) {
		settings = s;
	}

	public boolean isAccessCount() {
		return accessCount;
	}

	public boolean isAddAttributes() {
		return addAttributes;
	}

	public boolean isAdditionalFormats() {
		return additionalFormats;
	}

	public boolean isAuthor() {
		return author;
	}

	public boolean isClassType() {
		return classType;
	}

	public boolean isCreationDate() {
		return creationDate;
	}

	public boolean isDescription() {
		return description;
	}

	public boolean isDynaAttributes() {
		return dynaAttributes;
	}

	public boolean isExpires() {
		return expires;
	}

	public boolean isFileFormat() {
		return fileFormat;
	}

	public boolean isFileName() {
		return fileName;
	}

	public boolean isFileSize() {
		return fileSize;
	}

	public boolean isKeywords() {
		return keywords;
	}

	public boolean isLastAccessed() {
		return lastAccessed;
	}

	public boolean isPublished() {
		return published;
	}

	public boolean isPublisher() {
		return publisher;
	}

	public boolean isStatusType() {
		return statusType;
	}

	public boolean isVersionDescr() {
		return versionDescr;
	}

	public boolean isVersionLabel() {
		return versionLabel;
	}

	public boolean isVersionNo() {
		return versionNo;
	}

	public void setAccessCount(boolean b) {
		accessCount = b;
	}

	public void setAddAttributes(boolean b) {
		addAttributes = b;
	}

	public void setAdditionalFormats(boolean b) {
		additionalFormats = b;
	}

	public void setAuthor(boolean b) {
		author = b;
	}

	public void setClassType(boolean b) {
		classType = b;
	}

	public void setCreationDate(boolean b) {
		creationDate = b;
	}

	public void setDescription(boolean b) {
		description = b;
	}

	public void setDynaAttributes(boolean b) {
		dynaAttributes = b;
	}

	public void setExpires(boolean b) {
		expires = b;
	}

	public void setFileFormat(boolean b) {
		fileFormat = b;
	}

	public void setFileName(boolean b) {
		fileName = b;
	}

	public void setFileSize(boolean b) {
		fileSize = b;
	}

	public void setKeywords(boolean b) {
		keywords = b;
	}

	public void setLastAccessed(boolean b) {
		lastAccessed = b;
	}

	public void setPublished(boolean b) {
		published = b;
	}

	public void setPublisher(boolean b) {
		publisher = b;
	}

	public void setStatusType(boolean b) {
		statusType = b;
	}

	public void setVersionDescr(boolean b) {
		versionDescr = b;
	}

	public void setVersionLabel(boolean b) {
		versionLabel = b;
	}

	public void setVersionNo(boolean b) {
		versionNo = b;
	}

	public HashMap getDefaultFields() throws DBException {
		if (defaultFields == null) {
			defaultFields = VKCDocumentViewFieldSerializer.getAllDefaultDocumentViewFields();
		}
		
		return defaultFields;
	}

	public Object getDefaultField(String key) throws DBException {
		return getDefaultFields().get(key);
	}

	public String getDocumentViewType() {
		return documentViewType;
	}

	public void setDocumentViewType(String string) {
		documentViewType = string;
	}

	public boolean isFullViewType() {
		return getDocumentViewType().equals("fullview");
	}

	public boolean isDefaultViewType() {
		return getDocumentViewType().equals("defaultview");
	}

	public boolean isUserViewType() {
		return getDocumentViewType().equals("userview");
	}
}