use of org.jbei.ice.lib.account.AccountTransfer in project ice by JBEI.
the class Groups method setGroupMembers.
protected void setGroupMembers(Group group, List<AccountTransfer> members, List<RemoteUser> remoteUsers) {
//
// deal with local members
//
Set<String> memberHash = new HashSet<>();
for (Account member : group.getMembers()) {
memberHash.add(member.getEmail());
}
for (AccountTransfer accountTransfer : members) {
String email = accountTransfer.getEmail();
// do not add if found in set
if (memberHash.remove(email))
continue;
// not found so add
Account memberToAdd = accountDAO.getByEmail(email);
if (memberToAdd == null) {
Logger.error("Could not find account " + email + " to add to group");
continue;
}
group.getMembers().add(memberToAdd);
memberToAdd.getGroups().add(group);
accountDAO.update(memberToAdd);
}
// all emails remaining should be removed
for (String memberEmail : memberHash) {
Account memberAccount = accountDAO.getByEmail(memberEmail);
if (memberAccount == null)
continue;
memberAccount.getGroups().remove(group);
group.getMembers().remove(memberAccount);
accountDAO.update(memberAccount);
}
//
// deal with remote users
//
Map<String, RemoteClientModel> remoteMembers = new HashMap<>();
// get remote members
List<RemoteClientModel> clients = remoteClientModelDAO.getClientsForGroup(group);
for (RemoteClientModel clientModel : clients) {
// technically the same email can be at different instances so adding partner id
String identifier = (clientModel.getEmail() + clientModel.getRemotePartner().getUrl()).toLowerCase();
remoteMembers.put(identifier, clientModel);
}
for (RemoteUser remoteUser : remoteUsers) {
String identifier = (remoteUser.getUser().getEmail() + remoteUser.getPartner().getUrl()).toLowerCase();
// do not add if found in set (already exists)
if (remoteMembers.remove(identifier) != null)
continue;
// add // todo : duplicates line 147. move to separate method
RemotePartner remotePartner = remotePartnerDAO.getByUrl(remoteUser.getPartner().getUrl());
if (remotePartner == null)
continue;
AccountTransfer accountTransfer = remoteUser.getUser();
if (accountTransfer == null || StringUtils.isEmpty(accountTransfer.getEmail()))
continue;
String email = accountTransfer.getEmail();
RemoteClientModel remoteClientModel = remoteClientModelDAO.getModel(email, remotePartner);
if (remoteClientModel == null) {
remoteClientModel = new RemoteClientModel();
remoteClientModel.setEmail(email);
remoteClientModel.setRemotePartner(remotePartner);
remoteClientModel = remoteClientModelDAO.create(remoteClientModel);
}
remoteClientModel.getGroups().add(group);
remoteClientModelDAO.update(remoteClientModel);
}
// remove all remaining
if (remoteMembers.isEmpty())
return;
for (String identifier : remoteMembers.keySet()) {
RemoteClientModel remoteClientModel = remoteMembers.get(identifier);
remoteClientModel.getGroups().remove(group);
remoteClientModelDAO.update(remoteClientModel);
}
}
Aggregations