DtoCarrierServiceImpl extends AbstractDtoServiceImpl<CarrierDTO, CarrierDTOImpl, Carrier> implements DtoCarrierService { @Override public SearchResult<CarrierDTO> findCarriers(final SearchContext filter) throws UnmappedInterfaceException, UnableToCreateInstanceException { final Map<String, List> params = filter.reduceParameters("filter", "shopIds"); final String textFilter = FilterSearchUtils.getStringFilter(params.get("filter")); final List shopIds = params.get("shopIds"); final int pageSize = filter.getSize(); final int startIndex = filter.getStart() * pageSize; final Map<String, List> currentFilter = new HashMap<>(); if (StringUtils.isNotBlank(textFilter)) { final String basic = textFilter; SearchContext.JoinMode.OR.setMode(currentFilter); currentFilter.put("name", Collections.singletonList(basic)); currentFilter.put("description", Collections.singletonList(basic)); currentFilter.put("guid", Collections.singletonList(basic)); } final CarrierService carrierService = (CarrierService) service; if (CollectionUtils.isNotEmpty(shopIds)) { currentFilter.put("shopIds", shopIds); } final int count = carrierService.findCarrierCount(currentFilter); if (count > startIndex) { final List<CarrierDTO> entities = new ArrayList<>(); final List<Carrier> carriers = carrierService.findCarriers(startIndex, pageSize, filter.getSortBy(), filter.isSortDesc(), currentFilter); fillDTOs(carriers, entities); return new SearchResult<>(filter, entities, count); } return new SearchResult<>(filter, Collections.emptyList(), count); } DtoCarrierServiceImpl(final GenericService<Carrier> carrierGenericService,
                                 final GenericDAO<Shop, Long> shopDao,
                                 final DtoFactory dtoFactory,
                                 final AdaptersRepository adaptersRepository); @Override SearchResult<CarrierDTO> findCarriers(final SearchContext filter); @Override Map<CarrierDTO, Map<ShopDTO, Boolean>> getAllWithShops(); @Override Map<CarrierDTO, Boolean> findAllByShopId(final long shopId); @Override Map<ShopDTO, Boolean> getAssignedCarrierShops(final long carrierId); @Override List<ShopDTO> getAvailableCarrierShops(final long carrierId); @Override void assignToShop(final long carrierId, final long shopId, final boolean soft); @Override void unassignFromShop(final long carrierId, final long shopId, final boolean soft); @Override Class<CarrierDTO> getDtoIFace(); @Override Class<CarrierDTOImpl> getDtoImpl(); @Override Class<Carrier> getEntityIFace();  }
@Test public void testFindCarriers() throws Exception { final List<Long> shopIdsNone = Collections.singletonList(9999L); final List<Long> shopIds = Arrays.asList(10L); SearchContext ctx; SearchResult<CarrierDTO> rez; ctx = createSearchContext("guid", false, 0, 10, "filter", "1_CARRIER", "shopIds", shopIds ); rez = dtoService.findCarriers(ctx); assertEquals(1, rez.getTotal()); assertEquals(1, rez.getItems().size()); assertEquals("1_CARRIER", rez.getItems().get(0).getCode()); ctx = createSearchContext("guid", false, 0, 10, "shopIds", shopIdsNone ); rez = dtoService.findCarriers(ctx); assertEquals(0, rez.getTotal()); ctx = createSearchContext("guid", false, 0, 10, "shopIds", shopIds ); rez = dtoService.findCarriers(ctx); assertEquals(2, rez.getTotal()); assertEquals(2, rez.getItems().size()); assertEquals("1_CARRIER", rez.getItems().get(0).getCode()); assertEquals("2_CARRIER", rez.getItems().get(1).getCode()); ctx = createSearchContext("guid", false, 0, 10, "filter", "_CARRIER" ); rez = dtoService.findCarriers(ctx); assertEquals(3, rez.getTotal()); assertEquals(3, rez.getItems().size()); assertEquals("1_CARRIER", rez.getItems().get(0).getCode()); assertEquals("2_CARRIER", rez.getItems().get(1).getCode()); assertEquals("3_CARRIER", rez.getItems().get(2).getCode()); }