<?xml version="1.0"?>
<xsp:page language="java" xmlns:xsp="http://apache.org/xsp" xmlns:sdx="http://www.culture.gouv.fr/ns/sdx/sdx">
    <xsp:structure>
        <xsp:include>fr.gouv.culture.sdx.user.Group</xsp:include>
        <xsp:include>fr.gouv.culture.sdx.user.User</xsp:include>
        <xsp:include>fr.gouv.culture.sdx.documentbase.IndexParameters</xsp:include>
        <xsp:include>java.util.Vector</xsp:include>
        <xsp:include>java.util.Hashtable</xsp:include>
        <xsp:include>java.util.Enumeration</xsp:include>
    </xsp:structure>
    <sdx:page langSession="lang" langParam="lang">
        <sdx:userIsAdminOrSu>
            <sdx:repo base="sdxuserdb"/>
            <sdx:parameter valueParam="id"/>
            <edit>
                <xsp:logic>
        sdx_id=sdx_parameter;
        String xml=null;
        String pass=request.getParameter("pass");
        String desc=request.getParameter("desc");
        Group group;
        User user;

        javax.xml.parsers.DocumentBuilderFactory factory =
        javax.xml.parsers.DocumentBuilderFactory.newInstance();
        javax.xml.parsers.DocumentBuilder builder =
        factory.newDocumentBuilder();


        
    // no id available do nothing
        if (sdx_id == null || "".equals(sdx_id) ) {}
    // no app, can't work    
        else if (sdx_app == null) {}
    // id=new, give an empty template
        else if("new".equals(sdx_id))
        {
            if ("user".equals(request.getParameter("doctype")))
            {
<sdx:user id="" locale="" firstname="" lastname="" email=""/>
            }
            if ("group".equals(request.getParameter("doctype")))
            {
<sdx:group id=""/>
            }
        
        }
    // action delete given
        else if (request.getParameter("delete") != null)
        {
            if (pass != null) sdx_app.deleteIdentity(new User(sdx_id));
            else if (desc != null) sdx_app.deleteIdentity(new Group(sdx_id));
            <sdx:action>Identity deleted
                <xsp:expr>sdx_id</xsp:expr>
                    </sdx:action>
        }
    // if there's a pass parameter, it's a user to create
        else if (pass != null)
        {
<!--
<sdx:message>User to edit: "<xsp:expr>sdx_id</xsp:expr>"</sdx:message>
			<sdx:message>
				<xsp:expr>xml</xsp:expr>
			</sdx:message>
            <sdx:message>User <xsp:expr>sdx_id</xsp:expr> saved</sdx:message>
-->
            // create DOM
            user= new User();
<!-- bug under tomcat 3.3
            org.w3c.dom.Document doc = builder.newDocument();
            org.w3c.dom.Element top = doc.createElementNS(sdx_nsURI, sdx_nsPrefix + ":user");
            top.setAttribute("id", sdx_id);
            top.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", request.getParameter("userlang"));
            top.setAttribute("firstname", request.getParameter("firstname"));
            top.setAttribute("lastname", request.getParameter("lastname"));
            top.setAttribute("email", request.getParameter("email"));
            doc.appendChild(top);
            user.setContent(doc);
--><![CDATA[
            xml="<?xml version=\"1.0\"?> \n";
            xml+= "<sdx:user  xmlns:sdx=\"http://www.culture.gouv.fr/ns/sdx/sdx\"";
            xml+= " id=\"" + sdx_id + "\"";
            if (request.getParameter("userlang") != null) xml+= " xml:lang=\"" + request.getParameter("userlang") + "\"";
            if (request.getParameter("firstname") != null) xml+= " firstname=\"" + request.getParameter("firstname") +"\"";
            if (request.getParameter("lastname") != null) xml+= " lastname=\"" + request.getParameter("lastname") + "\"";
            if (request.getParameter("email") != null) xml+= " email=\""+ request.getParameter("email") +"\"";
            xml+= "/> \n";
        ]]>
            user.setContent(xml.getBytes("UTF-8"));
            user.setPreferredFilename(sdx_id + ".xml");
            sdx_app.addIdentity(user, pass, sdx_repo, new IndexParameters(), this.contentHandler);

        }
    // if desc param present, it's a group to create
        else if (desc != null )
        {
<!--
            <sdx:message>Group <xsp:expr>sdx_id</xsp:expr> edited</sdx:message>
			<sdx:message>xml=<xsp:expr>xml</xsp:expr>
			</sdx:message>
-->
            group= new fr.gouv.culture.sdx.user.Group();
<!-- buggy under tomcat 3.2
            // create DOM
            org.w3c.dom.Document doc = builder.newDocument();
            org.w3c.dom.Element top = doc.createElementNS(sdx_nsURI, sdx_nsPrefix + ":group");
            top.setAttribute("id", sdx_id);
            top.appendChild(doc.createTextNode(desc));
            doc.appendChild(top);
            group.setContent(doc);
--><![CDATA[
            xml=  "<?xml version=\"1.0\"?> \n";
            xml+= "<sdx:group xmlns:sdx=\"http://www.culture.gouv.fr/ns/sdx/sdx\"";
            xml+= " id=\"" + sdx_id + "\"";
            xml+= "> \n";
            xml+= desc;
            xml+= "</sdx:group> \n"; 
        ]]>
            group.setContent(xml.getBytes("UTF-8"));
    				group.setPreferredFilename(sdx_id + ".xml");
            sdx_app.addIdentity(group, null, sdx_repo, new IndexParameters(), this.contentHandler);
            sdx_app.replaceMembers(group, request.getParameterValues("member"));
        }

        // alway include the identity requested
        // but catch exception if not exists

        if (!"new".equals(sdx_id)) try {
            <sdx:includeDocument base="sdxuserdb"/>
            String[] ids= sdx_app.getMembers(sdx_id);
            if (ids != null &amp;&amp; ids.length != 0)
            {<sdx:members>
                        <xsp:logic>
                    for(int i=0; i&lt;ids.length; i++) 
                    {
                            <sdx:identity>
                                <xsp:attribute name="id"><xsp:expr>ids[i]</xsp:expr></xsp:attribute>
                            </sdx:identity>
                    }
                </xsp:logic>
                    </sdx:members>}
            Hashtable groups= sdx_app.getParents(sdx_id);
            if (groups != null &amp;&amp; groups.size() != 0)
            {<sdx:parents>
                        <xsp:logic>
                    for( Enumeration e= groups.keys(); e.hasMoreElements(); )
                    {
                            <sdx:group>
                                <xsp:attribute name="id">
                                    <xsp:expr>e.nextElement()</xsp:expr>
                                </xsp:attribute>
                            </sdx:group>
                    }
                </xsp:logic>
                    </sdx:parents>}
         } catch (Exception e) {<sdx:message>
                        <xsp:expr>e</xsp:expr>
                    </sdx:message>}   
            </xsp:logic>
                <sdx:executeFieldQuery base="sdxuserdb" field="sdxall" value="1" hpp="-1"/>
            </edit>
            <sdx:fallback>
                <notadmin/>
            </sdx:fallback>
        </sdx:userIsAdminOrSu>
        <sdx:userIsAdminOrSu>
            <bar>
                <button id="welcome" href="index.xsp"/>
                <button id="login" href="login.xsp"/>
                <button id="identities" href="identities.xsp" self="true"/>
                <button id="bases" href="bases.xsp"/>
                <button id="explorer" href="explorer.xsp" self="true"/>
            </bar>
            <sdx:fallback>
                <bar>
                    <button id="welcome" href="index.xsp"/>
                    <button id="login" href="login.xsp"/>
                </bar>
            </sdx:fallback>
        </sdx:userIsAdminOrSu>
    </sdx:page>
</xsp:page>
