@Override
 public void removeRepresentations() throws ResourceException {
   Phonebook privatePhonebook = m_phonebookManager.getPrivatePhonebook(getUser());
   if (privatePhonebook != null) {
     Collection<PhonebookEntry> entries = privatePhonebook.getEntries();
     ArrayList<String> ids = new ArrayList<String>();
     for (PhonebookEntry entry : entries) {
       ids.add(String.valueOf(entry.getId()));
     }
     if (ids.contains(m_entryId)) {
       PhonebookEntry entry = m_phonebookManager.getPhonebookEntry(Integer.parseInt(m_entryId));
       m_phonebookManager.deletePhonebookEntry(entry);
     }
   }
 }
 @Override
 public Representation represent(Variant variant) throws ResourceException {
   Phonebook privatePhonebook = m_phonebookManager.getPrivatePhonebook(getUser());
   if (privatePhonebook != null) {
     Collection<PhonebookEntry> entries = privatePhonebook.getEntries();
     ArrayList<String> ids = new ArrayList<String>();
     for (PhonebookEntry entry : entries) {
       ids.add(String.valueOf(entry.getId()));
     }
     if (ids.contains(m_entryId)) {
       PhonebookEntry entry = m_phonebookManager.getPhonebookEntry(Integer.parseInt(m_entryId));
       PhonebookEntry reprEntry = (PhonebookEntry) entry.duplicate();
       return new PhonebookEntryRepresentation(variant.getMediaType(), reprEntry);
     }
   }
   return null;
 }
  @Override
  public void acceptRepresentation(Representation entity) throws ResourceException {
    PhonebookEntryRepresentation representation = new PhonebookEntryRepresentation(entity);
    PhonebookEntry newEntry = representation.getObject();
    if (!validatePhonebookEntry(newEntry)) {
      return;
    }

    if (m_phonebookManager.getDuplicatePhonebookEntry(newEntry, getUser()) != null) {
      setDuplicateEntryStatus();
      return;
    }

    User user = getUser();
    Phonebook privatePhonebook = m_phonebookManager.getPrivatePhonebookCreateIfRequired(user);
    newEntry.setPhonebook(privatePhonebook);
    (privatePhonebook.getEntries()).add(newEntry);
    m_phonebookManager.savePhonebook(privatePhonebook);
  }
  private boolean validatePhonebookEntry(PhonebookEntry newEntry) {
    // validate given values - first name, last name, number and e-mail (if provided)
    if (StringUtils.isEmpty(newEntry.getFirstName())
        || StringUtils.isEmpty(newEntry.getLastName())
        || StringUtils.isEmpty(newEntry.getNumber())) {
      getResponse()
          .setStatus(
              Status.CLIENT_ERROR_BAD_REQUEST, "First Name / Last Name / Number cannot be Null");
      return false;
    }

    if (!StringUtils.isEmpty(newEntry.getAddressBookEntry().getEmailAddress())) {
      Pattern emailPattern =
          Pattern.compile("^([a-zA-Z0-9_.\\-+])+@(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,4}$");
      Matcher matcher = emailPattern.matcher(newEntry.getAddressBookEntry().getEmailAddress());
      if (!matcher.matches()) {
        getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid Email Address");
        return false;
      }
    }
    return true;
  }
  @Override
  public void storeRepresentation(Representation entity) throws ResourceException {
    PhonebookEntryRepresentation representation = new PhonebookEntryRepresentation(entity);
    PhonebookEntry newEntry = representation.getObject();
    if (!validatePhonebookEntry(newEntry)) {
      return;
    }

    PhonebookEntry duplicateEntry =
        m_phonebookManager.getDuplicatePhonebookEntry(newEntry, getUser());
    if (duplicateEntry != null && !duplicateEntry.getId().equals(Integer.parseInt(m_entryId))) {
      setDuplicateEntryStatus();
      return;
    }

    Phonebook privatePhonebook = m_phonebookManager.getPrivatePhonebook(getUser());
    if (privatePhonebook != null) {
      newEntry.setPhonebook(privatePhonebook);
      PhonebookEntry entry = m_phonebookManager.getPhonebookEntry(Integer.parseInt(m_entryId));
      entry.update(newEntry);
      m_phonebookManager.updatePhonebookEntry(entry);
    }
  }