Search in sources :

Example 6 with FacilityDto

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

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

the class HomeFacilityPermissionService method checkProgramSupported.

/**
 * Check if program is supported by user's home facility.
 *
 * @param programId  the program's id.
 */
public void checkProgramSupported(UUID programId) {
    UUID homeFacilityId = authenticationHelper.getCurrentUser().getHomeFacilityId();
    FacilityDto homeFacility = null;
    if (homeFacilityId != null) {
        homeFacility = facilityService.findOne(homeFacilityId);
    }
    boolean isSupported = homeFacility != null && homeFacility.getSupportedPrograms().stream().anyMatch(supportedProgram -> programId.equals(supportedProgram.getId()));
    if (!isSupported) {
        throwException(ERROR_PROGRAM_NOT_SUPPORTED, programId.toString());
    }
}
Also used : ERROR_PROGRAM_NOT_SUPPORTED(org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_PROGRAM_NOT_SUPPORTED) FacilityReferenceDataService(org.openlmis.stockmanagement.service.referencedata.FacilityReferenceDataService) AuthenticationHelper(org.openlmis.stockmanagement.util.AuthenticationHelper) Message(org.openlmis.stockmanagement.util.Message) Service(org.springframework.stereotype.Service) Autowired(org.springframework.beans.factory.annotation.Autowired) UUID(java.util.UUID) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) PermissionMessageException(org.openlmis.stockmanagement.exception.PermissionMessageException) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) UUID(java.util.UUID)

Example 8 with FacilityDto

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

the class SourceDestinationAssignmentValidatorTest method createContextWithFacility.

private void createContextWithFacility(StockEventDto eventDto) {
    FacilityTypeDto facilityTypeDto = new FacilityTypeDto();
    facilityTypeDto.setId(UUID.randomUUID());
    FacilityDto facilityDto = new FacilityDto();
    facilityDto.setId(eventDto.getFacilityId());
    facilityDto.setType(facilityTypeDto);
    when(facilityService.findOne(eventDto.getFacilityId())).thenReturn(facilityDto);
    setContext(eventDto);
}
Also used : FacilityTypeDto(org.openlmis.stockmanagement.dto.referencedata.FacilityTypeDto) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto)

Example 9 with FacilityDto

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

the class MandatoryFieldsValidatorTest method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    stockEventDto = StockEventDtoDataBuilder.createStockEventDto();
    when(facilityService.findOne(stockEventDto.getFacilityId())).thenReturn(new FacilityDto());
    when(programService.findOne(stockEventDto.getProgramId())).thenReturn(new ProgramDto());
    setContext(stockEventDto);
}
Also used : ProgramDto(org.openlmis.stockmanagement.dto.referencedata.ProgramDto) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) Before(org.junit.Before)

Example 10 with FacilityDto

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

the class StockCardBaseService method createDtos.

protected List<StockCardDto> createDtos(List<StockCard> stockCards) {
    if (stockCards.isEmpty()) {
        return emptyList();
    }
    StockCard firstCard = stockCards.get(0);
    LOGGER.debug("Calling ref data to retrieve facility info for card");
    FacilityDto facility = facilityRefDataService.findOne(firstCard.getFacilityId());
    LOGGER.debug("Calling ref data to retrieve program info for card");
    ProgramDto program = programRefDataService.findOne(firstCard.getProgramId());
    return stockCards.stream().map(card -> cardToDto(facility, program, card)).collect(toList());
}
Also used : CollectionUtils.isEmpty(org.springframework.util.CollectionUtils.isEmpty) FacilityReferenceDataService(org.openlmis.stockmanagement.service.referencedata.FacilityReferenceDataService) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) Logger(org.slf4j.Logger) Collections.emptyList(java.util.Collections.emptyList) ProgramReferenceDataService(org.openlmis.stockmanagement.service.referencedata.ProgramReferenceDataService) LoggerFactory(org.slf4j.LoggerFactory) LotDto(org.openlmis.stockmanagement.dto.referencedata.LotDto) Autowired(org.springframework.beans.factory.annotation.Autowired) ProgramDto(org.openlmis.stockmanagement.dto.referencedata.ProgramDto) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) Service(org.springframework.stereotype.Service) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) StockCardLineItemDto(org.openlmis.stockmanagement.dto.StockCardLineItemDto) ProgramDto(org.openlmis.stockmanagement.dto.referencedata.ProgramDto) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto)

Aggregations

FacilityDto (org.openlmis.stockmanagement.dto.referencedata.FacilityDto)12 UUID (java.util.UUID)8 UUID.randomUUID (java.util.UUID.randomUUID)5 Test (org.junit.Test)5 ProgramDto (org.openlmis.stockmanagement.dto.referencedata.ProgramDto)5 Node (org.openlmis.stockmanagement.domain.sourcedestination.Node)4 ValidSourceDestinationDto (org.openlmis.stockmanagement.dto.ValidSourceDestinationDto)4 LotDto (org.openlmis.stockmanagement.dto.referencedata.LotDto)4 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)4 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)3 ValidDestinationAssignment (org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment)3 ValidSourceAssignment (org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment)3 List (java.util.List)2 StockCardDto (org.openlmis.stockmanagement.dto.StockCardDto)2 StockCardLineItemDto (org.openlmis.stockmanagement.dto.StockCardLineItemDto)2 FacilityReferenceDataService (org.openlmis.stockmanagement.service.referencedata.FacilityReferenceDataService)2 StockEventProcessContext (org.openlmis.stockmanagement.util.StockEventProcessContext)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Service (org.springframework.stereotype.Service)2 ArrayList (java.util.ArrayList)1