All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wizzdi.messaging.service.ChatToChatUserService Maven / Gradle / Ivy

There is a newer version: 8.0.3
Show newest version
package com.wizzdi.messaging.service;

import com.flexicore.model.Baseclass;
import com.flexicore.model.Basic;
import com.flexicore.security.SecurityContextBase;
import com.wizzdi.flexicore.boot.base.interfaces.Plugin;
import com.wizzdi.flexicore.security.response.PaginationResponse;
import com.wizzdi.flexicore.security.service.BasicService;
import com.wizzdi.messaging.data.ChatToChatUserRepository;
import com.wizzdi.messaging.model.*;
import com.wizzdi.messaging.request.ChatToChatUserCreate;
import com.wizzdi.messaging.request.ChatToChatUserFilter;
import com.wizzdi.messaging.request.ChatToChatUserUpdate;
import org.pf4j.Extension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;

import jakarta.persistence.metamodel.SingularAttribute;
import java.util.*;
import java.util.stream.Collectors;

@Extension
@Component
public class ChatToChatUserService implements Plugin {


	@Autowired
	private ChatToChatUserRepository chatToChatUserRepository;
	@Autowired
	private BasicService basicService;


	public ChatToChatUser createChatToChatUser(ChatToChatUserCreate chatToChatUserCreate, SecurityContextBase securityContext) {
		ChatToChatUser chatToChatUser = createChatToChatUserNoMerge(chatToChatUserCreate, securityContext);
		chatToChatUserRepository.merge(chatToChatUser);
		return chatToChatUser;
	}

	public void merge(Object o) {
		chatToChatUserRepository.merge(o);
	}

	public void massMerge(List list) {
		chatToChatUserRepository.massMerge(list);
	}

	public  List listByIds(Class c, Set ids, SecurityContextBase securityContext) {
		return chatToChatUserRepository.listByIds(c, ids, securityContext);
	}

	public  List listByIds(Class c, Set ids, SingularAttribute baseclassAttribute, SecurityContextBase securityContext) {
		return chatToChatUserRepository.listByIds(c, ids, baseclassAttribute, securityContext);
	}

	public ChatToChatUser createChatToChatUserNoMerge(ChatToChatUserCreate chatToChatUserCreate, SecurityContextBase securityContext) {
		ChatToChatUser chatToChatUser = new ChatToChatUser();
		chatToChatUser.setId(Baseclass.getBase64ID());
		updateChatToChatUserNoMerge(chatToChatUserCreate, chatToChatUser);
		return chatToChatUser;
	}

	public boolean updateChatToChatUserNoMerge(ChatToChatUserCreate chatToChatUserCreate, ChatToChatUser chatToChatUser) {
		boolean update = basicService.updateBasicNoMerge(chatToChatUserCreate, chatToChatUser);
		if (chatToChatUserCreate.getChatUser() != null && (chatToChatUser.getChatUser() == null || !chatToChatUserCreate.getChatUser().getId().equals(chatToChatUser.getChatUser().getId()))) {
			chatToChatUser.setChatUser(chatToChatUserCreate.getChatUser());
			update = true;
		}

		if (chatToChatUserCreate.getChat() != null && (chatToChatUser.getChat() == null || !chatToChatUserCreate.getChat().getId().equals(chatToChatUser.getChat().getId()))) {
			chatToChatUser.setChat(chatToChatUserCreate.getChat());
			update = true;
		}
		if (chatToChatUserCreate.getDisabled() != null && !chatToChatUserCreate.getDisabled().equals(chatToChatUser.isDisabled())) {
			chatToChatUser.setDisabled(chatToChatUserCreate.getDisabled());
			update = true;
		}
		return update;
	}

