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

io.mateu.jpa.domain.ui.cruds.queries.count.CountQueryHandler Maven / Gradle / Ivy

There is a newer version: 3.0-alpha.63
Show newest version
package io.mateu.jpa.domain.ui.cruds.queries.count;

import io.mateu.core.domain.model.reflection.ReflectionHelper;
import io.mateu.jpa.domain.ui.cruds.queries.QueryHelper;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.Query;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Mono;

@Service
@Slf4j
public class CountQueryHandler {

  @PersistenceContext private EntityManager em;
  @Autowired ReflectionHelper reflectionHelper;

  @Transactional
  public Mono run(CountQuery query) {
    final long[] count = {0};

    try {
      Query q =
          new QueryHelper(reflectionHelper)
              .buildJpaQuery(
                  query,
                  em,
                  query.getSelectColumnsForCount(),
                  query.getSearchtext(),
                  query.getFilters(),
                  null,
                  null,
                  0,
                  1000,
                  false);
      log.debug(q.toString());

      Object r = q.getSingleResult();

      if (r instanceof Long) {
        count[0] = ((Long) r).longValue();
      } else if (r instanceof Object[]) {
        Object[] v = (Object[]) r;

        count[0] = ((Long) v[0]).longValue();
      }
    } catch (Throwable throwable) {
      throwable.printStackTrace();
    }

    return Mono.just(count[0]);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy