User user = service.getUserHandler().findUserByName(userName);
Group group = service.getGroupHandler().findGroupById(groupId);
MembershipType mt = service.getMembershipTypeHandler().findMembershipType(membershipTypeName);
Membership existingMembership = findMembershipByUserGroupAndType(userName, groupId, membershipTypeName);
if (existingMembership != null)
{
removeMembership(existingMembership.getId(), false);
}
linkMembership(user, group, mt, false);
}
}
}