@Override
  public Sms toExternalMessage(Message message, boolean keepOriginals) {
    Sms sms = toSms(message);

    if (keepOriginals) {
      sms.setAddress(message.getOriginalAddress());
      sms.setContactName(message.getOriginalContactName());
    }

    return sms;
  }
  @Override
  public Message toInternalMessage(Sms sms)
      throws IllegalArgumentException, UnknownProtocolException, UnknownStatusException,
          UnknownTypeException {
    Message message = new Message(-1); // set id later

    // determine type of message
    Type type = Type.toType(Objects.requireNonNull(sms.getType()));

    // internationalize contact / address
    Contact c = getNormalizedContact(sms.getContactName(), sms.getAddress());

    message.setContact(c);
    message.setType(type);
    message.setBody(sms.getBody());
    message.setDate(getDateOrNull(sms.getDate()));

    // if message type is not SENT, do not set the date sent!
    // this would result in duplicate messages on the phone later.
    message.setDateSent((type != Type.Sent) ? null : getDateOrNull(sms.getDateSent()));

    message.setProtocol(Protocol.toProtocol(Objects.requireNonNull(sms.getProtocol())));
    message.setRead(Integer.valueOf(sms.getRead()));
    message.setServiceCenter(sms.getServiceCenter());
    message.setStatus(Status.toStatus(Objects.requireNonNull(sms.getStatus())));
    message.setSubject(sms.getSubject());

    // keep original values
    message.setOriginalAddress(sms.getAddress());
    message.setOriginalContactName(sms.getContactName());

    return message;
  }
  /**
   * Converts a Message to Sms. Overwrites old address and contact name (international format).
   *
   * @param message
   * @return
   */
  protected Sms toSms(final Message message) {
    Sms sms = new Sms();

    sms.setAddress(message.getContact().getAddressIntl());
    sms.setBody(message.getBody());
    sms.setContactName(message.getContact().getContactName());
    sms.setDate(message.getDate().getTime());

    // This validation already takes place while importing messages.
    /*
    if (message.getType() == Type.Received && message.getDateSent() != null) {
    	sms.setDateSent(null);
    	System.out.println("date information wrong.");
    } else if (message.getType() == Type.Sent) {
    	sms.setDateSent(getTimeOrNull(message.getDateSent()));
    } else {
    	System.out.println("help " + message);
    }
    */

    sms.setDateSent(getTimeOrNull(message.getDateSent()));
    sms.setLocked((short) 0); // TODO check
    sms.setProtocol((short) message.getProtocol().getValue());
    sms.setRead(message.getRead().shortValue());
    sms.setReadableDate(sdf.format(message.getDate()));
    sms.setScToa("null");
    sms.setServiceCenter(message.getServiceCenter());
    sms.setStatus((byte) message.getStatus().getValue());
    sms.setSubject(message.getSubject());
    sms.setToa("null");
    sms.setType((short) message.getType().getValue());

    return sms;
  }