use of org.kuali.kfs.core.api.delegation.DelegationType in project cu-kfs by CU-CommunityApps.
the class OrgReviewRoleServiceImpl method saveDelegateMemberToKim.
protected void saveDelegateMemberToKim(OrgReviewRole orr) {
if (LOG.isDebugEnabled()) {
LOG.debug("Saving delegate member from OrgReviewRole: " + orr);
}
RoleService roleService = KimApiServiceLocator.getRoleService();
// Save delegation(s)
List<KfsKimDocDelegateMember> delegationMembers = getDelegationMembersToSave(orr);
for (KfsKimDocDelegateMember dm : delegationMembers) {
// retrieve the delegate type so it can be updated
DelegationType delegationType = dm.getDelegationType();
DelegateType delegateType = roleService.getDelegateTypeByRoleIdAndDelegateTypeCode(orr.getRoleId(), delegationType);
if (shouldCreateNewDelegateType(delegateType)) {
DelegateType newDelegateType = new DelegateType();
newDelegateType.setRoleId(orr.getRoleId());
newDelegateType.setDelegationType(delegationType);
newDelegateType.setDelegationMembers(new ArrayList<>(1));
// ensure this is set (for new delegation types)
newDelegateType.setKimTypeId(orr.getKimTypeId());
delegateType = roleService.createDelegateType(newDelegateType);
if (LOG.isDebugEnabled()) {
LOG.debug("No DelegateType in KIM. Created new one: " + delegateType);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Pulled DelegateType from KIM: " + delegateType);
}
}
boolean foundExistingMember = false;
DelegateMember addedMember = null;
// check for an existing delegation member given its unique ID if found, update that record
if (StringUtils.isNotBlank(dm.getDelegationMemberId())) {
DelegateMember member = roleService.getDelegationMemberById(dm.getDelegationMemberId());
if (member != null) {
foundExistingMember = true;
if (LOG.isDebugEnabled()) {
LOG.debug("Found existing delegate member - updating existing record. " + member);
}
// KFSMI-9628 : fixing issue with the delegate switch from primary to secondary
// IN this case, we need to delete the member from the "other" delegate type
// need to determine what the "existing" type was
DelegateType originalDelegateType = roleService.getDelegateTypeByDelegationId(member.getDelegationId());
// if they are the same, we can just update the existing record
if (originalDelegateType.getDelegationType().equals(dm.getDelegationType())) {
updateDelegateMemberFromDocDelegateMember(member, dm);
addedMember = roleService.updateDelegateMember(member);
} else {
// Otherwise, we need to remove the old one and add a new one
// Remove old
roleService.removeDelegateMembers(Collections.singletonList(member));
// add new
DelegateMember newMember = new DelegateMember();
newMember.setDelegationId(delegateType.getDelegationId());
updateDelegateMemberFromDocDelegateMember(newMember, dm);
addedMember = roleService.createDelegateMember(newMember);
}
}
}
// if we did not find one, then we need to create a new member
if (!foundExistingMember) {
if (LOG.isDebugEnabled()) {
LOG.debug("No existing delegate member found, adding as a new delegate: " + dm);
}
DelegateMember newMember = new DelegateMember();
newMember.setDelegationId(delegateType.getDelegationId());
updateDelegateMemberFromDocDelegateMember(newMember, dm);
addedMember = roleService.createDelegateMember(newMember);
}
if (addedMember != null) {
orr.setDelegationMemberId(addedMember.getDelegationMemberId());
}
}
}
Aggregations