<%
/******************************************************************************
 * Copyright (c) 2000-2004 e-Deal
 *
 * e-Deal S.A.
 * 55 rue Brillat Savarin
 * 75013 Paris
 * France
 *
 * T: +33 (0)1 53 80 80 30
 * F: +33 (0)1 73 01 69 77
 * http://www.e-deal.com
 * 
 * La diffusion de ce code source sous quelque forme que ce soit sans
 * l'autorisation de E-DEAL est interdite.
 *
 * Vous êtes autorisés à modifier ce code source uniquement pour votre usage
 * propre et sous réserve que les mentions de copyright demeurent intactes.
 *
 * Ce code est fourni en l'état. Aucune garantie d'aucune sorte, explicite ou
 * implicite n'est donnée. En aucun cas E-DEAL ne pourra être tenu pour
 * responsable des dommages pouvant résulter de l'utilisation de ce code
 * source.
 *
 *
 ******************************************************************************/
%>
<%@page session="true" contentType="text/html; charset=UTF-8"%>
<%@ page import="com.edeal.frontline.navigation.FlRequest" %>
<%@ page import="com.edeal.frontline.FlContext" %>
<%@ page import="com.edeal.frontline.DataDictionary" %>
<%@ page import="javax.swing.tree.DefaultMutableTreeNode" %>
<%@ page import="com.edeal.frontline.CriteriaBean" %>
<%@ page import="com.edeal.frontline.FieldNotInitializedException" %>
<%@ page import="com.edeal.frontline.FieldBean" %>
<%@ page import="com.edeal.frontline.ObjectsBean" %>
<%@ page import="java.util.Vector" %>
<%@ page import="com.edeal.frontline.Utils" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="com.edeal.frontline.EventBean" %>
<%@ page import="com.edeal.frontline.FormBean" %>
<%@ page import="java.util.Hashtable" %>
<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %>
<%
	
	final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(getClass());
	FlRequest flRequest = new FlRequest(request);
	FlContext context = flRequest.getContext();
	DataDictionary dico = context.getDataDictionary();

	String field = flRequest.getRequestParameterOrAttribute("RootFld");
	String objID = flRequest.getRequestParameterOrAttribute("ObjID");
	String pID = flRequest.getRequestParameterOrAttribute("CriID");
	
	String pNode = flRequest.getRequestParameterOrAttribute("Node");
	String pEdit = flRequest.getRequestParameterOrAttribute("Edit");
	boolean isEdit = false;
	if(pEdit == null) {
		pEdit = "0";
		isEdit = false;
	} else {
		isEdit = "1".equals(pEdit);
	}

	CriteriaBean obj = new CriteriaBean(pID, session);

	String[] fields = new String[]{"CriID"};
	String[][] query = new String[][]{{"CriFldID = null", "CriParentID = " + Utils.formatToSQL(context, pID)}};
	Vector vectCri = CriteriaBean.listSummary(context, fields, query);
%><table class="container" cellpadding="2">
	<tr valign="top">
