<!DOCTYPE html>
<%
    String url = request.getRequestURL().toString();
%>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>NLP processing demo</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
	
	 <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
	<script src="http://gregpike.net/demos/bootstrap-file-input/bootstrap.file-input.js"></script>
	
	
  </head>
     <body role="document">
    
     <div class="container theme-showcase jumbotron" role="main">
           <div class="row">
             <div class="col-md-5">
        <form id="load_form" method="post" >
          <textarea name="file" cols=50 rows=38>
<?xml version="1.0" encoding="UTF-8"?>
<lpmn>
<meta type="description">Opis </meta>

<source id="1">
   <path>/home/tomekw/engine</path> 
</source>

<activity name="wcrft2" id="2">
  <input  id="1" source="1"/>
</activity>

<gateway type="parallel">
    <input source="2"/>
    <output id="3"/>
    <output id="4"/>
</gateway>

<activity name="wsd" id="5">
  <input   source="3"/>
</activity>

<activity name="chunker" id="6">
  <input   source="4"/>
</activity>

<output id="7">
  <input source="5"/>
   <destination>/res/wsd</destination> 
</output>

<output id="8">
  <input source="6"/>
   <destination>/res/chunker</destination> 
</output>
<event when="finished" type="e-mail" address="twalkow@gmail.com">
          Processing of files has finished
</event>
</lpmn >
          </textarea></br>
           <input type="submit" class="btn-success pull-left" role="button"  value="Start">
         </form>
    </div>
          <div class="col-md-2">
          <textarea id="log" cols=50 rows=38 readonly="readonly">...</textarea>
          <div id="status">status</div>
          </div> </div>
       </div>
            
            
<script>
   $("#log").val("asa");
    		 $("#load_form").submit(function(event) {       
               event.preventDefault();
                
               //formData = new FormData($(this)[0]);
               formData = $(this).serialize();
               
	           nlp_upload(formData);  		   
			   });
                           
function getStatus(fileID)
		 {  var status="NIC";
		        $.ajax({type:"GET",url:"e/getStatus/"+fileID,
				         processData: false,
						 cache: false,
						 contentType: false,
						 
						 success: function(res) {status=res;},
						 error:function(jqXHR, exception) {nlp_error(jqXHR,exception);return;},
						   
						 async:false
						 });
			return status;		 
						 
		 }
                 
function getError(fileID)
		 {  var status="NIC";
		        $.ajax({type:"GET",url:"e/getError/"+fileID,
				         processData: false,
						 cache: false,
						 contentType: false,
						 
						 success: function(res) {status=res;},
						 error:function(jqXHR, exception) {nlp_error(jqXHR,exception);return;},
						   
						 async:false
						 });
			return status;		 
						 
		 }
function check_status(fileID,ones)
{  
		        $.ajax({type:"GET",url:"e/getExtendedStatus/"+fileID,
				         processData: false,
						 cache: false,
						 contentType: false,
						 
						 success: function(res) { $("#log").val((new XMLSerializer()).serializeToString(res));
                                                                         status=getStatus(fileID);
                                                                         $("#status").text(status);
                                                                    if (status==="PROCESSING")
                                                                             setTimeout(function() {check_status(fileID);}, 1000); 
                                                                    if (status==="DONE" && ones!="ones")
                                                                             setTimeout(function() {check_status(fileID,"ones");}, 100); 
                                                                     if (status==="ERROR")
                                                                              $("#status").text(getError(fileID));
    },
                                                                         
                                                                         
						 error:function(jqXHR, exception) {nlp_error(jqXHR,exception);return;},
						 
						 async:true
						 });
				 
						 
}
		 
		 function nlp_error(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
               alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
			throw "Ajax error";
        }
    
function nlp_upload(formData)
	 {  
	       var url = "e/execute"; 

    $.ajax({
           type: "POST",
           url: url,
           data: formData, // serializes the form's elements.
		   cache: false,
           contentType:"application/x-www-form-urlencoded;charset=UTF-8",
           processData: false,
           success: function(file_id)
           {   check_status(file_id);
               //file_id=nlp_process1("maca",file_id);
			  
		
           },
		   error: function(jqXHR, exception) {nlp_error(jqXHR,exception);return;}
         });
		

    return false; // avoid to execute the actual submit of the form.
};

</script>
    </body>
</html>
