<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%>
<%@page import="com.fujitsu.iflow.common.ErrorMessage"%>
<%@page import="com.fujitsu.iflow.common.exceptions.IflowException"%>
<%@page import="com.fujitsu.iflow.common.wfapp.WFAppConstants"%>
<%@page import="com.fujitsu.iflow.common.wfapp.WFAppManifest"%>
<%@page import="com.fujitsu.iflow.model.wfadapter.DocRepository"%>
<%@page import="com.fujitsu.iflow.model.workflow.DMS"%>
<%@page import="com.fujitsu.iflow.model.workflow.Plan"%>
<%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"%>
<%@page import="com.fujitsu.iflow.model.workflow.WFSession"%>
<%@page import="com.fujitsu.iflow.model.util.ModelException"%>
<%@page import="com.fujitsu.iflow.model.util.ModelInternalException"%>
<%@page import="com.fujitsu.iflow.model.util.StringUTF8InputStream"%>
<%@page import="com.fujitsu.iflow.model.util.WFAppPackager"%>
<%@page import="java.io.File"%>

<%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1

    WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");
    if (wfSession == null) {
        response.sendRedirect("Login.jsp");
        return;
    }
    
    String pwd = (String)session.getAttribute("password");
    DMS dms =  WFObjectFactory.getDMS(wfSession, wfSession.getUserName(), pwd);
%>
<%
    WFAppPackager packager = new WFAppPackager();
    WFAppManifest manifest = (WFAppManifest)session.getAttribute("WFAppManifest");
    String packageName = (String)request.getParameter("PackageName");

    if(packageName != null && packageName.trim().equals("") == false)
    {
       packageName = packageName.trim();
    }
    else
    {
       packageName = manifest.getPackageName();
    }
   
    char[] invalidChars = {'\\','/',':','*','"','?','<','>','|','\'',' '};
    for(int k=0; k < invalidChars.length; k++)
        packageName = packageName.replace(invalidChars[k],'_');
    
    String tmpName = "";
    for(int i = 0; i < packageName.length(); i++)
    {
       if(Character.getNumericValue(packageName.charAt(i)) != -1 || packageName.charAt(i) == '-')
       {
          tmpName = tmpName + packageName.charAt(i);
       }
       else
       {
          tmpName = tmpName + "_";
       }
    }
    packageName = tmpName;
    manifest.setPackageName(packageName);    

    String packageRootPath = application.getRealPath("/").trim();
    if (packageRootPath.endsWith(File.separator))
        packageRootPath = packageRootPath + WFAppConstants.PACKAGE_FOLDER + File.separator + manifest.getPackageName();
    else
        packageRootPath = packageRootPath + File.separator + WFAppConstants.PACKAGE_FOLDER + File.separator + manifest.getPackageName();
    File packageRoot = new File(packageRootPath);
    if(packageRoot.exists() == false)
       packageRoot.mkdirs();
    else
    {
       packageRoot.delete();
       packageRoot.mkdirs();
    }
    
    String packagePath = packageRoot.getAbsolutePath() + File.separator + manifest.getPackageName() + 
                         packager.getArchiveStrategy().getFileSuffix();
    String packageFile = packager.packageWFApp(wfSession,dms,packagePath,manifest);
    if(packageFile.lastIndexOf("\\") >= 0)
       packageFile = packageFile.substring(packageFile.lastIndexOf("\\") + 1);
    else
       packageFile = packageFile.substring(packageFile.lastIndexOf("/") + 1);
    packageFile = "../" + WFAppConstants.PACKAGE_FOLDER + "/" + packageFile.substring(0,packageFile.lastIndexOf(".")) + "/" + packageFile;
   // response.sendRedirect("ExportWFAppPackageStatus.jsp?packageFile="+"file://"+packageFile);
%>
<script language="javascript">
    document.location.href='<%= "ExportWFAppPackageStatus.jsp?packageFile="+ packageFile.replace('\\','/') %>'
</script>



