Search in sources :

Example 1 with District

use of org.motechproject.mots.domain.District in project mots by motech-implementations.

the class DistrictRepositoryIntegrationTest method shouldFindDistrictByName.

@Test
public void shouldFindDistrictByName() {
    // when
    Page<District> result = districtRepository.search(district1.getName(), null);
    // then
    assertThat(result.getTotalElements(), is(1L));
    District foundDistrict = result.getContent().get(0);
    assertThat(foundDistrict.getName(), is(district1.getName()));
}
Also used : District(org.motechproject.mots.domain.District) Test(org.junit.Test)

Example 2 with District

use of org.motechproject.mots.domain.District in project mots by motech-implementations.

the class LocationImporter method parseDistricts.

private void parseDistricts(XSSFSheet sheet) {
    XSSFRow row;
    XSSFCell cell;
    Iterator rows = sheet.rowIterator();
    HashSet<District> newDistrictSet = new HashSet<>();
    while (rows.hasNext()) {
        row = (XSSFRow) rows.next();
        cell = row.getCell(DISTRICT_COL_NUMBER);
        if (cell == null) {
            continue;
        }
        String cellText = cell.getStringCellValue();
        if (cellText.equals(DISTRICT_HEADER) || StringUtils.isEmpty(cellText)) {
            continue;
        }
        District district = new District(cellText);
        newDistrictSet.add(district);
    }
    newDistrictSet.forEach(newDistrict -> {
        if (!currentDistrictList.contains(newDistrict)) {
            locationService.createDistrict(newDistrict);
        }
    });
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) Iterator(java.util.Iterator) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) District(org.motechproject.mots.domain.District) HashSet(java.util.HashSet)

Example 3 with District

use of org.motechproject.mots.domain.District in project mots by motech-implementations.

the class DistrictRepositoryImpl method prepareQuery.

private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, String districtName, boolean count, Pageable pageable) throws IllegalArgumentException {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    Root<District> root = query.from(District.class);
    if (count) {
        CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query;
        query = (CriteriaQuery<T>) countQuery.select(builder.count(root));
    }
    Predicate predicate = builder.conjunction();
    if (districtName != null) {
        predicate = builder.and(predicate, builder.like(root.get(NAME), '%' + districtName + '%'));
    }
    query.where(predicate);
    if (!count && pageable != null && pageable.getSort() != null) {
        query = addSortProperties(query, root, pageable);
    }
    return query;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) District(org.motechproject.mots.domain.District) Predicate(javax.persistence.criteria.Predicate)

Example 4 with District

use of org.motechproject.mots.domain.District in project mots by motech-implementations.

the class DistrictRepositoryImpl method search.

/**
 * Finds Districts matching all of the provided parameters.
 * If there are no parameters, return all Districts.
 */
@Override
public Page<District> search(String districtName, Pageable pageable) throws IllegalArgumentException {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<District> query = builder.createQuery(District.class);
    query = prepareQuery(query, districtName, false, pageable);
    CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
    countQuery = prepareQuery(countQuery, districtName, true, pageable);
    Long count = entityManager.createQuery(countQuery).getSingleResult();
    int pageSize = getPageSize(pageable);
    int firstResult = getFirstResult(pageable, pageSize);
    List<District> 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) District(org.motechproject.mots.domain.District)

Example 5 with District

use of org.motechproject.mots.domain.District in project mots by motech-implementations.

the class ModuleAssignmentServiceTest method shouldAssignModulesToDistrict.

