co.com.konrad.interbolsa.ws.ValorAccionFacadeREST.java Source code

Java tutorial

Introduction

Here is the source code for co.com.konrad.interbolsa.ws.ValorAccionFacadeREST.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 co.com.konrad.interbolsa.ws;

import co.com.konrad.interbolsa.entities.ValorAccion;
import com.google.gson.JsonObject;
import java.util.List;
import java.util.logging.Level;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
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.MediaType;

/**
 *
 * @author ADGS
 */
@Stateless
@Path("co.com.konrad.interbolsa.entities.valoraccion")
public class ValorAccionFacadeREST extends AbstractFacade<ValorAccion> {

    @PersistenceContext(unitName = "interbolsaPU")
    private EntityManager em;

    public ValorAccionFacadeREST() {
        super(ValorAccion.class);
    }

    @POST
    @Override
    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public String create(ValorAccion entity) throws Exception {
        //VAriable json
        JsonObject res = new JsonObject();
        try {
            //retorno
            res.addProperty("codigo", 200);
            res.addProperty("mensaje", "operacion exitosa");
            super.create(entity);
        } catch (Throwable ex) {
            java.util.logging.Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE,
                    "Erro guardando entidad", ex);
            res.addProperty("codigo", 400);
            res.addProperty("mensaje", "operacion fallida");
        }
        return res.toString();
    }

    @PUT
    @Path("{id}")
    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public void edit(@PathParam("id") Integer id, ValorAccion entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public ValorAccion find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public List<ValorAccion> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public List<ValorAccion> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[] { from, to });
    }

    @GET
    @Path("count")
    @Produces(MediaType.TEXT_PLAIN)
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

}