Skip to content

Commit

Permalink
feature (Conversation/ContactMessage): Synch ContactMessage with upda…
Browse files Browse the repository at this point in the history
…ted specs
  • Loading branch information
JPPortier committed Aug 9, 2024
1 parent 223780e commit c2901f4
Show file tree
Hide file tree
Showing 32 changed files with 673 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.sinch.sdk.domains.conversation.api.v1.adapters.messages.WhatsAppInteractiveHeaderMapper;
import com.sinch.sdk.domains.conversation.models.v1.messages.internal.AppMessageInternalMapper;
import com.sinch.sdk.domains.conversation.models.v1.messages.internal.ChannelSpecificMessageInternalMapper;
import com.sinch.sdk.domains.conversation.models.v1.messages.internal.ContactMessageInternalMapper;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.carousel.CarouselMessageMapper;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.internal.ChoiceMessageMapper;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.template.TemplateMessageMapper;
Expand Down Expand Up @@ -127,6 +128,7 @@ private LocalLazyInit() {
ContactEventMapper.initMapper();
ContactMessageEventMapper.initMapper();
ContactMessageMapper.initMapper();
ContactMessageInternalMapper.initMapper();
ConversationMessageMapper.initMapper();
ListSectionMapper.initMapper();
OmniMessageOverrideMapper.initMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.sinch.sdk.core.utils.databind.Mapper;
import com.sinch.sdk.domains.conversation.models.v1.messages.AppMessage;
import com.sinch.sdk.domains.conversation.models.v1.messages.AppMessage.Builder;
import com.sinch.sdk.domains.conversation.models.v1.messages.AppMessageBody;
import com.sinch.sdk.domains.conversation.models.v1.messages.AppMessageImpl;
import com.sinch.sdk.domains.conversation.models.v1.messages.internal.AppMessageInternal;
import com.sinch.sdk.domains.conversation.models.v1.messages.internal.AppMessageInternalImpl;
Expand All @@ -29,14 +31,15 @@ public class AppMessageMapper {
private static final Logger LOGGER = Logger.getLogger(AppMessageMapper.class.getName());

public static void initMapper() {

SimpleModule module =
new SimpleModule()
.addSerializer(AppMessage.class, new Serializer())
.addDeserializer(AppMessage.class, new Deserializer());
Mapper.getInstance().registerModule(module);
}

static class Deserializer extends StdDeserializer<AppMessage<?>> {
static class Deserializer extends StdDeserializer<AppMessage> {

public Deserializer() {
this(null);
Expand All @@ -53,20 +56,20 @@ public AppMessage<?> deserialize(JsonParser jp, DeserializationContext ctxt)
AppMessageInternalImpl deserialized =
(AppMessageInternalImpl) jp.readValueAs(AppMessageInternal.class);

AppMessage.Builder internal = AppMessage.builder();
Builder<AppMessageBody> internal = AppMessage.builder();
deserialized.explicitChannelMessage().ifPresent(internal::setExplicitChannelMessage);
deserialized.explicitChannelOmniMessage().ifPresent(internal::setExplicitChannelOmniMessage);
deserialized.channelSpecificMessage().ifPresent(internal::setChannelSpecificMessage);
deserialized.agent().ifPresent(internal::setAgent);
deserialized.cardMessage().ifPresent(internal::setMessage);
deserialized.carouselMessage().ifPresent(internal::setMessage);
deserialized.choiceMessage().ifPresent(internal::setMessage);
deserialized.contactInfoMessage().ifPresent(internal::setMessage);
deserialized.listMessage().ifPresent(internal::setMessage);
deserialized.locationMessage().ifPresent(internal::setMessage);
deserialized.mediaMessage().ifPresent(internal::setMessage);
deserialized.templateMessage().ifPresent(internal::setMessage);
deserialized.textMessage().ifPresent(internal::setMessage);
deserialized.cardMessage().ifPresent(internal::setBody);
deserialized.carouselMessage().ifPresent(internal::setBody);
deserialized.choiceMessage().ifPresent(internal::setBody);
deserialized.contactInfoMessage().ifPresent(internal::setBody);
deserialized.listMessage().ifPresent(internal::setBody);
deserialized.locationMessage().ifPresent(internal::setBody);
deserialized.mediaMessage().ifPresent(internal::setBody);
deserialized.templateMessage().ifPresent(internal::setBody);
deserialized.textMessage().ifPresent(internal::setBody);
return internal.build();
}
}
Expand All @@ -93,7 +96,7 @@ public void serialize(AppMessage raw, JsonGenerator jgen, SerializerProvider pro
value.channelSpecificMessage().ifPresent(internal::setChannelSpecificMessage);
value.agent().ifPresent(internal::setAgent);
value
.message()
.body()
.ifPresent(
message -> {
if (message instanceof CardMessage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,118 @@
package com.sinch.sdk.domains.conversation.api.v1.adapters.messages;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.sinch.sdk.core.utils.databind.Mapper;
import com.sinch.sdk.domains.conversation.models.v1.messages.ContactMessage;
import com.sinch.sdk.domains.conversation.models.v1.messages.ContactMessage.Builder;
import com.sinch.sdk.domains.conversation.models.v1.messages.ContactMessageBody;
import com.sinch.sdk.domains.conversation.models.v1.messages.ContactMessageImpl;
import com.sinch.sdk.domains.conversation.models.v1.messages.internal.ContactMessageInternal;
import com.sinch.sdk.domains.conversation.models.v1.messages.internal.ContactMessageInternalImpl;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.channelspecific.ChannelSpecificContactMessage;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.choiceresponse.ChoiceResponseMessage;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.fallback.FallbackMessage;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.location.LocationMessage;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.media.MediaMessage;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.mediacard.MediaCardMessage;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.productresponse.ProductResponseMessage;
import com.sinch.sdk.domains.conversation.models.v1.messages.types.text.TextMessage;
import java.io.IOException;
import java.util.logging.Logger;

public class ContactMessageMapper {

private static final Logger LOGGER = Logger.getLogger(ContactMessageMapper.class.getName());

public static void initMapper() {
SimpleModule module =
new SimpleModule().addDeserializer(ContactMessage.class, new Deserializer());
new SimpleModule()
.addSerializer(ContactMessage.class, new Serializer())
.addDeserializer(ContactMessage.class, new Deserializer());
Mapper.getInstance().registerModule(module);
}

static class Deserializer extends StdDeserializer<ContactMessage> {
static class Deserializer extends StdDeserializer<ContactMessage<?>> {

public Deserializer() {
this(null);
}

public Deserializer(Class<ContactMessage> vc) {
public Deserializer(Class<ContactMessage<?>> vc) {
super(vc);
}

@Override
public ContactMessage deserialize(JsonParser jp, DeserializationContext ctxt)
public ContactMessage<?> deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {

Object deserialized = jp.readValueAs(ContactMessageInternalImpl.class).getActualInstance();
return (ContactMessage) deserialized;
ContactMessageInternalImpl deserialized =
(ContactMessageInternalImpl) jp.readValueAs(ContactMessageInternal.class);
Builder<ContactMessageBody> internal = ContactMessage.builder();

deserialized.channelSpecificMessage().ifPresent(internal::setBody);
deserialized.choiceResponseMessage().ifPresent(internal::setBody);
deserialized.fallbackMessage().ifPresent(internal::setBody);
deserialized.locationMessage().ifPresent(internal::setBody);
deserialized.mediaCardMessage().ifPresent(internal::setBody);
deserialized.mediaMessage().ifPresent(internal::setBody);
deserialized.productResponseMessage().ifPresent(internal::setBody);
deserialized.textMessage().ifPresent(internal::setBody);
deserialized.replyTo().ifPresent(internal::setReplyTo);

return internal.build();
}
}

static class Serializer extends StdSerializer<ContactMessage> {

public Serializer() {
this(null);
}

public Serializer(Class<ContactMessage> t) {
super(t);
}

@Override
public void serialize(ContactMessage raw, JsonGenerator jgen, SerializerProvider provider)
throws IOException {

ContactMessageImpl<?> value = (ContactMessageImpl<?>) raw;
ContactMessageInternal.Builder internal = ContactMessageInternal.builder();

value.reply().ifPresent(internal::setReplyTo);
value
.body()
.ifPresent(
message -> {
if (message instanceof ChannelSpecificContactMessage) {
internal.setChannelSpecificMessage((ChannelSpecificContactMessage) message);
} else if (message instanceof ChoiceResponseMessage) {
internal.setChoiceResponseMessage((ChoiceResponseMessage) message);
} else if (message instanceof FallbackMessage) {
internal.setFallbackMessage((FallbackMessage) message);
} else if (message instanceof LocationMessage) {
internal.setLocationMessage((LocationMessage) message);
} else if (message instanceof MediaCardMessage) {
internal.setMediaCardMessage((MediaCardMessage) message);
} else if (message instanceof MediaMessage) {
internal.setMediaMessage((MediaMessage) message);
} else if (message instanceof ProductResponseMessage) {
internal.setProductResponseMessage((ProductResponseMessage) message);
} else if (message instanceof TextMessage) {
internal.setTextMessage((TextMessage) message);
} else {
LOGGER.severe("Unexpected class '" + message.getClass() + "'");
}
});

jgen.writeObject(internal.build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ static <T extends AppMessageBody> Builder<T> builder() {
/** Dedicated Builder */
interface Builder<T extends AppMessageBody> {

Builder<T> setMessage(T message);
/**
* see getter
*
* @param message see getter
* @return Current builder
* @see #getBody()
*/
Builder<T> setBody(T message);

/**
* see getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public T getBody() {
return message.orElse(null);
}

public OptionalValue<T> message() {
public OptionalValue<T> body() {
return message;
}

Expand Down Expand Up @@ -99,7 +99,7 @@ static class Builder<T extends AppMessageBody> implements AppMessage.Builder<T>
OptionalValue.empty();
OptionalValue<Agent> agent = OptionalValue.empty();

public Builder<T> setMessage(T message) {
public Builder<T> setBody(T message) {
this.message = OptionalValue.of(message);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
package com.sinch.sdk.domains.conversation.models.v1.messages;

public interface ContactMessage extends ConversationMessageBody {}
/**
* Message originating from a Contact
*
* @param <T> Body type for message
* @since _NEXT_VERSION_
*/
public interface ContactMessage<T extends ContactMessageBody> extends ConversationMessageBody {

/**
* Get message body
*
* @return Message body
*/
T getBody();

/**
* @see ReplyTo
* @return reply to
*/
ReplyTo getReplyTo();

/**
* Getting Builder
*
* @return New Builder instance
* @since 1.0
*/
static <T extends ContactMessageBody> Builder<T> builder() {
return new ContactMessageImpl.Builder<>();
}

/** Dedicated Builder */
interface Builder<T extends ContactMessageBody> {

/**
* see getter
*
* @param message see getter
* @return Current builder
* @see #getBody()
*/
Builder<T> setBody(T message);

/**
* see getter
*
* @param replyTo see getter
* @return Current builder
* @see #getReplyTo()
*/
Builder<T> setReplyTo(ReplyTo replyTo);

ContactMessage<T> build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.sinch.sdk.domains.conversation.models.v1.messages;

public interface ContactMessageBody {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.sinch.sdk.domains.conversation.models.v1.messages;

import com.sinch.sdk.core.models.OptionalValue;

public class ContactMessageImpl<T extends ContactMessageBody> implements ContactMessage<T> {

private final OptionalValue<T> message;

private final OptionalValue<ReplyTo> replyTo;

public ContactMessageImpl(OptionalValue<T> message, OptionalValue<ReplyTo> replyTo) {
this.message = message;
this.replyTo = replyTo;
}

public T getBody() {
return message.orElse(null);
}

public OptionalValue<T> body() {
return message;
}

public ReplyTo getReplyTo() {
return replyTo.orElse(null);
}

public OptionalValue<ReplyTo> reply() {
return replyTo;
}

@Override
public String toString() {
return "ContactMessageImpl{" + "message=" + message + ", replyTo=" + replyTo + '}';
}

/** Dedicated Builder */
static class Builder<T extends ContactMessageBody> implements ContactMessage.Builder<T> {

OptionalValue<T> message = OptionalValue.empty();
OptionalValue<ReplyTo> replyTo = OptionalValue.empty();

public Builder<T> setBody(T message) {
this.message = OptionalValue.of(message);
return this;
}

public Builder<T> setReplyTo(ReplyTo replyTo) {
this.replyTo = OptionalValue.of(replyTo);
return this;
}

public ContactMessage<T> build() {
return new ContactMessageImpl<>(message, replyTo);
}
}
}
Loading

0 comments on commit c2901f4

Please sign in to comment.