<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="AdminException.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFAdminSession"
%><%@page import="com.fujitsu.iflow.model.wfcore.WFAdminSessionImpl"
%><%@page import="com.fujitsu.iflow.model.workflow.DMS"
%><%@page import="com.fujitsu.iflow.model.wfadapter.DocRepository"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstance"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectList"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.server.dataobjects.ProcessInstanceStatesEnum"
%><%@page import="com.fujitsu.iflow.server.dataobjects.ProcessDefinitionStatesEnum"
%><%@page import="com.fujitsu.iflow.server.dataobjects.FilterEnum"
%><%@page import="com.fujitsu.iflow.model.workflow.Filter"
%><%@page import="java.util.Date"
%><%@page import="java.util.Properties"
%><%@page import="java.util.Enumeration"
%><%@page import="java.text.SimpleDateFormat"
%><%@page import="java.io.Writer"
%><%@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 = null;

    String processId = request.getParameter("processId");
    if (processId == null) {
        processId = (String) session.getAttribute("processId");
    }
    long id = Long.parseLong(processId);

    String path = request.getParameter("path");

    String pwd = (String)session.getAttribute("password");

    returnMsg = saveProcessAsXML(wfSession, id, path, pwd);

    session.setAttribute("TabIndex","1");

%>

<html>
<head>
<title>Interstage Business Process Manager | Process Instance List</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
</head>

<body class="adminBodyClass">
<script>

function gotoProcessList()
{
    document.location.href='SortedProcessList.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="gotoProcessList()" value="Process Instance List" name="check" class="core-button">
            </td></tr>
          </TABLE>
</center>
</html>

<%!
    public String saveProcessAsXML(WFAdminSessionImpl wfSession, long id, String path, String password) throws Exception
    {
    String xml = wfSession.saveProcessToXML(id);
    byte[] xmlBytes = xml.getBytes("UTF-8");
        DocRepository docRepos = getDocRepository(wfSession, password);
        docRepos.checkInNew(replaceWithBackSlash(path), xmlBytes, true);

        return "Process instance with ID " + id + " is saved as XML successfully";

    }

    public
    DocRepository
    getDocRepository(
        WFAdminSessionImpl wfSession,
        String password)
        throws Exception
    {
        DocRepository docRepository = null;
        Properties sysProp = System.getProperties();
        String docRoot = sysProp.getProperty("user.home");
        DMS dms =  WFObjectFactory.getDMS(wfSession, wfSession.getUserName(), password);
        docRepository = WFObjectFactory.getDocRepository(dms, docRoot);

        return docRepository;
    }


    public String doNothing() throws Exception
    {
        return "this does nothing";
    }

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

    private String replaceWithBackSlash(String str)
    {
        return replace(str, "/", "\\");
    }

    private String replace(String str, String original, String replacement)
    {
        StringBuffer sb = new StringBuffer(str);
        for (int i=0; i<sb.length(); i++)
        {
            if (sb.substring(i,i+1).equals(original))
                sb.replace(i,i+1,replacement);
        }
        return sb.toString();
    }

%>
