com.grummages.app.rest.entity.service.AddressRESTFacade.java Source code

Java tutorial

Introduction

Here is the source code for com.grummages.app.rest.entity.service.AddressRESTFacade.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.grummages.app.rest.entity.service;

import javax.persistence.EntityManager;
import com.grummages.app.rest.entity.Address;
import java.net.URI;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.PersistenceContext;
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.Response;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author admin
 */
@Path("com.grummages.app.rest.entity.address")
@com.sun.jersey.spi.resource.Singleton
@com.sun.jersey.api.spring.Autowire
public class AddressRESTFacade {
    @PersistenceContext(unitName = "com.grummages_GrummagesWebApp_war_1.0-SNAPSHOTPU")
    protected EntityManager entityManager;

    public AddressRESTFacade() {
    }

    @POST
    @Consumes({ "application/xml", "application/json" })
    @Transactional
    public Response create(Address entity) {
        entityManager.persist(entity);
        return Response.created(URI.create(entity.getAddressId().toString())).build();
    }

    @PUT
    @Consumes({ "application/xml", "application/json" })
    @Transactional
    public void edit(Address entity) {
        entityManager.merge(entity);
    }

    @DELETE
    @Path("{id}")
    @Transactional
    public void remove(@PathParam("id") Short id) {
        Address entity = entityManager.getReference(Address.class, id);
        entityManager.remove(entity);
    }

    @GET
    @Path("{id}")
    @Produces({ "application/xml", "application/json" })
    @Transactional
    public Address find(@PathParam("id") Short id) {
        return entityManager.find(Address.class, id);
    }

    @GET
    @Produces({ "application/xml", "application/json" })
    @Transactional
    public List<Address> findAll() {
        return find(true, -1, -1);
    }

    @GET
    @Path("{max}/{first}")
    @Produces({ "application/xml", "application/json" })
    @Transactional
    public List<Address> findRange(@PathParam("max") Integer max, @PathParam("first") Integer first) {
        return find(false, max, first);
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    @Transactional
    public String count() {
        try {
            Query query = entityManager.createQuery("SELECT count(o) FROM Address AS o");
            return query.getSingleResult().toString();
        } finally {
            entityManager.close();
        }
    }

    private List<Address> find(boolean all, int maxResults, int firstResult) {
        try {
            Query query = entityManager.createQuery("SELECT object(o) FROM Address AS o");
            if (!all) {
                query.setMaxResults(maxResults);
                query.setFirstResult(firstResult);
            }
            return query.getResultList();
        } finally {
            entityManager.close();
        }
    }

}