<%-- $HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/modules/digitizer/geom_checker_proxy.jsp $
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by:
 - Department of Geography, University of Bonn -
 and
 - lat/lon GmbH -

 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 of the License, or (at your option)
 any later version.
 This library is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 details.
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 Contact information:

 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/

 Department of Geography, University of Bonn
 Prof. Dr. Klaus Greve
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/

 e-mail: info@deegree.org
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="org.deegree.portal.standard.wfs.control.GeometryValidator" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="org.deegree.enterprise.WebUtils" %>
<%@ page import="org.deegree.framework.util.CharsetUtils" %>
<%
    HashMap geomStateToLabel = new HashMap(3);
    geomStateToLabel.put( GeometryValidator.INVALID_GEOMETRY, "The geometry is not valid." );
    geomStateToLabel.put( GeometryValidator.INFORMALLY_VALID_GEOMETRY, "The geometry is not formally correct." );
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="author" content="lat/lon GmbH" />
        <meta name="KeyWords" content="deegree OGC iGeoPortal lat/lon" />
        <meta name="audience" content="" />
        <title>Hidden page for sending/handling requests for checking geometry</title>
        <link href="./../../css/deegree.css" rel="stylesheet" type="text/css" />
        <script language="JavaScript1.2" type="text/javascript" src="../../javascript/rpc.js"></script>
        <script language="JavaScript1.2" type="text/javascript">
		<!--
			
		function init(){
			parent.geomCheckerProxy = this;
		}
		
        function initializeModule(){
            var req = "<?xml version='1.0' encoding='UTF-8'?>" +
                      "<methodCall><methodName>dig:initClient</methodName></methodCall>";
            
            document.getElementById( 'initDig_rpc' ).value = req;
            document.getElementById( 'initForm' ).submit();
        }
        
		function updateParent(){
        <%
			String s = (String)request.getAttribute( GeometryValidator.GEOMETRY_STATE );
			out.println( "var hasResponse = " + (s != null ? "true" : "false" ) + ";" );
        %>
			var geomOK = false;
			var mesg = '';
        <%
			if( s != null ){
			    
			    if( GeometryValidator.VALID_GEOMETRY.equals( s ) 
                    || GeometryValidator.INFORMALLY_VALID_GEOMETRY.equals( s ) ){
			        out.println( "geomOK = true" );
			    }
			    
			    if( GeometryValidator.INVALID_GEOMETRY.equals( s ) ){
                    // don't be too fussy
                    //|| GeometryValidator.INFORMALLY_VALID_GEOMETRY.equals( s ) ){
 			        out.println( "mesg = '" + geomStateToLabel.get( s ) + "'" );
   			    }
        %>
		    	parent.updateGUI( mesg, geomOK );
        <%
			}    
        %>
		}

		function checkGeometry( geom ){
		
			var params = '<param><value><struct>';
			params+= createMember( 'GEOMETRY', geom, 'string' );
			params+= '</struct></value></param>';
			
			var rpc = createMethodCall( 'dig:checkGeometry', params, '<%= CharsetUtils.getSystemCharset() %>' );
			
			document.forms[0]['rpc'].value = encodeURIComponent(rpc);
			document.forms[0].submit();			  
		}

        // added here for time's sake -> revise
        function createMember( name, value, type ) {
            return "<member><name>"+ name +"</name><value><" + type + ">"+ value + "</" + type + "></value></member>";
        }
        //UT this method should be split into more
        function createParam( structContent ) {
            return "<param><value><struct>" + structContent + "</struct></value></param>"; 
        }
        function createArray( arrayData ) {
            return "<array><data>" + arrayData + "</data></array>"; 
        }
        
        function createMethodCall( methodName, params, encoding ) {
            return "<?xml version='1.0' encoding='UTF-8'?>" +
                   "<methodCall><methodName>" + methodName + "</methodName>" + 
                   "<params>" + params +"</params></methodCall>";
        }
        
		// -->
        </script>
    </head>
    <body onload="init();updateParent();">
        <form action="<%= WebUtils.getAbsoluteContextPath(request)%>/control" id="initForm" method="post">
            <input id="initDig_rpc" name="rpc" type="hidden" value="" />
        </form>
    </body>
</html>
