Java tutorial
/* * 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 com.grummages.app.rest.entity.ListingPaymentPK; import javax.persistence.EntityManager; import javax.ws.rs.core.PathSegment; import com.grummages.app.rest.entity.ListingPayment; 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.listingpayment") @com.sun.jersey.spi.resource.Singleton @com.sun.jersey.api.spring.Autowire public class ListingPaymentRESTFacade { @PersistenceContext(unitName = "com.grummages_GrummagesWebApp_war_1.0-SNAPSHOTPU") protected EntityManager entityManager; private ListingPaymentPK getPrimaryKey(PathSegment pathSegment) { /* * pathSemgent represents a URI path segment and any associated matrix parameters. * URI path part is supposed to be in form of 'somePath;listingId=listingIdValue;paymentId=paymentIdValue'. * Here 'somePath' is a result of getPath() method invocation and * it is ignored in the following code. * Matrix parameters are used as field names to build a primary key instance. */ com.grummages.app.rest.entity.ListingPaymentPK key = new com.grummages.app.rest.entity.ListingPaymentPK(); javax.ws.rs.core.MultivaluedMap<String, String> map = pathSegment.getMatrixParameters(); java.util.List<String> listingId = map.get("listingId"); if (listingId != null && !listingId.isEmpty()) { key.setListingId(new java.lang.Short(listingId.get(0))); } java.util.List<String> paymentId = map.get("paymentId"); if (paymentId != null && !paymentId.isEmpty()) { key.setPaymentId(new java.lang.Short(paymentId.get(0))); } return key; } public ListingPaymentRESTFacade() { } @POST @Consumes({ "application/xml", "application/json" }) @Transactional public Response create(ListingPayment entity) { entityManager.persist(entity); return Response.created(URI.create(entity.getListingPaymentPK().getListingId() + "," + entity.getListingPaymentPK().getPaymentId().toString())).build(); } @PUT @Consumes({ "application/xml", "application/json" }) @Transactional public void edit(ListingPayment entity) { entityManager.merge(entity); } @DELETE @Path("{id}") @Transactional public void remove(@PathParam("id") PathSegment id) { com.grummages.app.rest.entity.ListingPaymentPK key = getPrimaryKey(id); ListingPayment entity = entityManager.getReference(ListingPayment.class, key); entityManager.remove(entity); } @GET @Path("{id}") @Produces({ "application/xml", "application/json" }) @Transactional public ListingPayment find(@PathParam("id") PathSegment id) { com.grummages.app.rest.entity.ListingPaymentPK key = getPrimaryKey(id); return entityManager.find(ListingPayment.class, key); } @GET @Produces({ "application/xml", "application/json" }) @Transactional public List<ListingPayment> findAll() { return find(true, -1, -1); } @GET @Path("{max}/{first}") @Produces({ "application/xml", "application/json" }) @Transactional public List<ListingPayment> 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 ListingPayment AS o"); return query.getSingleResult().toString(); } finally { entityManager.close(); } } private List<ListingPayment> find(boolean all, int maxResults, int firstResult) { try { Query query = entityManager.createQuery("SELECT object(o) FROM ListingPayment AS o"); if (!all) { query.setMaxResults(maxResults); query.setFirstResult(firstResult); } return query.getResultList(); } finally { entityManager.close(); } } }