<%-- $HeadURL$ --%>
<%@ page contentType="text/html" %>
<jsp:include page="header.jsp" flush="true"/>
<jsp:include page="logotable.jsp" flush="true"/>
<jsp:include page="menutable.jsp" flush="true"/>

<%@ page import="java.util.*" %>
<%@ page import="org.deegree.security.drm.model.*" %>
<%@ page import="org.deegree.framework.util.StringTools" %>
<%@ page import="org.deegree.portal.standard.security.control.SecuredObjectRight" %>

<%
    Role role = (Role) request.getAttribute ("ROLE");
    SecuredObjectRight [] getMapRights = (SecuredObjectRight []) request.getAttribute ("RIGHTS_GET_MAP");
    SecuredObjectRight [] getFeatureRights = (SecuredObjectRight []) request.getAttribute ("RIGHTS_GET_FEATURE");
    boolean [] deleteFeatureRights = (boolean []) request.getAttribute ("RIGHTS_DELETE_FEATURE");
    boolean [] insertFeatureRights = (boolean []) request.getAttribute ("RIGHTS_INSERT_FEATURE");
    boolean [] updateFeatureRights = (boolean []) request.getAttribute ("RIGHTS_UPDATE_FEATURE");        
%> 

<script language="JavaScript1.2" type="text/javascript">
<!--
    var layers = new Array (<%= getMapRights.length %>);
    var featureTypes = new Array (<%= getFeatureRights.length %>);
    var featureConstraints = new Array (<%= getFeatureRights.length %>);
    
    /**
     * Constructs a new SecuredObject, consisting of:
     *
     * - id:          unique identifier
     * - name:        display name
     * - isAccessible right to be accessed (boolean)
     * - constraints  key/value-pairs specific to the certain SecuredObject type
     *                (may be null)
     */
    function SecuredObject (id, name, isAccessible, constraints) {
        this.id = id;
        this.name = name;
        this.isAccessible = isAccessible;
        this.constraints = constraints;
    }


    function toRPCParam (constraints) {
		var rpc = "<value><struct>";
	
        for (var constraint in constraints) {
            if (constraints [constraint] != null) {
 			    rpc += "<member><name>" + constraint + "</name><value>";
	            if (typeof (constraints [constraint]) == "boolean" ||
	                typeof (constraints [constraint]) == "string" ||
	                typeof (constraints [constraint]) == "number") {
	                // literal
	                rpc += "<string><![CDATA[" + constraints [constraint] + "]]></string>";
	            } else if (constraints[constraint].isAssociative) {
	                // associative array
	                rpc += "<array><data>";   
	                for (var value in constraints [constraint]) {
	                    if (value != "isAssociative") {
	     	                  rpc += "<value><string><![CDATA[" + value + "]]></string></value>";
	 	                  }
	                }
	                rpc += "</data></array>";
	            } else {
	                // normal array
	                rpc += "<array><data>";   
	                for (var i = 0; i < constraints[constraint].length; i++) {
	                    rpc += "<value><string><![CDATA[" + constraints[constraint][i] + "]]></string></value>";
	                }
	                rpc += "</data></array>";
	            }
              rpc += "</value></member>";
            }
        }
        
        rpc += "</struct></value>";
        return rpc;       
    }
    
    function init () {
        var getMapConstraints = new Array (<%= getMapRights.length %>);
<%

    for (int i = 0; i < getFeatureRights.length; i++) {
        SecuredObjectRight right = getFeatureRights [i];        
        out.println ("        featureConstraints [" + i + "] = new Array ();");        
        out.println ("        featureConstraints [" + i + "] [\"ACCESS\"] = true;");
        out.println ("        featureConstraints [" + i + "] [\"INSERT\"] = " + insertFeatureRights[i] + ";");
       	out.println ("        featureConstraints [" + i + "] [\"UPDATE\"] = " + updateFeatureRights[i] + ";");
        out.println ("        featureConstraints [" + i + "] [\"DELETE\"] = " + deleteFeatureRights[i] + ";");
        out.println ("        featureTypes [" + i + "] = new SecuredObject ("
            + right.getSecuredObject().getID () + ",'"
            + StringTools.replace (right.getSecuredObject().getName (), "'", "\\'", true) + "',"
            + right.isAccessible () + ","
            + "featureConstraints [" + i + "]);");
        
    }

   for (int i = 0; i < getMapRights.length; i++) {
        SecuredObjectRight right = getMapRights [i];        
        out.println ("        getMapConstraints [" + i + "] = new Array ();");
        Map constraints = right.getConstraints();

        String [] bgcolor = (String []) constraints.get ("bgcolor");
        if (bgcolor != null && bgcolor.length > 0) {
            out.print ("        getMapConstraints [" + i + "] [\"bgcolor\"] = new Array (");
            for (int j = 0; j < bgcolor.length; j++) {
                out.print ("\"" + bgcolor [j] + "\"");
		            if (j == bgcolor.length - 1) {
		                out.println (");");
		            } else {
		                out.print (",");
		            }
            }
        }

        String [] transparent= (String []) constraints.get ("transparent");
        if (transparent != null && transparent.length == 1) {
            out.println ("        getMapConstraints [" + i + "] [\"transparent\"] = " + transparent [0] + ";");
        }

        String [] format = (String []) constraints.get ("format");
        if (format != null && format.length > 0) {
            out.println ("        getMapConstraints [" + i + "] [\"format\"] = new Array ();");            
            for (int j = 0; j < format.length; j++) {
                out.println ("        getMapConstraints [" + i + "] [\"format\"] [\"" + format [j] + "\"] = true;");
            }
        }

        String [] bbox = (String []) constraints.get ("bbox");
        if (bbox != null && bbox.length > 0) {
            out.print ("        getMapConstraints [" + i + "] [\"bbox\"] = new Array (");
            for (int j = 0; j < bbox.length; j++) {
                out.print ("\"" + bbox [j] + "\"");
                if (j == bbox.length - 1) {
                    out.println (");");
                } else {
                    out.print (",");
                }
            }
        }

        String [] resolution = (String []) constraints.get ("resolution");
        if (resolution != null) {
            out.println ("        getMapConstraints [" + i + "] [\"resolution\"] = \"" + resolution [0] + "\";");
        }

        String [] width = (String []) constraints.get ("width");
        if (width != null && width.length == 1) {
            out.println ("        getMapConstraints [" + i + "] [\"width\"] = \"" + width [0] + "\";");
        }

        String [] height = (String []) constraints.get ("height");
        if (height != null && height.length == 1) {
            out.println ("        getMapConstraints [" + i + "] [\"height\"] = \"" + height [0] + "\";");
        }

        String [] exceptions = (String []) constraints.get ("exceptions");
        if (exceptions != null && exceptions.length > 0) {
            out.println ("        getMapConstraints [" + i + "] [\"exceptions\"] = new Array ();");            
            for (int j = 0; j < exceptions.length; j++) {
                out.println ("        getMapConstraints [" + i + "] [\"exceptions\"] [\"" + exceptions [j] + "\"] = true;");
            }
        }

        out.println ("        layers [" + i + "] = new SecuredObject ("
            + right.getSecuredObject().getID () + ",'"
            + StringTools.replace (right.getSecuredObject().getName (), "'", "\\'", true) + "',"
            + right.isAccessible () + ","
            + "getMapConstraints [" + i + "]);");
    }
%>
        layers.sort (namesort);
        featureTypes.sort (namesort);
        initForms ();
    }

    /**
     * Initializes the four listboxes with the data from the layers /
     * featureTypes arrays.
     */
    function initForms () {
        refreshAvailableListBox (document.layerSelectForm.layersAvailable, layers);
        refreshSelectedListBox  (document.layerSelectForm.layersSelected, layers);
        refreshAvailableListBox (document.featureTypeSelectForm.featureTypesAvailable, featureTypes);
        refreshSelectedListBox  (document.featureTypeSelectForm.featureTypesSelected, featureTypes);
    }
   
     /**
      * Fills the given 'available'-listbox with all SecuredObjects from the given
      * SecuredObjects array that are not enabled (selected=false).
      */
    function refreshAvailableListBox (selectedForm, securedObjects) {
        var oldCount = selectedForm.length;
        for (i = oldCount - 1; i >=0 ; i--) {
            selectedForm.options [i] = null;
        }       
        for (i = 0; i < securedObjects.length; i++) {
            if (!securedObjects [i].isAccessible) {
                  selectedForm.options [selectedForm.length] =
                      new Option (securedObjects [i].name, i);
            }
        }
    }

     /**
      * Fills the given 'selected'-listbox with all SecuredObjects from the given
      * SecuredObjects array that are enabled (selected=true).
      */
    function refreshSelectedListBox (selectedForm, securedObjects) {
        var oldCount = selectedForm.length;
        for (i = oldCount - 1; i >=0 ; i--) {
            selectedForm.options [i] = null;
        }       
        for (i = 0; i < securedObjects.length; i++) {
            if (securedObjects [i].isAccessible) {
                  selectedForm.options [selectedForm.length] =
                      new Option (securedObjects [i].name, i);
            }
        }
    }

    /**
     * Sets the access rights for the currently selected layers.
     */
    function setGetMapRights (value) {
        var selected;
        if (value) {
            selected = getSelectedOptions (document.layerSelectForm.layersAvailable.options);
        } else {
            selected = getSelectedOptions (document.layerSelectForm.layersSelected.options);
        }
        for (i = 0; i < selected.length; i++) {
            layers [selected [i].value].isAccessible = value;
        }
        refreshAvailableListBox (document.layerSelectForm.layersAvailable, layers);
        refreshSelectedListBox (document.layerSelectForm.layersSelected, layers);
        clearControls ();
    }

    /**
     * Sets the access right for the currently selected FeatureTypes.
     */
    function setFeatureTypeRights (value) {
        var selected;
        if (value) {
            selected = getSelectedOptions (document.featureTypeSelectForm.featureTypesAvailable.options);
        } else {
            selected = getSelectedOptions (document.featureTypeSelectForm.featureTypesSelected.options);
        }
        for (i = 0; i < selected.length; i++) {
            featureTypes [selected [i].value].isAccessible = value;
        }
        refreshAvailableListBox (document.featureTypeSelectForm.featureTypesAvailable, featureTypes);
        refreshSelectedListBox (document.featureTypeSelectForm.featureTypesSelected, featureTypes);
    }
    
    /**
     * Sets the modification rights (insert / update / delete) for the currently selected
     * FeatureType according to the state of the featureTypeModifications-checkboxes.
     */
    function setFeatureTypeModificationRights () {
        var selected = getSelectedOptions (document.featureTypeSelectForm.featureTypesSelected.options);
        if (selected.length == 0) {
            alert ("Wählen Sie bitte zuerst einen FeatureType aus.");
            document.featureTypeSelectForm.featureTypeModifications [0].checked = false;
            document.featureTypeSelectForm.featureTypeModifications [1].checked = false;
            document.featureTypeSelectForm.featureTypeModifications [2].checked = false;                        
            return;
        }
        
        var txt = selected [0].text;
        for ( var i = 0; i < featureTypes.length; i++) {
        	if ( txt == featureTypes[i].name ) {
        		featureTypes[i].constraints["DELETE"] = 
			        	document.featureTypeSelectForm.featureTypeModifications [0].checked;
			    featureTypes[i].constraints["INSERT"] = 
			        	document.featureTypeSelectForm.featureTypeModifications [1].checked;
			    featureTypes[i].constraints["UPDATE"] = 
			        	document.featureTypeSelectForm.featureTypeModifications [2].checked;
			    break;
        	}
        }
    }
    
     function displayModificationRights () {
        var selected = getSelectedOptions (document.featureTypeSelectForm.featureTypesSelected.options);
        
        var txt = selected [0].text;
        for ( var i = 0; i < featureTypes.length; i++) {
        	if ( txt == featureTypes[i].name ) {
        		document.featureTypeSelectForm.featureTypeModifications [0].checked =
        			featureTypes[i].constraints["DELETE"];
        		document.featureTypeSelectForm.featureTypeModifications [1].checked =
        			featureTypes[i].constraints["INSERT"];
        		document.featureTypeSelectForm.featureTypeModifications [2].checked =
        			featureTypes[i].constraints["UPDATE"];
        	}
        }
    }
    
    function perform() {
        if (confirm ("Wollen Sie die vorgenommenen Änderungen wirklich speichern?")) {
            securedObjectTypes = new Array (2);
            securedObjectTypes [0] = layers;
            securedObjectTypes [1] = featureTypes;            
            storeRights (securedObjectTypes, <%= role.getID () %>);
        }
    }
