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

com.wizzdi.messaging.data.MessageRepository Maven / Gradle / Ivy

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

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.data.BaseclassRepository;
import com.wizzdi.flexicore.security.data.BasicRepository;
import com.wizzdi.flexicore.security.request.BasicPropertiesFilter;
import com.wizzdi.flexicore.security.request.SoftDeleteOption;
import com.wizzdi.messaging.model.*;
import com.wizzdi.messaging.request.MessageFilter;
import com.wizzdi.messaging.response.UnreadMessagesSummaryItem;
import org.pf4j.Extension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.*;
import jakarta.persistence.metamodel.SingularAttribute;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@Extension
public class MessageRepository implements Plugin {
	@PersistenceContext
	private EntityManager em;
	@Autowired
	private BaseclassRepository baseclassRepository;
	@Autowired
	private BasicRepository basicRepository;


	public List listAllMessages(MessageFilter MessageFilter, SecurityContextBase securityContext) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery q = cb.createQuery(Message.class);
		Root r = q.from(Message.class);
		List predicates = new ArrayList<>();
		addMessagePredicates(MessageFilter, cb, q, r, predicates, securityContext);
		q.select(r).where(predicates.toArray(Predicate[]::new)).orderBy(cb.asc(r.get(Message_.creationDate)));
		TypedQuery query = em.createQuery(q);
		BasicRepository.addPagination(MessageFilter, query);
		return query.getResultList();

	}

	public  void addMessagePredicates(MessageFilter messageFilter, CriteriaBuilder cb, CommonAbstractCriteria q, From r, List predicates, SecurityContextBase securityContext) {
		
		if(messageFilter.getBasicPropertiesFilter()==null){
			messageFilter.setBasicPropertiesFilter(new BasicPropertiesFilter().setSoftDelete(SoftDeleteOption.NON_DELETED_ONLY));
		}
		BasicRepository.addBasicPropertiesFilter(messageFilter.getBasicPropertiesFilter(),cb,q,r,predicates);

		if(messageFilter.getAddressedTo()!=null&&!messageFilter.getAddressedTo().isEmpty()){
			Set ids=messageFilter.getAddressedTo().stream().map(f->f.getId()).collect(Collectors.toSet());

			Join join=r.join(Message_.chat);
			Join senderJoin=r.join(Message_.sender);
			Join linkJoin=join.join(Chat_.chatToChatUsers);
			Join chatUserJoin=linkJoin.join(ChatToChatUser_.chatUser);
			Predicate participatesInChat = chatUserJoin.get(ChatUser_.id).in(ids);
			Predicate notSender = cb.not(senderJoin.get(ChatUser_.id).in(ids));
			predicates.add(cb.and(participatesInChat, notSender));
		}
		if(messageFilter.getChats()!=null&&!messageFilter.getChats().isEmpty()){
			Set ids=messageFilter.getChats().stream().map(f->f.getId()).collect(Collectors.toSet());
			Join join=r.join(Message_.chat);
			predicates.add(join.get(Chat_.id).in(ids));
		}

		if(messageFilter.getSenders()!=null&&!messageFilter.getSenders().isEmpty()){
			Set ids=messageFilter.getSenders().stream().map(f->f.getId()).collect(Collectors.toSet());
			Join join=r.join(Message_.sender);
			predicates.add(join.get(ChatUser_.id).in(ids));
		}
		if(messageFilter.getUnreadBy()!=null&&!messageFilter.getUnreadBy().isEmpty()){

			Set ids=messageFilter.getUnreadBy().stream().map(f->f.getId()).collect(Collectors.toSet());
			Predicate pred = cb.or();
			for (String id : ids) {
				Expression userField = cb.function("jsonb_extract_path_text",
						String.class,
						r.get("other"),
						cb.literal(Message.CHATUSERS_FIELD),
						cb.literal(id));
				pred=cb.or(pred,userField.isNull());
			}
			predicates.add(pred);

		}


	}

	public long countAllMessages(MessageFilter MessageFilter, SecurityContextBase securityContext) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery q = cb.createQuery(Long.class);
		Root r = q.from(Message.class);
		List predicates = new ArrayList<>();
		addMessagePredicates(MessageFilter, cb, q, r, predicates, securityContext);
		q.select(cb.count(r)).where(predicates.toArray(Predicate[]::new));
		TypedQuery query = em.createQuery(q);
		return query.getSingleResult();

	}

	@Transactional
	public void merge(Object base) {
		basicRepository.merge(base);
	}

	@Transactional
	public void massMerge(List toMerge) {
		basicRepository.massMerge(toMerge);
	}

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

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

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

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

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

	public  T findByIdOrNull(Class type, String id) {
		return baseclassRepository.findByIdOrNull(type, id);
	}

	public List getMessageSummary(MessageFilter messageFilter, SecurityContextBase securityContext) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery q = cb.createQuery(UnreadMessagesSummaryItem.class);
		Root r = q.from(Message.class);
		List predicates = new ArrayList<>();
		addMessagePredicates(messageFilter, cb, q, r, predicates, securityContext);
		Join chatJoin=r.join(Message_.chat);
		q.select(cb.construct(UnreadMessagesSummaryItem.class,chatJoin.get(Chat_.id),cb.count(r)))
				.where(predicates.toArray(Predicate[]::new))
				.groupBy(chatJoin.get(Chat_.id))
				.orderBy(cb.asc(chatJoin.get(Chat_.id)));
		TypedQuery query = em.createQuery(q);
		return query.getResultList();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy