MergeUsersByUsernameDatabaseMigration implements ContextAwareTask { void transferMetadata(ApplicationContext applicationContext, List<User> oldMetadataOwnerList, User newMetadataOwner) throws Exception { MetadataRepository metadataRepository = applicationContext.getBean(MetadataRepository.class); DataManager dataManager = applicationContext.getBean(DataManager.class); MetadataStatusRepository metadataStatusRepository = applicationContext.getBean(MetadataStatusRepository.class); for (int i = 1; i < oldMetadataOwnerList.size(); i++) { User oldOwner = oldMetadataOwnerList.get(i); List<Metadata> metadataList = metadataRepository.findAll((Specification<Metadata>)MetadataSpecs.isOwnedByUser(oldOwner.getId())); for (Metadata metadata : metadataList) { dataManager.updateMetadataOwner(metadata.getId(), Integer.toString(newMetadataOwner.getId()), Integer.toString(metadata.getSourceInfo().getGroupOwner())); } List<MetadataStatus> metadataStatusList = metadataStatusRepository.findAll( MetadataStatusSpecs.hasUserId(oldOwner.getId())); for (MetadataStatus metadataStatus : metadataStatusList) { MetadataStatusId metadataStatusId = metadataStatus.getId(); MetadataStatusId newMetadataStatusId = new MetadataStatusId(); newMetadataStatusId.setUserId(newMetadataOwner.getId()); newMetadataStatusId.setMetadataId(metadataStatusId.getMetadataId()); newMetadataStatusId.setChangeDate(metadataStatusId.getChangeDate()); metadataStatusId.setStatusId(metadataStatusId.getStatusId()); MetadataStatus newMetadataStatus = new MetadataStatus(); newMetadataStatus.setId(newMetadataStatusId); newMetadataStatus.setChangeMessage(metadataStatus.getChangeMessage()); newMetadataStatus.setStatusValue(metadataStatus.getStatusValue()); metadataStatusRepository.save(newMetadataStatus); } metadataStatusRepository.deleteAllById_UserId(oldOwner.getId()); metadataStatusRepository.save(metadataStatusList); dataManager.indexMetadata(Lists.transform(metadataList, new Function<Metadata, String>() { @Nonnull @Override public String apply(@Nullable Metadata input) { return String.valueOf(input.getId()); } })); } }  @Transactional @Override void run(ApplicationContext applicationContext); User mergeUser(User toOverwrite, User toKeep);  }
@Test public void testTransferMetadata() throws Exception { User user1 = _userRepo.findOneByUsername("Username1"); User user2 = _userRepo.findOneByUsername("uSername1"); User user3 = _userRepo.findOneByUsername("usErname1"); User user4 = _userRepo.findOneByUsername("useRname1"); MergeUsersByUsernameDatabaseMigration migration = new MergeUsersByUsernameDatabaseMigration(); List<User> duplicatedUserNames = _userRepo.findByUsernameIgnoreCase("username1"); duplicatedUserNames.sort(Comparator.comparing(User::getProfile)); User greatestProfileUser = duplicatedUserNames.get(0); final DataManager dataManager = _applicationContext.getBean(DataManager.class); final MetadataRepository metadataRepository = _applicationContext.getBean(MetadataRepository.class); migration.transferMetadata(_applicationContext, duplicatedUserNames, greatestProfileUser); for (Map.Entry<Integer, Integer> entry : metadataIdList.entrySet()) { Integer metadataId = entry.getKey(); Integer groupId = entry.getValue(); Metadata metadata = metadataRepository.findOne(metadataId); assertEquals((Integer) greatestProfileUser.getId(), metadata.getSourceInfo().getOwner()); assertEquals(groupId, metadata.getSourceInfo().getGroupOwner()); } assertEquals(0, metadataRepository.findAll((Specification<Metadata>)MetadataSpecs.isOwnedByUser(user1.getId())).size()); assertEquals(0, metadataRepository.findAll((Specification<Metadata>)MetadataSpecs.isOwnedByUser(user2.getId())).size()); assertEquals(0, metadataRepository.findAll((Specification<Metadata>)MetadataSpecs.isOwnedByUser(user3.getId())).size()); assertEquals(6, metadataRepository.findAll((Specification<Metadata>)MetadataSpecs.isOwnedByUser(user4.getId())).size()); }