1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public void associateDissociateLodgerAndContact(Long lodgerId, List<Long> contactIdToDissociates, List<Long> contactIdToAssociates) {
Lodger lodger = lodgerRepository.findOne(lodgerId);
List<Contact> contactList; // = contactRepository.findAll(contactIdList);
if (lodger.getContactList() == null) {
lodger.setContactList(new ArrayList<>());
contactList = lodger.getContactList();
} else {
contactList = lodger.getContactList();
}
if (contactIdToDissociates != null) {
for (Iterator<Contact> iterator = contactList.iterator(); iterator.hasNext();) {
Contact contact = iterator.next();
for (Long contactId : contactIdToDissociates) {
if (contact.getContactId().longValue() == contactId.longValue()) {
iterator.remove();
break;
}
}
}
}
List<Contact> contactToAssign = contactRepository.findAll(contactIdToAssociates);
lodger.getContactList().addAll(contactToAssign);
lodgerRepository.save(lodger);
} |
Partager