/*
 * $Id: ArchiveCategoryForm.java,v 1.15 2006/09/18 13:54:21 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ArchiveCategoryForm.java,v $
 * Revision 1.15  2006/09/18 13:54:21  marbobo
 * + Archivierungordner wird jetzt aus den Systemparametern gelesen
 *
 * Revision 1.14  2006/04/29 18:39:47  marbobo
 * + Refactoriengs und eine Ausgaben vereinheitlicht
 *
 * Revision 1.13  2006/04/11 19:50:21  marbobo
 * + Komprimierung auf 7z-Format umgestellt
 *
 * Revision 1.12  2004/07/20 08:53:51  jonyp
 * added copyright info + cvs log
 *
 * Revision 1.11  2004/06/27 17:39:28  marbobo
 * + Quellcode maintenance
 *
 * Revision 1.10  2004/06/24 23:35:16  marbobo
 * Quellcode maintenance
 *
 * Revision 1.9  2004/05/10 12:57:23  marbobo
 * + Archivierungssprache auswählbar
 *
 * Revision 1.8  2004/05/09 16:25:53  marbobo
 * + Internationalisierung
 *
 * Revision 1.7  2004/04/27 18:56:23  marbobo
 * + Virtuelle Dokumente archivieren
 *
 * Revision 1.6  2004/03/02 23:06:31  bigmac
 * Anpassungen an VKCDocumentSerializer und VKCCategorySerializer
 *
 * Revision 1.5  2004/02/09 16:56:56  bigmac
 * Rework
 *
 * Revision 1.4  2004/02/06 18:36:53  teng
 * *** empty log message ***
 *
 * Revision 1.3  2004/01/29 19:41:55  teng
 * *** empty log message ***
 *
 * Revision 1.2  2004/01/07 17:13:43  teng
 * *** empty log message ***
 *
 * 
 */
package vkc.view.form;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

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

import vkc.db.entities.VKCCategory;
import vkc.db.serializer.VKCCategorySerializer;
import vkc.db.serializer.VKCLanguageSerializer;
import vkc.exception.DBException;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;

/**
 * the bean class containing the data for the class ArchiveCategoryAction
 */
public class ArchiveCategoryForm extends BasicActionForm {
	private boolean includeVersion = true;
	private boolean recursive = true;
	private boolean virtualDocuments = false;

	private File archiveFile = null;

	private Integer categoryId = null;
	private Integer languageId = null;

	private String fileSize = "";

	private VKCCategory category = null;

	/**
	 * Reset all properties to their default values.
	 *
	 * @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);

		includeVersion = true;
		recursive = true;
		virtualDocuments = false;
		
		archiveFile = null;

		categoryId = null;
		languageId = null;

		fileSize = "";

		category = null;
	}

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        
		setRecursive(request.getParameter("recursive") != null);
		setIncludeVersion(request.getParameter("includeVersion") != null);
		setVirtualDocuments(request.getParameter("virtualDocuments") != null);

		return errors;
    }
    
	public void setArchiveFile(File file) {
		archiveFile = file;
	}

	public File getArchiveFile() {
		return archiveFile;
	}
	
	public String getArchiveFilename() {
		return getArchiveFile().getName();
	}

	public void setFileSize(String sizeStr) {
		fileSize = sizeStr;
	}

	public String getFileSize() {
		return fileSize;
	}

	public void setCategoryId(Integer id) {
		categoryId = id;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setRecursive(boolean bool) {
		recursive = bool;
	}

	public boolean getRecursive() {
		return recursive;
	}

	public void setIncludeVersion(boolean bool) {
		includeVersion = bool;
	}

	public boolean getIncludeVersion() {
		return includeVersion;
	}

	public VKCCategory getCategory() throws DBException {
		if (category == null) {
			category = VKCCategorySerializer.retrieveCategory(getCategoryId());
		}

		return category;
	}

	public String getArchiveDirectoryPath() {
		return getArchiveDirectory().getAbsolutePath();
	}

	public File getArchiveDirectory() {
		File file = null;
		String path = WebShare.getSysParameterValue("ARCHIVECATEGORY", "ArchiveCategoryPath", "");
		
		if (!isEmpty(path)) {
			file = new File(path);
		} else {
			file = new File(WebShare.getWebApplicationDir(), "WEB-INF/archives");
		}
		
		return file.getAbsoluteFile();
	}

	public boolean isVirtualDocuments() {
		return virtualDocuments;
	}

	public void setVirtualDocuments(boolean b) {
		virtualDocuments = b;
	}

	public Integer getLanguageId() {
		if (languageId == null) {
			languageId = getUser().getLanguage().getLanguageId();
		}
		
		return languageId;
	}

	public void setLanguageId(Integer integer) {
		languageId = integer;
	}

	public List getLanguages() {
		return VKCLanguageSerializer.getLanguages();
	}
	
	public String getEncodedArchiveFilename() throws IOException {
		String name = VKCStringUtils.filterUnicodeChars(getArchiveFile().getCanonicalPath());
        
        return VKCStringUtils.base64Encode(name);
	}
}