/** * Persist the given entity. * * @param entity the entity to persist */ protected void createEntity(PrivacyPreferences entity) { entity.setId(null); EntityManager em = PersistenceService.getInstance().getEntityManager(); em.persist(entity); UbikeUser owner = entity.getOwner(); if (owner != null) { owner.setPreferences(entity); } }
/** * Post method for creating an instance of PrivacyPreferences using XML as the input format. * * @param data an PrivacyPreferencesConverter entity that is deserialized from an XML stream * @return an instance of PrivacyPreferencesConverter */ @POST @Consumes({"application/xml", "application/json"}) public Response post(PrivacyPreferencesConverter data) { PersistenceService persistenceSvc = PersistenceService.getInstance(); try { persistenceSvc.beginTx(); EntityManager em = persistenceSvc.getEntityManager(); PrivacyPreferences entity = data.resolveEntity(em); createEntity(data.resolveEntity(em)); persistenceSvc.commitTx(); return Response.created(uriInfo.getAbsolutePath().resolve(entity.getId() + "/")).build(); } finally { persistenceSvc.close(); } }
/** * Get method for retrieving a collection of PrivacyPreferences instance in XML format. * * @return an instance of PrivacyPreferencessConverter */ @Secured({"ROLE_ADMIN", "ADMIN_ACCESS"}) @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public PrivacyPreferencessConverter get( @QueryParam("start") @DefaultValue("0") int start, @QueryParam("max") @DefaultValue("10") int max, @QueryParam("expandLevel") @DefaultValue("1") int expandLevel, @QueryParam("query") @DefaultValue("SELECT e FROM PrivacyPreferences e") String query) { PersistenceService persistenceSvc = PersistenceService.getInstance(); try { persistenceSvc.beginTx(); return new PrivacyPreferencessConverter( getEntities(start, max, query), uriInfo.getAbsolutePath(), expandLevel); } finally { persistenceSvc.commitTx(); persistenceSvc.close(); } }
/** * Returns all the entities associated with this resource. * * @return a collection of PrivacyPreferences instances */ protected Collection<PrivacyPreferences> getEntities(int start, int max, String query) { EntityManager em = PersistenceService.getInstance().getEntityManager(); return em.createQuery(query).setFirstResult(start).setMaxResults(max).getResultList(); }
/** * @param id * @return */ public PrivacyPreferences getById(long id) { EntityManager em = PersistenceService.getInstance().getEntityManager(); return em.find(PrivacyPreferences.class, id); }