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.util.List; |
---|
35 | |
---|
36 | import javax.jdo.annotations.PersistenceAware; |
---|
37 | |
---|
38 | import org.datanucleus.query.typesafe.TypesafeQuery; |
---|
39 | |
---|
40 | import org.springframework.beans.factory.annotation.Autowired; |
---|
41 | |
---|
42 | import serpro.mailarchiver.domain.metaarchive.Folder; |
---|
43 | import serpro.mailarchiver.domain.metaarchive.QFolder; |
---|
44 | import serpro.mailarchiver.service.BaseService; |
---|
45 | import serpro.mailarchiver.service.dto.TFolder; |
---|
46 | import serpro.mailarchiver.service.find.FFolder; |
---|
47 | import serpro.mailarchiver.session.Session; |
---|
48 | import serpro.mailarchiver.util.Logger; |
---|
49 | import serpro.mailarchiver.util.jdo.PersistenceManager; |
---|
50 | import serpro.mailarchiver.util.transaction.WithReadWriteTx; |
---|
51 | |
---|
52 | @PersistenceAware |
---|
53 | public class DefaultCreateFolderOperation |
---|
54 | extends BaseService |
---|
55 | implements CreateFolderOperation |
---|
56 | { |
---|
57 | private static final Logger log = Logger.getLocalLogger(); |
---|
58 | |
---|
59 | @Autowired |
---|
60 | private FFolder findFolder; |
---|
61 | |
---|
62 | @WithReadWriteTx |
---|
63 | @Override |
---|
64 | public TFolder apply(String parentFolderId, String name) throws ServiceFault { |
---|
65 | |
---|
66 | PersistenceManager pm = getPersistenceManager(); |
---|
67 | |
---|
68 | if(name.isEmpty()) { |
---|
69 | ServiceFault.invalidFolderName() |
---|
70 | .setActor("createFolder") |
---|
71 | .setMessage("New folder name is null or empty.") |
---|
72 | .raise(); |
---|
73 | } |
---|
74 | |
---|
75 | Folder parentFolder = findFolder.byId(parentFolderId); |
---|
76 | |
---|
77 | if(parentFolder == null) { |
---|
78 | ServiceFault.folderNotFound() |
---|
79 | .setActor("createFolder") |
---|
80 | .setMessage("Parent folder not found.") |
---|
81 | .addValue("parentFolderId", parentFolderId) |
---|
82 | .raise(); |
---|
83 | } |
---|
84 | |
---|
85 | TypesafeQuery<Folder> tq = pm.newTypesafeQuery(Folder.class); |
---|
86 | QFolder cand = QFolder.candidate(); |
---|
87 | |
---|
88 | List<Folder> results = tq.filter( |
---|
89 | cand.name.equalsIgnoreCase(name) |
---|
90 | .and(cand.parent.eq(tq.parameter("p", Folder.class)))) |
---|
91 | .setParameter("p", parentFolder) |
---|
92 | .orderBy(cand.name.asc()) |
---|
93 | .executeList(); |
---|
94 | |
---|
95 | Folder folder = null; |
---|
96 | |
---|
97 | switch(results.size()) { |
---|
98 | case 0: |
---|
99 | folder = new Folder(); |
---|
100 | |
---|
101 | folder.setName(name); |
---|
102 | folder.setParent(parentFolder); |
---|
103 | |
---|
104 | try { |
---|
105 | Files.createDirectory(folder.getAbsolutePath()); |
---|
106 | |
---|
107 | if(parentFolder.isUserHomeFolder()) { |
---|
108 | |
---|
109 | if(name.equalsIgnoreCase("Inbox")) { |
---|
110 | folder.setOid(Session.getInboxFolderId()); |
---|
111 | } |
---|
112 | else if(name.equalsIgnoreCase("Outbox")) { |
---|
113 | folder.setOid(Session.getOutboxFolderId()); |
---|
114 | } |
---|
115 | else if(name.equalsIgnoreCase("Drafts")) { |
---|
116 | folder.setOid(Session.getDraftsFolderId()); |
---|
117 | } |
---|
118 | else if(name.equalsIgnoreCase("Sent")) { |
---|
119 | folder.setOid(Session.getSentFolderId()); |
---|
120 | } |
---|
121 | else if(name.equalsIgnoreCase("Spam")) { |
---|
122 | folder.setOid(Session.getSpamFolderId()); |
---|
123 | } |
---|
124 | else if(name.equalsIgnoreCase("Trash")) { |
---|
125 | folder.setOid(Session.getTrashFolderId()); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | pm.makePersistent(folder); |
---|
130 | } |
---|
131 | catch(IOException ex) { |
---|
132 | ServiceFault.fileSystemFailure() |
---|
133 | .setActor("createFolder") |
---|
134 | .setMessage("Filesystem create directory failure.") |
---|
135 | .addValue("parentFolderId", parentFolderId) |
---|
136 | .addValue("folderId", folder.getOid()) |
---|
137 | .addValue("folderPath", folder.getRelativePath()) |
---|
138 | .setCause(ex) |
---|
139 | .raise(); |
---|
140 | } |
---|
141 | break; |
---|
142 | |
---|
143 | case 1: |
---|
144 | folder = results.get(0); |
---|
145 | break; |
---|
146 | |
---|
147 | default: |
---|
148 | ServiceFault.moreThanOneFolderFound() |
---|
149 | .setActor("createFolder") |
---|
150 | .setMessage("More than one folder found.") |
---|
151 | .addValue("size", results.size()) |
---|
152 | .raise(); |
---|
153 | } |
---|
154 | |
---|
155 | return new TFolder(folder); |
---|
156 | } |
---|
157 | } |
---|