Creating and updating contacts

Contacts are created by UserAddressBookResource's createNew method, which is called from a PUT:

    @Override
    public Resource createNew(String newName, InputStream in, Long length, String contentType) throws IOException, ConflictException, NotAuthorizedException, BadRequestException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        IOUtils.copy(in, bout);
        String icalData = bout.toString("UTF-8");
        Contact cNew = contactManager.createContact(newName, icalData);
        return new ContactResource(this, cNew);
    }

The data which is PUT is just the iCal data like this:

BEGIN:VCARD
VERSION:3.0
PRODID:-//Inverse inc.//SOGo Connector 1.0//EN
UID:C58AF283-7320-0001-2447-9D801A301A9A
N:Bloggs;Joe
FN:Joe Bloggs
X-MOZILLA-HTML:FALSE
EMAIL;TYPE=work:joe@bloggs.com
END:VCARD

Our implementation delegates to a ContactManager class to do the actual processing. That returns a domain object which we wrap with a milton resource to represent the new object.

ContactManager is going to create our contact, then parse the iCal data and set individual fields, and 'persist' the original ical data:

    public ContactsDao.Contact createContact(String newName, String icalData) throws UnsupportedEncodingException {
        ContactsDao.Contact e = contactsDao.addContact(newName);
        update(e, icalData);        
        return e;
    }
       
    public void update(ContactsDao.Contact contact, String data) {
        log.info("update: " + data);
        VCard vcard = parse(data);
        if (vcard.getUID() != null && vcard.getUID().hasUID()) {
            contact.setUid(vcard.getUID().getUID());
        } else {
            log.warn("NO UID FOUND, making up our own");
            contact.setUid(UUID.randomUUID().toString());
        }
        if (vcard.getName() != null) {
            contact.setGivenName(vcard.getName().getGivenName());
            contact.setSurName(vcard.getName().getFamilyName());
            log.info("parsed name: " + contact.getGivenName() + " " + contact.getSurName());
        } else {
            log.warn("No name component found!");
        }
        contact.setMail(""); // reset in case none given
        Iterator<EmailFeature> it = vcard.getEmails();
        while (it.hasNext()) {
            contact.setMail(it.next().getEmail());
        }
        log.info("email: " + contact.getMail());

        String ph = getPhone(vcard);
        contact.setTelephonenumber(ph);
        log.info("phone: " + contact.getTelephonenumber());
        contact.setIcalData(data);
        contactsDao.incrementContactsVersion();
    }