Skip to content

Commit

Permalink
added participants to chat
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelgloeckner committed Jan 26, 2024
1 parent a56a230 commit dea3a5c
Show file tree
Hide file tree
Showing 16 changed files with 493 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ChatSearchCriteria {

private String appId;

private String creationUser;
private String participant;

private Integer pageNumber;

Expand Down
15 changes: 12 additions & 3 deletions src/main/java/io/github/onecx/chat/domain/daos/ChatDAO.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.github.onecx.chat.domain.daos;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.EntityGraph;
import jakarta.persistence.NoResultException;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.Predicate;
import jakarta.transaction.Transactional;

import org.tkit.quarkus.jpa.daos.AbstractDAO;
Expand All @@ -14,6 +17,7 @@
import io.github.onecx.chat.domain.criteria.ChatSearchCriteria;
import io.github.onecx.chat.domain.models.Chat;
import io.github.onecx.chat.domain.models.Chat_;
import io.github.onecx.chat.domain.models.Participant;

@ApplicationScoped
@Transactional(Transactional.TxType.NOT_SUPPORTED)
Expand All @@ -27,7 +31,10 @@ public Chat findById(Object id) throws DAOException {
var cq = cb.createQuery(Chat.class);
var root = cq.from(Chat.class);
cq.where(cb.equal(root.get(TraceableEntity_.ID), id));
return this.getEntityManager().createQuery(cq).getSingleResult();

EntityGraph graph = this.em.getEntityGraph(Chat.CHAT_LOAD);

return this.getEntityManager().createQuery(cq).setHint(HINT_LOAD_GRAPH, graph).getSingleResult();
} catch (NoResultException nre) {
return null;
} catch (Exception e) {
Expand All @@ -49,8 +56,10 @@ public PageResult<Chat> findChatsByCriteria(ChatSearchCriteria criteria) {
cq.where(cb.equal(root.get(Chat_.type), criteria.getType()));
}

if (criteria.getCreationUser() != null) {
cq.where(cb.equal(root.get(Chat_.creationUser), criteria.getCreationUser()));
if (criteria.getParticipant() != null) {
Join<Chat, Participant> participantsJoin = root.join("participants");
Predicate predicate = cb.equal(participantsJoin.get("userId"), criteria.getParticipant());
cq.where(predicate);
}

return createPageQuery(cq, Page.of(criteria.getPageNumber(), criteria.getPageSize())).getPageResult();
Expand Down
23 changes: 0 additions & 23 deletions src/main/java/io/github/onecx/chat/domain/daos/MessageDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,6 @@
@Transactional(Transactional.TxType.NOT_SUPPORTED)
public class MessageDAO extends AbstractDAO<Message> {

/*
* @Transactional(value = Transactional.TxType.REQUIRED, rollbackOn = DAOException.class)
* public Message addMessage(String chatId, Message message) {
*
* try {
* Chat chat = this.findById(chatId);
*
* // Ensure the chat is not null
* if (chat != null) {
* // Add the new message to the chat's messages set
* chat.getMessages().add(message);
* this.update(chat);
* }
* return message;
* } catch (NoResultException nre) {
* return null;
* } catch (Exception e) {
* throw new DAOException(ErrorKeys.ERROR_CREATE_MESSAGE, e, entityName, chatId);
* }
*
* }
*/

public enum ErrorKeys {

ERROR_CREATE_MESSAGE,
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/github/onecx/chat/domain/daos/ParticipantDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.onecx.chat.domain.daos;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.transaction.Transactional;

import org.tkit.quarkus.jpa.daos.AbstractDAO;

import io.github.onecx.chat.domain.models.Participant;

@ApplicationScoped
@Transactional(Transactional.TxType.NOT_SUPPORTED)
public class ParticipantDAO extends AbstractDAO<Participant> {

public enum ErrorKeys {

ERROR_CREATE_PARTICIPANT,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public abstract class DataImportMapperV1 {
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "tenantId", ignore = true)
@Mapping(target = "messages", ignore = true)
@Mapping(target = "participants", ignore = true)
public abstract Chat importChat(DataImportChatDTOV1 dto);

@Named("properties")
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/github/onecx/chat/domain/models/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public class Chat extends TraceableEntity {
@OrderBy("creationDate ASC")
private Set<Message> messages = new HashSet<>();

@OneToMany(cascade = ALL, fetch = LAZY, mappedBy = "chat", orphanRemoval = true)
@OrderBy("creationDate ASC")
private Set<Participant> participants = new HashSet<>();

public enum ChatType {
HUMAN_CHAT,
AI_CHAT
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/io/github/onecx/chat/domain/models/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ public class Message extends TraceableEntity {
@Column(name = "RELIABILITY")
private Float reliability;

@Column(name = "APP_ID")
private String appId;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "CHAT_ID")
private Chat chat;
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/io/github/onecx/chat/domain/models/Participant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.github.onecx.chat.domain.models;

import static jakarta.persistence.FetchType.LAZY;

import jakarta.persistence.*;

import org.hibernate.annotations.TenantId;
import org.tkit.quarkus.jpa.models.TraceableEntity;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "PARTICIPANT")
public class Participant extends TraceableEntity {

@TenantId
@Column(name = "TENANT_ID")
private String tenantId;

@Column(name = "USER_ID")
private String userId;

@Column(name = "EMAIL")
private String email;

@Column(name = "TYPE")
@Enumerated(EnumType.STRING)
private ParticipantType type;

@Column(name = "USER_NAME")
private String userName;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "CHAT_ID")
private Chat chat;

public enum ParticipantType {
HUMAN,
ASSISTANT,
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.validation.ConstraintViolationException;
import jakarta.ws.rs.Path;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
Expand All @@ -22,22 +23,26 @@
import gen.io.github.onecx.chat.rs.internal.model.*;
import io.github.onecx.chat.domain.daos.ChatDAO;
import io.github.onecx.chat.domain.daos.MessageDAO;
import io.github.onecx.chat.domain.daos.ParticipantDAO;
import io.github.onecx.chat.domain.models.Chat.ChatType;
import io.github.onecx.chat.domain.models.Message;
import io.github.onecx.chat.domain.models.Message.MessageType;
import io.github.onecx.chat.domain.models.Participant;
import io.github.onecx.chat.rs.internal.mappers.ChatMapper;
import io.github.onecx.chat.rs.internal.mappers.ExceptionMapper;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Path("/internal/chats") // remove this after quarkus fix ServiceExceptionMapper for impl classes
@ApplicationScoped
@Transactional(value = NOT_SUPPORTED)
public class ChatsRestController implements ChatsInternalApi {

@Inject
ChatDAO dao;

@Inject
ParticipantDAO participantDao;

@Inject
MessageDAO msgDao;

Expand All @@ -53,11 +58,22 @@ public class ChatsRestController implements ChatsInternalApi {
@Override
@Transactional
public Response createChat(CreateChatDTO createChatDTO) {

var chat = mapper.create(createChatDTO);
chat = dao.create(chat);

if (createChatDTO.getParticipants() != null && !createChatDTO.getParticipants().isEmpty()) {
var participants = mapper.mapParticipantDTOs(createChatDTO.getParticipants());
for (Participant participant : participants) {
participant.setChat(chat);
participant = participantDao.create(participant);
}

}

return Response
.created(uriInfo.getAbsolutePathBuilder().path(chat.getId()).build())
.entity(mapper.map(chat))
.entity(mapper.mapChat(chat))
.build();
}

Expand All @@ -74,7 +90,7 @@ public Response getChatById(String id) {
if (chat == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.ok(mapper.map(chat)).build();
return Response.ok(mapper.mapChat(chat)).build();
}

@Override
Expand Down Expand Up @@ -121,7 +137,6 @@ public Response createChatMessage(String chatId, CreateMessageDTO createMessageD
if (ChatType.AI_CHAT.equals(chat.getType())) {
//TODO call onecx-ai-svc rest api to generate answer
Message aiMessage = new Message();
aiMessage.setAppId(chat.getAppId());
aiMessage.setChat(chat);
aiMessage.setCreationUser(ChatType.AI_CHAT.name());
aiMessage.setModificationUser(ChatType.AI_CHAT.name());
Expand Down Expand Up @@ -150,7 +165,42 @@ public Response getChatMessages(String chatId) {

var messages = chat.getMessages();
List<Message> messageList = new ArrayList<>(messages);
return Response.ok(mapper.mapList(messageList)).build();
return Response.ok(mapper.mapMessageList(messageList)).build();

}

@Override
public Response addParticipant(String chatId, @Valid @NotNull AddParticipantDTO addParticipantDTO) {

var chat = dao.findById(chatId);

if (chat == null) {
throw new ConstraintException("Chat does not exist", ChatErrorKeys.CHAT_DOES_NOT_EXIST, null);
}

var participant = mapper.addParticipant(addParticipantDTO);
participant.setChat(chat);
participant = participantDao.create(participant);

return Response
.created(uriInfo.getAbsolutePathBuilder().path(participant.getId()).build())
.build();

}

@Override
public Response getChatParticipants(String chatId) {

var chat = dao.findById(chatId);

if (chat == null || chat.getParticipants() == null) {
// Handle the case where chat or its messages are null
return Response.status(Response.Status.NOT_FOUND).build();
}

var participants = chat.getParticipants();
List<Participant> participantList = new ArrayList<>(participants);
return Response.ok(mapper.mapParticipantList(participantList)).build();

}

Expand Down
Loading

0 comments on commit dea3a5c

Please sign in to comment.