Search in sources :

Example 11 with CommunityHealthWorker

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

the class CommunityHealthWorkerDataBuilder method build.

/**
 * Builds instance of {@link CommunityHealthWorker}.
 */
public CommunityHealthWorker build() {
    CommunityHealthWorker chw = buildAsNew();
    chw.setId(id);
    return chw;
}
Also used : CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker)

Example 12 with CommunityHealthWorker

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

the class ModuleAssignmentService method assignModules.

/**
 * Assign modules to CHW.
 * @param assignedModules modules assigned for CHW
 */
@SuppressWarnings("checkstyle:variabledeclarationusagedistance")
@Transactional
@PreAuthorize(RoleNames.HAS_ASSIGN_MODULES_ROLE)
public void assignModules(AssignedModules assignedModules) {
    AssignedModules existingAssignedModules = getAssignedModules(assignedModules.getHealthWorker().getId());
    CommunityHealthWorker assignedModulesChw = existingAssignedModules.getHealthWorker();
    Set<Module> oldModules = new HashSet<>(existingAssignedModules.getModules());
    existingAssignedModules.setModules(assignedModules.getModules());
    repository.save(existingAssignedModules);
    entityManager.flush();
    entityManager.refresh(existingAssignedModules);
    Set<Module> newModules = new HashSet<>(existingAssignedModules.getModules());
    Set<Module> modulesToAdd = getModulesToAdd(oldModules, newModules);
    Set<Module> modulesToRemove = getModulesToRemove(oldModules, newModules);
    validateModulesToUnassign(assignedModulesChw, modulesToRemove);
    String ivrId = assignedModulesChw.getIvrId();
    if (ivrId == null) {
        throw new ModuleAssignmentException("Could not assign module to CHW, because CHW has empty IVR id");
    }
    try {
        ivrService.addSubscriberToGroups(ivrId, getIvrGroupsFromModules(modulesToAdd));
        ivrService.removeSubscriberFromGroups(ivrId, getIvrGroupsFromModules(modulesToRemove));
    } catch (IvrException ex) {
        String message = "Could not assign or delete module for CHW, " + "because of IVR module assignment error.\n\n" + ex.getClearVotoInfo();
        throw new ModuleAssignmentException(message, ex);
    }
    moduleProgressService.removeModuleProgresses(assignedModulesChw, modulesToRemove);
    moduleProgressService.createModuleProgresses(assignedModulesChw, modulesToAdd);
}
Also used : AssignedModules(org.motechproject.mots.domain.AssignedModules) IvrException(org.motechproject.mots.exception.IvrException) ModuleAssignmentException(org.motechproject.mots.exception.ModuleAssignmentException) CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker) CourseModule(org.motechproject.mots.domain.CourseModule) Module(org.motechproject.mots.domain.Module) HashSet(java.util.HashSet) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with CommunityHealthWorker

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

the class ModuleAssignmentService method assignModulesToDistrict.

/**
 * Creates DistrictAssignmentLog for district assignment,
 * and assigns modules to each CHW from a district.
 * @param assignmentDto dto with district id, list of modules assigned to it
 *     and start and end dates
 */
@Transactional
@PreAuthorize(RoleNames.HAS_ASSIGN_MODULES_ROLE)
public void assignModulesToDistrict(DistrictAssignmentDto assignmentDto) {
    List<CommunityHealthWorker> communityHealthWorkers = communityHealthWorkerRepository.findByCommunityFacilityChiefdomDistrictIdAndSelected(UUID.fromString(assignmentDto.getDistrictId()), true);
    Set<Module> newChwModules = new HashSet<>();
    String userName = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    User currentUser = userService.getUserByUserName(userName);
    for (String moduleId : assignmentDto.getModules()) {
        Module moduleToAssign = moduleRepository.findById(UUID.fromString(moduleId)).orElseThrow(() -> new EntityNotFoundException("No module found for Id: {0}", moduleId));
        newChwModules.add(moduleToAssign);
        assignmentLogRepository.save(new DistrictAssignmentLog(districtRepository.findOne(UUID.fromString(assignmentDto.getDistrictId())), LocalDate.parse(assignmentDto.getStartDate()), LocalDate.parse(assignmentDto.getEndDate()), moduleToAssign, currentUser));
    }
    for (CommunityHealthWorker chw : communityHealthWorkers) {
        AssignedModules existingAssignedModules = getAssignedModules(chw.getId());
        Set<Module> oldModules = existingAssignedModules.getModules();
        Set<Module> modulesToAdd = getModulesToAdd(oldModules, newChwModules);
        existingAssignedModules.getModules().addAll(modulesToAdd);
        repository.save(existingAssignedModules);
        entityManager.flush();
        entityManager.refresh(existingAssignedModules);
        String ivrId = existingAssignedModules.getHealthWorker().getIvrId();
        if (ivrId == null) {
            throw new ModuleAssignmentException("Could not assign module to CHW, because CHW has empty IVR id");
        }
        try {
            ivrService.addSubscriberToGroups(ivrId, getIvrGroupsFromModules(modulesToAdd));
        } catch (IvrException ex) {
            String message = "Could not assign or delete module for CHW, " + "because of IVR module assignment error.\n\n" + ex.getClearVotoInfo();
            throw new ModuleAssignmentException(message, ex);
        }
        moduleProgressService.createModuleProgresses(chw, modulesToAdd);
    }
}
Also used : AssignedModules(org.motechproject.mots.domain.AssignedModules) IvrException(org.motechproject.mots.exception.IvrException) User(org.motechproject.mots.domain.security.User) ModuleAssignmentException(org.motechproject.mots.exception.ModuleAssignmentException) CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) CourseModule(org.motechproject.mots.domain.CourseModule) Module(org.motechproject.mots.domain.Module) DistrictAssignmentLog(org.motechproject.mots.domain.DistrictAssignmentLog) HashSet(java.util.HashSet) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

CommunityHealthWorker (org.motechproject.mots.domain.CommunityHealthWorker)13 Test (org.junit.Test)4 AssignedModules (org.motechproject.mots.domain.AssignedModules)4 HashSet (java.util.HashSet)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 CourseModule (org.motechproject.mots.domain.CourseModule)2 Module (org.motechproject.mots.domain.Module)2 IvrException (org.motechproject.mots.exception.IvrException)2 ModuleAssignmentException (org.motechproject.mots.exception.ModuleAssignmentException)2 AssignedModulesDataBuilder (org.motechproject.mots.testbuilder.AssignedModulesDataBuilder)2 CommunityHealthWorkerDataBuilder (org.motechproject.mots.testbuilder.CommunityHealthWorkerDataBuilder)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Transactional (org.springframework.transaction.annotation.Transactional)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 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1