org.openfact.models.jpa.DocumentCriteria Maven / Gradle / Ivy
The newest version!
package org.openfact.models.jpa;
import org.openfact.models.DocumentModel;
import org.openfact.models.OrganizationModel;
import org.openfact.models.jpa.entities.DocumentEntity;
import org.openfact.models.jpa.entities.DocumentRequiredActionEntity;
import org.openfact.models.search.SearchCriteriaFilterOperator;
import org.openfact.models.types.DocumentRequiredAction;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.*;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DocumentCriteria {
private final EntityManager em;
private final CriteriaBuilder cb;
private final CriteriaQuery cq;
private final Root root;
private final ArrayList predicates;
private Map orderBy;
public DocumentCriteria(OrganizationModel organization, EntityManager em, Class rClass, Class qClass) {
this.em = em;
cb = em.getCriteriaBuilder();
cq = cb.createQuery(qClass);
root = getCriteriaQuery().from(rClass);
predicates = new ArrayList<>();
orderBy = new HashMap<>();
this.predicates.add(cb.equal(root.get(JpaDocumentProvider.ORGANIZATION_ID), organization.getId()));
}
public DocumentCriteria currencyCode(String... currencyCode) {
List currencyCodes = Arrays.asList(currencyCode).stream().map(String::toUpperCase).collect(Collectors.toList());
predicates.add(cb.upper(root.get(JpaDocumentProvider.DOCUMENT_CURRENCY_CODE)).in(currencyCodes));
return this;
}
public DocumentCriteria documentType(String... documentType) {
List documentTypes = Arrays.asList(documentType).stream().map(String::toUpperCase).collect(Collectors.toList());
predicates.add(cb.upper(root.get(JpaDocumentProvider.DOCUMENT_TYPE)).in(documentTypes));
return this;
}
public DocumentCriteria filterText(String filterText, String... fieldName) {
Predicate[] orPredicates = Stream.of(fieldName)
.map(f -> cb.like(cb.upper(root.get(f)), "%" + filterText.toUpperCase() + "%"))
.toArray(size -> new Predicate[fieldName.length]);
predicates.add(cb.or(orPredicates));
return this;
}
public DocumentCriteria filterTextReplaceAsterisk(String filterText, String... fieldName) {
Predicate[] orPredicates = Stream.of(fieldName)
.map(f -> cb.like(cb.upper(root.get(f)), filterText.toUpperCase().replace('*', '%')))
.toArray(size -> new Predicate[fieldName.length]);
predicates.add(cb.or(orPredicates));
return this;
}
public void customerSendEventFailures(int numberFailures, boolean greatherThan) {
if (greatherThan) {
predicates.add(cb.greaterThan(root.get(JpaDocumentProvider.CUSTOMER_SEND_EVENT_FAILURES), numberFailures));
} else {
predicates.add(cb.lessThan(root.get(JpaDocumentProvider.CUSTOMER_SEND_EVENT_FAILURES), numberFailures));
}
}
public void thirdPartySendEventFailures(int numberFailures, boolean greatherThan) {
if (greatherThan) {
predicates.add(cb.greaterThan(root.get(JpaDocumentProvider.THIRD_PARTY_SEND_EVENT_FAILURES), numberFailures));
} else {
predicates.add(cb.lessThan(root.get(JpaDocumentProvider.THIRD_PARTY_SEND_EVENT_FAILURES), numberFailures));
}
}
public void enabled(boolean isEnabled) {
this.predicates.add(cb.equal(root.get(JpaDocumentProvider.ENABLED), isEnabled));
}
public DocumentCriteria addFilter(String key, String value) {
if (key.equals(DocumentModel.DOCUMENT_ID)) {
predicates.add(cb.equal(cb.upper(root.get(JpaDocumentProvider.DOCUMENT_ID)), value.toUpperCase()));
} else if (key.equals(DocumentModel.DOCUMENT_TYPE)) {
predicates.add(cb.equal(cb.upper(root.get(JpaDocumentProvider.DOCUMENT_TYPE)), value.toUpperCase()));
} else if (key.equals(DocumentModel.DOCUMENT_CURRENCY_CODE)) {
predicates.add(cb.equal(cb.upper(root.get(JpaDocumentProvider.DOCUMENT_CURRENCY_CODE)), value.toUpperCase()));
} else if (key.equals(DocumentModel.CUSTOMER_ASSIGNED_ACCOUNT_ID)) {
predicates.add(cb.equal(cb.upper(root.get(JpaDocumentProvider.CUSTOMER_ASSIGNED_ACCOUNT_ID)), value.toUpperCase()));
} else if (key.equals(DocumentModel.CUSTOMER_REGISTRATION_NAME)) {
predicates.add(cb.equal(cb.upper(root.get(JpaDocumentProvider.CUSTOMER_REGISTRATION_NAME)), value.toUpperCase()));
} else {
predicates.add(cb.equal(cb.upper(root.get(key)), value.toUpperCase()));
}
return this;
}
public DocumentCriteria addFilter(Map filters) {
for (Map.Entry entry : filters.entrySet()) {
addFilter(entry.getKey(), entry.getValue());
}
return this;
}
public DocumentCriteria addFilter(String key, Object value, SearchCriteriaFilterOperator operator) {
if (operator == SearchCriteriaFilterOperator.eq) {
Path