Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.62 KB | None | 0 0
  1. private List<ContentProviderJsonResponse> revokeActPermissions(String userEmail, Map<Long, List<Long>> accountMap) {
  2.         return accountMap.entrySet().stream().
  3.             filter(entry -> !entry.getValue().isEmpty()).
  4.             map(entry -> {
  5.                 long contentProviderId = entry.getKey();
  6.                 ContentProvider contentProvider = contentProviderDAO.getContentProvider(contentProviderId);
  7.                 return userApi.revokeActPermissions(contentProvider, userEmail);
  8.             }).collect(toList());
  9.     }
  10.  
  11.     private List<String> addUserToAccounts(ContentProviderUsersCreationData usersCreationData) throws Exception {
  12.         if (usersCreationData.getCreateUserAccounts() == null) {
  13.             return Collections.emptyList();
  14.         }
  15.  
  16.         try {
  17.             BrooklynUser user = userManagementService.getUserByEmail(usersCreationData.getEmail());
  18.             MasterAccount userMa = masterAccountService.getMasterAccount(usersCreationData.getMasterAccountId());
  19.  
  20.             Map<Long, List<Long>> createUserAccounts = usersCreationData.getCreateUserAccounts();
  21.  
  22.             Predicate<Map.Entry<Long, List<Long>>> filterEmptyCP = entry -> {
  23.                 return !CollectionUtils.isEmpty(entry.getValue());
  24.             };
  25.  
  26.             Function<Map.Entry<Long, List<Long>>, SimpleEntry<ContentProvider, List<Long>>> convertContentProvider =
  27.                     entry -> {
  28.                         return new SimpleEntry<ContentProvider, List<Long>>(
  29.                                 contentProviderDAO.getContentProvider(entry.getKey()), entry.getValue());
  30.                     };
  31.  
  32.             Function<SimpleEntry<ContentProvider, List<Long>>,
  33.                 CompletableFuture<SimpleEntry<ContentProvider, List<Long>>>> convertToSupplier =
  34.                     entry-> {
  35.                         return SubjectAwareCompletableFuture.supplyAsync(
  36.                             () -> { // clear existing user accounts
  37.                                 ContentProvider provider = entry.getKey();
  38.                                 String userEmail = user.getUserEmail();
  39.                                 long masterAccountId = usersCreationData.getMasterAccountId();
  40.  
  41.                                 List<Long> existsList = userApi.getUserCPAccounts(provider, userEmail, Boolean.TRUE);
  42.                                 List<Long> linkUserAccList = Lists.newArrayList(entry.getValue());
  43.  
  44.                                 entry.getValue().removeAll(existsList);
  45.                                 linkUserAccList.retainAll(existsList);
  46.  
  47.                                 userApi.enableUserInCpAccounts(provider, userEmail, linkUserAccList);
  48.  
  49.                                 linkUserAccList.forEach(accountId -> {
  50.                                     linkCmpUser(provider.getId(), accountId, masterAccountId, userEmail);
  51.                                 });
  52.  
  53.                                 return entry;
  54.                             });
  55.                     };
  56.  
  57.             List<CompletableFuture<SimpleEntry<ContentProvider, List<Long>>>> clearExistingAccounts =
  58.                     createUserAccounts.entrySet().stream().
  59.                         filter(filterEmptyCP).map(convertContentProvider).map(convertToSupplier).collect(toList());
  60.  
  61.             String userEmail = usersCreationData.getEmail();
  62.             String password = StringUtils.isEmpty(usersCreationData.getPassword()) ?
  63.                 passwordService.generatePassword(user.getDefaultMasterAccountId()) :
  64.                 usersCreationData.getPassword();
  65.             user.setPassword(password);
  66.  
  67.             List<CompletableFuture<List<String>>> futures = clearExistingAccounts.stream()
  68.                     .map(future -> future.<List<String>>thenApply(entry -> {
  69.                         if (entry.getValue().isEmpty())
  70.                             return Collections.emptyList();
  71.                         long cpId = entry.getKey().getId();
  72.                         List<CompletableFuture<String>> errors = entry.getValue().stream()
  73.                                 //.map(acc -> getCPUserObject(user, entry.getKey().getId(), acc, baseUserName, licenseId, adminOrg))
  74.                                 .map(accountId -> usersCreationData.getContentProviderUser(cpId, accountId, user))
  75.                                 .map(cpUser -> performCreateUserRequest(userEmail, cpUser, entry.getKey(), userMa))
  76.                                 .collect(toList());
  77.                         // wait for all responses to single cp
  78.                         SubjectAwareCompletableFuture.allOf(errors.toArray(new CompletableFuture[errors.size()])).join();
  79.                         return errors.stream().map(f -> f.join()).collect(toList());
  80.                     }))
  81.                     .collect(toList());
  82.             // wait for top-level requests to content providers
  83.             SubjectAwareCompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).join();
  84.             return futures.stream()
  85.                     .<String>flatMap(future -> future.join().stream())
  86.                     .filter(s -> !StringUtils.isEmpty(s)) // remove nulls and empty messages
  87.                     .distinct() // remove duplicates, when error occured in multiple accounts of 1 cp
  88.                     .collect(toList());
  89.  
  90.         } catch (Exception ex) {
  91.             return Arrays.asList(ex.getMessage());
  92.         }
  93.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement