updated UsersResource
git-svn-id: svn://anubis/gvsu@279 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
0d0ffb45be
commit
1ec2298033
@ -18,9 +18,17 @@ public class User extends RegistrationInfo
|
||||
{
|
||||
DomRepresentation rep = new DomRepresentation(MediaType.TEXT_XML);
|
||||
Document d = rep.getDocument();
|
||||
|
||||
Element eltRoot = d.createElement("user");
|
||||
d.appendChild(eltRoot);
|
||||
|
||||
|
||||
fillNodes(d, eltRoot);
|
||||
|
||||
return rep;
|
||||
}
|
||||
|
||||
public void fillNodes(Document d, Element eltRoot)
|
||||
{
|
||||
Element eltName = d.createElement("name");
|
||||
eltName.appendChild(d.createTextNode(getUserName()));
|
||||
eltRoot.appendChild(eltName);
|
||||
@ -37,7 +45,5 @@ public class User extends RegistrationInfo
|
||||
eltStatus.appendChild(
|
||||
d.createTextNode(getStatus() ? "available" : "away"));
|
||||
eltRoot.appendChild(eltStatus);
|
||||
|
||||
return rep;
|
||||
}
|
||||
}
|
||||
|
@ -23,13 +23,55 @@ import org.restlet.resource.Variant;
|
||||
|
||||
public class UsersResource extends Resource
|
||||
{
|
||||
|
||||
public UsersResource(Context context, Request request, Response response)
|
||||
{
|
||||
super(context, request, response);
|
||||
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.
|
||||
*/
|
||||
@ -42,8 +84,16 @@ public class UsersResource extends Resource
|
||||
try
|
||||
{
|
||||
rep = new DomRepresentation(MediaType.TEXT_XML);
|
||||
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
||||
rep.getDocument().normalizeDocument();
|
||||
Document d = rep.getDocument();
|
||||
|
||||
Element eltRoot = d.createElement("users");
|
||||
d.appendChild(eltRoot);
|
||||
for (User user : getUsers())
|
||||
{
|
||||
user.fillNodes(d, eltRoot);
|
||||
}
|
||||
|
||||
d.normalizeDocument();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
@ -83,23 +133,20 @@ public class UsersResource extends Resource
|
||||
Form form = new Form(entity);
|
||||
|
||||
// create a new User object
|
||||
Book book = new Book();
|
||||
book.setId(form.getFirstValue("book[id]"));
|
||||
book.setAuthors(form.getFirstValue("book[authors]"));
|
||||
book.setIsbn(form.getFirstValue("book[isbn]"));
|
||||
book.setTitle(form.getFirstValue("book[title]"));
|
||||
book.setPublisher(form.getFirstValue("book[publisher]"));
|
||||
book.setYear(form.getFirstValue("book[year]"));
|
||||
User user = new Book();
|
||||
user.setUserName(form.getFirstValue("user[name]"));
|
||||
user.setHost(form.getFirstValue("user[host]"));
|
||||
user.setPort(Integer.parseInt(form.getFirstValue("user[port]")));
|
||||
user.setStatus(form.getFirstValue("user[status]").equals("available"));
|
||||
getResponse().setStatus(Status.SUCCESS_CREATED);
|
||||
|
||||
|
||||
// commit the changes.
|
||||
getContainer().set(book);
|
||||
getContainer().set(user);
|
||||
getContainer().commit();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user