-1

I have the following code :

userService.getUserWithAuthorityByLogin(principal.getName())
                .map(user->userRepository.findAllByClient(user.getClient().getId()));

The method findAllByClient return a List<User> type. the map method returns a Optional<Object> In my case it could (optional) return an Optional<List<User>>. The idea I want to code is to use a stream on this optional list. I'd like to chain my calls. Code in java 8 style is good looking and fewer lines of code.

I need to process this List<User> if it really exist (present). How can I chain my calls with java8 api : I wish to mix Optional and Stream.

Thanks.

EDIT :

I continue a bit further :

userService.getUserWithAuthorityByLogin(principal.getName())
.map(user->userRepository.findAllByClient(user.getClient().getId()).stream().forEach(user-> 
{
    managedUserDTOs.add(new ManagedUserDTO(user));

}));

You've got the whole process. But this doesn't compile on instruction

managedUserDTOs.add(new ManagedUserDTO(user))

I want to fill the list and return it at final.

EDIT2 :

The whole wrapping method :

 /**
     * GET  /users -> get all users.
     */
    @RequestMapping(value = "/user-liste",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    @Transactional(readOnly = true)
    @Secured({AuthoritiesConstants.TC_ADMIN})
    public ResponseEntity<List<ManagedUserDTO>> getUserListe(Principal principal)
        throws URISyntaxException {

        return new ResponseEntity<>(managedUserDTOs, HttpStatus.OK);
    }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
François F.
  • 229
  • 2
  • 17
  • Not sure I got the idea of your question, but: `userService.getUserWithAuthorityByLogin(principal.getName()) .flatMap(user->userRepository.findAllByClient(user.getClient().getId()).stream()).foreach();` – Aliaxander Mar 24 '16 at 15:30
  • ok I edit the original post – François F. Mar 24 '16 at 16:09
  • @FrancoisF I believe you are looking for, `userService.getUserWithAuthorityByLogin(principal.getName()) .map(user->userRepository.findAllByClient(user.getClient().getId()).stream()‌​).map(user->ManagedUserDTO::new).collect(Collectors.toList())` – Madhusudana Reddy Sunnapu Mar 24 '16 at 16:09
  • Yes. But it doesn't compile, eclipse underline all in red. – François F. Mar 24 '16 at 16:15
  • You have to show more of your code. Also, are you sure that your Eclipse is Java8-compatible? And that Java8 is enabled in the project settings? – tobias_k Mar 24 '16 at 16:17
  • I did an EDIT2. My eclipse is in java 8. I have a bunch of lines of code in java8 already – François F. Mar 24 '16 at 16:20
  • Have you read the error? Variables used in lambdas must be [effectively final or final](http://stackoverflow.com/questions/20938095/difference-between-final-and-effectively-final). Are they? – bcsb1001 Mar 24 '16 at 16:22
  • @FrançoisF. Can you post the error. Also I am assuming `getUserWithAuthorityByLogin` returns `Optional`. With that assumption, can you try `userService.getUserWithAuthorityByLogin(principal.getName()).map(user->userRepository.findAllByClient(user.getClient().getId()).stream().map(ManagedUserDTO::new).collect(Collectors.toList())).get();` – Madhusudana Reddy Sunnapu Mar 24 '16 at 17:37
  • what does `getUserWithAuthorityByLogin` return? – Jos Mar 24 '16 at 18:25
  • Yes it returns Optional . 2 errors in your code : Type mismatch: cannot convert from Optional to and userRepo‌​sitory cannot be resolved. Where as I inject like this : @Inject private UserRepository userRepository; – François F. Mar 25 '16 at 08:39
  • 1 error left : Optional to – François F. Mar 25 '16 at 08:54
  • @FrançoisF. That should work. Looks like some encoding issue. I see some ? characters when I copy/paste it. Can you try the one in the answer. If you still see the same error, copy/paste it in notepad and save it and trying it would help. – Madhusudana Reddy Sunnapu Mar 25 '16 at 09:20

2 Answers2

0

Here the answer but NOT IN JAVA8. I would like the Java8 answer :

/**
     * GET  /users -> get all users.
     */
    @RequestMapping(value = "/user-liste",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    @Transactional(readOnly = true)
    @Secured({AuthoritiesConstants.TC_ADMIN})
    public ResponseEntity<List<ManagedUserDTO>> getUserListe(Principal principal)
        throws URISyntaxException {


        Optional<User> ouser = userService.getUserWithAuthorityByLogin(principal.getName());
        List<ManagedUserDTO> managedUserDTOs = new ArrayList<>();
        if (ouser.isPresent())
        {
            List<User> userListe = userRepository.findAllByClient(ouser.get().getClient().getId());
            for (User user : userListe)
            {
                managedUserDTOs.add(new ManagedUserDTO(user));
            }
        }

         return new ResponseEntity<>(managedUserDTOs, HttpStatus.OK);

    }
François F.
  • 229
  • 2
  • 17
0

Can you try:

userService.getUserWithAuthorityByLogin(principal.getName()).map(user->userRepository.findAllByClient(user.getClient().getId()).stream().map(ManagedUserDTO::new).collect(Collectors.toList())).get();

Same as the one in the comments section, but I think that has some encoding issues. I see ? when I copy/pasted it. This should help.