/** * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ /******************************************************************************\ * * This product was developed by * * SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), * * a government company established under Brazilian law (5.615/70), * at Department of Development of Porto Alegre. * \******************************************************************************/ package serpro.mailarchiver.service.web; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import javax.jdo.annotations.PersistenceAware; import org.springframework.beans.factory.annotation.Autowired; import serpro.mailarchiver.domain.metaarchive.Folder; import serpro.mailarchiver.service.BaseService; import serpro.mailarchiver.service.dto.TFolder; import serpro.mailarchiver.service.find.FFolder; import serpro.mailarchiver.util.Logger; import serpro.mailarchiver.util.transaction.WithReadWriteTx; @PersistenceAware public class DefaultRenameFolderOperation extends BaseService implements RenameFolderOperation { private static final Logger log = Logger.getLocalLogger(); @Autowired private FFolder findFolder; @WithReadWriteTx @Override public TFolder apply(String folderId, String newName) throws ServiceFault { if(newName.isEmpty()) { ServiceFault.invalidFolderName() .setActor("renameFolder") .setMessage("New folder name is null or empty.") .raise(); } Folder folder = findFolder.byId(folderId); if(folder == null) { ServiceFault.folderNotFound() .setActor("renameFolder") .setMessage("Folder not found.") .addValue("folderId", folderId) .raise(); } if(folder.isUserHomeFolder()) { ServiceFault.invalidFolderId() .setActor("renameFolder") .setMessage("Invalid renaming of user home folder.") .addValue("folderId", folderId) .raise(); } String currentName = folder.getName(); if(folder.isSpecialFolder()) { if(!newName.equalsIgnoreCase(currentName)) { ServiceFault.invalidFolderId() .setActor("renameFolder") .setMessage("Invalid renaming of special folder.") .addValue("folderId", folderId) .raise(); } } for(Folder sibling : folder.getParent().getChildren()) { if(sibling == folder) { continue; } if(sibling.getName().equalsIgnoreCase(newName)) { ServiceFault.folderNameAlreadyExists() .setActor("renameFolder") .setMessage("An sibling folder with the same name already exist.") .addValue("folderId", folderId) .addValue("newName", newName) .raise(); } } if(!newName.equals(currentName)) { try { Path source = folder.getAbsolutePath(); folder.setName(newName); Path target = folder.getAbsolutePath(); Files.move(source, target, StandardCopyOption.ATOMIC_MOVE); } catch(IOException e) { folder.setName(currentName); ServiceFault.fileSystemFailure() .setActor("renameFolder") .setMessage("Filesystem rename folder failure.") .addValue("folderId", folderId) .addValue("currentName", currentName) .addValue("newName", newName) .setCause(e) .raise(); } } return new TFolder(folder); } }