/*
 * ContactRestService.java - address book RESTful webservice
 * 
 * Copyright (C) 2015 Paolo Rovelli 
 * 
 * Author: Paolo Rovelli <paolorovelli@yahoo.it> 
 */

package org.epalrov.addressbook;

import javax.ejb.Stateless;

import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.POST;
import javax.ws.rs.DELETE;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

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

/**
 * ContactRestService is an EJB exposed as RESTful webservice
 */
@Stateless
@Path("/contacts")
public class ContactRestService {

    @PersistenceContext
    private EntityManager em;

    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public List<Contact> getContacts(
            @DefaultValue("0") @QueryParam("start") int start,
            @DefaultValue("100") @QueryParam("max") int max) {
        Query query = em.createNamedQuery("getContacts")
            .setFirstResult(start)
            .setMaxResults(max);
        return (List<Contact>)query.getResultList();
    }

    @GET
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Contact getContact(@PathParam("id") Integer id) {
        Query query = em.createNamedQuery("getContact")
           .setParameter("id", id);
        return (Contact)query.getSingleResult();
        //return em.find(Contact.class, id);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response createContact(Contact contact) {
        em.persist(contact);
        return Response.created(URI.create("/" + contact.getId())).build();
    }

    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateContact(@PathParam("id") Integer id, Contact contact) {
        Query query = em.createNamedQuery("getContact")
           .setParameter("id", id);
        Contact managedContact = (Contact)query.getSingleResult();
        managedContact.setFirstName(contact.getFirstName());
        managedContact.setLastName(contact.getLastName());
        managedContact.setEmail(contact.getEmail());
        return Response.ok().status(303).build(); //return a seeOther code
    }

    @DELETE
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public void deleteContact(@PathParam("id") Integer id) {
        Query query = em.createNamedQuery("getContact")
           .setParameter("id", id);
        Contact contact = (Contact)query.getSingleResult();
        em.remove(contact);
    }

}

@@ -521,25 +521,23 @@
 rs.core.
-MediaType
+Context
 ;%0Aimport
@@ -563,16 +563,84 @@
 esponse;
+%0Aimport javax.ws.rs.core.UriInfo;%0Aimport javax.ws.rs.core.MediaType;
 %0A%0Aimport
@@ -767,15 +767,17 @@
 ava.
-net.URI
+util.List
 ;%0Aim
@@ -786,25 +786,23 @@
 rt java.
-util.List
+net.URI
 ;%0A%0A/**%0A 
@@ -986,16 +986,59 @@
 er em;%0A%0A
+    @Context%0A    private UriInfo uriInfo;%0A%0A
     @GET
@@ -1419,22 +1419,47 @@
         
-return
+List%3CContact%3E managedContacts =
  (List%3CC
@@ -1485,24 +1485,56 @@
 sultList();%0A
+        return managedContacts;%0A
     %7D%0A%0A    @
@@ -1800,22 +1800,40 @@
         
-return
+Contact managedContact =
  (Contac
@@ -1871,43 +1871,29 @@
     
-//
 return 
-em.find(Contact.class, id)
+managedContact
 ;%0A  
@@ -2043,65 +2043,295 @@
     
-return Response.created(URI.create(%22/%22 + contact.getId())
+// contact.Id is generated only after flush!%0A        em.flush();%0A        // returns the URI of the new resource in the HTTP header Location field%0A        URI uri = uriInfo.getAbsolutePathBuilder()%0A            .path(contact.getId().toString()).build();%0A        return Response.created(uri
 ).bu
@@ -2823,24 +2823,186 @@
 etEmail());%0A
+        // redirect towards the updated resource%0A        // URI uri = uriInfo.getAbsolutePathBuilder().build();%0A        // return Response.seeOther(uri).build();%0A
         retu
@@ -3042,33 +3042,8 @@
 d();
- //return a seeOther code
 %0A   
@@ -3133,12 +3133,16 @@
 lic 
-void
+Response
  del
@@ -3287,25 +3287,32 @@
     Contact 
-c
+managedC
 ontact = (Co
@@ -3360,23 +3360,111 @@
 .remove(
-contact
+managedContact);%0A        // ok response with no body%0A        return Response.noContent().build(
 );%0A    %7D

fix HTTP responses for CRUD operations