Search in sources :

Example 11 with Pageable

use of org.springframework.data.domain.Pageable in project apollo by ctripcorp.

the class InstanceServiceTest method testFindActiveInstanceConfigs.

@Test
@Rollback
public void testFindActiveInstanceConfigs() throws Exception {
    long someInstanceId = 1;
    long anotherInstanceId = 2;
    String someConfigAppId = "someConfigAppId";
    String someConfigClusterName = "someConfigClusterName";
    String someConfigNamespaceName = "someConfigNamespaceName";
    Date someValidDate = new Date();
    Pageable pageable = PageRequest.of(0, 10);
    String someReleaseKey = "someReleaseKey";
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, -2);
    Date someInvalidDate = calendar.getTime();
    prepareInstanceConfigForInstance(someInstanceId, someConfigAppId, someConfigClusterName, someConfigNamespaceName, someReleaseKey, someValidDate);
    prepareInstanceConfigForInstance(anotherInstanceId, someConfigAppId, someConfigClusterName, someConfigNamespaceName, someReleaseKey, someInvalidDate);
    Page<InstanceConfig> validInstanceConfigs = instanceService.findActiveInstanceConfigsByReleaseKey(someReleaseKey, pageable);
    assertEquals(1, validInstanceConfigs.getContent().size());
    assertEquals(someInstanceId, validInstanceConfigs.getContent().get(0).getInstanceId());
}
Also used : Pageable(org.springframework.data.domain.Pageable) InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) Calendar(java.util.Calendar) Date(java.util.Date) Test(org.junit.Test) AbstractIntegrationTest(com.ctrip.framework.apollo.biz.AbstractIntegrationTest) Rollback(org.springframework.test.annotation.Rollback)

Example 12 with Pageable

use of org.springframework.data.domain.Pageable in project apollo by ctripcorp.

the class NamespaceServiceTest method testFindPublicAppNamespaceWithWrongNamespace.

@Test(expected = BadRequestException.class)
public void testFindPublicAppNamespaceWithWrongNamespace() {
    Pageable page = PageRequest.of(0, 10);
    when(appNamespaceService.findPublicNamespaceByName(testPublicAppNamespace)).thenReturn(null);
    namespaceService.findPublicAppNamespaceAllNamespaces(testPublicAppNamespace, page);
}
Also used : Pageable(org.springframework.data.domain.Pageable) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.biz.AbstractUnitTest)

Example 13 with Pageable

use of org.springframework.data.domain.Pageable in project apollo by ctripcorp.

the class InstanceConfigController method getByRelease.

