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 | |
---|
30 | package serpro.mailarchiver.service.web; |
---|
31 | |
---|
32 | import java.io.IOException; |
---|
33 | import java.nio.file.Files; |
---|
34 | import java.nio.file.Path; |
---|
35 | import java.nio.file.StandardCopyOption; |
---|
36 | |
---|
37 | import javax.jdo.annotations.PersistenceAware; |
---|
38 | |
---|
39 | import org.springframework.beans.factory.annotation.Autowired; |
---|
40 | |
---|
41 | import serpro.mailarchiver.domain.metaarchive.Folder; |
---|
42 | import serpro.mailarchiver.domain.metaarchive.Message; |
---|
43 | import serpro.mailarchiver.service.BaseService; |
---|
44 | import serpro.mailarchiver.service.find.FFolder; |
---|
45 | import serpro.mailarchiver.service.find.FMessage; |
---|
46 | import serpro.mailarchiver.util.Logger; |
---|
47 | import serpro.mailarchiver.util.transaction.WithReadOnlyTx; |
---|
48 | import serpro.mailarchiver.util.transaction.WithReadWriteTx; |
---|
49 | |
---|
50 | import static com.google.common.base.Strings.isNullOrEmpty; |
---|
51 | |
---|
52 | @PersistenceAware |
---|
53 | public class DefaultMoveMessagesOperation |
---|
54 | extends BaseService |
---|
55 | implements MoveMessagesOperation |
---|
56 | { |
---|
57 | private static final Logger log = Logger.getLocalLogger(); |
---|
58 | |
---|
59 | @Autowired |
---|
60 | private FFolder findFolder; |
---|
61 | |
---|
62 | @Autowired |
---|
63 | private FMessage findMessage; |
---|
64 | |
---|
65 | @WithReadOnlyTx |
---|
66 | @Override |
---|
67 | public Integer apply(String folderId, String... messagesId) throws ServiceFault { |
---|
68 | |
---|
69 | Folder folder = findFolder.byId(folderId); |
---|
70 | |
---|
71 | if(folder == null) { |
---|
72 | ServiceFault.folderNotFound() |
---|
73 | .setActor("moveMessages") |
---|
74 | .setMessage("Destination folder not found.") |
---|
75 | .addValue("folderId", folderId) |
---|
76 | .raise(); |
---|
77 | } |
---|
78 | |
---|
79 | for(String messageId : messagesId) { |
---|
80 | if(isNullOrEmpty(messageId)) { |
---|
81 | continue; |
---|
82 | } |
---|
83 | move(folder, messageId); |
---|
84 | } |
---|
85 | |
---|
86 | return 0; |
---|
87 | } |
---|
88 | |
---|
89 | @WithReadWriteTx |
---|
90 | private void move(Folder folder, String messageId) throws ServiceFault { |
---|
91 | |
---|
92 | Message message = findMessage.byId(messageId); |
---|
93 | |
---|
94 | if(message == null) { |
---|
95 | ServiceFault.messageNotFound() |
---|
96 | .setActor("moveMessages") |
---|
97 | .setMessage("Message not found.") |
---|
98 | .addValue("messageId", messageId) |
---|
99 | .raise(); |
---|
100 | } |
---|
101 | |
---|
102 | Folder currentFolder = message.getFolder(); |
---|
103 | |
---|
104 | try { |
---|
105 | Path source = message.getAbsolutePath(); |
---|
106 | message.setFolder(folder); |
---|
107 | Path target = message.getAbsolutePath(); |
---|
108 | Files.move(source, target, StandardCopyOption.ATOMIC_MOVE); |
---|
109 | } |
---|
110 | catch(IOException e) { |
---|
111 | message.setFolder(currentFolder); |
---|
112 | ServiceFault.fileSystemFailure() |
---|
113 | .setActor("moveMessages") |
---|
114 | .setMessage("Filesystem move message failure.") |
---|
115 | .addValue("folderId", folder.getOid()) |
---|
116 | .addValue("folderPath", folder.getRelativePath()) |
---|
117 | .addValue("messageId", message.getOid()) |
---|
118 | .addValue("messagePath", message.getRelativePath()) |
---|
119 | .setCause(e) |
---|
120 | .raise(); |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|