diff --git a/escheduler-api/src/main/java/cn/escheduler/api/controller/QueueController.java b/escheduler-api/src/main/java/cn/escheduler/api/controller/QueueController.java index ac18b5c7c3..d34a9e0d30 100644 --- a/escheduler-api/src/main/java/cn/escheduler/api/controller/QueueController.java +++ b/escheduler-api/src/main/java/cn/escheduler/api/controller/QueueController.java @@ -17,6 +17,7 @@ package cn.escheduler.api.controller; +import cn.escheduler.api.enums.Status; import cn.escheduler.api.service.QueueService; import cn.escheduler.api.utils.Constants; import cn.escheduler.api.utils.Result; @@ -29,7 +30,7 @@ import org.springframework.web.bind.annotation.*; import java.util.Map; -import static cn.escheduler.api.enums.Status.QUERY_QUEUE_LIST_ERROR; +import static cn.escheduler.api.enums.Status.*; /** @@ -63,5 +64,82 @@ public class QueueController extends BaseController{ } } + /** + * query queue list paging + * @param loginUser + * @return + */ + @GetMapping(value="/list-paging") + @ResponseStatus(HttpStatus.OK) + public Result queryQueueListPaging(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("pageNo") Integer pageNo, + @RequestParam(value = "searchVal", required = false) String searchVal, + @RequestParam("pageSize") Integer pageSize){ + try{ + logger.info("login user {}, query queue list,search value:{}", loginUser.getUserName(),searchVal); + Map result = checkPageParams(pageNo, pageSize); + if(result.get(Constants.STATUS) != Status.SUCCESS){ + return returnDataListPaging(result); + } + + result = queueService.queryList(loginUser,searchVal,pageNo,pageSize); + return returnDataList(result); + }catch (Exception e){ + logger.error(QUERY_QUEUE_LIST_ERROR.getMsg(),e); + return error(QUERY_QUEUE_LIST_ERROR.getCode(), QUERY_QUEUE_LIST_ERROR.getMsg()); + } + } + + /** + * create queue + * + * @param loginUser + * @param queue + * @param queueName + * @return + */ + @PostMapping(value = "/create") + @ResponseStatus(HttpStatus.CREATED) + public Result createQueue(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "queue") String queue, + @RequestParam(value = "queueName") String queueName) { + logger.info("login user {}, create queue, queue: {}, queueName: {}", + loginUser.getUserName(), queue, queueName); + try { + Map result = queueService.createQueue(loginUser,queue,queueName); + return returnDataList(result); + + }catch (Exception e){ + logger.error(CREATE_QUEUE_ERROR.getMsg(),e); + return error(CREATE_QUEUE_ERROR.getCode(), CREATE_QUEUE_ERROR.getMsg()); + } + } + + /** + * update queue + * + * @param loginUser + * @param queue + * @param queueName + * @return + */ + @PostMapping(value = "/update") + @ResponseStatus(HttpStatus.CREATED) + public Result updateQueue(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "id") int id, + @RequestParam(value = "queue") String queue, + @RequestParam(value = "queueName") String queueName) { + logger.info("login user {}, update queue, id: {}, queue: {}, queueName: {}", + loginUser.getUserName(), id,queue, queueName); + try { + Map result = queueService.updateQueue(loginUser,id,queue,queueName); + return returnDataList(result); + + }catch (Exception e){ + logger.error(UPDATE_QUEUE_ERROR.getMsg(),e); + return error(UPDATE_QUEUE_ERROR.getCode(), UPDATE_QUEUE_ERROR.getMsg()); + } + } + } diff --git a/escheduler-api/src/main/java/cn/escheduler/api/service/QueueService.java b/escheduler-api/src/main/java/cn/escheduler/api/service/QueueService.java index 4f84cb7d67..222f2ee90e 100644 --- a/escheduler-api/src/main/java/cn/escheduler/api/service/QueueService.java +++ b/escheduler-api/src/main/java/cn/escheduler/api/service/QueueService.java @@ -18,12 +18,15 @@ package cn.escheduler.api.service; import cn.escheduler.api.enums.Status; import cn.escheduler.api.utils.Constants; +import cn.escheduler.api.utils.PageInfo; import cn.escheduler.dao.mapper.QueueMapper; import cn.escheduler.dao.model.Queue; import cn.escheduler.dao.model.User; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -57,4 +60,104 @@ public class QueueService extends BaseService{ return result; } + /** + * query queue list paging + * @param loginUser + * @param searchVal + * @param pageNo + * @param pageSize + * @return + */ + public Map queryList(User loginUser,String searchVal, Integer pageNo, Integer pageSize) { + Map result = new HashMap<>(5); + if (checkAdmin(loginUser, result)) { + return result; + } + + Integer count = queueMapper.countQueuePaging(searchVal); + + PageInfo pageInfo = new PageInfo<>(pageNo,pageSize); + + List queueList = queueMapper.queryQueuePaging(searchVal,pageInfo.getStart(),pageSize); + + pageInfo.setTotalCount(count); + pageInfo.setLists(queueList); + result.put(Constants.DATA_LIST, queueList); + putMsg(result,Status.SUCCESS); + + return result; + } + + public Map createQueue(User loginUser,String queue, String queueName) { + Map result = new HashMap<>(5); + if (checkAdmin(loginUser, result)) { + return result; + } + + if (checkQueueExists(queue)){ + putMsg(result, Status.QUEUE_EXIST, queue); + return result; + } + + Queue queueObj = new Queue(); + Date now = new Date(); + + queueObj.setQueue(queue); + queueObj.setQueueName(queueName); + queueObj.setCreateTime(now); + queueObj.setUpdateTime(now); + + queueMapper.insert(queueObj); + putMsg(result, Status.SUCCESS); + + return result; + } + + public Map updateQueue(User loginUser,int id,String queue, String queueName) { + Map result = new HashMap<>(5); + if (checkAdmin(loginUser, result)) { + return result; + } + + + + Queue queueObj = queueMapper.queryById(id); + if (queueObj == null) { + putMsg(result,Status.QUEUE_NOT_EXIST,id); + return result; + } + + //update queue + + if (StringUtils.isNotEmpty(queue)) { + if (!queue.equals(queueObj.getQueue()) && checkQueueExists(queue)) { + putMsg(result, Status.QUEUE_EXIST, queue); + return result; + } + queueObj.setQueue(queue); + } + if (StringUtils.isNotEmpty(queueName)) { + queueObj.setQueueName(queueName); + } + Date now = new Date(); + + queueObj.setUpdateTime(now); + + queueMapper.update(queueObj); + putMsg(result, Status.SUCCESS); + + return result; + } + + + /** + * check queue exists + * + * @param queue + * @return + */ + private boolean checkQueueExists(String queue) { + return queueMapper.queryByQueue(queue) == null ? false : true; + } + } diff --git a/escheduler-api/src/test/java/cn/escheduler/api/controller/QueueControllerTest.java b/escheduler-api/src/test/java/cn/escheduler/api/controller/QueueControllerTest.java index 3943c6ce9a..f1ced425fe 100644 --- a/escheduler-api/src/test/java/cn/escheduler/api/controller/QueueControllerTest.java +++ b/escheduler-api/src/test/java/cn/escheduler/api/controller/QueueControllerTest.java @@ -32,9 +32,12 @@ import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.context.WebApplicationContext; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -65,4 +68,61 @@ public class QueueControllerTest { Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue()); logger.info(mvcResult.getResponse().getContentAsString()); } + + @Test + public void queryPagingList() throws Exception { + + MultiValueMap paramsMap = new LinkedMultiValueMap<>(); + //paramsMap.add("processInstanceId","1380"); + paramsMap.add("searchVal",""); + paramsMap.add("pageNo","1"); + paramsMap.add("pageSize","20"); + + MvcResult mvcResult = mockMvc.perform(get("/queue/list-paging") + .header("sessionId", "d4541e0d-0349-4f05-9c68-300176cd3c91") + .params(paramsMap)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andReturn(); + Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); + Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue()); + logger.info(mvcResult.getResponse().getContentAsString()); + } + + @Test + public void createQueue() throws Exception { + + MultiValueMap paramsMap = new LinkedMultiValueMap<>(); + paramsMap.add("queue","ait1111"); + paramsMap.add("queueName","aitName"); + + MvcResult mvcResult = mockMvc.perform(post("/queue/create") + .header("sessionId", "d4541e0d-0349-4f05-9c68-300176cd3c91") + .params(paramsMap)) + .andExpect(status().isCreated()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andReturn(); + Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); + //Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue()); + logger.info(mvcResult.getResponse().getContentAsString()); + } + + @Test + public void updateQueue() throws Exception { + + MultiValueMap paramsMap = new LinkedMultiValueMap<>(); + paramsMap.add("id","2"); + paramsMap.add("queue","ait1"); + paramsMap.add("queueName","aitName"); + + MvcResult mvcResult = mockMvc.perform(post("/queue/update") + .header("sessionId", "d4541e0d-0349-4f05-9c68-300176cd3c91") + .params(paramsMap)) + .andExpect(status().isCreated()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andReturn(); + Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); + //Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue()); + logger.info(mvcResult.getResponse().getContentAsString()); + } } \ No newline at end of file