/**
  * Returns a list of all the known contacts contained within FreeAgent for the authorised account.
  *
  * @return A list of Contact instances.
  */
 public List<FreeAgentContact> getContacts() {
   FreeAgentContactWrapper contactsWrapper = freeAgentServiceInstance.getContacts();
   if (contactsWrapper != null) {
     return contactsWrapper.getContacts();
   }
   return null;
 }
 /**
  * Returns a list of all the known contacts contained within FreeAgentService for the authorised
  * account.
  *
  * @param viewType The view type {@link
  *     com.karlnosworthy.freeagent.FreeAgentClient.ContactViewType} filter to apply to the
  *     contacts
  * @return A list of Contact instances.
  */
 public List<FreeAgentContact> getContacts(ContactViewType viewType) {
   FreeAgentContactWrapper contactsWrapper =
       freeAgentServiceInstance.getContacts(viewType.identifier);
   if (contactsWrapper != null) {
     return contactsWrapper.getContacts();
   }
   return null;
 }
 /**
  * Retrieves the contact that matches the specified id.
  *
  * @param contactId The id to match.
  * @return A Contact instance or null if the id supplied was invalid or could not be matched.
  */
 public FreeAgentContact getContact(String contactId) {
   if (contactId != null && !contactId.isEmpty()) {
     FreeAgentContactWrapper contactWrapper = freeAgentServiceInstance.getContact(contactId);
     if (contactWrapper != null) {
       return contactWrapper.getContact();
     }
   }
   return null;
 }
 /**
  * Attempts to create a new contact entry in the associated FreeAgent account.
  *
  * <p>Will return null if the contact instance provided is null or cannot be saved into the
  * account.
  *
  * @param contact The populated contact instance.
  * @return The updated contact instance or null.
  */
 public FreeAgentContact createContact(FreeAgentContact contact) {
   if (contact != null) {
     FreeAgentContactWrapper contactWrapper =
         freeAgentServiceInstance.createContact(new FreeAgentContactWrapper(contact));
     if (contactWrapper != null) {
       return contactWrapper.getContact();
     }
   }
   return null;
 }
  /**
   * Retrieves the Contact associated with the specified project.
   *
   * @param project The populated project instance to retrieve the contact for.
   * @return A populated FreeAgentContact instance or null if the contact could not be found.
   */
  public FreeAgentContact getContactForProject(FreeAgentProject project) {
    if (project != null) {
      String contactId = extractIdentifier(project.getContact());

      if (contactId != null && !contactId.isEmpty()) {
        FreeAgentContactWrapper contactWrapper = freeAgentServiceInstance.getContact(contactId);

        if (contactWrapper != null) {
          return contactWrapper.getContact();
        }
      }
    }
    return null;
  }
  /**
   * Attempts to import a new contact into the associated FreeAgent account by deserialising the
   * specified JSON contact information and requesting a new contact be created.
   *
   * <p>NOTE: The import (creation within FreeAgent) will only be actioned if no URL property is
   * present or if the URL property is not populated. Otherwise null will be returned.
   *
   * @param contactJSON A string containing contact information in FreeAgent friendly format.
   * @return The newly populated contact instance that has been imported into FreeAgent or null.
   * @throws JsonSyntaxException If the format does not match the FreeAgent V2 Contact format.
   */
  public FreeAgentContact importContact(String contactJSON) throws JsonSyntaxException {
    if (contactJSON == null || contactJSON.isEmpty()) {
      return null;
    }

    FreeAgentContact contact = buildContact(contactJSON);

    if (contact != null && (contact.getUrl() == null || contact.getUrl().isEmpty())) {
      FreeAgentContactWrapper contactWrapper =
          freeAgentServiceInstance.createContact(new FreeAgentContactWrapper(contact));

      if (contactWrapper != null) {
        return contactWrapper.getContact();
      }
    }

    return null;
  }