public String clearTable() { // TODO Auto-generated method stub String req = ("from Contact contact"); Query query = em.createQuery(req); List<Contact> l = query.getResultList(); for (Contact i : l) { em.remove(i); } req = "from ContactGroup group"; query = em.createQuery(req); List<ContactGroup> listGroup = query.getResultList(); for (ContactGroup i : listGroup) { i.setContacts(new HashSet<Contact>()); em.persist(i); } return ServerUtils.opTableRemoved; }
public String deleteContact(long id) { StringBuffer requeteContact = new StringBuffer(); requeteContact.append("FROM Contact contact").append(" WHERE contact.id = " + id); Query queryContact = em.createQuery(requeteContact.toString()); Contact contact = (Contact) queryContact.getResultList().get(0); Query queryCg = em.createQuery( "select cg from ContactGroup cg JOIN FETCH cg.contacts c where c.contactId = :id"); queryCg.setParameter("id", id); List<ContactGroup> cgList = queryCg.getResultList(); for (ContactGroup cg : cgList) { Query q = em.createNativeQuery( "DELETE FROM ContactGroup_Contact " + "WHERE contacts_contactId = :cid and contactGroups_contactGpoupId = :gid"); q.setParameter("gid", cg.getContactGpoupId()); q.setParameter("cid", contact.getContactId()); q.executeUpdate(); } em.remove(contact); return ServerUtils.opTableRemoved; }
public String addContact( String firstName, String lastName, String email, String street, String city, String zip, String country, String phoneKind, String phoneNumber, String group) { System.out.println("Server side, 9 params"); // TODO Auto-generated method stub Contact contact = new Contact(); contact.setFirstName(firstName); contact.setLastName(lastName); contact.setEmail(email); Address address = new Address(); address.setStreet(street); address.setCity(city); address.setZip(zip); address.setCountry(country); contact.setAddress(address); // Uni birectionnel PhoneNumber phone = new PhoneNumber(); phone.setPhoneKind(phoneKind); phone.setPhoneNumber(phoneNumber); contact.getPhoneNumbers().add(phone); phone.setContact(contact); String request = "from ContactGroup contactGroup"; Query query = em.createQuery(request); List<ContactGroup> l = query.getResultList(); Iterator<ContactGroup> ite = l.iterator(); ContactGroup contactGroup = null; String rvalue = null; while (ite.hasNext()) { contactGroup = ite.next(); if (contactGroup.getGroupName().equals(group)) { contact.getContactGroups().add(contactGroup); contactGroup.getContacts().add(contact); em.persist(contact); rvalue = ServerUtils.opFait; return rvalue; } } // If new contact Group contactGroup = new ContactGroup(); contactGroup.setGroupName(group); contact.getContactGroups().add(contactGroup); contactGroup.getContacts().add(contact); em.persist(contact); // em.persist(address); // em.persist(phone); // em.persist(contactGroup); return ServerUtils.opFait; }