package com.digiplug.rest;

import java.net.URI;
import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.glassfish.jersey.server.JSONP;

import com.digiplug.persistence.entities.User;
import com.digiplug.persistence.services.UserService;

@Path("users")
public class UserResource {

	@Context
	private UriInfo uriInfo;

	@Inject
	private UserService userService;

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<User> getUsers() {
		return this.userService.findAll();
	}

	/**
	 * @response.representation.200.mediaType application/json
	 * @response.representation.404.mediaType text/plain
	 * 
	 * @param userId
	 *        the technical identifier of the user to retrieve
	 * @return
	 */
	@GET
	@Path("{userId}")
	@Produces(MediaType.APPLICATION_JSON)
	public User getUser(@PathParam("userId") Long userId) {
		return this.userService.findUserById(userId);
	}

	@GET
	@Path("jsonp/{userId}")
	@JSONP(callback = "eval", queryParam = "jsonp")
	@Produces("application/javascript")
	public User getUserAsJsonP(@PathParam("userId") Long userId) {
		return this.getUser(userId);
	}

	/**
	 * Handles the request sent to the server to accept the {@code User} entity
	 * given in parameter as a new subordinate of the resource identified by the
	 * Request-URI.
	 * 
	 * <blockquote>POST /../webapi/users HTTP/1.1</blockquote>
	 * 
	 * @response.representation.201.mediaType application/json
	 * 
	 * @param user
	 *        the user entity
	 * @return
	 */
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response postUser(User user) {
		User createdUser = this.userService.persist(user);

		URI createdUri = this.uriInfo.getAbsolutePathBuilder().path(String.valueOf(createdUser.getId())).build();

		return Response.created(createdUri).entity(createdUser).build();
	}

	/**
	 * Handles the request sent to the server to delete the {@code User}
	 * resource identified by the Request-URI.
	 * 
	 * @param userId
	 *        the technical identifier of the user to delete
	 */
	@DELETE
	@Path("{userId}")
	public void deleteUser(@PathParam("userId") Long userId) {
		User user = this.userService.findUserById(userId);

		this.userService.delete(user);
	}

	@PUT
	@Path("{userId}")
	@Produces(MediaType.APPLICATION_JSON)
	public User putUser(@PathParam("userId") Long userId, User user) {
		return this.userService.update(user);
	}

}

@@ -713,16 +713,81 @@
 rvice;%0A%0A
+%09/**%0A%09 * Read all existing user resources.%0A%09 * %0A%09 * @return%0A%09 */%0A
 %09@GET%0A%09@
@@ -897,24 +897,65 @@
 );%0A%09%7D%0A%0A%09/**%0A
+%09 * Read an existing user resource.%0A%09 * %0A
 %09 * @respons
@@ -1142,32 +1142,55 @@
 ieve%0A%09 * @return
+ the requested resource
 %0A%09 */%0A%09@GET%0A%09@Pa
@@ -1581,240 +1581,52 @@
 %09 * 
-Handles the request sent to the server to accept the %7B@code User%7D entity%0A%09 * given in parameter as a new subordinate of the resource identified by the%0A%09 * Request-URI.%0A%09 * %0A%09 * %3Cblockquote%3EPOST /../webapi/users HTTP/1.1%3C/blockquote%3E
+Request the creation of a new user resource.
 %0A%09 *
@@ -1728,22 +1728,24 @@
 he user 
-entity
+resource
 %0A%09 * @re
@@ -1740,32 +1740,58 @@
 urce%0A%09 * @return
+ the created user resource
 %0A%09 */%0A%09@POST%0A%09@C
@@ -2151,77 +2151,48 @@
 %09 * 
-Handles the request sent to the server to delete the %7B@code User%7D%0A%09 *
+Request the deletion of an existing user
  res
@@ -2218,17 +2218,21 @@
  the
- R
+%0A%09 * r
 equest
--
+ 
 URI.
@@ -2266,34 +2266,24 @@
         the 
-technical 
 identifier o
@@ -2297,16 +2297,15 @@
 ser 
-to delet
+resourc
 e%0A%09 
@@ -2486,16 +2486,293 @@
 r);%0A%09%7D%0A%0A
+%09/**%0A%09 * Request the update of an existing user resource.%0A%09 * %0A%09 * @param userId%0A%09 *        the identifier of the user resource%0A%09 * @param user%0A%09 *        the modified version of the user resource to push on the server%0A%09 * @return the updated version of the user resource%0A%09 */%0A
 %09@PUT%0A%09@

Update jersey REST APIs documentation