use of cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException in project perun by CESNET.
the class VosManagerBlImpl method createMemberCandidates.
/**
* Creates MemberCandidates for given RichUsers, vo, group and candidates. If the given group is not null
* then to all members who are in this group is assigned the sourceGroupId of the given group.
* The given group can be null.
*
* @param sess session
* @param users users
* @param group group
* @param candidates candidates
* @param attrNames names of attributes that will be returned
* @return list of MemberCandidates for given RichUsers, group and candidates
* @throws InternalErrorException internal error
*/
public List<MemberCandidate> createMemberCandidates(PerunSession sess, List<RichUser> users, Vo vo, Group group, List<Candidate> candidates, List<String> attrNames) {
List<MemberCandidate> memberCandidates = new ArrayList<>();
Set<Integer> allUsersIds = new HashSet<>();
int userId;
// try to find matching RichUser for candidates
for (Candidate candidate : candidates) {
MemberCandidate mc = new MemberCandidate();
try {
User user = getPerunBl().getUsersManagerBl().getUserByUserExtSources(sess, candidate.getUserExtSources());
userId = user.getId();
// check if user already exists in the list
if (!allUsersIds.contains(userId)) {
RichUser richUser = getPerunBl().getUsersManagerBl().convertUserToRichUserWithAttributesByNames(sess, user, attrNames);
mc.setRichUser(richUser);
memberCandidates.add(mc);
}
allUsersIds.add(userId);
} catch (UserNotExistsException ignored) {
// no matching user was found
mc.setCandidate(candidate);
memberCandidates.add(mc);
}
}
List<RichUser> foundRichUsers = memberCandidates.stream().map(MemberCandidate::getRichUser).collect(Collectors.toList());
// create MemberCandidates for RichUsers without candidate
for (RichUser richUser : users) {
if (!foundRichUsers.contains(richUser)) {
MemberCandidate mc = new MemberCandidate();
mc.setRichUser(richUser);
memberCandidates.add(mc);
}
}
// try to find member for MemberCandidates with not null RichUser
for (MemberCandidate memberCandidate : memberCandidates) {
if (memberCandidate.getRichUser() != null) {
Member member = null;
try {
member = getPerunBl().getMembersManagerBl().getMemberByUser(sess, vo, memberCandidate.getRichUser());
if (group != null) {
member = getPerunBl().getGroupsManagerBl().getGroupMemberById(sess, group, member.getId());
}
} catch (MemberNotExistsException ignored) {
// no matching VO member was found
} catch (NotGroupMemberException e) {
// not matching Group member was found
}
// put null or matching member
memberCandidate.setMember(member);
}
}
return memberCandidates;
}
Aggregations