RESTful 資源:ContactResource.java

package com.emprogria.rs.service; import com.emprogria.jpa.Contact; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.DELETE; import javax.ws.rs.Produces; import javax.ws.rs.Consumes; import javax.ws.rs.QueryParam; import javax.ws.rs.DefaultValue; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.WebApplicationException; import javax.persistence.NoResultException; import javax.persistence.EntityManager; import com.emprogria.rs.converter.ContactConverter; import com.sun.jersey.api.core.ResourceContext; import java.util.logging.Logger; public class ContactResource { private Logger myLogger = Logger.getLogger("ContactResource"); @Context protected ResourceContext resourceContext; @Context protected UriInfo uriInfo; protected String id; /** Creates a new instance of ContactResource */ public ContactResource() { } public void setId(String id) { this.id = id; } /** * Get method for retrieving an instance of Contact identified by id in XML format. * * @param id identifier for the entity * @return an instance of ContactConverter */ @GET @Produces({"application/xml", "application/json"}) public ContactConverter get(@QueryParam("expandLevel") @DefaultValue("1") int expandLevel) { PersistenceService persistenceSvc = PersistenceService.getInstance(); ContactConverter myContactConverter = null; try { persistenceSvc.beginTx(); myContactConverter = new ContactConverter(getEntity(), uriInfo.getAbsolutePath(), expandLevel); persistenceSvc.commitTx(); } catch (Exception ex) { persistenceSvc.rollbackTx(); this.myLogger.severe("GET:" + ex.getMessage()); } finally { PersistenceService.getInstance().close(); } return myContactConverter; } /** * Put method for updating an instance of Contact identified by id using XML as the input format. * * @param id identifier for the entity * @param data an ContactConverter entity that is deserialized from a XML stream */ @PUT @Consumes({"application/xml", "application/json"}) public void put(ContactConverter data) { PersistenceService persistenceSvc = PersistenceService.getInstance(); try { persistenceSvc.beginTx(); EntityManager em = persistenceSvc.getEntityManager(); updateEntity(getEntity(), data.resolveEntity(em)); persistenceSvc.commitTx(); } catch (Exception ex) { persistenceSvc.rollbackTx(); this.myLogger.severe("PUT:" + ex.getMessage()); } finally { persistenceSvc.close(); } } /** * Delete method for deleting an instance of Contact identified by id. * * @param id identifier for the entity */ @DELETE public void delete() { PersistenceService persistenceSvc = PersistenceService.getInstance(); try { persistenceSvc.beginTx(); deleteEntity(getEntity()); persistenceSvc.commitTx(); } catch (Exception ex) { // persistenceSvc.rollbackTx(); this.myLogger.severe("DELETE:" + ex.getMessage()); } finally { persistenceSvc.close(); } } /** * Returns an instance of Contact identified by id. * * @param id identifier for the entity * @return an instance of Contact */ protected Contact getEntity() { EntityManager em = PersistenceService.getInstance().getEntityManager(); try { return (Contact) em.createQuery("SELECT e FROM Contact e where e.msn = :msn").setParameter("msn", id).getSingleResult(); } catch (NoResultException ex) { throw new WebApplicationException(new Throwable("Resource for " + uriInfo.getAbsolutePath() + " does not exist."), 404); } } /** * Updates entity using data from newEntity. * * @param entity the entity to update * @param newEntity the entity containing the new data * @return the updated entity */ private Contact updateEntity(Contact entity, Contact newEntity) { EntityManager em = PersistenceService.getInstance().getEntityManager(); entity = em.merge(newEntity); return entity; } /** * Deletes the entity. * * @param entity the entity to deletle */ private void deleteEntity(Contact entity) { EntityManager em = PersistenceService.getInstance().getEntityManager(); em.remove(entity); } }