use of org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment in project openlmis-stockmanagement by OpenLMIS.
the class StockEventProcessContextBuilder method buildContext.
/**
* Before processing events, put all needed ref data into context so we don't have to do frequent
* network requests.
*
* @param eventDto event dto.
* @return a context object that includes all needed ref data.
*/
public StockEventProcessContext buildContext(StockEventDto eventDto) {
XLOGGER.entry(eventDto);
Profiler profiler = new Profiler("BUILD_CONTEXT");
profiler.setLogger(XLOGGER);
LOGGER.info("build stock event process context");
StockEventProcessContext context = new StockEventProcessContext();
profiler.start("CREATE_LAZY_USER");
OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
Supplier<UUID> userIdSupplier;
if (authentication.isClientOnly()) {
userIdSupplier = eventDto::getUserId;
} else {
userIdSupplier = () -> authenticationHelper.getCurrentUser().getId();
}
LazyResource<UUID> userId = new LazyResource<>(userIdSupplier);
context.setCurrentUserId(userId);
profiler.start("CREATE_LAZY_PROGRAM");
UUID programId = eventDto.getProgramId();
Supplier<ProgramDto> programSupplier = new ReferenceDataSupplier<>(programService, programId);
LazyResource<ProgramDto> program = new LazyResource<>(programSupplier);
context.setProgram(program);
profiler.start("CREATE_LAZY_FACILITY");
UUID facilityId = eventDto.getFacilityId();
Supplier<FacilityDto> facilitySupplier = new ReferenceDataSupplier<>(facilityService, facilityId);
LazyResource<FacilityDto> facility = new LazyResource<>(facilitySupplier);
context.setFacility(facility);
profiler.start("CREATE_LAZY_APPROVED_PRODUCTS");
Supplier<List<OrderableDto>> productsSupplier = () -> orderableReferenceDataService.findAll();
LazyList<OrderableDto> products = new LazyList<>(productsSupplier);
context.setAllApprovedProducts(products);
profiler.start("CREATE_LAZY_LOTS");
Supplier<List<LotDto>> lotsSupplier = () -> getLots(eventDto);
LazyList<LotDto> lots = new LazyList<>(lotsSupplier);
LazyGrouping<UUID, LotDto> lotsGroupedById = new LazyGrouping<>(lots, LotDto::getId);
context.setLots(lotsGroupedById);
profiler.start("CREATE_LAZY_EVENT_REASONS");
Supplier<List<StockCardLineItemReason>> eventReasonsSupplier = () -> reasonRepository.findByIdIn(eventDto.getReasonIds());
LazyList<StockCardLineItemReason> eventReasons = new LazyList<>(eventReasonsSupplier);
LazyGrouping<UUID, StockCardLineItemReason> eventReasonsGroupedById = new LazyGrouping<>(eventReasons, StockCardLineItemReason::getId);
context.setEventReasons(eventReasonsGroupedById);
profiler.start("CREATE_LAZY_NODES");
Supplier<List<Node>> nodesSupplier = () -> nodeRepository.findByIdIn(eventDto.getNodeIds());
LazyList<Node> nodes = new LazyList<>(nodesSupplier);
LazyGrouping<UUID, Node> nodesGroupedById = new LazyGrouping<>(nodes, Node::getId);
context.setNodes(nodesGroupedById);
profiler.start("CREATE_LAZY_STOCK_CARDS");
Supplier<List<StockCard>> cardsSupplier = () -> stockCardRepository.findByProgramIdAndFacilityId(eventDto.getProgramId(), eventDto.getFacilityId());
LazyList<StockCard> cards = new LazyList<>(cardsSupplier);
LazyGrouping<OrderableLotIdentity, StockCard> cardsGroupedByIdentity = new LazyGrouping<>(cards, OrderableLotIdentity::identityOf);
context.setCards(cardsGroupedByIdentity);
profiler.start("CREATE_LAZY_CARD_REASONS");
Supplier<List<StockCardLineItemReason>> cardReasonsSupplier = () -> getCardReasons(eventDto);
LazyList<StockCardLineItemReason> cardReasons = new LazyList<>(cardReasonsSupplier);
LazyGrouping<UUID, StockCardLineItemReason> cardReasonsGroupedById = new LazyGrouping<>(cardReasons, StockCardLineItemReason::getId);
context.setCardReasons(cardReasonsGroupedById);
profiler.start("CREATE_LAZY_SOURCES");
Supplier<List<ValidSourceAssignment>> sourcesSupplier = () -> validSourceAssignmentRepository.findByProgramIdAndFacilityTypeId(eventDto.getProgramId(), context.getFacilityTypeId());
LazyList<ValidSourceAssignment> sources = new LazyList<>(sourcesSupplier);
context.setSources(sources);
profiler.start("CREATE_LAZY_DESTINATIONS");
Supplier<List<ValidDestinationAssignment>> destinationsSupplier = () -> validDestinationAssignmentRepository.findByProgramIdAndFacilityTypeId(eventDto.getProgramId(), context.getFacilityTypeId());
LazyList<ValidDestinationAssignment> destinations = new LazyList<>(destinationsSupplier);
context.setDestinations(destinations);
profiler.stop().log();
XLOGGER.exit(context);
return context;
}
use of org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment in project openlmis-stockmanagement by OpenLMIS.
the class ValidSourceDestinationDataBuilder method getValidDestinationAssignment.
private static ValidDestinationAssignment getValidDestinationAssignment(Node facilityNode) {
ValidDestinationAssignment destination = new ValidDestinationAssignment();
destination.setNode(facilityNode);
return destination;
}
use of org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment in project openlmis-stockmanagement by OpenLMIS.
the class ValidSourceDestinationControllerIntegrationTest method shouldReturn200WhenDestinationAssignmentAlreadyExist.
@Test
public void shouldReturn200WhenDestinationAssignmentAlreadyExist() throws Exception {
// given
UUID programId = UUID.randomUUID();
UUID facilityTypeId = UUID.randomUUID();
UUID destinationId = UUID.randomUUID();
ValidDestinationAssignment assignment = createDestination(programId, facilityTypeId, destinationId);
ValidSourceDestinationDto validSourceDestinationDto = new ValidSourceDestinationDto();
validSourceDestinationDto.setProgramId(programId);
validSourceDestinationDto.setFacilityTypeId(facilityTypeId);
validSourceDestinationDto.setNode(assignment.getNode());
when(validDestinationService.findByProgramFacilityDestination(assignment)).thenReturn(validSourceDestinationDto);
// when
ResultActions resultActions = mvc.perform(post(API_VALID_DESTINATIONS).param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE).contentType(MediaType.APPLICATION_JSON).content(objectToJsonString(assignment)));
// then
resultActions.andExpect(status().isOk()).andExpect(jsonPath(PROGRAM_EXP, is(programId.toString()))).andExpect(jsonPath(FACILITY_TYPE_EXP, is(facilityTypeId.toString()))).andExpect(jsonPath(NODE_REFERENCE_ID_EXP, is(destinationId.toString())));
}
use of org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment in project openlmis-stockmanagement by OpenLMIS.
the class ValidSourceDestinationControllerIntegrationTest method return201WhenAssignDestinationSuccessfully.
@Test
public void return201WhenAssignDestinationSuccessfully() throws Exception {
// given
UUID programId = UUID.randomUUID();
UUID facilityTypeId = UUID.randomUUID();
UUID destinationId = UUID.randomUUID();
ValidDestinationAssignment assignment = createDestination(programId, facilityTypeId, destinationId);
ValidSourceDestinationDto validSourceDestinationDto = new ValidSourceDestinationDto();
validSourceDestinationDto.setProgramId(programId);
validSourceDestinationDto.setFacilityTypeId(facilityTypeId);
validSourceDestinationDto.setNode(assignment.getNode());
when(validDestinationService.assignDestination(assignment)).thenReturn(validSourceDestinationDto);
// when
ResultActions resultActions = mvc.perform(post(API_VALID_DESTINATIONS).param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE).contentType(MediaType.APPLICATION_JSON).content(objectToJsonString(assignment)));
// then
resultActions.andExpect(status().isCreated()).andExpect(jsonPath(PROGRAM_EXP, is(programId.toString()))).andExpect(jsonPath(FACILITY_TYPE_EXP, is(facilityTypeId.toString()))).andExpect(jsonPath(NODE_REFERENCE_ID_EXP, is(destinationId.toString())));
}
use of org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment in project openlmis-stockmanagement by OpenLMIS.
the class SourceDestinationBaseServiceTest method shouldReturn400WhenDestinationNotFound.
@Test(expected = ValidationMessageException.class)
public void shouldReturn400WhenDestinationNotFound() throws Exception {
// given
UUID programId = randomUUID();
UUID facilityTypeId = randomUUID();
UUID destinationId = randomUUID();
ValidDestinationAssignment assignment = createDestination(programId, facilityTypeId, destinationId);
when(organizationRepository.findOne(destinationId)).thenReturn(null);
when(nodeRepository.findByReferenceId(destinationId)).thenReturn(null);
// when
validDestinationService.assignDestination(assignment);
}
Aggregations