/** @param args the command line arguments */ public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("CA2_ORM_REST_AJAXPU"); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); em.getTransaction().commit(); System.out.println(Facade.getPersons()); try { System.out.println(Facade.getPerson(1)); } catch (Exception e) { } System.out.println("Create new person: " + Facade.createPerson(new Person("Bob", "Apit"))); try { System.out.println("P1s email: " + Facade.getPerson(1).getEmail()); } catch (Exception e) { } System.out.println("Personer fra 2800: " + Facade.getPersonsByZip(2800)); try { System.out.println("Cvr=123: " + Facade.getCompany(123)); } catch (Exception e) { } // System.out.println("All phones:"+Facade.getPhones()); // System.out.println("Person 1 phone list:"+Facade.getPhone(1)); }
@GET @Produces("application/json") @Path("/{id}") public String getCompany(@PathParam("id") int id) throws EntityNotFoundException { Company c = f.getCompany(id); JsonObject co = createCompanyObject(c); String jasonCompany = gson.toJson(co, JsonObject.class); return jasonCompany; }
@POST @Consumes("application/json") @Path("phone") public void addPhoneToCompany(String content) throws EntityNotFoundException { // json: id, number, description JsonObject jo = new JsonParser().parse(content).getAsJsonObject(); Company c = f.getCompany(jo.get("id").getAsInt()); f.addPhoneCompany(c, jo.get("description").getAsString(), jo.get("number").getAsInt()); }