source: contrib/MailArchiver/sources/src/serpro/mailarchiver/service/web/DefaultRenameFolderOperation.java @ 6785

Revision 6785, 4.9 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
1/**
2 * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface.
3 * Copyright (C) 2012  Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/******************************************************************************\
20*
21*  This product was developed by
22*
23*        SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO),
24*
25*  a government company established under Brazilian law (5.615/70),
26*  at Department of Development of Porto Alegre.
27*
28\******************************************************************************/
29
30package serpro.mailarchiver.service.web;
31
32import java.io.IOException;
33import java.nio.file.Files;
34import java.nio.file.Path;
35import java.nio.file.StandardCopyOption;
36
37import javax.jdo.annotations.PersistenceAware;
38
39import org.springframework.beans.factory.annotation.Autowired;
40
41import serpro.mailarchiver.domain.metaarchive.Folder;
42import serpro.mailarchiver.service.BaseService;
43import serpro.mailarchiver.service.dto.TFolder;
44import serpro.mailarchiver.service.find.FFolder;
45import serpro.mailarchiver.util.Logger;
46import serpro.mailarchiver.util.transaction.WithReadWriteTx;
47
48@PersistenceAware
49public class DefaultRenameFolderOperation
50    extends BaseService
51    implements RenameFolderOperation
52{
53    private static final Logger log = Logger.getLocalLogger();
54
55    @Autowired
56    private FFolder findFolder;
57
58    @WithReadWriteTx
59    @Override
60    public TFolder apply(String folderId, String newName) throws ServiceFault {
61
62        if(newName.isEmpty()) {
63            ServiceFault.invalidFolderName()
64                    .setActor("renameFolder")
65                    .setMessage("New folder name is null or empty.")
66                    .raise();
67        }
68
69        Folder folder = findFolder.byId(folderId);
70
71        if(folder == null) {
72            ServiceFault.folderNotFound()
73                    .setActor("renameFolder")
74                    .setMessage("Folder not found.")
75                    .addValue("folderId", folderId)
76                    .raise();
77        }
78
79        if(folder.isUserHomeFolder()) {
80            ServiceFault.invalidFolderId()
81                    .setActor("renameFolder")
82                    .setMessage("Invalid renaming of user home folder.")
83                    .addValue("folderId", folderId)
84                    .raise();
85        }
86
87        String currentName = folder.getName();
88
89        if(folder.isSpecialFolder()) {
90            if(!newName.equalsIgnoreCase(currentName)) {
91                ServiceFault.invalidFolderId()
92                        .setActor("renameFolder")
93                        .setMessage("Invalid renaming of special folder.")
94                        .addValue("folderId", folderId)
95                        .raise();
96            }
97        }
98
99        for(Folder sibling : folder.getParent().getChildren()) {
100
101            if(sibling == folder) {
102                continue;
103            }
104
105            if(sibling.getName().equalsIgnoreCase(newName)) {
106                ServiceFault.folderNameAlreadyExists()
107                        .setActor("renameFolder")
108                        .setMessage("An sibling folder with the same name already exist.")
109                        .addValue("folderId", folderId)
110                        .addValue("newName", newName)
111                        .raise();
112            }
113        }
114
115        if(!newName.equals(currentName)) {
116            try {
117                Path source = folder.getAbsolutePath();
118                folder.setName(newName);
119                Path target = folder.getAbsolutePath();
120                Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
121            }
122            catch(IOException e) {
123                folder.setName(currentName);
124                ServiceFault.fileSystemFailure()
125                        .setActor("renameFolder")
126                        .setMessage("Filesystem rename folder failure.")
127                        .addValue("folderId", folderId)
128                        .addValue("currentName", currentName)
129                        .addValue("newName", newName)
130                        .setCause(e)
131                        .raise();
132            }
133        }
134
135        return new TFolder(folder);
136    }
137}
Note: See TracBrowser for help on using the repository browser.