@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))); }
@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); }
@RequestMapping( value = "/adminrules/count", method = RequestMethod.GET, produces = {"application/xml", "application/json"} ) public @ResponseBody JaxbAdminRuleList count( @RequestParam(value = "userName", required = false) String userName, @RequestParam(value = "userAny", required = false) Boolean userDefault, @RequestParam(value = "roleName", required = false) String roleName, @RequestParam(value = "roleAny", required = false) Boolean roleDefault, @RequestParam(value = "workspace", required = false) String workspace, @RequestParam(value = "workspaceAny", required = false) Boolean workspaceDefault) { RuleFilter filter = buildFilter( userName, userDefault, roleName, roleDefault, workspace, workspaceDefault); return new JaxbAdminRuleList(adminService.count(filter)); }
/** * 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)); }
public void save(ShortAdminRule rule) { if (rule.getId() == null) { shiftIfNecessary(rule.getPriority(), rule); int i = 0; while (i < rules.size() && rules.get(i).getPriority() < rule.getPriority()) { i++; } rules.add(i, rule); AdminRule bigRule = new AdminRule(); syncRule(rule, bigRule); rule.setId(adminService().insert(bigRule)); } else { AdminRule bigRule = adminService().get(rule.getId()); if (bigRule.getPriority() != rule.getPriority()) { shiftIfNecessary(rule.getPriority(), rule); } syncRule(rule, bigRule); adminService().update(bigRule); } }
@RequestMapping( value = "/adminrules/id/{id}", method = RequestMethod.GET, produces = {"application/xml", "application/json"} ) public @ResponseBody JaxbAdminRule get(@PathVariable("id") Long id) { return new JaxbAdminRule(adminService.get(id)); }
@Override @Transactional(propagation = Propagation.REQUIRED, value = "geofenceTransactionManager") public Response insert(RESTInputAdminRule inputAdminRule) throws NotFoundRestEx, BadRequestRestEx, InternalErrorRestEx { if (inputAdminRule.getPosition() == null || inputAdminRule.getPosition().getPosition() == null) { throw new BadRequestRestEx("Bad position: " + inputAdminRule.getPosition()); } if (inputAdminRule.getGrant() == null) { throw new BadRequestRestEx("Missing grant type"); } AdminRule rule = fromInput(inputAdminRule); InsertPosition position = inputAdminRule.getPosition().getPosition() == RulePosition.fixedPriority ? InsertPosition.FIXED : inputAdminRule.getPosition().getPosition() == RulePosition.offsetFromBottom ? InsertPosition.FROM_END : inputAdminRule.getPosition().getPosition() == RulePosition.offsetFromTop ? InsertPosition.FROM_START : null; // ok: insert it try { Long id = adminRuleAdminService.insert(rule, position); return Response.status(Status.CREATED).tag(id.toString()).entity(id).build(); } catch (BadRequestServiceEx ex) { LOGGER.error(ex.getMessage()); throw new BadRequestRestEx(ex.getMessage()); } catch (Exception ex) { LOGGER.error(ex.getMessage(), ex); throw new InternalErrorRestEx(ex.getMessage()); } }
@RequestMapping(value = "/adminrules/id/{id}", method = RequestMethod.DELETE) public @ResponseStatus(HttpStatus.OK) void delete(@PathVariable("id") Long id) { adminService.delete(id); }
userName, userDefault, roleName, roleDefault, workspace, workspaceDefault); return new JaxbAdminRuleList(adminService.getListFull(filter, page, entries));
public GeofenceAdminRulesModel() { rules = adminService().getAll(); setSort("priority", SortOrder.ASCENDING); }
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); } }
@Override public RESTOutputAdminRule get(Long id) throws BadRequestRestEx, NotFoundRestEx, InternalErrorRestEx { try { AdminRule ret = adminRuleAdminService.get(id); return toOutput(ret); } catch (NotFoundServiceEx ex) { LOGGER.warn("AdminRule not found: " + id); throw new NotFoundRestEx("AdminRule not found: " + id); } catch (Exception ex) { LOGGER.error(ex); throw new InternalErrorRestEx(ex.getMessage()); } }
adminService.insert( new AdminRule( 5, null, AdminGrantType.ADMIN)); adminService.insert( new AdminRule( 2, null, AdminGrantType.ADMIN)); adminService.insert( new AdminRule( 1, null, AdminGrantType.ADMIN)); adminService.insert( new AdminRule( 4, null, AdminGrantType.ADMIN)); adminService.insert( new AdminRule( 3, null,
public void remove(Collection<ShortAdminRule> selected) { rules.removeAll(selected); for (ShortAdminRule rule : selected) { adminService().delete(rule.getId()); } }
List<AdminRule> listFull = adminRuleAdminService.getListFull(filter, page, entries); return toOutput(listFull); } catch (Exception ex) {
/** Helper method that will parse and retrieve the provided rules sorted by their priority. */ private List<AdminRule> findRules(String rulesIds) { return Arrays.stream(rulesIds.split(",")) .map( ruleId -> { try { // parsing the rule id return Long.parseLong(ruleId); } catch (NumberFormatException exception) { // error parsing the rule id throw new InvalidRulesIds(); } }) .map( ruleId -> { // search the rule by id return adminService.get(ruleId); }) .filter(rule -> rule != null) .sorted((ruleA, ruleB) -> Long.compare(ruleA.getPriority(), ruleB.getPriority())) .collect(Collectors.toList()); }
@Override public Response delete(Long id) throws NotFoundRestEx, InternalErrorRestEx { try { if (!adminRuleAdminService.delete(id)) { LOGGER.warn("Rule not found: " + id); throw new NotFoundRestEx("Rule not found: " + id); } return Response.status(Status.OK).entity("OK\n").build(); } catch (GeoFenceRestEx ex) { // already handled throw ex; } catch (NotFoundServiceEx ex) { LOGGER.warn("Group not found: " + id); throw new NotFoundRestEx("Group not found: " + id); } catch (Exception ex) { LOGGER.error(ex.getMessage(), ex); throw new InternalErrorRestEx(ex.getMessage()); } }
@Override public long count( String userName, Boolean userDefault, String roleName, Boolean groupDefault, Long instanceId, String instanceName, Boolean instanceDefault, String workspace, Boolean workspaceDefault) throws BadRequestRestEx, InternalErrorRestEx { RuleFilter filter = buildFilter( userName, userDefault, roleName, groupDefault, instanceId, instanceName, instanceDefault, workspace, workspaceDefault); try { return adminRuleAdminService.count(filter); } catch (Exception ex) { LOGGER.error(ex); throw new InternalErrorRestEx(ex.getMessage()); } }
AdminRule realRule = adminService.get(id); realRule = adminService.get(id); long id2 = controller.insert(rule2).getBody(); realRule = adminService.get(id); assertEquals(6L, realRule.getPriority()); controller.update(id2, rule2Mods); realRule = adminService.get(id2); assertEquals(3L, realRule.getPriority()); controller.update(id2, rule2Mods); realRule = adminService.get(id2); assertEquals(6L, realRule.getPriority()); realRule = adminService.get(id); assertEquals(7L, realRule.getPriority()); boolean notfound = false; try { adminService.get(id); } catch (NotFoundServiceEx e) { notfound = true;