<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" session="true"%>
<jsp:useBean id="listVerb" scope="session"
	class="java.lang.String" />	    
<jsp:useBean id="reqVerb" scope="session"
	class="java.lang.String" />	    	
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="java.util.ResourceBundle"%>
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page import="org.apache.log4j.Logger"%>
<%@page import="org.injac.oai.util.XMLStreamParser"%>
<%@page import="org.injac.oai.util.XSLTransformer"%>
<%@page import="org.injac.oai.ihm.Verbs"%>
<%@page import="org.injac.oai.ihm.OAIHTMLMenu"%>
<!-- main GUI file for testing OAI storage 
	values for index parameter :
	0 Identify
	1 ListSets
	2 ListMetadataFormats
	3 ListRecods/ListIndetifiers from sets sub-menu
	4 ListRecords/ListIndetifiers from formats sub-menu
	5 Verbs from query

-->
<%!	

static Logger logger = Logger.getLogger("INJAC_OAI");
	
	static String[] menus ={"Identité entrepôt","Liste des ensembles(set)","Liste des formats"};
	static String[] submenus ={"","Enregistrements par ensembles","Enregistrements par formats"};
	static String[] menusVERBS ={"Identify","ListSets","ListMetadataFormats"};
	static String mainIHMxsl="xsl/ihm_main.xsl";
	static String menuIHMxsl="leftmenu.xsl";
	private Verbs verbs;
	public void jspInit() {
		// init resource for configuration file
		try{
		
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		verbs = new Verbs();
	}

%>

<%// initialization 
if(listVerb.equals("")){
	listVerb="ListIdentifiers";
}
if(reqVerb.equals("")){
	reqVerb= Verbs.verbs[0];
}

String stringIndex= request.getParameter("index");
String verb= request.getParameter("verb");
String baseURL = request.getRequestURL().toString();
String realPath = getServletContext().getRealPath("/");
// recreates menu for each page load
OAIHTMLMenu htmlMenu = new OAIHTMLMenu(realPath+"xsl", baseURL+"OAIHandler");
String xml = null;
InputStream xmlStream = null;
String xslFile = null;
String includeHtml = "";
XSLTransformer xslt;
int index = 0;
if( stringIndex!=null){
	try{
		index = Integer.parseInt(stringIndex);
		if(index > 6 || index < 0){
			throw new Exception("Index out of bound : "+index);
		}
	}catch(Exception e){
		logger.error(e.getMessage());
		index=0;
	}
}
// a verb cancel menu index
if((verb!=null) && (index!=6)){
	String oaiRequest = baseURL+"OAIHandler?"+ verbs.getOAIParamsFromRequest(request);
	xslFile=realPath+mainIHMxsl;
	XMLStreamParser parser = new XMLStreamParser();
	xml= null;
	xmlStream = parser.getStreamFromURL(oaiRequest);
}
// select menu index


// listVerb update
if(index==3)
	listVerb=verb;
//listVerb update
if((index==6) || (index==5))
	reqVerb=verb;
%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Entrepôt OAI Injac</title>
<meta http-equiv="Content-Type" content="text/html; "/>
<link href="css/style_1.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="page">
<div id="header">
<h1><img src="images/logo_haut_1.gif" height="98" width="799"></h1>
</div>
<div id="login">
<h1>Entrepôt OAI Injac</h1>
</div>

<div id="menu">
<div id="navbar"></div>

<div id="sectionMenu">
<div id="iNavPan">
<h2>Menu</h2>
<ul class="ispaces">
<% 
//affichage du menu principal
for(int i =0;i<menus.length;i++){
%>		
	
	<li class="<%=(i==index?"seldoc":"doc") %>"><a title="Accueil"
		href="<%="?index="+i %>" > <%= menus[i]%></a></li>
		
<%	} %>
</ul>
<%
// affichage du sous menu (liste des sets et des formats)
	
// drawing sub-choices menu 


String menuIncludeHtml = htmlMenu.getListsMenu(menuIHMxsl, index, listVerb, request); 
%>		
<%= menuIncludeHtml %>	
<%
// init xml/xslFile for further insertion
switch(index){
case 0:
	XMLStreamParser parser = new XMLStreamParser();
	String OAIrequest = baseURL+"OAIHandler?verb="+menusVERBS[index];
	xml= parser.getStringFromURL(OAIrequest);
	xslFile =  realPath+mainIHMxsl;		
	break;
case 1:
	xml = htmlMenu.getXmlSets();
	xslFile =  realPath+mainIHMxsl;	
	break;
case 2:
	xml = htmlMenu.getXmlFormats();
	xslFile =  realPath+mainIHMxsl;	
	break;
default: // cases managed by xmlStream
	break;
}	

// affichage requête dynamique
%>

<h2>Requête OAI</h2>
<%

String reqHTML = htmlMenu.getOAIRequestForm("request.xsl", reqVerb, request);

%>	
<%= reqHTML %>

</div>
</div>
</div>
<div id="iDocRender">
<!-- affichage option choisie -->
<% 
	if(xslFile != null && xml != null){
		xslt = new XSLTransformer(xslFile);
		htmlMenu.setOAIParametersFromRequest(xslt,request);
		includeHtml = xslt.transform(xml);
	
%>
<%= includeHtml %>
<%  }
	else if(xslFile != null && xmlStream != null){
		xslt = new XSLTransformer(xslFile);
		xslt.setParameter("index", ""+index);
		htmlMenu.setOAIParametersFromRequest(xslt,request);
		includeHtml = xslt.transform(xmlStream);

%>
<%= includeHtml %>
<%  } 
%>

<!-- Fin affichage option choisie -->
</div>
</div>
</body>
</html>

