package com.github.daniel.shuy.ws.rs.jpa.crud;

import java.io.Closeable;
import java.util.List;
import javax.transaction.Transactional;
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.MediaType;

/**
 * Extend this class to create a CRUD JAX-RS Repository Class.
 *
 * @param <E> The Entity Class type
 */
public interface ResourceCRUD<E extends EntityCRUD> extends Closeable {
    /**
     * Override this method to provide a Repository instance.
     * 
     * @return A Repository instance.
     */
    public abstract RepositoryCRUD<E> getRepository();

    @POST
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Transactional(rollbackOn = Exception.class)
    public default void create(E content) {
        try {
            getRepository().create(content);
        } finally {
            close();
        }
    }

    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public default List<E> findAll() {
        try {
            return getRepository().findAll();
        } finally {
            close();
        }
    }

    @GET
    @Path("{id}")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public default E find(@PathParam("id") Long id) {
        try {
            return getRepository().find(id);
        } finally {
            close();
        }
    }

    @GET
    @Path("{from}/{to}")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public default List<E> findRange(@PathParam("from") Long from, @PathParam("to") Long to) {
        try {
            return getRepository().findRange(from, to);
        } finally {
            close();
        }
    }

    @GET
    @Path("count")
    @Produces(MediaType.TEXT_PLAIN)
    public default String count() {
        try {
            return String.valueOf(getRepository().count());
        } finally {
            close();
        }
    }
    
    @PUT
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Transactional(rollbackOn = Exception.class)
    public default void edit(E content) {
        try {
            getRepository().edit(content);
        } finally {
            close();
        }
    }

    @DELETE
    @Path("{id}")
    @Transactional(rollbackOn = Exception.class)
    public default void remove(@PathParam("id") Long id) {
        try {
            getRepository().remove(id);
        } finally {
            close();
        }
    }

    /**
     * Override this method to implement actions to perform after each transaction.
     */
    @Override
    public default void close() {}
}

@@ -45,34 +45,8 @@
 d;%0A%0A
-import java.io.Closeable;%0A
 impo
@@ -518,26 +518,8 @@
 RUD%3E
- extends Closeable
  %7B%0A 
@@ -871,34 +871,16 @@
 tent) %7B%0A
-        try %7B%0A    
         
@@ -916,59 +916,8 @@
 t);%0A
-        %7D finally %7B%0A            close();%0A        %7D%0A
     
@@ -1038,34 +1038,16 @@
 All() %7B%0A
-        try %7B%0A    
         
@@ -1084,59 +1084,8 @@
 ();%0A
-        %7D finally %7B%0A            close();%0A        %7D%0A
     
@@ -1239,34 +1239,16 @@
 g id) %7B%0A
-        try %7B%0A    
         
@@ -1284,59 +1284,8 @@
 d);%0A
-        %7D finally %7B%0A            close();%0A        %7D%0A
     
@@ -1487,34 +1487,16 @@
 g to) %7B%0A
-        try %7B%0A    
         
@@ -1543,59 +1543,8 @@
 o);%0A
-        %7D finally %7B%0A            close();%0A        %7D%0A
     
@@ -1646,34 +1646,16 @@
 unt() %7B%0A
-        try %7B%0A    
         
@@ -1706,59 +1706,8 @@
 ));%0A
-        %7D finally %7B%0A            close();%0A        %7D%0A
     
@@ -1884,34 +1884,16 @@
 tent) %7B%0A
-        try %7B%0A    
         
@@ -1927,59 +1927,8 @@
 t);%0A
-        %7D finally %7B%0A            close();%0A        %7D%0A
     
@@ -2072,26 +2072,8 @@
 ) %7B%0A
-        try %7B%0A    
     
@@ -2112,209 +2112,8 @@
     
-    %7D finally %7B%0A            close();%0A        %7D%0A    %7D%0A%0A    /**%0A     * Override this method to implement actions to perform after each transaction.%0A     */%0A    @Override%0A    public default void close() %7B
 %7D%0A%7D%0A

Remove Closeable Interface