@GetMapping("/by-release")
public PageDTO<InstanceDTO> getByRelease(@RequestParam("releaseId") long releaseId, Pageable pageable) {
    Release release = releaseService.findOne(releaseId);
    if (release == null) {
        throw new NotFoundException(String.format("release not found for %s", releaseId));
    }
    Page<InstanceConfig> instanceConfigsPage = instanceService.findActiveInstanceConfigsByReleaseKey(release.getReleaseKey(), pageable);
    List<InstanceDTO> instanceDTOs = Collections.emptyList();
    if (instanceConfigsPage.hasContent()) {
        Multimap<Long, InstanceConfig> instanceConfigMap = HashMultimap.create();
        Set<String> otherReleaseKeys = Sets.newHashSet();
        for (InstanceConfig instanceConfig : instanceConfigsPage.getContent()) {
            instanceConfigMap.put(instanceConfig.getInstanceId(), instanceConfig);
            otherReleaseKeys.add(instanceConfig.getReleaseKey());
        }
        Set<Long> instanceIds = instanceConfigMap.keySet();
        List<Instance> instances = instanceService.findInstancesByIds(instanceIds);
        if (!CollectionUtils.isEmpty(instances)) {
            instanceDTOs = BeanUtils.batchTransform(InstanceDTO.class, instances);
        }
        for (InstanceDTO instanceDTO : instanceDTOs) {
            Collection<InstanceConfig> configs = instanceConfigMap.get(instanceDTO.getId());
            List<InstanceConfigDTO> configDTOs = configs.stream().map(instanceConfig -> {
                InstanceConfigDTO instanceConfigDTO = new InstanceConfigDTO();
                // to save some space
                instanceConfigDTO.setRelease(null);
                instanceConfigDTO.setReleaseDeliveryTime(instanceConfig.getReleaseDeliveryTime());
                instanceConfigDTO.setDataChangeLastModifiedTime(instanceConfig.getDataChangeLastModifiedTime());
                return instanceConfigDTO;
            }).collect(Collectors.toList());
            instanceDTO.setConfigs(configDTOs);
        }
    }
    return new PageDTO<>(instanceDTOs, pageable, instanceConfigsPage.getTotalElements());
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) Multimap(com.google.common.collect.Multimap) BeanUtils(com.ctrip.framework.apollo.common.utils.BeanUtils) Strings(com.google.common.base.Strings) HashMultimap(com.google.common.collect.HashMultimap) InstanceConfigDTO(com.ctrip.framework.apollo.common.dto.InstanceConfigDTO) Instance(com.ctrip.framework.apollo.biz.entity.Instance) Map(java.util.Map) InstanceDTO(com.ctrip.framework.apollo.common.dto.InstanceDTO) GetMapping(org.springframework.web.bind.annotation.GetMapping) InstanceService(com.ctrip.framework.apollo.biz.service.InstanceService) Pageable(org.springframework.data.domain.Pageable) Splitter(com.google.common.base.Splitter) Collection(java.util.Collection) ReleaseService(com.ctrip.framework.apollo.biz.service.ReleaseService) InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) PageRequest(org.springframework.data.domain.PageRequest) Set(java.util.Set) Release(com.ctrip.framework.apollo.biz.entity.Release) Page(org.springframework.data.domain.Page) Maps(com.google.common.collect.Maps) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) PageDTO(com.ctrip.framework.apollo.common.dto.PageDTO) CollectionUtils(org.springframework.util.CollectionUtils) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Collections(java.util.Collections) PageDTO(com.ctrip.framework.apollo.common.dto.PageDTO) Instance(com.ctrip.framework.apollo.biz.entity.Instance) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) InstanceDTO(com.ctrip.framework.apollo.common.dto.InstanceDTO) InstanceConfigDTO(com.ctrip.framework.apollo.common.dto.InstanceConfigDTO) Release(com.ctrip.framework.apollo.biz.entity.Release) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 14 with Pageable

use of org.springframework.data.domain.Pageable in project sic by belluccifranco.

the class CajaServiceImpl method getCajasCriteria.

@Override
public Page<Caja> getCajasCriteria(BusquedaCajaCriteria criteria) {
    // Empresa
    if (criteria.getEmpresa() == null) {
        throw new EntityNotFoundException(ResourceBundle.getBundle("Mensajes").getString("mensaje_empresa_no_existente"));
    }
    // Fecha
    if (criteria.isBuscaPorFecha() == true && (criteria.getFechaDesde() == null || criteria.getFechaHasta() == null)) {
        throw new BusinessServiceException(ResourceBundle.getBundle("Mensajes").getString("mensaje_caja_fechas_invalidas"));
    }
    if (criteria.isBuscaPorFecha() == true) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(criteria.getFechaDesde());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        criteria.setFechaDesde(cal.getTime());
        cal.setTime(criteria.getFechaHasta());
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        criteria.setFechaHasta(cal.getTime());
    }
    if (criteria.getEmpresa() == null) {
        throw new EntityNotFoundException(ResourceBundle.getBundle("Mensajes").getString("mensaje_empresa_no_existente"));
    }
    QCaja qcaja = QCaja.caja;
    BooleanBuilder builder = new BooleanBuilder();
    builder.and(qcaja.empresa.eq(criteria.getEmpresa()).and(qcaja.eliminada.eq(false)));
    if (criteria.isBuscaPorUsuario() == true) {
        builder.and(qcaja.usuarioCierraCaja.eq(criteria.getUsuario()));
    }
    if (criteria.isBuscaPorFecha() == true) {
        FormatterFechaHora formateadorFecha = new FormatterFechaHora(FormatterFechaHora.FORMATO_FECHAHORA_INTERNACIONAL);
        DateExpression<Date> fDesde = Expressions.dateTemplate(Date.class, "convert({0}, datetime)", formateadorFecha.format(criteria.getFechaDesde()));
        DateExpression<Date> fHasta = Expressions.dateTemplate(Date.class, "convert({0}, datetime)", formateadorFecha.format(criteria.getFechaHasta()));
        builder.and(qcaja.fechaApertura.between(fDesde, fHasta));
    }
    int pageNumber = 0;
    int pageSize = Integer.MAX_VALUE;
    Sort sorting = new Sort(Sort.Direction.DESC, "fechaApertura");
    if (criteria.getPageable() != null) {
        pageNumber = criteria.getPageable().getPageNumber();
        pageSize = criteria.getPageable().getPageSize();
        sorting = criteria.getPageable().getSort();
    }
    Pageable pageable = new PageRequest(pageNumber, pageSize, sorting);
    return cajaRepository.findAll(builder, pageable);
}
Also used : FormatterFechaHora(sic.util.FormatterFechaHora) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) EntityNotFoundException(javax.persistence.EntityNotFoundException) Date(java.util.Date) LocalDate(java.time.LocalDate) PageRequest(org.springframework.data.domain.PageRequest) BusinessServiceException(sic.service.BusinessServiceException) QCaja(sic.modelo.QCaja) Pageable(org.springframework.data.domain.Pageable) BooleanBuilder(com.querydsl.core.BooleanBuilder) Sort(org.springframework.data.domain.Sort)

