Skip to content

Instantly share code, notes, and snippets.

@gitjs77
Last active September 27, 2017 11:06
Show Gist options
  • Save gitjs77/517022bf078285696975954a8e5b2020 to your computer and use it in GitHub Desktop.
Save gitjs77/517022bf078285696975954a8e5b2020 to your computer and use it in GitHub Desktop.
JPA @query with search to Querydsl with Projections.constructor
/* SQLQueryFactory for Querydsl querying*/
private SQLQueryFactory queryFactory;
/* Q - generated objects for use in the Querydsl querying*/
private static QCard card = QCard.card;
private static QAccounts account = QAccounts.accounts;
private static QCustomer customer = QCustomer.customer;
private static QBankBranch bankBranch = QBankBranch.bankBranch;
/**
* Gets accounts by parameters and searchText term.
*
* @param searchText - searchText term
* @param currency - account currency
* @param type - account type
* @param branchId - bank branch id
* @param pageable - pagination
* @return SQLQuery<AccountView>
* @URL /manager/accounts
* @API GET: /account
* @Test Tested in account.AccountResourceManagerTest#testGetAllPositive
*/
/*@Query("select u from Account u" +
" where (u.currency=:currency or :currency is null)" +
"and(u.type=:accType or :accType is null)" +
"and(u.bankBranch.id=:branchId or :branchId is null)" +
"and (:searchText is null or :searchText ='' " +
"or lower(u.number) like lower(concat('%',:searchText,'%'))" +
"or lower(u.customer.organization.name) like lower(concat('%',:searchText,'%')) " +
"or lower(u.customer.organization.taxCode) like lower(concat('%',:searchText,'%'))" +
")"
)*/
public SQLQuery<AccountView> findByParametersAndSearch(final String searchText, final String currency, final String type,
final Long branchId, final Pageable pageable) {
return queryFactory
.select(
Projections.constructor(
AccountView.class,
account.id, account.accountNumber,
account.currency, account.accountType,
account.alias, bankBranch.shortName,
account.balance, account.status,
customer.name, customer.taxCode
) // Projections end
) // Select end
.from(account)
.leftJoin(customer).on(account.customerId.eq(customer.id))
.leftJoin(bankBranch).on(account.bankBranchId.eq(bankBranch.id))
.where(
new OptionalBuilder(currency, () -> account.currency.eq(currency))
.optionalAnd(type, () -> account.accountType.eq(type))
.optionalAnd(branchId, () -> account.bankBranchId.eq(branchId))
.and(
new OptionalBuilder(searchText != null && !searchText.isEmpty(),
() -> account.accountNumber.likeIgnoreCase("%" + searchText + "%")
.or(customer.name.likeIgnoreCase("%" + searchText + "%"))
.or(customer.taxCode.likeIgnoreCase("%" + searchText + "%"))
)
) //and end
); //where end
}
/**
* Gets accounts by parameters and searchText term.
*
* @param searchText - searchText term
* @param currency - account currency
* @param accountType - account type
* @param branchId - bank branch id
* @param status - account status
* @param request - pagination
* @return Page<AccountView>
* @URL /manager/accounts
* @API GET: /account
* @Test Tested in account.AccountResourceManagerTest#testGetAllPositive
*/
public Page<AccountView> getAllAccountsByParametersAndSearch(final String searchText, final String currency,
final String accountType, final Long branchId,
final String status, final GridRequest request) {
SQLQuery<AccountView> accountViewSQLQuery;
if (status.isEmpty())
accountViewSQLQuery = accountRepositoryDB.findByParametersAndSearch(searchText, currency, accountType, branchId, request);
else
accountViewSQLQuery = accountRepositoryDB.findByParametersAndSearch(searchText, currency, accountType, branchId, status, request);
return Page.build(accountViewSQLQuery, request);
}
/**
* Gets accounts by parameters and searchText term for managers.
*
* @param searchText - searchText term
* @param currency - account currency
* @param accountType - account type
* @param branchId - bank branch id
* @param status - account status
* @param request - pagination
* @return GridResponse<AccountView>
* @URL /manager/accounts
* @API GET: /account
* @Test Tested in account.AccountResourceManagerTest#testGetAllPositive
*/
/*
@RestController
@RequestMapping(Constants.API_BASE + "/account")
public class AccountResource {...
*/
@ApiOperation(
value = "Gets all accounts by parameters for manager."/*short operation description*/,
notes = "Gets all accounts by parameters and search term for manager."/*extended operation description*/)
@PreAuthorize("hasAuthority('m_account_view')")
@RequestMapping(method = RequestMethod.GET)
public GridResponse<AccountView> getAll(final GridRequest request,
@RequestParam(defaultValue = "") final String searchText,
@RequestParam(required = false) final String currency,
@RequestParam(required = false) final String accountType,
@RequestParam(required = false) final Long branchId,
@RequestParam(defaultValue = "", required = false) final String status) {
//FIXME Input data validation is absent
com.infin.it.ibank.db.Page<AccountView> accountViewPage
= accountService.getAllAccountsByParametersAndSearch(searchText, currency, accountType, branchId, status, request);
return new GridResponse<>(accountViewPage.content, new GridResponse.Total(accountViewPage.totalSize));
}
/**
* Test for
*
* @RequestMapping(method = RequestMethod.GET)
* public GridResponse<AccountView> getAll(final GridRequest request,
* @RequestParam(defaultValue = "") final String searchText,
* @RequestParam(required = false) final String currency,
* @RequestParam(required = false) final String accountType,
* @RequestParam(required = false) final Long branchId,
* @RequestParam(defaultValue = "", required = false) final String status)
* @URL /manager/accounts
* @API GET: /account?page=0&size=10&sort=id&order=desc
*/
@Test
public void testGetAllPositive() throws Exception {
final int expectedSize = 55;
final int expectedFirstAccountIdDESC = 55;
mockMvc.perform(get(Constants.API_BASE
+ "/account")
.param("page", "0")
.param("size", "50")
.param("sort", "id")
.param("order", "desc") // desc
.cookie(COOKIE)//cookie settings
.header("authorization", "Bearer " + MANAGER_TOKEN)
.contentType(CONTENT_TYPE))//return content type
.andDo(print())//print more info
.andExpect(status().isOk())
.andExpect(jsonPath("$.total.count", is(expectedSize)))//compare JSON response field with value in the is(value)
.andExpect(jsonPath("$.rows[0].id", is(expectedFirstAccountIdDESC)));//compare JSON response field with value in the is(value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment