/** * 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 javax.jdo.annotations.PersistenceAware; import org.springframework.beans.factory.annotation.Autowired; import serpro.mailarchiver.domain.metaarchive.Folder; import serpro.mailarchiver.domain.metaarchive.Message; import serpro.mailarchiver.service.BaseService; import serpro.mailarchiver.service.find.FFolder; import serpro.mailarchiver.session.Session; import serpro.mailarchiver.util.Logger; import serpro.mailarchiver.util.jdo.PersistenceManager; import serpro.mailarchiver.util.transaction.WithReadWriteTx; @PersistenceAware public class DefaultDeleteFolderOperation extends BaseService implements DeleteFolderOperation { private static final Logger log = Logger.getLocalLogger(); @Autowired private FFolder findFolder; @WithReadWriteTx @Override public Integer apply(String folderId, boolean recursive) throws ServiceFault { PersistenceManager pm = getPersistenceManager(); Folder folder = findFolder.byId(folderId); if(folder == null) { ServiceFault.folderNotFound() .setActor("deleteFolder") .setMessage("Folder not found.") .addValue("folderId", folderId) .raise(); } if(folder.isUserHomeFolder()) { ServiceFault.invalidFolderId() .setActor("deleteFolder") .setMessage("Invalid delete of home folder.") .raise(); } if(recursive) { deleteRecursively(folder, pm); } else { delete(folder, pm); } return 0; } private void deleteRecursively(Folder folder, PersistenceManager pm) throws ServiceFault { for(Folder childFolder : folder.getChildren()) { deleteRecursively(childFolder, pm); } for(Message message : folder.getMessages()) { try { Files.deleteIfExists(message.getAbsolutePath()); Session.getLuceneIndex().deleteMessage(message.getOid()); pm.deletePersistent(message); } catch(IOException e) { ServiceFault.fileSystemFailure() .setActor("deleteFolder") .setMessage("Filesystem delete message failure.") .addValue("messagePath", message.getRelativePath()) .setCause(e) .raise(); } } delete(folder, pm); } private void delete(Folder folder, PersistenceManager pm) throws ServiceFault { if((folder.getChildren().size() > 0) || (folder.getMessages().size() > 0)) { ServiceFault.folderNotEmpty() .setActor("deleteFolder") .setMessage("Folder not empty.") .addValue("folderPath", folder.getRelativePath()) .raise(); } if(folder.getAbsolutePath().toFile().list().length > 0) { ServiceFault.fileSystemFolderNotEmpty() .setActor("deleteFolder") .setMessage("Filesystem directory not empty.") .addValue("folderPath", folder.getRelativePath()) .raise(); } try { Files.deleteIfExists(folder.getAbsolutePath()); pm.deletePersistent(folder); } catch (IOException e) { ServiceFault.fileSystemFailure() .setActor("deleteFolder") .setMessage("Filesystem delete directory failure.") .addValue("folder path", folder.getRelativePath()) .setCause(e) .raise(); } } }