Last active
September 27, 2017 10:57
-
-
Save gitjs77/991fd1820a6c8bf2035eb260f5f8de5c to your computer and use it in GitHub Desktop.
JPA @query with search to QueryDSL with Tuple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* SQLQueryFactory for Querydsl querying8*/ | |
private SQLQueryFactory queryFactory; | |
/* Q - generated objects for use in the Querydsl querying*/ | |
private static QDeposit deposit = QDeposit.deposit; | |
private static QAccounts account = QAccounts.accounts; | |
private static QCustomer customer = QCustomer.customer; | |
/*@Query("select u from Deposit u" + | |
" where (u.currency=:depositCurrency or :depositCurrency is null)" + | |
"and(u.depositType=:depositType or :depositType is null)" + | |
"and(u.bankBranch.id=:bankBranchId or :bankBranchId is null)" + | |
"and (:searchText is null or :searchText ='' " + | |
"or lower(u.number) like lower(concat('%',:searchText,'%'))" + | |
"or lower(u.account.customer.organization.name) like lower(concat('%',:searchText,'%')) " + | |
"or lower(u.account.customer.organization.taxCode) like lower(concat('%',:searchText,'%'))" + | |
")" | |
)*/ | |
public Page<DepositAdminDTO> findByParametersAndSearch(final String searchText, final String depositCurrency, | |
final String depositType, final Long bankBranchId, | |
final GridRequest gridRequest) { | |
List<Tuple> depositAdminTupleList = queryFactory | |
.select( | |
deposit.id, deposit.nameDeposit, | |
deposit.num, deposit.status, | |
deposit.currentAmount, deposit.currency, | |
deposit.depositPercent, deposit.currentPercentAmount, | |
deposit.depositType, deposit.finishDate, | |
deposit.accountId, deposit.bankBranchId, | |
customer.name, customer.taxCode | |
) // select end | |
.from(deposit) | |
.leftJoin(account).on(account.id.eq(deposit.accountId)) | |
.leftJoin(customer).on(customer.id.eq(account.customerId)) | |
.where( | |
new OptionalBuilder(depositCurrency, () -> deposit.currency.eq(depositCurrency)) | |
.optionalAnd(depositType, () -> deposit.depositType.eq(depositType)) | |
.optionalAnd(bankBranchId, () -> deposit.bankBranchId.eq(bankBranchId)) | |
.and( | |
new OptionalBuilder(searchText != null && !searchText.isEmpty(), | |
() -> deposit.num.likeIgnoreCase("%" + searchText + "%") | |
.or(customer.name.likeIgnoreCase("%" + searchText + "%")) | |
.or(customer.taxCode.likeIgnoreCase("%" + searchText + "%")) | |
) | |
) // search - and end | |
) // where end | |
.orderBy(gridRequest.order()) | |
.restrict(gridRequest.restrict()) | |
.fetch(); | |
List<DepositAdminDTO> depositAdminDTOList = | |
depositAdminTupleList.stream().map(DepositRepositoryDB::convertToDepositAdminDTO).collect(Collectors.toList()); | |
return new Page<>(depositAdminDTOList, depositAdminDTOList.size()); | |
} | |
/** | |
* Converts tuple result to DepositAdminDTO. | |
* | |
* @param tuple - tuple result | |
* @return DepositAdminDTO | |
*/ | |
public static DepositAdminDTO convertToDepositAdminDTO(final Tuple tuple) { | |
DepositAdminDTO depositAdminDTO = new DepositAdminDTO(); | |
depositAdminDTO.setAccountId(tuple.get(deposit.id)); | |
depositAdminDTO.setNameDeposit(tuple.get(deposit.nameDeposit)); | |
depositAdminDTO.setNumber(tuple.get(deposit.num)); | |
depositAdminDTO.setStatus(tuple.get(deposit.status)); | |
depositAdminDTO.setCurrentAmount(tuple.get(deposit.currentAmount)); | |
depositAdminDTO.setCurrency(tuple.get(deposit.currency)); | |
depositAdminDTO.setPercent(tuple.get(deposit.depositPercent)); | |
depositAdminDTO.setCurrentPercentAmount(tuple.get(deposit.currentPercentAmount)); | |
depositAdminDTO.setDepositType(tuple.get(deposit.depositType)); | |
depositAdminDTO.setFinishDate(tuple.get(deposit.finishDate) == null ? null : tuple.get(deposit.finishDate).toLocalDate()); | |
depositAdminDTO.setAccountId(tuple.get(deposit.accountId)); | |
depositAdminDTO.setBankBranchId(tuple.get(deposit.bankBranchId)); | |
depositAdminDTO.setOrganizationName(tuple.get(customer.name)); | |
depositAdminDTO.setOrganizationTaxCode(tuple.get(customer.taxCode)); | |
return depositAdminDTO; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment