/*
 * $Id: ArchiveProjectAction.java,v 1.2 2007/10/12 12:38:46 korowa Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ArchiveProjectAction.java,v $
 * Revision 1.2  2007/10/12 12:38:46  korowa
 * Beim Archivieren eines Projektes wird im Anschluss ein Link angeboten um das Archiv runterzuladen
 *
 * Revision 1.1  2006/10/04 03:14:47  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.11  2006/09/18 13:54:21  marbobo
 * + Archivierungordner wird jetzt aus den Systemparametern gelesen
 *
 * Revision 1.10  2006/05/09 15:28:59  marbobo
 * + Refactoring
 *
 * Revision 1.9  2005/09/22 13:18:20  marbobo
 * + Bugfix beim löschen von Kategorien und Dokumenten
 *
 * Revision 1.8  2005/09/01 16:31:31  jonyp
 * changes due to new signature of VKCCategory.deleteCategory/VKCDocument.deleteDocument
 *
 * Revision 1.7  2005/08/26 14:54:29  jonyp
 * changes due to new paramter(user) for deleteDocument
 *
 * Revision 1.6  2004/11/02 21:42:31  marbobo
 * + Einige Funktionen für Gast-Benutzer gesperrt
 * + WebShare.LOGIN_PAGE mit WebShare.getLoginPage() ersetzt
 *
 * Revision 1.5  2004/07/27 14:55:48  jonyp
 * added new parameter for deletion of categories (definite = true)
 *
 * Revision 1.4  2004/07/20 09:00:52  jonyp
 * added copyright info + cvs log
 *
 */
package vkc.procoba.project;

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.serializer.VKCCategorySerializer;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.misc.ArchiveCategory;
import vkc.procoba.project.entities.VKCProject;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;
import vkc.view.bean.ResourceInfo;

public class ArchiveProjectAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/procoba/project/ArchiveProject.jsp", false);

	public ActionForward execute(ActionMapping mapping, 
								 ActionForm f,
								 HttpServletRequest request, 
								 HttpServletResponse response)
	throws ServletException {
		try {
			if (!VKCSysParameterSerializer.isProjectManagementEnabled()) {
				return new ActionForward("/showCategory.do", true);
			}

			ArchiveProjectForm form = (ArchiveProjectForm) f;

			VKCProject project = (VKCProject) VKCCategorySerializer.retrieveCategory(form.getProjectId());

			form.setProjectName(project.getName());

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

			File targetDirectory = file.getAbsoluteFile();

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

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

			File zipFile = new ArchiveCategory().createZipFile(project,
															   targetDirectory.getAbsolutePath(), 
															   null, 
															   true, 
															   true, 
															   true,
															   form.getUser(), 
															   resource);
			form.setArchiveFilename(zipFile.getName());
			form.setArchiveFile(zipFile);
			form.setFileSize(VKCStringUtils.formatBytes(zipFile.length(), form.getUser().getLanguage().getLanguageId().toString()));
			VKCCategorySerializer.deleteCategory(form.getUser(), form.getProjectId(), !WebShare.isTrashEnabled(), null);

			return DEFAULT_FORWARD;
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}
}