Search in sources :

Example 6 with OrderableDto

use of org.openlmis.stockmanagement.dto.referencedata.OrderableDto 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 7 with OrderableDto

use of org.openlmis.stockmanagement.dto.referencedata.OrderableDto in project openlmis-stockmanagement by OpenLMIS.

the class ApprovedProductReferenceDataService method getApprovedProducts.

/**
 * Retrieves all facility approved products from the reference data service, based on the
 * provided facility and full supply flag. It can be optionally filtered by the program ID.
 *
 * @param facilityId id of the facility
 * @param programId  id of the program
 * @return a collection of approved products matching the search criteria
 */
public Page<OrderableDto> getApprovedProducts(UUID facilityId, UUID programId, Collection<UUID> orderableIds) {
    RequestParameters params = RequestParameters.init();
    params.set("programId", programId);
    if (!isEmpty(orderableIds)) {
        params.set("orderableId", orderableIds);
    }
    Page<ApprovedProductDto> approvedProductPage = getPage(facilityId + "/approvedProducts", params);
    List<OrderableDto> content = approvedProductPage.getContent().stream().map(ApprovedProductDto::getOrderable).collect(Collectors.toList());
    return Pagination.getPage(content);
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) ApprovedProductDto(org.openlmis.stockmanagement.dto.referencedata.ApprovedProductDto) RequestParameters(org.openlmis.stockmanagement.util.RequestParameters)

Example 8 with OrderableDto

use of org.openlmis.stockmanagement.dto.referencedata.OrderableDto in project openlmis-stockmanagement by OpenLMIS.

the class VvmValidatorTest method generateOrderable.

private OrderableDto generateOrderable() {
    OrderableDto orderable = new OrderableDto();
    orderable.setId(UUID.randomUUID());
    given(orderableReferenceDataService.findByIds(anyListOf(UUID.class))).willReturn(Collections.singletonList(orderable));
    return orderable;
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) UUID(java.util.UUID)

Example 9 with OrderableDto

use of org.openlmis.stockmanagement.dto.referencedata.OrderableDto in project openlmis-stockmanagement by OpenLMIS.

the class VvmValidatorTest method shouldRejectIfOrderableDisabledVvmAndLineItemHasVvmStatus.

@Test(expected = ValidationMessageException.class)
public void shouldRejectIfOrderableDisabledVvmAndLineItemHasVvmStatus() {
    OrderableDto orderable = generateOrderable();
    orderable.setExtraData(Collections.singletonMap("useVVM", "false"));
    VvmApplicable lineItem = generateVvmApplicable(orderable);
    lineItem.setExtraData(Collections.singletonMap("vvmStatus", "status"));
    validator.validate(Collections.singletonList(lineItem), ERROR_MESSAGE, false);
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) Test(org.junit.Test)

Example 10 with OrderableDto

use of org.openlmis.stockmanagement.dto.referencedata.OrderableDto in project openlmis-stockmanagement by OpenLMIS.

the class VvmValidatorTest method shouldNotRejectIfOrderableDisabledVvmAndLineItemHasNoVvmStatus.

@Test
public void shouldNotRejectIfOrderableDisabledVvmAndLineItemHasNoVvmStatus() {
    OrderableDto orderable = generateOrderable();
    orderable.setExtraData(Collections.singletonMap("useVVM", "false"));
    VvmApplicable lineItem = generateVvmApplicable(orderable);
    validator.validate(Collections.singletonList(lineItem), ERROR_MESSAGE, false);
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) Test(org.junit.Test)

Aggregations

OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)23 UUID (java.util.UUID)16 Test (org.junit.Test)16 UUID.randomUUID (java.util.UUID.randomUUID)8 LotDto (org.openlmis.stockmanagement.dto.referencedata.LotDto)6 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)5 VvmApplicable (org.openlmis.stockmanagement.domain.common.VvmApplicable)5 StockCardDto (org.openlmis.stockmanagement.dto.StockCardDto)5 URI (java.net.URI)4 StockEventProcessContext (org.openlmis.stockmanagement.util.StockEventProcessContext)4 List (java.util.List)3 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)3 StockEventDto (org.openlmis.stockmanagement.dto.StockEventDto)3 ApprovedProductDto (org.openlmis.stockmanagement.dto.referencedata.ApprovedProductDto)3 FacilityDto (org.openlmis.stockmanagement.dto.referencedata.FacilityDto)3 ProgramDto (org.openlmis.stockmanagement.dto.referencedata.ProgramDto)3 DynamicPageTypeReference (org.openlmis.stockmanagement.util.DynamicPageTypeReference)3 Map (java.util.Map)2 UUID.fromString (java.util.UUID.fromString)2 StockEvent (org.openlmis.stockmanagement.domain.event.StockEvent)2