Ejemplo n.º 1
0
  public String updateContact(
      long id,
      String firstName,
      String lastName,
      String email,
      String street,
      String city,
      String zip,
      String country,
      String phoneKind,
      String phoneNumber) {
    // TODO Auto-generated method stub
    String rvalue = null;
    StringBuffer requeteS = new StringBuffer();
    requeteS
        .append("from Contact contact")
        .append(" left join contact.address as address")
        .append(" left join contact.phoneNumbers as phoneNumber")
        .append(" left join contact.contactGroups as contactGroup where contact.id=" + id);
    Query query = em.createQuery(requeteS.toString());
    List<Object[]> l = query.getResultList();
    if (l.size() == 0) {
      rvalue = "Contact dont exist!";
      return rvalue;
    }
    Contact contact = (Contact) l.get(0)[0];
    if (firstName != null) contact.setFirstName(firstName);
    if (lastName != null) contact.setLastName(lastName);
    if (email != null) contact.setEmail(email);

    Address address = (Address) l.get(0)[1];
    if (street != null) address.setStreet(street);
    if (city != null) address.setCity(city);
    if (zip != null) address.setZip(zip);
    if (country != null) address.setCountry(country);
    contact.setAddress(address); // Uni birectionnel

    PhoneNumber phone = (PhoneNumber) l.get(0)[2];
    if (phoneKind != null) phone.setPhoneKind(phoneKind);
    if (phoneNumber != null) phone.setPhoneNumber(phoneNumber);

    //		ContactGroup cg = (ContactGroup)l.get(0)[3];
    //		if ()
    em.merge(contact);
    rvalue = ServerUtils.opFait;
    return rvalue;
  }
Ejemplo n.º 2
0
  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;
  }