updated UsersResource

git-svn-id: svn://anubis/gvsu@279 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-11-29 17:30:01 +00:00
parent 0d0ffb45be
commit 1ec2298033
2 changed files with 70 additions and 17 deletions

View File

@ -18,9 +18,17 @@ public class User extends RegistrationInfo
{ {
DomRepresentation rep = new DomRepresentation(MediaType.TEXT_XML); DomRepresentation rep = new DomRepresentation(MediaType.TEXT_XML);
Document d = rep.getDocument(); Document d = rep.getDocument();
Element eltRoot = d.createElement("user"); Element eltRoot = d.createElement("user");
d.appendChild(eltRoot); d.appendChild(eltRoot);
fillNodes(d, eltRoot);
return rep;
}
public void fillNodes(Document d, Element eltRoot)
{
Element eltName = d.createElement("name"); Element eltName = d.createElement("name");
eltName.appendChild(d.createTextNode(getUserName())); eltName.appendChild(d.createTextNode(getUserName()));
eltRoot.appendChild(eltName); eltRoot.appendChild(eltName);
@ -37,7 +45,5 @@ public class User extends RegistrationInfo
eltStatus.appendChild( eltStatus.appendChild(
d.createTextNode(getStatus() ? "available" : "away")); d.createTextNode(getStatus() ? "available" : "away"));
eltRoot.appendChild(eltStatus); eltRoot.appendChild(eltStatus);
return rep;
} }
} }

View File

@ -23,13 +23,55 @@ import org.restlet.resource.Variant;
public class UsersResource extends Resource public class UsersResource extends Resource
{ {
public UsersResource(Context context, Request request, Response response) public UsersResource(Context context, Request request, Response response)
{ {
super(context, request, response); super(context, request, response);
getVariants().add(new Variant(MediaType.TEXT_XML)); getVariants().add(new Variant(MediaType.TEXT_XML));
} }
/**
* Returns the parent application.
*
* @return the parent application.
*/
public ChatServer getApplication()
{
return (ChatServer) getContext().getAttributes().get(ChatServer.KEY);
}
/**
* Returns the database container.
*
* @return the database container.
*/
public ObjectContainer getContainer()
{
return getApplication().getContainer();
}
/**
* Search the database for all user resources.
* @return a List of User references.
*/
public List<User> getUsers()
{
// Create the query predicate
Predicate<User> predicate = new Predicate<User>()
{
private static final long serialVersionUID = 1L;
@Override
public boolean match(User candidate)
{
return true;
}
};
// Query the database and get the first result
List<User> users = getContainer().query(predicate);
return users;
}
/** /**
* handle HTTP GET requests. * handle HTTP GET requests.
*/ */
@ -42,8 +84,16 @@ public class UsersResource extends Resource
try try
{ {
rep = new DomRepresentation(MediaType.TEXT_XML); rep = new DomRepresentation(MediaType.TEXT_XML);
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND); Document d = rep.getDocument();
rep.getDocument().normalizeDocument();
Element eltRoot = d.createElement("users");
d.appendChild(eltRoot);
for (User user : getUsers())
{
user.fillNodes(d, eltRoot);
}
d.normalizeDocument();
} }
catch (IOException e) catch (IOException e)
{ {
@ -83,23 +133,20 @@ public class UsersResource extends Resource
Form form = new Form(entity); Form form = new Form(entity);
// create a new User object // create a new User object
Book book = new Book(); User user = new Book();
book.setId(form.getFirstValue("book[id]")); user.setUserName(form.getFirstValue("user[name]"));
book.setAuthors(form.getFirstValue("book[authors]")); user.setHost(form.getFirstValue("user[host]"));
book.setIsbn(form.getFirstValue("book[isbn]")); user.setPort(Integer.parseInt(form.getFirstValue("user[port]")));
book.setTitle(form.getFirstValue("book[title]")); user.setStatus(form.getFirstValue("user[status]").equals("available"));
book.setPublisher(form.getFirstValue("book[publisher]"));
book.setYear(form.getFirstValue("book[year]"));
getResponse().setStatus(Status.SUCCESS_CREATED); getResponse().setStatus(Status.SUCCESS_CREATED);
// commit the changes. // commit the changes.
getContainer().set(book); getContainer().set(user);
getContainer().commit(); getContainer().commit();
} }
else else
{ {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND); getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
} }
} }
} }