<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="AdminException.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@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.wfcore.WFAdminSessionImpl"
%><%@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"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMAdmin"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMAdminImpl"
%><%
    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;
    }

    BPMAdmin bpas = bps.getAdmin();
    WFAdminSessionImpl wfSession = ((BPMAdminImpl)bpas)._wfas;

    String returnMsg = "Problems in importing XPDL.";
    long id = -1;

%><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();

    InputStream bais = new JspDumbInputStream(firstFile);
    Plan newPlan = WFObjectFactory.getPlan();
    newPlan.setWFSession(wfSession);
    newPlan.convertFromXPDL(bais);
    Plan createdPlan = newPlan.createProcessDef();
    id = createdPlan.getId();
    System.out.println("ID " + id);
    if (id != -1)
    returnMsg = "Process Definition with id " + id + " created from " + fileName;
%>
<html>
<head>
<title>Interstage Business Process Manager | Import XPDL</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
</head>
<body class="adminBodyClass">
<script>

function gotoPlanList()
{
    document.location.href='SortedPlanList.jsp';
}

</script>
<center>
<jsp:include page="AdminHeader.jsp" flush="true"/>

       <TABLE  border="0" cellpadding="2" cellspacing="1" class="tableClass" width="75%" align="center">
            <tr><td>&nbsp;</td></tr>
            <tr><td align="center">
            <img src="images/icons/info_excep.gif" border="0"><br/></td></tr>
            <tr><td>&nbsp;</td></tr>
        <tr align="center" class="headrow">
            <td bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2"><%= returnMsg %></font></td>
            </tr><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>
            <tr><td>&nbsp;</td></tr>
            <tr><td align ="center">
            <input type="button" onClick="gotoPlanList()" value="Process Definition List" name="check" class="core-button">
            </td></tr>
          </TABLE>
 </center>
</body>
</html>

<%!

    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();
    }

    public WFAdminSessionImpl getiFlowSession(Object modelObject)
    {
            return (WFAdminSessionImpl)(modelObject);
    }

%>
