Search in sources :

Example 1 with Incharge

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()));
}
Also used : Incharge(org.motechproject.mots.domain.Incharge) Test(org.junit.Test)

Example 2 with Incharge

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;
}
Also used : Incharge(org.motechproject.mots.domain.Incharge)

Example 3 with 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));
}
Also used : Incharge(org.motechproject.mots.domain.Incharge) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with Incharge

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;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Incharge(org.motechproject.mots.domain.Incharge) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Predicate(javax.persistence.criteria.Predicate)

Example 5 with Incharge

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);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) PageImpl(org.springframework.data.domain.PageImpl) Incharge(org.motechproject.mots.domain.Incharge)

Aggregations

Incharge (org.motechproject.mots.domain.Incharge)10 UUID (java.util.UUID)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Test (org.junit.Test)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1