	public ChatToChatUser updateChatToChatUser(ChatToChatUserUpdate chatToChatUserUpdate, SecurityContextBase securityContext) {
		ChatToChatUser ChatToChatUser = chatToChatUserUpdate.getChatToChatUser();
		if (updateChatToChatUserNoMerge(chatToChatUserUpdate, ChatToChatUser)) {
			chatToChatUserRepository.merge(ChatToChatUser);
		}
		return ChatToChatUser;
	}

	public void validate(ChatToChatUserCreate chatToChatUserCreate, SecurityContextBase securityContext) {
		basicService.validate(chatToChatUserCreate, securityContext);
		String chatId = chatToChatUserCreate.getChatId();
		Chat chat = chatId != null ? getByIdOrNull(chatId, Chat.class, Chat_.security, securityContext) : null;
		if (chatId != null && chat == null) {
			throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "no chat with id " + chatId);
		}
		chatToChatUserCreate.setChat(chat);

		String chatUserId = chatToChatUserCreate.getChatUserId();
		ChatUser chatUser = chatUserId != null ? getByIdOrNull(chatUserId, ChatUser.class, ChatUser_.security, securityContext) : null;
		if (chatUserId != null && chatUser == null) {
			throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "no chatUser with id " + chatUserId);
		}
		chatToChatUserCreate.setChatUser(chatUser);

	}

	public void validate(ChatToChatUserFilter chatToChatUserFilter, SecurityContextBase securityContext) {
		basicService.validate(chatToChatUserFilter, securityContext);
		Set chatIds=chatToChatUserFilter.getChatsIds();
		Map chatMap=chatIds.isEmpty()?new HashMap<>():chatToChatUserRepository.listByIds(Chat.class,chatIds,Chat_.security,securityContext).stream().collect(Collectors.toMap(f->f.getId(),f->f));
		chatIds.removeAll(chatMap.keySet());
		if(!chatIds.isEmpty()){
			throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "no chats with ids " + chatIds);
		}
		chatToChatUserFilter.setChats(new ArrayList<>(chatMap.values()));

		Set chatUserIds=chatToChatUserFilter.getChatUsersIds();
		Map chatUserMap=chatUserIds.isEmpty()?new HashMap<>():listByIds(ChatUser.class,chatUserIds,ChatUser_.security,securityContext).stream().collect(Collectors.toMap(f->f.getId(),f->f));
		chatUserIds.removeAll(chatUserMap.keySet());
		if(!chatUserIds.isEmpty()){
			throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "no chat users with ids " + chatUserIds);
		}
		chatToChatUserFilter.setChatUsers(new ArrayList<>(chatUserMap.values()));

		if(chatToChatUserFilter.getChatUsers().isEmpty()&&chatToChatUserFilter.getChats().isEmpty()){
			throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"must specify at least one chat or chat user");
		}



	}

	public  T getByIdOrNull(String id, Class c, SingularAttribute baseclassAttribute, SecurityContextBase securityContext) {
		return chatToChatUserRepository.getByIdOrNull(id, c, baseclassAttribute, securityContext);
	}

	public  T getByIdOrNull(String id, Class c, SecurityContextBase securityContext) {
		return chatToChatUserRepository.getByIdOrNull(id, c, securityContext);
	}

	public PaginationResponse getAllChatToChatUsers(ChatToChatUserFilter ChatToChatUserFilter, SecurityContextBase securityContext) {
		List list = listAllChatToChatUsers(ChatToChatUserFilter, securityContext);
		long count = chatToChatUserRepository.countAllChatToChatUsers(ChatToChatUserFilter, securityContext);
		return new PaginationResponse<>(list, ChatToChatUserFilter, count);
	}

	public List listAllChatToChatUsers(ChatToChatUserFilter ChatToChatUserFilter, SecurityContextBase securityContext) {
		return chatToChatUserRepository.listAllChatToChatUsers(ChatToChatUserFilter, securityContext);
	}

	public  List findByIds(Class c, Set requested) {
		return chatToChatUserRepository.findByIds(c, requested);
	}

}