<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMSessionImpl"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMAdminImpl"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.workflow.DMS"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstantiator"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstance"
%><%@page import="com.fujitsu.iflow.model.wfadapter.DocRepository"
%><%@page import="com.fujitsu.iflow.model.workflow.AttachmentRef"
%><%@page import="com.fujitsu.iflow.common.exceptions.IflowException"
%><%@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.QuickForm"
%><%@page import="com.fujitsu.iflow.common.ErrorMessage"
%><%@page import="com.fujitsu.iflow.model.util.*"
%><%@page import="java.util.*"
%><%@page import="java.io.File"
%><%@page import="java.io.OutputStream"
%><%@page import="java.io.ByteArrayInputStream"
%><%@page import="java.io.FileOutputStream"
%><%@page import="java.io.OutputStreamWriter"
%><%@page import="java.io.InputStream"
%><%@page import="java.io.InputStreamReader"
%><%@page import="java.io.Writer"
%><%@page import="java.net.URLEncoder"
%><%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    
    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }
    
    String type =   request.getParameter("type");
    if(type == null)
        type = "xpdl";

%><jsp:useBean id="myUpload" scope="page" class="com.fujitsu.iflow.model.util.Upload" /><%

    myUpload.initialize(pageContext);
    myUpload.upload();
    UploadFile firstFile = myUpload.getFiles().getFile(0);
    String fileName = firstFile.getFileName();
    String fileType = firstFile.getContentType();
    String fileSubType = firstFile.getContentDisp();

    // OK, stupid upload class does not convert to a string correctly.
    // so can't use getContentString.
    // it does not know the character encoding, so it can't;
    // BUT it also does not give an array oriented access to the
    // internal byte array, so I have to loop and get it byte by byte.

    //int fileSize = firstFile.getSize();
    //byte[] buf = new byte[fileSize];
    //for (int i=0; i<fileSize; i++) {
    //    buf[i] = firstFile.getBinaryData(i);
    //}

    //for testing
    //InputStream bais = new JspDumbInputStream(firstFile);
    //copyInputToFile(bais, "C:\\temp\\innout.txt");

    InputStream bais = new JspDumbInputStream(firstFile);
    Plan newPlan = null;
    if(type.equalsIgnoreCase("xml"))
    {
        byte[] fbuf = new byte[512];
        int read;
        String xmlString = "";
        while((read = bais.read(fbuf)) != -1)
        {
            xmlString = xmlString + new String(fbuf,0,read,"UTF-8");
        }
        newPlan = ((BPMAdminImpl)bps.getAdmin())._wfas.createPlanFromXML(xmlString);
    }
    else {
        newPlan = WFObjectFactory.getPlan();
        newPlan.setWFSession(((BPMSessionImpl)bps)._wfs);
        newPlan.convertFromXPDL(bais);
        newPlan.createProcessDef();
    }
    response.sendRedirect("TemplateList.jsp");
%>


<%!

    class JspDumbInputStream extends InputStream
    {

        UploadFile attachment;
        int pointer = 0;
        int fileSize = 0;

        public
        JspDumbInputStream(UploadFile dumbFile)
        {
            attachment = dumbFile;
            pointer = 0;
            fileSize = dumbFile.getSize();
        }

        public
        int
        read()
        {
            if (pointer >= fileSize) {
                return -1;
            }
            return (attachment.getBinaryData(pointer++) & 255);
        }
    }

    public
    void
    copyInputToFile(InputStream is, String fileName)
        throws Exception
    {
        File outFile = new File(fileName);
        OutputStream os = new FileOutputStream(outFile);

        int byteBuf = is.read();
        while (byteBuf >= 0)
        {
            os.write(byteBuf);
            byteBuf = is.read();
        }

        os.flush();
        os.close();
    }

%>
