/*
 * $Id: ArchiveCategoryAction.java,v 1.19 2006/04/11 19:50:21 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ArchiveCategoryAction.java,v $
 * Revision 1.19  2006/04/11 19:50:21  marbobo
 * + Komprimierung auf 7z-Format umgestellt
 *
 * Revision 1.18  2005/08/24 18:43:32  marbobo
 * + Systemparameterüberprüfung eingebaut
 *
 * Revision 1.17  2005/02/14 14:01:53  bigmac
 * Applikationsweites CSS konfigurierbar gemacht + Refactorings
 *
 * Revision 1.16  2004/11/10 15:02:45  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.15  2004/11/02 21:42:30  marbobo
 * + Einige Funktionen für Gast-Benutzer gesperrt
 * + WebShare.LOGIN_PAGE mit WebShare.getLoginPage() ersetzt
 *
 * Revision 1.14  2004/07/20 08:48:48  jonyp
 * added copyright info + cvs log
 *
 * Revision 1.13  2004/05/10 12:57:23  marbobo
 * + Archivierungssprache auswählbar
 *
 * Revision 1.12  2004/05/09 16:25:53  marbobo
 * + Internationalisierung
 *
 * Revision 1.11  2004/04/27 18:56:24  marbobo
 * + Virtuelle Dokumente archivieren
 *
 * Revision 1.10  2004/02/12 18:03:30  teng
 * dokumentiert
 *
 * Revision 1.9  2004/02/09 20:49:57  bigmac
 * Abfrage für globalen Administrator hinzugefügt
 *
 * Revision 1.8  2004/02/09 16:56:56  bigmac
 * Rework
 *
 */
package vkc.view.action;

import java.io.File;
import javax.servlet.ServletException;
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.entities.VKCCategory;
import vkc.db.serializer.VKCLanguageSerializer;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.misc.ArchiveCategory;
import vkc.misc.VKCLogManager;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;
import vkc.view.bean.ResourceInfo;
import vkc.view.form.ArchiveCategoryForm;

/**
 * An action class for creating zip file using the documents under a category.
 */
public class ArchiveCategoryAction extends Action {
	private ActionForward DEFAULT_FORWARD_END = new ActionForward("/templates/${template.name}/common/ArchiveCategoryTemplate.jsp");
	private ActionForward DEFAULT_FORWARD_START = new ActionForward("/templates/${template.name}/common/ArchiveCategoryInfoTemplate.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 ServletException {
		try {
			ArchiveCategoryForm form = (ArchiveCategoryForm) f;

			if (VKCSysParameterSerializer.retrieveIntValue("ARCHIVECATEGORY", "EnableArchiveCategory", 0) == 0) {
				request.setAttribute(WebShare.INFO_KEY, "vkc.info.action_not_allowed");

				return INFO_FORWARD;
			}

			if (!form.getUser().hasAdminPrivileges() || form.getUser().isGuestUser()) {
				request.setAttribute(WebShare.INFO_KEY, "vkc.info.access_denied");

				return INFO_FORWARD;
			}

			if (request.getParameter("action.create") != null) {
				return handleCreateArchiveAction(form, request);
			} else if (request.getParameter("action.back") != null) {
				// zurück zur Kategorieansicht
				return new ActionForward("/showCategory.do?categoryId=" + form.getCategoryId(), true);
			} else if (request.getParameter("action.cancel") != null) {
				// zurück zur Kategorieansicht
				return new ActionForward("/showCategory.do?categoryId=" + form.getCategoryId(), true);
			} else {
				return handleInitializeAction(form, request);
			}
		} catch (Exception e) {
			VKCLogManager.errorLog(this, "", e.getMessage(), e);
			throw new ServletException(e);
		}
	}

	private ActionForward handleInitializeAction(ArchiveCategoryForm form, HttpServletRequest request) {
		form.setIncludeVersion(true);
		form.setRecursive(true);
		
		return DEFAULT_FORWARD_START;
	}

	private ActionForward handleCreateArchiveAction(ArchiveCategoryForm form, HttpServletRequest request) throws ServletException {
		// Zip-Datei zusammenbauen
		try {
			VKCCategory cat = form.getCategory();

			File targetDirectory = form.getArchiveDirectory();

			if (!targetDirectory.exists()) {
				targetDirectory.mkdirs();
				targetDirectory.mkdir();
			}

			ResourceInfo resource = new ResourceInfo(form.getResources(), VKCLanguageSerializer.getLanguage(form.getLanguageId()).getLocale());

			File zipFile = new ArchiveCategory().createZipFile(cat, targetDirectory.getAbsolutePath(), null, form.getRecursive(), form.getIncludeVersion(), form.isVirtualDocuments(), form.getUser(), resource);
			form.setArchiveFile(zipFile);
			form.setFileSize(VKCStringUtils.formatBytes(zipFile.length(), form.getUser().getLanguage().getLanguageId().toString()));

			return DEFAULT_FORWARD_END;
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(), ex);
			throw new ServletException(ex);
		}
	}
}