MergeUsersByUsernameDatabaseMigration implements ContextAwareTask { void mergeGroups(ApplicationContext applicationContext, List<User> duplicatedUserList, User userToKeep) { GroupRepository groupRepository = applicationContext.getBean(GroupRepository.class); UserGroupRepository userGroupRepository = applicationContext.getBean(UserGroupRepository.class); Set<String> listOfAddedProfiles = new HashSet<>(); List<UserGroup> mergedUserGroups = new ArrayList<>(); List<Integer> userIdList = new ArrayList<>(duplicatedUserList.size()); for (int i = 0; i < duplicatedUserList.size(); i++) { User userToProcess = duplicatedUserList.get(i); userIdList.add(userToProcess.getId()); List<UserGroup> userGroupsToProcess = userGroupRepository.findAll( UserGroupSpecs.hasUserId(userToProcess.getId())); if (Log.isDebugEnabled(Log.JEEVES)) { Log.debug(Log.JEEVES, "Groups of user {" + userToProcess.getId() + " - " + userToProcess.getUsername() + "}: " + userGroupsToProcess.size()); Log.debug(Log.JEEVES, userGroupsToProcess); } for (UserGroup ug : userGroupsToProcess) { String key = ug.getProfile().name() + "#" + ug.getGroup().getId(); if (!listOfAddedProfiles.contains(key)) { listOfAddedProfiles.add(key); } } } for (String key : listOfAddedProfiles) { String[] splitKey = StringUtils.split(key, "#"); String profileName = splitKey[0]; Integer groupId = Integer.valueOf(splitKey[1]); Group group = groupRepository.findOne(groupId); Profile profile = Profile.findProfileIgnoreCase(profileName); UserGroup userGroup = new UserGroup().setGroup(group).setProfile(profile).setUser(userToKeep); mergedUserGroups.add(userGroup); } userGroupRepository.deleteAllByIdAttribute(UserGroupId_.userId, userIdList); userGroupRepository.save(mergedUserGroups); }  @Transactional @Override void run(ApplicationContext applicationContext); User mergeUser(User toOverwrite, User toKeep);  }
@Test public void testMergeGroups() { User user1 = _userRepo.findOneByUsername("Username1"); User user2 = _userRepo.findOneByUsername("uSername1"); User user3 = _userRepo.findOneByUsername("usErname1"); User user4 = _userRepo.findOneByUsername("useRname1"); Group group1 = _groupRepo.findByName("groupTest1"); Group group2 = _groupRepo.findByName("groupTest2"); MergeUsersByUsernameDatabaseMigration migration = new MergeUsersByUsernameDatabaseMigration(); List<User> duplicatedUserNames = _userRepo.findByUsernameIgnoreCase("username1"); duplicatedUserNames.sort(Comparator.comparing(User::getProfile)); User greatestProfileUser = duplicatedUserNames.get(0); migration.mergeGroups(_applicationContext, duplicatedUserNames, greatestProfileUser); assertNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.Administrator))); assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.UserAdmin))); assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.Reviewer))); assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.Editor))); assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.RegisteredUser))); assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.Guest))); assertNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.Administrator))); assertNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.UserAdmin))); assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.Reviewer))); assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.Editor))); assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.RegisteredUser))); assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId()) .setUserId(greatestProfileUser.getId()).setProfile(Profile.Guest))); assertEquals(0, _userGroupRepo.findAll(UserGroupSpecs.hasUserId(user1.getId())).size()); assertEquals(0, _userGroupRepo.findAll(UserGroupSpecs.hasUserId(user2.getId())).size()); assertEquals(0, _userGroupRepo.findAll(UserGroupSpecs.hasUserId(user3.getId())).size()); assertEquals(9, _userGroupRepo.findAll(UserGroupSpecs.hasUserId(user4.getId())).size()); }