// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/person")
public class PersonManager {
	private static List<Person> data = Collections.synchronizedList(new ArrayList<Person>());
	static {
	    data.add(new Person(1, "A A", "A vej 1"));
	    data.add(new Person(2, "B B", "B vej 2"));
	}
	@POST
	@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public void add(Person p) {
		data.add(p);
	}
	@GET
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public List<Person> getAll() {
		return data;
	}
	@GET
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	@Path("/{id}")
	public Person getOne(@PathParam("id") int id) {
	    for(Person p : data) {
	        if(p.getId() == id) {
	            return p;
	        }
	    }
	    throw new WebApplicationException(Response.Status.NOT_FOUND);
	}
}
