Search in sources :

Example 1 with ERROR_EVENT_ORDERABLE_INVALID

use of org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_EVENT_ORDERABLE_INVALID in project openlmis-stockmanagement by OpenLMIS.

the class VvmValidator method validate.

/**
 * Validates whether the vvm applicables have proper vvm status (if applicable).
 * Throws ValidationMessageException if any of items is in invalid state.
 *  @param vvmApplicables list of items to test
 * @param messageKey error message key for exception
 * @param ignoreMissingOrderable whether should
 */
public void validate(List<? extends VvmApplicable> vvmApplicables, String messageKey, boolean ignoreMissingOrderable) {
    Set<UUID> orderableIds = vvmApplicables.stream().map(VvmApplicable::getOrderableId).collect(Collectors.toSet());
    List<OrderableDto> orderables = orderableReferenceDataService.findByIds(orderableIds);
    Map<UUID, OrderableDto> groupById = orderables.stream().collect(Collectors.toMap(OrderableDto::getId, orderable -> orderable));
    for (VvmApplicable item : vvmApplicables) {
        OrderableDto orderable = groupById.get(item.getOrderableId());
        if (null == orderable) {
            if (ignoreMissingOrderable) {
                continue;
            } else {
                throw new ValidationMessageException(ERROR_EVENT_ORDERABLE_INVALID);
            }
        }
        boolean useVvm = false;
        boolean hasVvmStatus = false;
        if (orderable.getExtraData() != null) {
            useVvm = Boolean.parseBoolean(orderable.getExtraData().get(USE_VVM));
        }
        if (item.getExtraData() != null) {
            hasVvmStatus = item.getExtraData().get(VVM_STATUS) != null;
        }
        if (!useVvm && hasVvmStatus) {
            throw new ValidationMessageException(messageKey);
        }
    }
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) Component(org.springframework.stereotype.Component) List(java.util.List) ERROR_EVENT_ORDERABLE_INVALID(org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_EVENT_ORDERABLE_INVALID) OrderableReferenceDataService(org.openlmis.stockmanagement.service.referencedata.OrderableReferenceDataService) OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) Map(java.util.Map) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) UUID(java.util.UUID)

Aggregations

List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 VvmApplicable (org.openlmis.stockmanagement.domain.common.VvmApplicable)1 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)1 ValidationMessageException (org.openlmis.stockmanagement.exception.ValidationMessageException)1 ERROR_EVENT_ORDERABLE_INVALID (org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_EVENT_ORDERABLE_INVALID)1 OrderableReferenceDataService (org.openlmis.stockmanagement.service.referencedata.OrderableReferenceDataService)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Component (org.springframework.stereotype.Component)1