@RequestMapping(value = "/adminrules", method = RequestMethod.POST) public ResponseEntity<Long> insert(@RequestBody JaxbAdminRule rule) { long priority = rule.getPriority() == null ? 0 : rule.getPriority(); if (adminService.getRuleByPriority(priority) != null) { adminService.shift(priority, 1); } return new ResponseEntity<>(adminService.insert(rule.toRule()), HttpStatus.CREATED); }
/** * Move the provided rules to the target priority. Rules will be sorted by their priority, first * rule will be updated with a priority equal to the target priority and the next ones will get * an incremented priority value. */ @RequestMapping( value = "/adminrules/move", method = RequestMethod.GET, produces = {"application/xml", "application/json"} ) public @ResponseBody ResponseEntity<JaxbAdminRuleList> move( @RequestParam(value = "targetPriority", required = true) int targetPriority, @RequestParam(value = "rulesIds", required = true) String rulesIds) { // let's find the rules that need to be moved List<AdminRule> rules = findRules(rulesIds); if (rules.isEmpty()) { return ResponseEntity.ok(null); } // shift priorities of rules with a priority equal or lower than the target priority adminService.shift(targetPriority, rules.size()); // update moved rules priority long priority = targetPriority; for (AdminRule rule : rules) { rule.setPriority(priority); adminService.update(rule); priority++; } // return moved rules with their priority updated return ResponseEntity.ok(new JaxbAdminRuleList(rules)); }
@RequestMapping(value = "/adminrules/id/{id}", method = RequestMethod.POST) public @ResponseStatus(HttpStatus.OK) void update( @PathVariable("id") Long id, @RequestBody JaxbAdminRule rule) { if (rule.getPriority() != null) { ShortAdminRule priorityRule = adminService.getRuleByPriority(rule.getPriority()); if (priorityRule != null && priorityRule.getId().longValue() != id) { adminService.shift(rule.getPriority(), 1); } } adminService.update(rule.toRule(adminService.get(id))); }
protected void shiftIfNecessary(long priority, ShortAdminRule keep) { boolean necessary = false; for (ShortAdminRule rule : rules) { if (rule.getPriority() == priority) { necessary = true; continue; } } if (necessary) { for (ShortAdminRule rule : rules) { if (rule.getPriority() >= priority && rule != keep) { rule.setPriority(rule.getPriority() + 1); } } adminService().shift(priority, 1); } }