Source:
private void addMissingGroupsWhileSynchronization(PerunSession sess, Group baseGroup, List<CandidateGroup> candidateGroupsToAdd, List<String> skippedGroups) throws InternalErrorException { for (CandidateGroup candidateGroup: candidateGroupsToAdd) { Group destinationGroup = specifyDestinationGroupForAddedGroup(sess, baseGroup, candidateGroup); try { Group createdGroup = createGroup(sess, destinationGroup, candidateGroup.asGroup()); log.info("Group structure synchronization under base group {}: New Group id created during synchronization.", baseGroup, createdGroup.getId()); } catch (GroupExistsException e) { log.warn("Group was added to group structure before adding process. Skip this group.", candidateGroup, baseGroup); skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because it was added to group structure before adding process: Exception: " + e.getName() + " => " + e.getMessage() + "]"); continue; } catch (GroupRelationNotAllowed e) { log.warn("Can't create group from candidate group due to group relation not allowed exception {}.", candidateGroup, e); skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because group relation was not allowed: Exception: " + e.getName() + " => " + e.getMessage() + "]"); continue; } catch (GroupRelationAlreadyExists e) { log.warn("Can't create group from candidate group due to group relation already exists exception {}.", candidateGroup, e); skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because group relation already exists: Exception: " + e.getName() + " => " + e.getMessage() + "]"); continue; } } }

Target:
private void addMissingGroupsWhileSynchronization(PerunSession sess, Group baseGroup, List<CandidateGroup> candidateGroupsToAdd, List<String> skippedGroups) throws InternalErrorException { Map<CandidateGroup, Group> groupsToUpdate = new HashMap<>(); for (CandidateGroup candidateGroup: candidateGroupsToAdd) { Group destinationGroup = specifyDestinationGroupForAddedGroup(sess, baseGroup, candidateGroup); try { Group createdGroup = createGroup(sess, destinationGroup, candidateGroup.asGroup()); groupsToUpdate.put(candidateGroup, createdGroup); log.info("Group structure synchronization under base group {}: New Group id created during synchronization.", baseGroup, createdGroup.getId()); } catch (GroupExistsException e) { log.warn("Group was added to group structure before adding process. Skip this group.", candidateGroup, baseGroup); skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because it was added to group structure before adding process: Exception: " + e.getName() + " => " + e.getMessage() + "]"); } catch (GroupRelationNotAllowed e) { log.warn("Can't create group from candidate group due to group relation not allowed exception {}.", candidateGroup, e); skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because group relation was not allowed: Exception: " + e.getName() + " => " + e.getMessage() + "]"); } catch (GroupRelationAlreadyExists e) { log.warn("Can't create group from candidate group due to group relation already exists exception {}.", candidateGroup, e); skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because group relation already exists: Exception: " + e.getName() + " => " + e.getMessage() + "]"); } } updateExistingGroupsWhileSynchronization(sess, baseGroup, groupsToUpdate, skippedGroups); }