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

info.unterrainer.commons.httpserver.daos.ListQuery Maven / Gradle / Ivy

There is a newer version: 0.3.14
Show newest version
package info.unterrainer.commons.httpserver.daos;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.NoResultException;
import jakarta.persistence.NonUniqueResultException;
import jakarta.persistence.TypedQuery;

import info.unterrainer.commons.httpserver.jsons.ListJson;
import info.unterrainer.commons.rdbutils.Transactions;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class ListQuery

{ protected final EntityManagerFactory emf; protected final QueryInterface builder; protected V withEntityManager(final Function func) { if (builder.getEntityManager() == null) return Transactions.withNewTransactionReturning(emf, em -> func.apply(em)); return func.apply(builder.getEntityManager()); } /** * Gets all the rows as a {@link ListJson} as returned by a web-service. *

* The list contains additional information about the data. * * @return a {@link ListJson} containing the rows as specified */ public ListJson getListJson() { return getListJson(0, Long.MAX_VALUE); } /** * Gets all the rows specified by offset and size as a {@link ListJson} as * returned by a web-service. *

* The list contains additional information about the data. * * @param offset the row-number to start at * @param size the number-of-rows to return * @return a {@link ListJson} containing the rows as specified */ public ListJson getListJson(final long offset, final long size) { return withEntityManager(em -> { ListJson r = new ListJson<>(); r.setEntries(builder.getDao().getList(em, builder.getTypedQuery(em), offset, size)); r.setCount((Long) builder.getCountQuery(em).getSingleResult()); return r; }); } /** * Gets the first row this query returns. * * @return the result-row as a JPA */ public T getFirst() { List r = getList(0, 1); if (r.size() == 1) { T jpa = r.get(0); return jpa; } return null; } /** * Get the first N rows from this queries' results. * * @param count the number of rows to get * @return the list of the first N result-rows as JPAs */ public List getN(final long count) { return getList(0, count); } /** * Execute a SELECT query that returns a single result-row. * * @return the result-row as a JPA * @throws NoResultException if there is no result * @throws NonUniqueResultException if more than one result */ public T getSingle() { return withEntityManager(em -> { TypedQuery typedQuery = builder.getTypedQuery(em); return typedQuery.getSingleResult(); }); } /** * Execute a SELECT query that returns multiple result-rows. * * @return the list of result-rows as JPAs */ public List getList() { return withEntityManager(em -> builder.getDao().getList(em, builder.getTypedQuery(em), 0, Long.MAX_VALUE)); } /** * Gets all the rows specified by offset and size as a list. * * @param offset the row-number to start at * @param size the number-of-rows to return * @return a list containing the rows as specified */ public List getList(final long offset, final long size) { return withEntityManager(em -> { TypedQuery query = builder.getTypedQuery(em); List list = builder.getDao().getList(em, query, offset, size); return list; }); } /** * Execute a SELECT query that returns multiple result-rows and reverse the * order of results. *

* The reversing-process happens after retrieval of the result, since the whole * list is going to be transferred anyway.
* The list is reversed using {@code Collections.reverse(list)}; * * @return the reversed list of result-rows as JPAs */ public List getListReversed() { List l = getList(); Collections.reverse(l); return l; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy