sensor paging sorting

This commit is contained in:
insanity 2017-08-28 14:46:42 +09:00
parent 066c966588
commit bcd742fca0
8 changed files with 73 additions and 77 deletions

View File

@ -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()));
}

View File

@ -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<Infra> 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<Infra> readAllByProbe(Probe probe, PageParams pageParams) {
return this.infraDAO.findAllByProbe(probe, PageUtil.getPageRequest(pageParams));
}
public Page<Infra> readAllByDomain(Domain domain, int pageNo, int countPerPage) {
public Page<Infra> readAllByDomain(Domain domain, PageParams pageParams) {
List<Probe> 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<Infra> readAllByProbeList(List<Probe> probeList) {

View File

@ -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<Sensor, Long> {
List<Sensor> findAllByTarget(Target target);
Page<Sensor> findAllByTarget(Target target, Pageable pageable);
@Query("SELECT s from Sensor s WHERE s.target in (:targetList)")
List<Sensor> findAllByTargetList(@Param("targetList") List<Target> targetList);
Page<Sensor> findAllByTargetList(@Param("targetList") List<Target> targetList, Pageable pageable);
// List<Sensor> findAllByTargetList(List<Target> targets);
}

View File

@ -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<SensorItem, Long> {
List<SensorItem> findAllBySensor(Sensor sensor);
List<SensorItem> findAllBySensor(Sensor sensor, Pageable pageable);
}

View File

@ -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<SensorItem> readAllBySensor(Sensor sensor) {
return this.sensorItemDAO.findAllBySensor(sensor);
public List<SensorItem> readAllBySensor(Sensor sensor, PageParams pageParams) {
return this.sensorItemDAO.findAllBySensor(sensor, PageUtil.getPageRequest(pageParams));
}
public void remove(SensorItem sensorItem) {

View File

@ -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<Sensor> readAllByTarget(Target target) {
return this.sensorDAO.findAllByTarget(target);
}
// public List<Sensor> readAllByTarget(Target target, PageParams pageParams) {
// return this.sensorDAO.findAllByTarget(target, PageUtil.getPageRequest(pageParams));
// }
public List<Sensor> readAllByDomain(Domain domain) {
public Page<Sensor> readAllByDomain(Domain domain, PageParams pageParams) {
List<Probe> probeList = this.probeService.readAllByDomain(domain);
List<Target> targetList = this.infraService.readAllTargetByProbeList(probeList);
return this.sensorDAO.findAllByTargetList(targetList);
return this.sensorDAO.findAllByTargetList(targetList, PageUtil.getPageRequest(pageParams));
}
public List<Sensor> readAllByInfra(long infraId) {
public Page<Sensor> 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));
}

View File

@ -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<SensorItem> 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<SensorItem> sensorItems = this.sensorItemService.readAllBySensor(sensor);
//
// ObjectMapper objectMapper = new ObjectMapper();
// String json = objectMapper.writeValueAsString(sensorItems);
// System.out.println(json);
// }
}

View File

@ -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<Sensor> sensors = this.sensorService.readAllByTarget(target);
// @Test
// public void readAllByTarget() throws Exception {
// Target target = new Target();
// target.setId(1);
// List<Sensor> 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<Sensor> 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<Sensor> sl = this.sensorService.readAllByDomain(domain);
Assert.assertNotEquals(sl.size(), 0);
}
@Test
public void readAllByInfra() {
Infra infra = new InfraMachine();
infra.setId(1);
List<Sensor> sl = this.sensorService.readAllByInfra(1);
Assert.assertNotEquals(sl.size(), 0);
}
// @Test
// public void readAllByInfra() {
//
// Infra infra = new InfraMachine();
// infra.setId(1);
//
// List<Sensor> sl = this.sensorService.readAllByInfra(1);
//
// Assert.assertNotEquals(sl.size(), 0);
//
// }
}