private void doRenameMailbox(MailboxPath from, MailboxPath to, MailboxSession session, MailboxMapper mapper) throws MailboxException { // TODO put this into a serilizable transaction Mailbox mailbox = Optional.ofNullable(mapper.findMailboxByPath(from)) .orElseThrow(() -> new MailboxNotFoundException(from)); mailbox.setNamespace(to.getNamespace()); mailbox.setUser(to.getUser()); mailbox.setName(to.getName()); mapper.save(mailbox); dispatcher.mailboxRenamed(session, from, mailbox); // rename submailboxes MailboxPath children = new MailboxPath(from.getNamespace(), from.getUser(), from.getName() + getDelimiter() + "%"); locker.executeWithLock(session, children, (LockAwareExecution<Void>) () -> { List<Mailbox> subMailboxes = mapper.findMailboxWithPathLike(children); for (Mailbox sub : subMailboxes) { String subOriginalName = sub.getName(); String subNewName = to.getName() + subOriginalName.substring(from.getName().length()); MailboxPath fromPath = new MailboxPath(children, subOriginalName); sub.setName(subNewName); mapper.save(sub); dispatcher.mailboxRenamed(session, fromPath, sub); LOGGER.debug("Rename mailbox sub-mailbox {} to {}", subOriginalName, subNewName); } return null; }, true); }