Example 15 with Pageable

use of org.springframework.data.domain.Pageable in project sic by belluccifranco.

the class PagoController method getPagosPorClienteEntreFechas.

@GetMapping("/pagos/clientes/{idCliente}")
@ResponseStatus(HttpStatus.OK)
public Page<Pago> getPagosPorClienteEntreFechas(@PathVariable long idCliente, @RequestParam(required = false) Long desde, @RequestParam(required = false) Long hasta, @RequestParam(required = false) Integer pagina, @RequestParam(required = false) Integer tamanio) {
    Calendar fechaDesde = Calendar.getInstance();
    Calendar fechaHasta = Calendar.getInstance();
    if ((desde != null) && (hasta != null)) {
        fechaDesde.setTimeInMillis(desde);
        fechaHasta.setTimeInMillis(hasta);
    }
    fechaDesde.set(Calendar.HOUR_OF_DAY, 0);
    fechaDesde.set(Calendar.MINUTE, 0);
    fechaDesde.set(Calendar.SECOND, 0);
    fechaHasta.set(Calendar.HOUR_OF_DAY, 23);
    fechaHasta.set(Calendar.MINUTE, 59);
    fechaHasta.set(Calendar.SECOND, 59);
    if (tamanio == null || tamanio <= 0) {
        tamanio = TAMANIO_PAGINA_DEFAULT;
    }
    if (pagina == null || pagina < 0) {
        pagina = 0;
    }
    Pageable pageable = new PageRequest(pagina, tamanio, new Sort(Sort.Direction.DESC, "fecha"));
    return pagoService.getPagosPorClienteEntreFechas(idCliente, fechaDesde.getTime(), fechaHasta.getTime(), pageable);
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) Calendar(java.util.Calendar) Sort(org.springframework.data.domain.Sort) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Aggregations

Pageable (org.springframework.data.domain.Pageable)185 PageRequest (org.springframework.data.domain.PageRequest)88 Sort (org.springframework.data.domain.Sort)81 Test (org.junit.Test)39 PageImpl (org.springframework.data.domain.PageImpl)28 ArrayList (java.util.ArrayList)18 Collectors (java.util.stream.Collectors)17 List (java.util.List)15 Page (org.springframework.data.domain.Page)15 Autowired (org.springframework.beans.factory.annotation.Autowired)14 GetMapping (org.springframework.web.bind.annotation.GetMapping)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 Test (org.junit.jupiter.api.Test)11 UUID (java.util.UUID)10 ApiOperation (io.swagger.annotations.ApiOperation)9 Calendar (java.util.Calendar)9 java.util (java.util)8 Lists (com.google.common.collect.Lists)7 Map (java.util.Map)7 Transactional (org.springframework.transaction.annotation.Transactional)6