Search in sources :

Example 1 with StockEventProcessContext

use of org.openlmis.stockmanagement.util.StockEventProcessContext in project openlmis-stockmanagement by OpenLMIS.

the class StockEventDtoTest method shouldConvertFromDtoToJpaModel.

@Test
public void shouldConvertFromDtoToJpaModel() throws Exception {
    // given
    UUID userId = UUID.randomUUID();
    StockEventProcessContext context = new StockEventProcessContext();
    context.setCurrentUserId(new LazyResource<>(() -> userId));
    StockEventDto stockEventDto = StockEventDtoDataBuilder.createStockEventDto();
    stockEventDto.setContext(context);
    // when
    StockEvent event = stockEventDto.toEvent();
    // then
    assertThat(event.getDocumentNumber(), is(stockEventDto.getDocumentNumber()));
    assertThat(event.getSignature(), is(stockEventDto.getSignature()));
    assertThat(event.getLineItems(), hasSize(stockEventDto.getLineItems().size()));
    assertThat(event.getProgramId(), is(stockEventDto.getProgramId()));
    assertThat(event.getFacilityId(), is(stockEventDto.getFacilityId()));
    assertThat(event.getUserId(), is(userId));
    ZonedDateTime processedDate = event.getProcessedDate();
    long between = SECONDS.between(processedDate, ZonedDateTime.now());
    assertThat(between, lessThan(2L));
}
Also used : StockEvent(org.openlmis.stockmanagement.domain.event.StockEvent) ZonedDateTime(java.time.ZonedDateTime) UUID(java.util.UUID) StockEventProcessContext(org.openlmis.stockmanagement.util.StockEventProcessContext) Test(org.junit.Test)

Example 2 with StockEventProcessContext

use of org.openlmis.stockmanagement.util.StockEventProcessContext 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;
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) LazyList(org.openlmis.stockmanagement.util.LazyList) LazyResource(org.openlmis.stockmanagement.util.LazyResource) Node(org.openlmis.stockmanagement.domain.sourcedestination.Node) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) ValidSourceAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment) ValidDestinationAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment) Profiler(org.slf4j.profiler.Profiler) ProgramDto(org.openlmis.stockmanagement.dto.referencedata.ProgramDto) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) LazyList(org.openlmis.stockmanagement.util.LazyList) List(java.util.List) UUID(java.util.UUID) StockEventProcessContext(org.openlmis.stockmanagement.util.StockEventProcessContext) ReferenceDataSupplier(org.openlmis.stockmanagement.util.ReferenceDataSupplier) LotDto(org.openlmis.stockmanagement.dto.referencedata.LotDto) LazyGrouping(org.openlmis.stockmanagement.util.LazyGrouping) StockCardLineItemReason(org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)

Example 3 with StockEventProcessContext

use of org.openlmis.stockmanagement.util.StockEventProcessContext in project openlmis-stockmanagement by OpenLMIS.

the class ApprovedOrderableValidatorTest method stockEventWithOrderableIdInApprovedListShouldPass.

@Test
public void stockEventWithOrderableIdInApprovedListShouldPass() throws Exception {
    // given:
    String orderableIdString = "d8290082-f9fa-4a37-aefb-a3d76ff805a8";
    UUID orderableId = UUID.fromString(orderableIdString);
    StockEventDto stockEventDto = StockEventDtoDataBuilder.createStockEventDto();
    stockEventDto.getLineItems().get(0).setOrderableId(orderableId);
    OrderableDto orderableDto = new OrderableDto();
    orderableDto.setId(UUID.fromString(orderableIdString));
    StockEventProcessContext context = new StockEventProcessContext();
    context.setAllApprovedProducts(new LazyList<>(() -> singletonList(orderableDto)));
    stockEventDto.setContext(context);
    // when:
    approvedOrderableValidator.validate(stockEventDto);
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) UUID(java.util.UUID) StockEventProcessContext(org.openlmis.stockmanagement.util.StockEventProcessContext) Test(org.junit.Test)

Example 4 with StockEventProcessContext

use of org.openlmis.stockmanagement.util.StockEventProcessContext in project openlmis-stockmanagement by OpenLMIS.

the class StockEventProcessor method process.

/**
 * Validate and persist event and create stock card and line items from it.
 *
 * @param eventDto stock event dto.
 * @return the persisted event ids.
 */
public UUID process(StockEventDto eventDto) {
    XLOGGER.entry(eventDto);
    Profiler profiler = new Profiler("PROCESS");
    profiler.setLogger(XLOGGER);
    profiler.start("BUILD_CONTEXT");
    StockEventProcessContext context = contextBuilder.buildContext(eventDto);
    eventDto.setContext(context);
    profiler.start("VALIDATE");
    stockEventValidationsService.validate(eventDto);
    UUID eventId = saveEventAndGenerateLineItems(eventDto, profiler.startNested("SAVE_AND_GENERATE_LINE_ITEMS"));
    profiler.stop().log();
    XLOGGER.exit(eventId);
    return eventId;
}
Also used : Profiler(org.slf4j.profiler.Profiler) UUID(java.util.UUID) StockEventProcessContext(org.openlmis.stockmanagement.util.StockEventProcessContext)

Example 5 with StockEventProcessContext

use of org.openlmis.stockmanagement.util.StockEventProcessContext in project openlmis-stockmanagement by OpenLMIS.

the class BaseIntegrationTest method setContext.

protected void setContext(StockEventDto event) {
    StockEventProcessContext context = contextBuilder.buildContext(event);
    event.setContext(context);
}
Also used : StockEventProcessContext(org.openlmis.stockmanagement.util.StockEventProcessContext)

Aggregations

StockEventProcessContext (org.openlmis.stockmanagement.util.StockEventProcessContext)9 UUID (java.util.UUID)6 Test (org.junit.Test)4 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)4 StockEventDto (org.openlmis.stockmanagement.dto.StockEventDto)3 ZonedDateTime (java.time.ZonedDateTime)2 FacilityDto (org.openlmis.stockmanagement.dto.referencedata.FacilityDto)2 LotDto (org.openlmis.stockmanagement.dto.referencedata.LotDto)2 ProgramDto (org.openlmis.stockmanagement.dto.referencedata.ProgramDto)2 Profiler (org.slf4j.profiler.Profiler)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID.randomUUID (java.util.UUID.randomUUID)1 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)1 StockEvent (org.openlmis.stockmanagement.domain.event.StockEvent)1 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)1 StockCardLineItemReason (org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason)1 Node (org.openlmis.stockmanagement.domain.sourcedestination.Node)1 ValidDestinationAssignment (org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment)1 ValidSourceAssignment (org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment)1