remove access from particular role using Iterator.remove method

Viewed 31

i tried to remove access from particular role using Iterator.remove(). but still not working. I have no idea why. Hopefully i can grab some ideas on how to solve this from here. This is what i tried to do:

public ResponseEntity<Object> delAccInRole(Role role, String roleName) {
        if (roleRepository.findByRoleName(roleName).isPresent()) {
            Role r = roleRepository.findByRoleName(roleName).get();

            for (int i = 0; i < r.getAccess().size(); i++) {
                Access getAllAcc = r.getAccess().get(i);

                Optional<Access> getAcc = accessRepository.findByAccName(role.getAccess().get(i).getAccName());

                if(getAcc.isPresent()){

                    Access acc= getAcc.get();

                    List<Access> ls = Arrays.asList(getAllAcc);

                    Iterator<Access> it = ls.iterator();

                    while(it.hasNext()){
                        if(acc.equals(it.next())){
                            it.remove();
                            break;
                        }else{
                            return ResponseEntity.unprocessableEntity().body("failed to remove access from role");

                        }
                    }

                }else{
                    return ResponseEntity.unprocessableEntity().body("access name not found");

                }
            }

        } else {
            return ResponseEntity.unprocessableEntity().body("role name not found");

        }
        return ResponseEntity.ok("access is successfully delete");
    }
0 Answers
Related