@Test
public void shouldAssignModulesToDistrict() throws Exception {
    when(userService.getUserByUserName(eq(user.getUsername()))).thenReturn(user);
    when(communityHealthWorkerRepository.findByCommunityFacilityChiefdomDistrictIdAndSelected(any(), any())).thenReturn(Collections.singletonList(CHW));
    moduleAssignmentService.assignModulesToDistrict(districtAssignmentDto);
    ArgumentCaptor<DistrictAssignmentLog> districtAssignmentLogCaptor = ArgumentCaptor.forClass(DistrictAssignmentLog.class);
    verify(districtAssignmentLogRepository, times(2)).save(districtAssignmentLogCaptor.capture());
    List<DistrictAssignmentLog> districtAssignmentLogs = districtAssignmentLogCaptor.getAllValues();
    final Set<Module> passedModules = new HashSet<>(Arrays.asList(MODULE_2, MODULE_3));
    assertTrue(districtAssignmentLogs.stream().allMatch(l -> passedModules.contains(l.getModule())));
    for (DistrictAssignmentLog log : districtAssignmentLogs) {
        assertEquals(DISTRICT, log.getDistrict());
        assertEquals(districtAssignmentDto.getStartDate(), log.getStartDate().toString());
        assertEquals(districtAssignmentDto.getEndDate(), log.getEndDate().toString());
        assertEquals(user, log.getOwner());
    }
    ArgumentCaptor<AssignedModules> assignedModulesCaptor = ArgumentCaptor.forClass(AssignedModules.class);
    verify(assignedModulesRepository).save(assignedModulesCaptor.capture());
    final Set<Module> allModules = new HashSet<>(Arrays.asList(MODULE_1, MODULE_2, MODULE_3));
    assertEquals(allModules, assignedModulesCaptor.getValue().getModules());
    verify(ivrService).addSubscriberToGroups(eq(CHW.getIvrId()), eq(Collections.singletonList(IVR_GROUP)));
    verify(moduleProgressService).createModuleProgresses(any(), eq(Collections.singleton(MODULE_3)));
}
Also used : Arrays(java.util.Arrays) DistrictAssignmentLogRepository(org.motechproject.mots.repository.DistrictAssignmentLogRepository) ModuleProgress(org.motechproject.mots.domain.ModuleProgress) CommunityHealthWorkerRepository(org.motechproject.mots.repository.CommunityHealthWorkerRepository) Mockito.doThrow(org.mockito.Mockito.doThrow) ModuleAssignmentException(org.motechproject.mots.exception.ModuleAssignmentException) AssignedModules(org.motechproject.mots.domain.AssignedModules) Matchers.eq(org.mockito.Matchers.eq) DistrictAssignmentLog(org.motechproject.mots.domain.DistrictAssignmentLog) DistrictAssignmentDtoDataBuilder(org.motechproject.mots.testbuilder.DistrictAssignmentDtoDataBuilder) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) DistrictAssignmentDto(org.motechproject.mots.dto.DistrictAssignmentDto) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) AssignedModulesDataBuilder(org.motechproject.mots.testbuilder.AssignedModulesDataBuilder) MotsException(org.motechproject.mots.exception.MotsException) Set(java.util.Set) UUID(java.util.UUID) Matchers.any(org.mockito.Matchers.any) List(java.util.List) District(org.motechproject.mots.domain.District) Optional(java.util.Optional) User(org.motechproject.mots.domain.security.User) ModuleDataBuilder(org.motechproject.mots.testbuilder.ModuleDataBuilder) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) ProgressStatus(org.motechproject.mots.domain.enums.ProgressStatus) CommunityHealthWorkerDataBuilder(org.motechproject.mots.testbuilder.CommunityHealthWorkerDataBuilder) CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker) HashSet(java.util.HashSet) IvrException(org.motechproject.mots.exception.IvrException) ArgumentCaptor(org.mockito.ArgumentCaptor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Module(org.motechproject.mots.domain.Module) ModuleProgressDataBuilder(org.motechproject.mots.testbuilder.ModuleProgressDataBuilder) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) ModuleRepository(org.motechproject.mots.repository.ModuleRepository) DistrictDataBuilder(org.motechproject.mots.testbuilder.DistrictDataBuilder) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) EntityManager(javax.persistence.EntityManager) Mockito.verify(org.mockito.Mockito.verify) AssignedModulesRepository(org.motechproject.mots.repository.AssignedModulesRepository) Collections(java.util.Collections) DistrictRepository(org.motechproject.mots.repository.DistrictRepository) Assert.assertEquals(org.junit.Assert.assertEquals) TestUtils(org.motechproject.mots.utils.TestUtils) AssignedModules(org.motechproject.mots.domain.AssignedModules) DistrictAssignmentLog(org.motechproject.mots.domain.DistrictAssignmentLog) Module(org.motechproject.mots.domain.Module) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

District (org.motechproject.mots.domain.District)8 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)2 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)2 Test (org.junit.Test)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1 EntityManager (javax.persistence.EntityManager)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Before (org.junit.Before)1 RunWith (org.junit.runner.RunWith)1