* @param original original UserTO
* @param incremental perform incremental diff (without removing existing info)
* @return UserMod containing differences
*/
public static UserMod diff(final UserTO updated, final UserTO original, boolean incremental) {
UserMod result = new UserMod();
diff(updated, original, result, incremental);
// 1. password
if (original.getPassword() != null && !original.getPassword().equals(updated.getPassword())) {
result.setPassword(updated.getPassword());
}
// 2. username
if (original.getUsername() != null && !original.getUsername().equals(updated.getUsername())) {
result.setUsername(updated.getUsername());
}
// 3. memberships
Map<Long, MembershipTO> updatedMembs = updated.getMembershipMap();
Map<Long, MembershipTO> originalMembs = original.getMembershipMap();
for (Map.Entry<Long, MembershipTO> entry : updatedMembs.entrySet()) {
MembershipMod membMod = new MembershipMod();
membMod.setRole(entry.getValue().getRoleId());
if (originalMembs.containsKey(entry.getKey())) {
diff(entry.getValue(), originalMembs.get(entry.getKey()), membMod, false);
} else {
for (AttributeTO attr : entry.getValue().getAttributes()) {
AttributeMod attrMod = new AttributeMod();
attrMod.setSchema(attr.getSchema());
attrMod.setValuesToBeAdded(attr.getValues());
if (!attrMod.isEmpty()) {
membMod.addAttributeToBeUpdated(attrMod);
membMod.addAttributeToBeRemoved(attrMod.getSchema());
}
}
for (AttributeTO attr : entry.getValue().getDerivedAttributes()) {
membMod.addDerivedAttributeToBeAdded(attr.getSchema());
}
for (AttributeTO attr : entry.getValue().getVirtualAttributes()) {
AttributeMod attrMod = new AttributeMod();
attrMod.setSchema(attr.getSchema());
attrMod.setValuesToBeAdded(attr.getValues());
if (!attrMod.isEmpty()) {
membMod.addVirtualAttributeToBeUpdated(attrMod);
membMod.addAttributeToBeRemoved(attrMod.getSchema());
}
}
membMod.setResourcesToBeAdded(entry.getValue().getResources());
}
if (!membMod.isEmpty()) {
result.addMembershipToBeAdded(membMod);
}
}
if (!incremental) {
Set<Long> originalRoles = new HashSet<Long>(originalMembs.keySet());
originalRoles.removeAll(updatedMembs.keySet());
for (Long roleId : originalRoles) {
result.addMembershipToBeRemoved(originalMembs.get(roleId).getId());
}
}
return result;
}