public GroupDetail renameGroup(final AccountGroup.Id groupId,
final String newName) throws OrmException, NameAlreadyUsedException,
NoSuchGroupException {
final GroupControl ctl = groupControlFactory.validateFor(groupId);
final AccountGroup group = db.accountGroups().get(groupId);
if (group == null || !ctl.isOwner()) {
throw new NoSuchGroupException(groupId);
}
final AccountGroup.NameKey old = group.getNameKey();
final AccountGroup.NameKey key = new AccountGroup.NameKey(newName);
try {
final AccountGroupName id = new AccountGroupName(key, groupId);
db.accountGroupNames().insert(Collections.singleton(id));
} catch (OrmDuplicateKeyException dupeErr) {
// If we are using this identity, don't report the exception.
//
AccountGroupName other = db.accountGroupNames().get(key);
if (other != null && other.getId().equals(groupId)) {
return groupDetailFactory.create(groupId).call();
}
// Otherwise, someone else has this identity.
//
throw new NameAlreadyUsedException();
}
group.setNameKey(key);
db.accountGroups().update(Collections.singleton(group));
AccountGroupName priorName = db.accountGroupNames().get(old);
if (priorName != null) {
db.accountGroupNames().delete(Collections.singleton(priorName));
}
groupCache.evict(group);
groupCache.evictAfterRename(old, key);
renameGroupOpFactory.create( //
currentUser.newCommitterIdent(new Date(), TimeZone.getDefault()), //
group.getGroupUUID(), //
old.get(), newName).start(0, TimeUnit.MILLISECONDS);
return groupDetailFactory.create(groupId).call();
}