<%	if(vectCri != null && !vectCri.isEmpty()) {
%>		<td width="1" style="background-color: #84A6AE;border: thin solid #84A6AE;"><% if(isEdit) { %><input name="CriID" type="checkbox" value="<%= obj.getID() %>"><% } else { %>&nbsp;<% } %></td>
		<td style="border: thin solid #84A6AE;"><%
		for(int i = 0; i < vectCri.size(); i++) {
			String childID = (String)((Hashtable)vectCri.elementAt(i)).get("CriID");
%>
			<span class="dataValue"><%= i == 0 ? "" : (obj.isCriAnd() ? Utils.getMessage(session, "L604B", "et") : Utils.getMessage(session, "L604C", "ou")) %></span><br>
			<% String nodeVal1 = pNode + "." + i; %>
			<jsp:include page="read.jsp" flush="true">
				<jsp:param name="CriID" value="<%= childID %>"/>
				<jsp:param name="GrpID" value="<%= obj.getID() %>"/>
				<jsp:param name="Node" value="<%= nodeVal1 %>"/>
				<jsp:param name="Edit" value="<%= pEdit %>"/>
				<jsp:param name="ObjID" value="<%= objID %>"/>
				<jsp:param name="RootFld" value="<%= field %>"/>
			</jsp:include>
<%		}
		if(isEdit) {
%>			<a href="javascript:doAddEvent('<%= obj.getID() %>', '<%= objID %>', '<%= field %>')">(+)</a><% } %>
		</td>
<%	} else {
		Vector children = obj.getChildren();
		CriteriaBean[] hCriBean = new CriteriaBean[6];
		for(int i = 0; i < children.size(); i++) {
			try {
				CriteriaBean criBean = new CriteriaBean((String)children.elementAt(i), session);
				String fldSql = dico.getFieldByID(criBean.getCriFldID()).getFldSql();
				if("PvtEventID".equalsIgnoreCase(fldSql)) {
					hCriBean[0] = criBean;
				} else if("PvtStatus".equalsIgnoreCase(fldSql)) {
					hCriBean[1] = criBean;
				} else if("PvtFrpID".equalsIgnoreCase(fldSql)) {
					hCriBean[2] = criBean;
				} else if("FrpBasicResponse0".equalsIgnoreCase(fldSql)) {
					hCriBean[3] = criBean;
				} else if("FrpBasicResponse1".equalsIgnoreCase(fldSql)) {
					hCriBean[4] = criBean;
				} else if("FrpBasicResponse2".equalsIgnoreCase(fldSql)) {
					hCriBean[5] = criBean;
				}
			} catch (Exception ex) {
			}
		}

		String[][] hDisp = new String[6][2];
		EventBean evtBean = new EventBean((String)hCriBean[0].getCriChx().firstElement(), context);
		try {
			hDisp[0] = new String[]{Utils.getMessage(session, "L1033", "Action"), Utils.formatToWeb(evtBean.getEvtLabel(), false)};
		} catch (Exception ex) {
			hDisp[0] = new String[]{Utils.getMessage(session, "L1033", "Action"), Utils.getMessage(session, "L004B", "(Non renseigné)")};
		}
		if(hCriBean[1] != null) {
			try {
				hDisp[1] = new String[]{Utils.getMessage(session, "L618B", "Statut"), Utils.formatToWeb((String)evtBean.getClass().getMethod("getEvtStatus" + (String)hCriBean[1].getCriChx().firstElement(), null).invoke(evtBean, null), false)};
			} catch(Exception ex) {
				logger.error("STACK TRACE",ex);
			}
		}
		if(hCriBean[2] != null) {
			hDisp[2] = new String[]{Utils.getMessage(session, "L618C", "Coupon réponse"), Utils.formatToWeb("<>".equalsIgnoreCase(dico.getRefCode("CriOpe", hCriBean[2].getCriOpe())) ? "oui" : "non", false)};
		}

		FormBean frmBean = null;
	
		String[][] frmQuery = new String[][]{{"FrmEventID = " + Utils.formatToSQL(context, evtBean.getEvtID())}};
		Vector vectFrm = FormBean.list(session, frmQuery);
		if(vectFrm != null && !vectFrm.isEmpty()) {
			frmBean = (FormBean)vectFrm.firstElement();
		}

		for(int i = 0; i < 3; i++) {
			if(hCriBean[3 + i] != null) {
				String strDisp = null;
				try {
					strDisp = Utils.formatToWeb((String)frmBean.getClass().getMethod("getFrmBasicQuestion" + i, null).invoke(frmBean, null), false);
				} catch (Exception ex) {
					strDisp = Utils.getMessage(session, "F6053", "Question de base") + Utils.formatToWeb(" " + (i + 1), false);
				}
				hDisp[3 + i] = new String[]{strDisp, Utils.formatToWeb("true".equalsIgnoreCase((String)hCriBean[3 + i].getCriChx().firstElement()) ? Utils.getMessage(session, "L2005", "Oui") : Utils.getMessage(session, "L2006", "Non"), false)};
			}
		}

%>		<td style="background-color: #84A6AE;border: thin solid #84A6AE;" width="1"><% if(isEdit) { %><input name="CriID" type="checkbox" value="<%= obj.getID() %>"><% } else { %>&nbsp;<% } %></td>
		<td class="dataValue" style="border: thin solid #84A6AE; width: 100%;">
			<table class="container" cellpadding="2">
<%		for(int i = 0; i < hCriBean.length; i++) {
			if(hCriBean[i] != null) {
				request.setAttribute("Criteria", hCriBean[i]);
%>				<tr style="vertical-align: top">
					<td class="dataValue" style="border: thin solid #84A6AE; width: 50%"><% if(isEdit && i == 0) { %><a href="javascript:doEditEvent('<%= obj.getID() %>', '<%= objID %>', '<%= field %>')"><% } %><%= hDisp[i][0] %><% if(isEdit) { %></a><% } %></td>
					<td class="dataValue" style="border: thin solid #84A6AE; width: 50%"><%= hDisp[i][1] %></td>
				</tr>
<%			}
		}
%>			</table>
		</td>
<%	}
%>	</tr>
</table>
