51 lines
1.7 KiB
Java
51 lines
1.7 KiB
Java
/*
|
|
* Copyright 2025 Google. This software is provided as-is, without warranty or representation for any use or purpose.
|
|
* Your use of it is subject to your agreement with Google.
|
|
*/
|
|
|
|
package com.example.mapper.conversation;
|
|
import com.google.cloud.Timestamp;
|
|
|
|
import com.example.dto.dialogflow.conversation.ConversationMessageDTO;
|
|
import com.example.dto.dialogflow.conversation.MessageType;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.time.Instant;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
@Component
|
|
public class ConversationMessageMapper {
|
|
|
|
public Map<String, Object> toMap(ConversationMessageDTO message) {
|
|
Map<String, Object> map = new HashMap<>();
|
|
map.put("entidad", message.type().name());
|
|
map.put("tiempo", message.timestamp());
|
|
map.put("mensaje", message.text());
|
|
if (message.parameters() != null) {
|
|
map.put("parametros", message.parameters());
|
|
}
|
|
if (message.canal() != null) {
|
|
map.put("canal", message.canal());
|
|
}
|
|
return map;
|
|
}
|
|
|
|
public ConversationMessageDTO fromMap(Map<String, Object> map) {
|
|
Object timeObject = map.get("tiempo");
|
|
Instant timestamp = null;
|
|
if (timeObject instanceof Timestamp) {
|
|
timestamp = ((Timestamp) timeObject).toDate().toInstant();
|
|
} else if (timeObject instanceof Instant) {
|
|
timestamp = (Instant) timeObject;
|
|
}
|
|
return new ConversationMessageDTO(
|
|
MessageType.valueOf((String) map.get("entidad")),
|
|
timestamp,
|
|
(String) map.get("mensaje"),
|
|
(Map<String, Object>) map.get("parametros"),
|
|
(String) map.get("canal")
|
|
);
|
|
}
|
|
}
|