From bcd742fca08d91b3bdf092505005baccded0ec7f Mon Sep 17 00:00:00 2001 From: insanity Date: Mon, 28 Aug 2017 14:46:42 +0900 Subject: [PATCH] sensor paging sorting --- .../overflow/commons/utils/PageUtil.java | 3 + .../module/infra/service/InfraService.java | 20 ++---- .../overflow/module/sensor/dao/SensorDAO.java | 6 +- .../module/sensor/dao/SensorItemDAO.java | 3 +- .../sensor/service/SensorItemService.java | 6 +- .../module/sensor/service/SensorService.java | 17 +++-- .../sensor/service/SensorItemServiceTest.java | 23 +++--- .../sensor/service/SensorServiceTest.java | 72 +++++++++---------- 8 files changed, 73 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/loafle/overflow/commons/utils/PageUtil.java b/src/main/java/com/loafle/overflow/commons/utils/PageUtil.java index e39518c..61dc91a 100644 --- a/src/main/java/com/loafle/overflow/commons/utils/PageUtil.java +++ b/src/main/java/com/loafle/overflow/commons/utils/PageUtil.java @@ -16,6 +16,9 @@ public class PageUtil { } public static PageRequest getPageRequest(PageParams pageParams) { + if(pageParams.getSortCol().isEmpty()) pageParams.setSortCol("id"); + if(pageParams.getSortDirection().isEmpty()) pageParams.setSortDirection("descending"); + return new PageRequest(pageParams.getPageNo(), pageParams.getCountPerPage(), new Sort(PageUtil.getSortDirection(pageParams.getSortDirection()), pageParams.getSortCol())); } diff --git a/src/main/java/com/loafle/overflow/module/infra/service/InfraService.java b/src/main/java/com/loafle/overflow/module/infra/service/InfraService.java index e43e4dd..822bcd5 100644 --- a/src/main/java/com/loafle/overflow/module/infra/service/InfraService.java +++ b/src/main/java/com/loafle/overflow/module/infra/service/InfraService.java @@ -1,5 +1,7 @@ package com.loafle.overflow.module.infra.service; +import com.loafle.overflow.commons.model.PageParams; +import com.loafle.overflow.commons.utils.PageUtil; import com.loafle.overflow.module.domain.model.Domain; import com.loafle.overflow.module.infra.dao.InfraDAO; import com.loafle.overflow.module.infra.model.Infra; @@ -8,9 +10,6 @@ import com.loafle.overflow.module.probe.service.ProbeService; import com.loafle.overflow.module.target.model.Target; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import java.util.List; @@ -36,20 +35,13 @@ public class InfraService { return this.infraDAO.findOne(id); } - public Page readAllByProbe(Probe probe, int pageNo, int countPerPage) { - Pageable pageRequest = - new PageRequest(pageNo, countPerPage, new Sort(Sort.Direction.DESC, "id")); - return this.infraDAO.findAllByProbe(probe, pageRequest); + public Page readAllByProbe(Probe probe, PageParams pageParams) { + return this.infraDAO.findAllByProbe(probe, PageUtil.getPageRequest(pageParams)); } - public Page readAllByDomain(Domain domain, int pageNo, int countPerPage) { - + public Page readAllByDomain(Domain domain, PageParams pageParams) { List probeList = this.probeService.readAllByDomain(domain); - - Pageable pageRequest = - new PageRequest(pageNo, countPerPage, new Sort(Sort.Direction.DESC, "id")); - - return this.infraDAO.findAllByProbeList(probeList, pageRequest); + return this.infraDAO.findAllByProbeList(probeList, PageUtil.getPageRequest(pageParams)); } // public List readAllByProbeList(List probeList) { diff --git a/src/main/java/com/loafle/overflow/module/sensor/dao/SensorDAO.java b/src/main/java/com/loafle/overflow/module/sensor/dao/SensorDAO.java index faa2d1f..fd48353 100644 --- a/src/main/java/com/loafle/overflow/module/sensor/dao/SensorDAO.java +++ b/src/main/java/com/loafle/overflow/module/sensor/dao/SensorDAO.java @@ -3,6 +3,8 @@ package com.loafle.overflow.module.sensor.dao; import com.loafle.overflow.module.sensor.model.Sensor; import com.loafle.overflow.module.target.model.Target; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -17,10 +19,10 @@ import java.util.List; public interface SensorDAO extends JpaRepository { - List findAllByTarget(Target target); + Page findAllByTarget(Target target, Pageable pageable); @Query("SELECT s from Sensor s WHERE s.target in (:targetList)") - List findAllByTargetList(@Param("targetList") List targetList); + Page findAllByTargetList(@Param("targetList") List targetList, Pageable pageable); // List findAllByTargetList(List targets); } diff --git a/src/main/java/com/loafle/overflow/module/sensor/dao/SensorItemDAO.java b/src/main/java/com/loafle/overflow/module/sensor/dao/SensorItemDAO.java index 196f62f..dcd4df4 100644 --- a/src/main/java/com/loafle/overflow/module/sensor/dao/SensorItemDAO.java +++ b/src/main/java/com/loafle/overflow/module/sensor/dao/SensorItemDAO.java @@ -2,6 +2,7 @@ package com.loafle.overflow.module.sensor.dao; import com.loafle.overflow.module.sensor.model.Sensor; import com.loafle.overflow.module.sensor.model.SensorItem; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -12,5 +13,5 @@ import java.util.List; */ @Repository public interface SensorItemDAO extends JpaRepository { - List findAllBySensor(Sensor sensor); + List findAllBySensor(Sensor sensor, Pageable pageable); } diff --git a/src/main/java/com/loafle/overflow/module/sensor/service/SensorItemService.java b/src/main/java/com/loafle/overflow/module/sensor/service/SensorItemService.java index cf2e1d8..c0eb4ac 100644 --- a/src/main/java/com/loafle/overflow/module/sensor/service/SensorItemService.java +++ b/src/main/java/com/loafle/overflow/module/sensor/service/SensorItemService.java @@ -1,5 +1,7 @@ package com.loafle.overflow.module.sensor.service; +import com.loafle.overflow.commons.model.PageParams; +import com.loafle.overflow.commons.utils.PageUtil; import com.loafle.overflow.module.sensor.dao.SensorItemDAO; import com.loafle.overflow.module.sensor.model.Sensor; import com.loafle.overflow.module.sensor.model.SensorItem; @@ -29,8 +31,8 @@ public class SensorItemService { return this.sensorItemDAO.findOne(Long.valueOf(id)); } - public List readAllBySensor(Sensor sensor) { - return this.sensorItemDAO.findAllBySensor(sensor); + public List readAllBySensor(Sensor sensor, PageParams pageParams) { + return this.sensorItemDAO.findAllBySensor(sensor, PageUtil.getPageRequest(pageParams)); } public void remove(SensorItem sensorItem) { diff --git a/src/main/java/com/loafle/overflow/module/sensor/service/SensorService.java b/src/main/java/com/loafle/overflow/module/sensor/service/SensorService.java index 1f2c6b1..f4a8975 100644 --- a/src/main/java/com/loafle/overflow/module/sensor/service/SensorService.java +++ b/src/main/java/com/loafle/overflow/module/sensor/service/SensorService.java @@ -1,5 +1,7 @@ package com.loafle.overflow.module.sensor.service; +import com.loafle.overflow.commons.model.PageParams; +import com.loafle.overflow.commons.utils.PageUtil; import com.loafle.overflow.module.domain.model.Domain; import com.loafle.overflow.module.infra.model.Infra; import com.loafle.overflow.module.infra.service.InfraService; @@ -11,6 +13,7 @@ import com.loafle.overflow.module.sensor.model.Sensor; import com.loafle.overflow.module.sensor.model.SensorItem; import com.loafle.overflow.module.target.model.Target; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import javax.transaction.Transactional; @@ -38,25 +41,25 @@ public class SensorService { return this.sensorDAO.save(sensor); } - public List readAllByTarget(Target target) { - return this.sensorDAO.findAllByTarget(target); - } +// public List readAllByTarget(Target target, PageParams pageParams) { +// return this.sensorDAO.findAllByTarget(target, PageUtil.getPageRequest(pageParams)); +// } - public List readAllByDomain(Domain domain) { + public Page readAllByDomain(Domain domain, PageParams pageParams) { List probeList = this.probeService.readAllByDomain(domain); List targetList = this.infraService.readAllTargetByProbeList(probeList); - return this.sensorDAO.findAllByTargetList(targetList); + return this.sensorDAO.findAllByTargetList(targetList, PageUtil.getPageRequest(pageParams)); } - public List readAllByInfra(long infraId) { + public Page readAllByInfra(long infraId, PageParams pageParams) { Infra dbInfra = this.infraService.read(infraId); if(dbInfra == null || dbInfra.getTarget() == null) return null; - return this.sensorDAO.findAllByTarget(dbInfra.getTarget()); + return this.sensorDAO.findAllByTarget(dbInfra.getTarget(), PageUtil.getPageRequest(pageParams)); } diff --git a/src/test/java/com/loafle/overflow/module/sensor/service/SensorItemServiceTest.java b/src/test/java/com/loafle/overflow/module/sensor/service/SensorItemServiceTest.java index bf1176c..134e226 100644 --- a/src/test/java/com/loafle/overflow/module/sensor/service/SensorItemServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/sensor/service/SensorItemServiceTest.java @@ -4,7 +4,6 @@ import com.loafle.overflow.module.meta.model.MetaSensorItem; import com.loafle.overflow.module.sensor.model.Sensor; import com.loafle.overflow.module.sensor.model.SensorItem; import com.loafle.overflow.spring.AppConfigTest; -import org.codehaus.jackson.map.ObjectMapper; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @@ -13,9 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.io.IOException; import java.util.Date; -import java.util.List; /** * Created by insanity on 17. 6. 28. @@ -49,14 +46,14 @@ public class SensorItemServiceTest { Assert.assertNotNull(res); } - @Test - public void readAllBySensor() throws IOException { - Sensor sensor = new Sensor(); - sensor.setId(1); - List sensorItems = this.sensorItemService.readAllBySensor(sensor); - - ObjectMapper objectMapper = new ObjectMapper(); - String json = objectMapper.writeValueAsString(sensorItems); - System.out.println(json); - } +// @Test +// public void readAllBySensor() throws IOException { +// Sensor sensor = new Sensor(); +// sensor.setId(1); +// List sensorItems = this.sensorItemService.readAllBySensor(sensor); +// +// ObjectMapper objectMapper = new ObjectMapper(); +// String json = objectMapper.writeValueAsString(sensorItems); +// System.out.println(json); +// } } diff --git a/src/test/java/com/loafle/overflow/module/sensor/service/SensorServiceTest.java b/src/test/java/com/loafle/overflow/module/sensor/service/SensorServiceTest.java index 5e8fc4a..62a5c8e 100644 --- a/src/test/java/com/loafle/overflow/module/sensor/service/SensorServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/sensor/service/SensorServiceTest.java @@ -1,8 +1,5 @@ package com.loafle.overflow.module.sensor.service; -import com.loafle.overflow.module.domain.model.Domain; -import com.loafle.overflow.module.infra.model.Infra; -import com.loafle.overflow.module.infra.model.InfraMachine; import com.loafle.overflow.module.meta.model.MetaCrawler; import com.loafle.overflow.module.meta.model.MetaSensorStatus; import com.loafle.overflow.module.sensor.model.Sensor; @@ -18,7 +15,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Date; -import java.util.List; /** * Created by insanity on 17. 6. 28. @@ -62,40 +58,40 @@ public class SensorServiceTest { System.out.println(json); } - @Test - public void readAllByTarget() throws Exception { - Target target = new Target(); - target.setId(1); - List sensors = this.sensorService.readAllByTarget(target); +// @Test +// public void readAllByTarget() throws Exception { +// Target target = new Target(); +// target.setId(1); +// List sensors = this.sensorService.readAllByTarget(target); +// +// ObjectMapper objectMapper = new ObjectMapper(); +// +// String json = objectMapper.writeValueAsString(sensors); +// +// System.out.println(json); +// } - ObjectMapper objectMapper = new ObjectMapper(); +// @Test +// public void readAllByTargetList() { +// +// Domain domain = new Domain(); +// domain.setId(1); +// +// List sl = this.sensorService.readAllByDomain(domain); +// +// Assert.assertNotEquals(sl.size(), 0); +// +// } - String json = objectMapper.writeValueAsString(sensors); - - System.out.println(json); - } - - @Test - public void readAllByTargetList() { - - Domain domain = new Domain(); - domain.setId(1); - - List sl = this.sensorService.readAllByDomain(domain); - - Assert.assertNotEquals(sl.size(), 0); - - } - - @Test - public void readAllByInfra() { - - Infra infra = new InfraMachine(); - infra.setId(1); - - List sl = this.sensorService.readAllByInfra(1); - - Assert.assertNotEquals(sl.size(), 0); - - } +// @Test +// public void readAllByInfra() { +// +// Infra infra = new InfraMachine(); +// infra.setId(1); +// +// List sl = this.sensorService.readAllByInfra(1); +// +// Assert.assertNotEquals(sl.size(), 0); +// +// } }