//-->
</script>

<table class="maintable">
  <tr>
    <td valign="top">
      <h2>Rechte-Editor</h2>
      <h3>Rolle: '<%= role.getName () %>'</h3>

      <table border="0" cellpadding="3" cellspacing="0">
        <tr>
          <td valign="top">
            <form action="" name="featureTypeSelectForm">
              <table border="0">
                <tr>
                  <td class="caption">zugreifbare FeatureTypes</td>
                  <td/>
                  <td class="caption">gesperrte FeatureTypes</td>
                </tr>
                <tr>
                  <td>
                    <select class="largeselect" name="featureTypesSelected" size="10" multiple="multiple" onchange="displayModificationRights()">
                      <option>Formularinitialisierung...</option>
                    </select>
                  </td>
	                <td valign="middle" align="center">
	                  <a href="javascript:setFeatureTypeRights (true)">
	                    <img src="images/admin_bt_add.gif" alt="+" border="0"/>
	                  </a>
	                  <br/>
	                  <a href="javascript:setFeatureTypeRights (false)">
	                    <img src="images/admin_bt_remove.gif" alt="-" border="0"/>
	                  </a>
	                </td>
                  <td>
                    <select class="largeselect" name="featureTypesAvailable" size="10" multiple="multiple">
                      <option>Formularinitialisierung...</option>
                    </select>
                  </td>
                </tr>
                <tr>
		          <td>
		            <table border="0" width="400">
		              <tr>
		                <td>
						  <fieldset>
							<legend>
								<b>Modifikationsrechte f&uuml;r gew&auml;hlten FeatureType</b>
			  			     </legend>          
								<table border="0">
						           <tr>
	                        		<td>&nbsp;&nbsp;&nbsp;<input type="checkbox" name="featureTypeModifications" onclick="setFeatureTypeModificationRights ()"/>delete</td>
					                <td>&nbsp;&nbsp;&nbsp;<input type="checkbox" name="featureTypeModifications" onclick="setFeatureTypeModificationRights ()"/>insert</td>
					                <td>&nbsp;&nbsp;&nbsp;<input type="checkbox" name="featureTypeModifications" onclick="setFeatureTypeModificationRights ()"/>update</td>
					              </tr>
						        </table>
					        </fieldset>
						  </td>
					    </tr>
					</table>
		          </td>
		        </tr>
              </table>
            </form>
          </td>
        </tr>        
      </table>
      <hr/>
      <table border="0" cellpadding="3" cellspacing="0">
        <tr>
          <td valign="top">
            <form action="" name="layerSelectForm">
              <table border="0">
                <tr>
                  <td class="caption">zugreifbare Layer (mit Beschränkungen)</td>
                  <td/>
                  <td class="caption">gesperrte Layer</td>
                </tr>
                <tr>
                  <td>
                    <select class="largeselect" name="layersSelected" size="10" onchange="displayConstraints()">
                      <option>Formularinitialisierung...</option>
                    </select>
                  </td>
                  <td valign="middle" align="center">
                    <a href="javascript:setGetMapRights (true)">
                      <img src="images/admin_bt_add.gif" alt="+" border="0"/>
                    </a>
                    <br/>
                    <a href="javascript:setGetMapRights (false)">
                      <img src="images/admin_bt_remove.gif" alt="-" border="0"/>
                    </a>
                  </td>                  
                  <td>
                    <select class="largeselect" name="layersAvailable" size="10" multiple="multiple">
                      <option>Formularinitialisierung...</option>
                    </select>
                  </td>
                </tr>
              </table>
            </form>
          </td>
        </tr>
      </table>

    </td>
  </tr>
</table>

<jsp:include page="submittable.jsp" flush="true"/>
<jsp:include page="footer.jsp" flush="true"/>