<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.util.WriterUTF8OutputStream"
%><%@page import="java.util.Properties"
%><%@page import="java.io.*"
%><%
    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;
    }
    session.setAttribute("TabIndex","-1");
    String planId = (String)request.getParameter("planId");    
    String xpdlVersion = (String)request.getParameter("version");
    if (planId == null) {
        throw new Exception("PDGraphic.jsp requires the process definition id to be specified in a 'planId' parameter.  No such parameter was found on this invocation.");
    }
    String tab = "Header.jsp";
    
    String type =   request.getParameter("type");
    if(type != null)
    {
    	tab = "AdminHeader.jsp";
    }
    else
    {
    	tab = "Header.jsp";        
        type = "xpdl";
    }
    Plan plan = bps.getPlan(Long.parseLong(planId));
 
    String filePath = application.getRealPath("/").trim();
    String fileName = filePath;

 	   try {
			   
			if (filePath.endsWith(File.separator))
				  filePath = filePath + type;
			 else
				 filePath = filePath + File.separator + type;
  
			File path = new File(filePath);
			if(path.exists() == false)
			   path.mkdirs();
			fileName = planId+"." + type;
			File file = new File(filePath + File.separator + fileName);

			FileOutputStream fout = new FileOutputStream(file);	
			
            		if(type.equalsIgnoreCase("xml")){
            		    byte[] xmlBytes = bps.getAdmin().savePlanToXML(plan.getId());
            		    fout.write(xmlBytes);
            		}
            		else {
			    if(xpdlVersion != null){
				Properties props = new Properties();
				props. setProperty("version",xpdlVersion);
				plan.convertToXPDL(fout, props);       

			    }else{
            		    plan.convertToXPDL(fout);       
            		}
            		}
			fout.close();		
		} catch (Exception e) {
			throw new Exception("Error creating XPDL ", e);
		}

%>
<html>
<head><title>Interstage Business Process Manager | Export <%=type.toUpperCase()%></title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
</head>
<body class="bodyClass" onLoad="goforit();">
<jsp:include page="<%= tab %>" flush="true"/>
<br/><br/>
  <TABLE border="0" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC" class="tableClass" width="600" align="center">
    <tr>
        
      
    <td class="headrow">Export <%=type.toUpperCase()%> for Process Definition: <%= plan.getName() %></td>
    </tr>
    <tr>
            
      
    <td bgcolor="#FFFFFF" align="center">
      <p><b><br>
        </b>Please <a href="<%= "../" + type + "/"+fileName %>"><b>Right click here</b></a> and select 
        <b>&quot;Save Target As&quot;</b> to download <%=type%> file to your local 
        system</p>
      <p> or simply <a href="<%= "../" + type + "/"+fileName %>"><b> click here</b></a> to view in 
        browser.<br>
        <br>
      </p>
      </td>
    </tr>
    <tr>
        <td valign="middle" align="center" bgcolor="#EEEEEE">
        <input type="button" value="Back" onClick="javascript:history.back();" class="submitClass" name="Button">
        </td>
    </tr>
</table>
</body>
</html>
