<%/******************************************************************************
 * 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"%>
<%@ page import="com.edeal.frontline.navigation.FlRequest" %>
<%@ page import="com.edeal.frontline.FlContext" %>
<%@ page import="com.edeal.frontline.Utils" %>
<%@ page import="com.edeal.frontline.FieldNotInitializedException" %>
<%@ page import="com.edeal.frontline.services.FlServiceException"%>
<%@ page import="com.edeal.frontline.DataDictionary" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="com.edeal.frontline.FrontlineException" %>
<%@ page import="java.util.Vector" %>
<%@ page import="com.edeal.frontline.WorkflowBean" %>
<%@ page import="com.edeal.frontline.AttachmentBean"%>
<%@ page import="com.edeal.frontline.helper.GraphVizHelper" %>
<%@ page import="com.edeal.frontline.helper.custom.WorkflowHelper" %>
<%@ page import="org.apache.axis.encoding.Base64" %>
<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %>
<jsp:useBean class="com.edeal.frontline.WorkflowBean" id="Workflow" scope="request"/>
<%
	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 id = null;
	try {
		id = Workflow.getID();
	} catch (FieldNotInitializedException e) {
	}
	
	boolean hasGraphVizService;
	try{
		context.getServiceManager().getServiceClient("GraphViz");
		hasGraphVizService = true;
	}catch(FlServiceException e){	
		hasGraphVizService = false;
	}
	
	String workflowGraph = flRequest.getRequestParameterOrAttribute("WkfGraph");
	boolean noImage = false;
	AttachmentBean img = new AttachmentBean(context);
	String map = "";
	String img64 = "";
	String serviceError = "";
	if(Utils.isEmpty(workflowGraph)){
		noImage = true;
	}else{
		if(hasGraphVizService){
			StringBuilder graphSource = new StringBuilder();
			graphSource.append(workflowGraph);
			GraphVizHelper gv = new GraphVizHelper();
			gv.setDotSource(graphSource);			
			try{
				img = WorkflowHelper.getGraphAsAnImg(context, gv.getDotSource(), "png");
				Base64 base64 = new Base64();
				img64 = base64.encode(img.getAttFile());
			
				map = WorkflowHelper.getGraphAsAMap(context, gv.getDotSource(), "cmapx");
				if(map != null){
					map = map.replaceAll("\\n\\n\\n\\n", "");
					map = map.replace("\\n", " ");
				}
			}catch(FrontlineException e){
				serviceError = e.getMessage();
			}
		}	
	}
%>
<script type="text/javascript" src="<fl:webapp/>/js/jquery-url-params.js"></script>
<script type="text/javascript" language="javascript">
function doSubmit()
{
	presentFieldsValue();
	document.forms[0].submit();
}

function openLayer(node){
	$.openPopupLayer({
		name: "GraphPopup",
		width: "auto",
		height: "auto",
		url: "<fl:webapp/>/graph_design.fl?node="+node+"&obj="+$('#WkfObjID').val(),
		cache: false,
        error: function () {
			alert("Erreur lors de l'ouverture de la popup !");
	    }
	});
}

function openTransitionLayer(id){
	$.openPopupLayer({
		name: "GraphPopup",
		width: "auto",
		height: "auto",
		url: "<fl:webapp/>/graph_edit_transition.fl?id="+id+"&obj="+$('#WkfObjID').val(),
		cache: false,
        error: function () {
			alert("Erreur lors de l'ouverture de la popup !");
	    }
	});
}

function initWorkflow(){
	$.openPopupLayer({
		name: "GraphPopup",
		width: "auto",
		height: "auto",
		url: "<fl:webapp/>/graph_initialize.fl?obj="+$('#WkfObjID').val(),
		cache: false,
        error: function () {
			alert("Erreur lors de l'ouverture de la popup !");
	    }
	});
}

function refresh(val){
	url = document.location.href;
	url = $.setUrlVar(url, "getFromSession", val);
	url = $.setUrlVar(url, "initWkfObjID", $('#WkfObjID').val());
	url = $.setUrlVar(url, "initWkfName", $('#WkfName').val());
	url = $.setUrlVar(url, "initWkfStatus", $('#WkfStatus').val());
	url = $.setUrlVar(url, "initWkfDescription", $('#WkfDescription').val());
	document.location.href = url;
}

function submitAndRefresh(getFromSession){
	document.forms[0].action = "<fl:webapp/>/refresh_workflow.fl";
	document.forms[0].getFromSession.value = getFromSession;
	document.forms[0].submit();
}

$(function(){
	$('#graphContainer').attr('style', 'overflow-y:hidden;overflow-x:auto;width: '+($(window).width()-290)+'px');
});
</script>

<fl:form action="/save_workflow.fl" bean="Workflow" method="POST">
<input type="hidden"  id="getFromSession" name="getFromSession" value=""/>
<fl:bigTabs objectTitle="<%= Utils.getMessage(session, \"L65E6\", false, true, \"Workflow\") %>" ou="<%= \"/edit_workflow.fl\" + (id != null ? \"?id=\" + id : \"\") %>" height="700px" showFooter="false">	
	<table class="container" cellpadding="0">
		<tr class="fieldline">
			<td colspan="3">
				<table class="container" cellpadding="0">
					<tr class="fieldline">
						<td class="labelTitreFiche">
							<fl:label field="WkfName" labelClassName="titreFiche"/>
						</td>
						<td class="fieldTitreFiche">
							<fl:input property="WkfName" mandatoryMsgCode="L6294" alt="" unicityMsgCode="M60A1" />
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr><td colspan="3" class="ligne_separation"/></tr>
		<tr class="fieldline">		
			<td colspan="3">
				<fl:autodisplay name="Workflow" zone="WorkflowMain" edit="true"/>
			</td>
		</tr>
	</table>
	<table class="container" cellpadding="0">
		<tr class="ligne_separation">
			<td></td>
		</tr>
		<tr>
			<td class="titrePartie"><fl:getMessage code="__L2007" alt="Description"/></td>
		</tr>
		<tr class="ligne_separation_couleur">
			<td></td>
		</tr>
		<tr class="ligne_separation">
			<td></td>
		</tr>
		<tr>
			<td><fl:input property="WkfDescription" nodiv="false" /></td>
		</tr>
		<tr class="ligne_separation">
			<td></td>
		</tr>
	</table>
	<link href="<fl:webapp/>/css/smalltabs_css.jsp" rel="stylesheet" type="text/css">
	<fl:smallTabs name="DETAIL">
		<fl:sTab id="wflow1Graph" title="Graphe" onselect="submitAndRefresh(0);">
			<div id="graphContainer" align="center">
				<%if(noImage){ %>
					<a href="#" onclick="initWorkflow();"><img src="<fl:webapp/>/icons/close.png"/></a>
				<%}else if(!"".equals(serviceError)){ %>
					<img src="<fl:webapp/>/js/progressbar/images/failure.png" title="<%=serviceError %>"/>
				<%}else{ %>
					<img src="data:<%=img.getAttContentType()%>;base64,<%=img64 %>"	usemap="#G"/>
					<%=map %>
				<%} %>
			</div>
		</fl:sTab>
		<fl:sTab id="wflow2Source" title="Source">
			<table class="container" cellpadding="0">
				<tr class="ligne_separation">
					<td></td>
				</tr>
				<tr>
				<td class="titrePartie"><fl:getMessage code="F203D" alt="Contenu"/></td>
				</tr>
				<tr class="ligne_separation_couleur">
					<td></td>
				</tr>
				<tr class="ligne_separation">
					<td></td>
				</tr>
				<tr>
					<td><fl:input property="WkfContent" nodiv="true" mandatoryMsgCode="M60E4" height="450px"/></td>
				</tr>
				<tr class="ligne_separation">
					<td></td>
				</tr>
			</table>
		</fl:sTab>
	</fl:smallTabs>	
</fl:bigTabs>
</fl:form>
