Search in sources :

Example 6 with OrderableLotIdentity

use of org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity 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 OrderableLotIdentity

use of org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity in project openlmis-stockmanagement by OpenLMIS.

the class ActiveStockCardsValidatorTest method shouldThrowExceptionIfExistingCardOrderableNotCovered.

@Test
public void shouldThrowExceptionIfExistingCardOrderableNotCovered() throws Exception {
    expectedEx.expectMessage(ERROR_PHYSICAL_INVENTORY_NOT_INCLUDE_ACTIVE_STOCK_CARD);
    // given
    StockEventDto stockEventDto = createNoSourceDestinationStockEventDto();
    stockEventDto.getLineItems().get(0).setReasonId(null);
    when(stockCardRepository.getIdentitiesBy(stockEventDto.getProgramId(), stockEventDto.getFacilityId())).thenReturn(singletonList(new OrderableLotIdentity(randomUUID(), randomUUID())));
    // when
    activeStockCardsValidator.validate(stockEventDto);
}
Also used : OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) StockEventDtoDataBuilder.createNoSourceDestinationStockEventDto(org.openlmis.stockmanagement.testutils.StockEventDtoDataBuilder.createNoSourceDestinationStockEventDto) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) Test(org.junit.Test)

Example 8 with OrderableLotIdentity

use of org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity in project openlmis-stockmanagement by OpenLMIS.

the class OrderableLotIdentityTest method sameOrderableAndLotIdShouldEqualAndHaveSameHash.

@Test
public void sameOrderableAndLotIdShouldEqualAndHaveSameHash() throws Exception {
    // given
    OrderableLotIdentity identity1 = new OrderableLotIdentity(randomUUID(), randomUUID());
    OrderableLotIdentity identity2 = new OrderableLotIdentity(fromString(identity1.getOrderableId().toString()), fromString(identity1.getLotId().toString()));
    // when
    boolean equals = identity1.equals(identity2);
    int identity1Hash = identity1.hashCode();
    int identity2Hash = identity2.hashCode();
    // then
    assertTrue(equals);
    assertEquals(identity1Hash, identity2Hash);
}
Also used : OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) Test(org.junit.Test)

Example 9 with OrderableLotIdentity

use of org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity in project openlmis-stockmanagement by OpenLMIS.

the class StockEventNotificationProcessor method callNotifications.

private void callNotifications(StockEventDto event, StockEventLineItemDto eventLine) {
    XLOGGER.entry(event, eventLine);
    Profiler profiler = new Profiler("CALL_NOTIFICATION_FOR_LINE_ITEM");
    profiler.setLogger(XLOGGER);
    profiler.start("COPY_STOCK_CARD");
    OrderableLotIdentity identity = OrderableLotIdentity.identityOf(eventLine);
    StockCard card = event.getContext().findCard(identity);
    StockCard copy = card.shallowCopy();
    for (StockCardLineItem line : copy.getLineItems()) {
        StockCardLineItemReason reason = line.getReason();
        if (null != reason) {
            line.setReason(event.getContext().findCardReason(reason.getId()));
        }
    }
    profiler.start("CALCULATE_STOCK_ON_HAND");
    copy.calculateStockOnHand();
    profiler.start("NOTIFY_STOCK_CARD_EDITORS");
    if (copy.getStockOnHand() == 0) {
        stockoutNotifier.notifyStockEditors(copy);
    }
    profiler.stop().log();
    XLOGGER.exit();
}
Also used : StockCardLineItemReason(org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason) Profiler(org.slf4j.profiler.Profiler) StockCardLineItem(org.openlmis.stockmanagement.domain.card.StockCardLineItem) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) StockCard(org.openlmis.stockmanagement.domain.card.StockCard)

Example 10 with OrderableLotIdentity

use of org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity in project openlmis-stockmanagement by OpenLMIS.

the class OrderableLotDuplicationValidator method validate.

@Override
public void validate(StockEventDto stockEventDto) {
    // duplication is not allow in physical inventory, but is allowed in adjustment
    if (!stockEventDto.hasLineItems() || !stockEventDto.isPhysicalInventory()) {
        return;
    }
    Set<OrderableLotIdentity> nonDuplicates = new HashSet<>();
    Set<OrderableLotIdentity> duplicates = stockEventDto.getLineItems().stream().map(OrderableLotIdentity::identityOf).filter(lotIdentity -> !nonDuplicates.add(lotIdentity)).collect(toSet());
    if (duplicates.size() > 0) {
        throw new ValidationMessageException(new Message(ERROR_EVENT_ORDERABLE_LOT_DUPLICATION, duplicates));
    }
}
Also used : HashSet(java.util.HashSet) ERROR_EVENT_ORDERABLE_LOT_DUPLICATION(org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_EVENT_ORDERABLE_LOT_DUPLICATION) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) Message(org.openlmis.stockmanagement.util.Message) Component(org.springframework.stereotype.Component) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) Set(java.util.Set) Collectors.toSet(java.util.stream.Collectors.toSet) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) Message(org.openlmis.stockmanagement.util.Message) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) HashSet(java.util.HashSet)

Aggregations

OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)12 UUID (java.util.UUID)6 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)6 Test (org.junit.Test)5 List (java.util.List)4 StockEventDto (org.openlmis.stockmanagement.dto.StockEventDto)4 Message (org.openlmis.stockmanagement.util.Message)4 StockCardDto (org.openlmis.stockmanagement.dto.StockCardDto)3 LotDto (org.openlmis.stockmanagement.dto.referencedata.LotDto)3 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)3 StockCardRepository (org.openlmis.stockmanagement.repository.StockCardRepository)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Service (org.springframework.stereotype.Service)3 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors.toMap (java.util.stream.Collectors.toMap)2