use of org.motechproject.mots.domain.Incharge in project mots by motech-implementations.
the class InchargeRepositoryIntegrationTest method shouldFindInchargeByFirstName.
@Test
public void shouldFindInchargeByFirstName() {
// when
Page<Incharge> result = inchargeRepository.searchIncharges(incharge1.getFirstName(), null, null, null, null, null, false, null);
// then
assertThat(result.getTotalElements(), is(1L));
Incharge foundIncharge = result.getContent().get(0);
assertThat(foundIncharge.getFirstName(), is(incharge1.getFirstName()));
}
use of org.motechproject.mots.domain.Incharge in project mots by motech-implementations.
the class InchargeDataBuilder method build.
/**
* Builds instance of {@link Incharge}.
*/
public Incharge build() {
Incharge incharge = buildAsNew();
incharge.setId(id);
return incharge;
}
use of org.motechproject.mots.domain.Incharge in project mots by motech-implementations.
the class InchargeController method saveIncharge.
/**
* Update incharge.
* @param id id of Incharge to update
* @param inchargeDto DTO of Incharge to update
* @return updated Incharge
*/
@RequestMapping(value = "/incharge/{id}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public InchargeDto saveIncharge(@PathVariable("id") UUID id, @RequestBody @Valid InchargeDto inchargeDto, BindingResult bindingResult) {
checkBindingResult(bindingResult);
Incharge existingIncharge = inchargeService.getIncharge(id);
inchargeMapper.updateFromDto(inchargeDto, existingIncharge);
return inchargeMapper.toDto(inchargeService.saveIncharge(existingIncharge));
}
use of org.motechproject.mots.domain.Incharge in project mots by motech-implementations.
the class InchargeRepositoryImpl method prepareQuery.
private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, String firstName, String secondName, String otherName, String phoneNumber, String email, String facilityName, Boolean selected, boolean count, Pageable pageable) throws IllegalArgumentException {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
Root<Incharge> root = query.from(Incharge.class);
if (count) {
CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query;
query = (CriteriaQuery<T>) countQuery.select(builder.count(root));
}
Predicate predicate = builder.conjunction();
if (firstName != null) {
predicate = builder.and(predicate, builder.like(root.get(FIRST_NAME), '%' + firstName + '%'));
}
if (secondName != null) {
predicate = builder.and(predicate, builder.like(root.get(SECOND_NAME), '%' + secondName + '%'));
}
if (otherName != null) {
predicate = builder.and(predicate, builder.like(root.get(OTHER_NAME), '%' + otherName + '%'));
}
if (phoneNumber != null) {
predicate = builder.and(predicate, builder.like(root.get(PHONE_NUMBER), '%' + phoneNumber + '%'));
}
if (email != null) {
predicate = builder.and(predicate, builder.like(root.get(EMAIL), '%' + email + '%'));
}
if (facilityName != null) {
predicate = builder.and(predicate, builder.like(root.get(FACILITY).get(NAME), '%' + facilityName + '%'));
}
if (selected != null) {
predicate = builder.and(predicate, builder.equal(root.get(SELECTED), selected));
}
query.where(predicate);
if (!count && pageable != null && pageable.getSort() != null) {
query = addSortProperties(query, root, pageable);
}
return query;
}
use of org.motechproject.mots.domain.Incharge in project mots by motech-implementations.
the class InchargeRepositoryImpl method searchIncharges.
/**
* Finds Incharges matching all of the provided parameters.
* If there are no parameters, return all Incharges.
*/
@Override
public Page<Incharge> searchIncharges(String firstName, String secondName, String otherName, String phoneNumber, String email, String facilityName, Boolean selected, Pageable pageable) throws IllegalArgumentException {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Incharge> query = builder.createQuery(Incharge.class);
query = prepareQuery(query, firstName, secondName, otherName, phoneNumber, email, facilityName, selected, false, pageable);
CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
countQuery = prepareQuery(countQuery, firstName, secondName, otherName, phoneNumber, email, facilityName, selected, true, pageable);
Long count = entityManager.createQuery(countQuery).getSingleResult();
int pageSize = getPageSize(pageable);
int firstResult = getFirstResult(pageable, pageSize);
List<Incharge> incharges = entityManager.createQuery(query).setMaxResults(pageSize).setFirstResult(firstResult).getResultList();
return new PageImpl<>(incharges, pageable, count);
}
Aggregations