<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%@ include file="nlsResource.jsp" %>
<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.*, java.util.*, jet.cs.util.*, jet.server.api.*, jet.server.api.http.*, jet.server.log.*" %>
<%@ page errorPage="errorpage.jsp" %>
<%@ include file="AuthCheck.jsp" %>
<%

/**
 * This JSP shows how to print reports by using the ViewerApplet.
 * The example JSP printCustomerlist.jsp calls this JSP.
 * Usage:
 * Copy this file, printCustomerlist.jsp and printDemo.jsp into <server_install_home>/public_html/jinfonet. 
 * Then start standalone report server and type url like 
 * "http://localhost:8888/jinfonet/printDemo.jsp".
 */

	try{

		//get properties for running the report
		Properties ht = new Properties();

		Properties htTmp = new Properties();
		String name, value;
		String values[];
		Enumeration enu = request.getParameterNames();
		while (enu.hasMoreElements()) {
			name = (String)enu.nextElement();
			values = request.getParameterValues(name);
			int sz = values.length;
			value = null;
			htTmp.clear();
			for (int i = 0; i < sz; i++) {
				if (i == 0) {
					value = values[0];
					htTmp.put(values[0], "");
				} else {
					if (htTmp.get(values[i]) == null) {
						value += ";" + values[i];
						htTmp.put(values[i], "");
					}
				}
			}
			if (value != null) {
				ht.put(name, value);
			}
		}

		HttpRptServer httpRptServer = HttpUtil.getHttpRptServer();

		String user = HttpUtil.getUser(request);

		String rst = null;

		String cat = request.getParameter(APIConst.TAG_CATALOG);
		String rptName = request.getParameter(APIConst.TAG_REPORT);
		
		//run the report
		rst = httpRptServer.runReport(user, cat, rptName, ht);

		System.out.println("===================rst="+rst);
		if(rst == null) {
			response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, "result file is null!");
		}else{
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
body {
	line-height: 150%;
	color: #000000;
	font-family: Tahoma, Verdana, Arial, sans-serif;
	font-size: 12px;
}
</style>
<title>Viewer Applet Print</title>
</head>
<%
	//HttpRptServer httpRptServer = HttpUtil.getHttpRptServer();

    rst = new File(rst).getName(); //request.getParameter("jrs.filename");
    httpRptServer.getTempResultOwnerManager().registerOwner(user, HttpUtil.getTempResultKey(rst));
    boolean isTempRslt = true; //"true".equals(request.getParameter("tempResult"));

System.out.println("===================request.getParameter(isPrint)="+request.getParameter("isPrint"));
    boolean isPrint = "true".equals(request.getParameter("isPrint"));
				System.out.println("===================isPrint="+isPrint);

	int applettype = 0;
	
	try{
	    applettype = APIUtil.parseInt(request.getParameter(APIConst.TAG_APPLET_TYPE), APIConst.APPPLET_JAVA_PLUG_IN_1_3);
	    switch(applettype){
	    case APIConst.APPPLET_JAVA_PLUG_IN_1_2:
	    case  APIConst.APPPLET_JAVA_PLUG_IN_1_3:
	        break;
	    default:
%>	    
Unknown applet type: <%= applettype %>
<%
			return;
		}
	}catch(Throwable e){
%>	
Unknown applet type <%= request.getParameter(APIConst.TAG_APPLET_TYPE) %>
<%
	    return;
	}
	
	String s = request.getParameter(APIConst.TAG_FRAMEAPPLET);
	boolean toFrame = (s != null && s.trim().equalsIgnoreCase("on"));
	
	// create archive param for addtional jars
	String addJars = "";
	boolean hasUDO12Jar = false;
	Vector udo12jars = new Vector(3);
	// FIXME:  here should be codebase
	String home = httpRptServer.getServerEnv().getHomePath();
	if( !home.endsWith(File.separator) ){
		home += home.endsWith(File.separator);
	}
	File udo = new File(new File(home+"lib"), "udo");
	if(udo.isDirectory()){
	    String[] list = udo.list();
	    for(int i = 0; i < list.length; i ++){
			if (list[i].equalsIgnoreCase("udo12.jar")) {
				hasUDO12Jar = true;
				udo12jars.addElement(list[i]);
			} else {
				addJars += ",lib/udo/"+list[i];
			}
	    }
	}
	
	if (hasUDO12Jar) {
		int sz = udo12jars.size();
		for (int i = 0; i < sz; i++) {
			addJars = ",lib/udo/"+ (String)udo12jars.elementAt(i) + addJars;
		}
	}
	
	
	String archive	= "";  
	switch (applettype) {
		case  APIConst.APPPLET_JAVA_PLUG_IN_1_2:
		case  APIConst.APPPLET_JAVA_PLUG_IN_1_3:
		    archive	+= "lib/viewer12.jar";
		    break;
		default:
		    // It should not go here, we checked before
    }
	archive += addJars + ",lib/log4j-1.2.8.jar";
	String codebase	= HttpUtil.getWebAppUrl(request, HttpUtil.getJSPWebAppName(APIConst.APPNAME_SENDFILE));
%>
	
<p>Please wait for loading applet of JReport Viewer Frame ...
<hr>

<%
	String userAgent = request.getHeader("User-Agent");
	boolean isIE = (userAgent!=null && userAgent.indexOf("MSIE")>=0);
	
	ReportInfo rptInfo = httpRptServer.getReportInfo(user, cat, -1, rptName, -1, null);        
	String sheet = null;
	try {
		ReportSheetInfo curRptSheet = rptInfo.getCurrentReportSheet();
		if (curRptSheet != null) {
		    sheet = curRptSheet.getName();  
		}
    } catch (RptServerException e) {
    	e.printStackTrace();
    }	

	String reportName = HttpUtil.getWebAppUrl(request, HttpUtil.getJSPWebAppName(APIConst.APPNAME_SENDFILE)) + 
			rptName + "?" + 
			APIConst.TAG_CMD + "=" + 
			(isTempRslt ? APIConst.CMD_GET_TEMP_RESULT : 
			APIConst.CMD_GET_VER_RESULT ) + 
			"&" + APIConst.TAG_FILE + "=" + HttpUtil.encodeEsc(rst)
			+ "&" + APIConst.TAG_REPORT_SHEET + "=" + sheet;	

	if (isIE) {
%>		
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
    width   = 200  
    height  = 200 
    name    = "JReport Viewer Applet for JReport Enterprise Server"
    alt     = "JReport Viewer Applet for JReport Enterprise Server"
<%    
    		if ( applettype == APIConst.APPPLET_JAVA_PLUG_IN_1_2 ) {
%>    		
    codebase="http://java.sun.com/products/plugin/1.2/jinstall-12-win32.cab#Version=1,2,0,0"
<%    
    		} else {
%>    		
    codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0"
<%    
    		}
%>    		
>
<PARAM NAME = CODE VALUE = "jet.swingviewer.ViewerApplet" >
<PARAM NAME = CODEBASE VALUE = "<%= codebase %>" >
<PARAM NAME = ARCHIVE VALUE = "<%= archive %>" >
<PARAM NAME = NAME VALUE = "JReport Viewer Applet for JReport Enterprise Server" >
<PARAM NAME="type" VALUE="application/x-java-applet;version=<%= applettype == APIConst.APPPLET_JAVA_PLUG_IN_1_2 ? "1.2" : "1.3" %>">
<PARAM name = "PopFrame" value = "<%= toFrame %>">
<PARAM name = "reportName" value = "<%= reportName %>">
<%	       
			String cookie = request.getHeader("Cookie");
			if(cookie != null){
%>	
<PARAM name = "sessionId" value = "<%= cookie %>">
<%
			}
%>	
<PARAM name = "HSInterval" value = "99999">

<PARAM name = "action" value = "<%= isPrint ? "print" : "" %>">
<PARAM name = "interactive" value = "<%= APIUtil.getBoolean(request.getParameter("interactive"), false)%>">
<PARAM name = "background" value = "<%= APIUtil.getBoolean(request.getParameter("background"), false)%>">
<PARAM name = "useJDK11" value = "<%= APIUtil.getBoolean(request.getParameter("useJDK11"), false)%>">
<PARAM name = "wait" value = "<%= APIUtil.getBoolean(request.getParameter("wait"), false)%>">
<PARAM name = "separateLargePage" value = "<%= APIUtil.getBoolean(request.getParameter("separateLargePage"), false)%>">
<PARAM name = "notifyComplete" value = "<%= APIUtil.getBoolean(request.getParameter("notifyComplete"), true)%>">
<PARAM name = "printer" value = "<%= request.getParameter("printer")%>">

Your browser does not support Applet
</OBJECT>
<%
	} else {	// Netscape
%>		
<EMBED 
    type="application/x-java-applet;version=<%= applettype == APIConst.APPPLET_JAVA_PLUG_IN_1_2 ? "1.2" : "1.3" %>"
    java_CODE = "jet.swingviewer.ViewerApplet" 
    java_CODEBASE = "<%= codebase %>"
    java_ARCHIVE = "<%= archive %>" 
    ALT = "JReport Viewer Applet for JReport Enterprise Server" 
    NAME = "JReport Viewer Applet for JReport Enterprise Server" 
    WIDTH = 200
    HEIGHT = 200
    pluginspage="http://java.sun.com/products/plugin/download/windows.html"

	PopFrame = "<%= toFrame %>"
	reportName = "<%= reportName %>"
<%	       
			String cookie = request.getHeader("Cookie");
			if(cookie != null){
%>	
	sessionId = "<%= cookie %>"
<%
			}
%>	
	HSInterval = "99999"
	action = "<%= isPrint ? "print" : "" %>"
	interactive = "<%= APIUtil.getBoolean(request.getParameter("interactive"), false)%>"
	background = "<%= APIUtil.getBoolean(request.getParameter("background"), false)%>"
	useJDK11 = "<%= APIUtil.getBoolean(request.getParameter("useJDK11"), false)%>"
	wait = "<%= APIUtil.getBoolean(request.getParameter("wait"), false)%>"
	separateLargePage = "<%= APIUtil.getBoolean(request.getParameter("separateLargePage"), false)%>"
	notifyComplete = "<%= APIUtil.getBoolean(request.getParameter("notifyComplete"), true)%>"
	printer = "<%= request.getParameter("printer")%>">
>
<NOEMBED>
Your browser does not support Applet
</NOEMBED>
</EMBED>
<%
	}
%>
<p>JReport Viewer is running ...
<hr>
</body>
</html>
<%
		}
	} catch(Throwable t) {
		ServiceLogger.logError(t);
		response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, t.getMessage());
	}
%>
