
/* Josh Holtrop
 * 2008-12-04
 * CS656
 * Lab 4
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.restlet.data.MediaType;
import org.restlet.resource.DomRepresentation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.db4o.query.Predicate;

import edu.gvsu.cis.cs656.lab4.server.RegistrationInfo;

/**
 * This class holds basic user information to be stored in the server's database.
 *
 * It extends RegistrationInfo so that the methods it provides are easily
 * available. It then adds methods specific for the REST framework.
 */
public class User extends RegistrationInfo
{
    public DomRepresentation getDomRepresentation() throws IOException
    {
        DomRepresentation rep = new DomRepresentation(MediaType.TEXT_XML);
        Document d = rep.getDocument();

        fillNodes(d, null);

        return rep;
    }

    public void fillNodes(Document d, Element eltParent)
    {
        Element eltRoot = d.createElement("user");
        if (eltParent == null)
            d.appendChild(eltRoot);
        else
            eltParent.appendChild(eltRoot);

        Element eltName = d.createElement("name");
        eltName.appendChild(d.createTextNode(getUserName()));
        eltRoot.appendChild(eltName);

        Element eltHost = d.createElement("host");
        eltHost.appendChild(d.createTextNode(getHost()));
        eltRoot.appendChild(eltHost);

        Element eltPort = d.createElement("port");
        eltPort.appendChild(d.createTextNode((new Integer(getPort())).toString()));
        eltRoot.appendChild(eltPort);

        Element eltStatus = d.createElement("status");
        eltStatus.appendChild(
            d.createTextNode(getStatus() ? "available" : "away"));
        eltRoot.appendChild(eltStatus);
    }
}
