/*
 * $Id: RepositoryAction.java,v 1.2 2004/09/20 10:26:12 koppy Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 14.07.2004
 *
 * $Log: RepositoryAction.java,v $
 * Revision 1.2  2004/09/20 10:26:12  koppy
 * +javadoc Dokumentation
 *
 * Revision 1.1  2004/08/30 14:49:57  koppy
 * Initiale Version der Workflow-Komponente
 *
 */
package vkc.workflow.web;

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 org.apache.struts.upload.FormFile;

import vkc.db.entities.VKCUser;
import vkc.util.ServletUtils;
import vkc.workflow.WorkflowException;
import vkc.workflow.WorkflowServiceFactory;
import vkc.workflow.service.VKCWorkflowService;
import vkc.workflow.service.WfMConnection;
import vkc.workflow.service.manager.PackageManager;
import vkc.workflow.service.manager.RepositoryManager;
import vkc.workflow.util.WorkflowUtils;

/**
 * This class represents a struts action for 
 * the following workflow operations:
 * <ul>
 * <li>loading/unload workflow package into engine</li>
 * <li>deleting a workflow package</li>
 * <li>uploading XPDL-files (workflow definitions)</li>
  * </ul>
 * @author Andreas Koop
 */
public class RepositoryAction extends Action {
    /** 
     * (non-Javadoc)
     * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public ActionForward execute(ActionMapping mapping, ActionForm f,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        RepositoryForm form				= (RepositoryForm)f;
        VKCUser 		user 			= ServletUtils.getUser( request );
        VKCWorkflowService wfService 	= WorkflowServiceFactory.getWorkflowService();
        RepositoryManager rep 			= wfService.getRepositoryManager();
        PackageManager pkgMan			= wfService.getPackageManager();
        WfMConnection sConn				= new WfMConnection( user.getUser(), user.getClearPassword());
        
        
        String action = form.getAction();
        String path = request.getParameter( "path");
        String pkgId = request.getParameter( "pkgId");
        
        try{
	        if( "open".equals( action ))
	            pkgMan.openPackage( sConn, path);
	        else if( "close".equals( action )){
	            pkgMan.closePackage( sConn, pkgId);
	        } else if( "delete".equals( action )){
	            rep.deletePackage(sConn, path);
	        } else if( "upload".equals( action )){
	           FormFile file = form.getWfFile();
	           rep.uploadPackage( sConn, file.getFileData(), file.getFileName().replaceAll( "\\s", "_"));
	           file.destroy();
	        }
        } catch (WorkflowException e) {
            WorkflowUtils.handle( request, e);
        }
        
        return new ActionForward( "/setupRepository.do", false);
    }
}
