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 DefaultCreateAbsoluteFolderOperation |
---|
54 | extends BaseService |
---|
55 | implements CreateAbsoluteFolderOperation |
---|
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 path) throws ServiceFault { |
---|
65 | |
---|
66 | PersistenceManager pm = getPersistenceManager(); |
---|
67 | |
---|
68 | if(path.isEmpty()) { |
---|
69 | ServiceFault.invalidFolderPath() |
---|
70 | .setActor("createAbsoluteFolder") |
---|
71 | .setMessage("New folder path is null or empty.") |
---|
72 | .raise(); |
---|
73 | } |
---|
74 | |
---|
75 | Folder folder = findFolder.byId("home"); |
---|
76 | |
---|
77 | if(folder == null) { |
---|
78 | ServiceFault.folderNotFound() |
---|
79 | .setActor("createAbsoluteFolder") |
---|
80 | .setMessage("Home folder not found.") |
---|
81 | .raise(); |
---|
82 | } |
---|
83 | |
---|
84 | //TODO: aprimorar validação |
---|
85 | String[] names = path.split("/|\\\\"); |
---|
86 | |
---|
87 | for(String name : names) { |
---|
88 | |
---|
89 | if(name.isEmpty()) { |
---|
90 | continue; |
---|
91 | } |
---|
92 | |
---|
93 | Folder parentFolder = folder; |
---|
94 | |
---|
95 | TypesafeQuery<Folder> tq = pm.newTypesafeQuery(Folder.class); |
---|
96 | QFolder cand = QFolder.candidate(); |
---|
97 | |
---|
98 | List<Folder> results = tq.filter( |
---|
99 | cand.name.equalsIgnoreCase(name) |
---|
100 | .and(cand.parent.eq(tq.parameter("p", Folder.class)))) |
---|
101 | .setParameter("p", parentFolder) |
---|
102 | .orderBy(cand.name.asc()) |
---|
103 | .executeList(); |
---|
104 | |
---|
105 | switch(results.size()) { |
---|
106 | case 0: |
---|
107 | folder = new Folder(); |
---|
108 | |
---|
109 | folder.setName(name); |
---|
110 | folder.setParent(parentFolder); |
---|
111 | |
---|
112 | try { |
---|
113 | Files.createDirectory(folder.getAbsolutePath()); |
---|
114 | |
---|
115 | if(parentFolder.isUserHomeFolder()) { |
---|
116 | |
---|
117 | if(name.equalsIgnoreCase("Inbox")) { |
---|
118 | folder.setOid(Session.getInboxFolderId()); |
---|
119 | } |
---|
120 | else if(name.equalsIgnoreCase("Outbox")) { |
---|
121 | folder.setOid(Session.getOutboxFolderId()); |
---|
122 | } |
---|
123 | else if(name.equalsIgnoreCase("Drafts")) { |
---|
124 | folder.setOid(Session.getDraftsFolderId()); |
---|
125 | } |
---|
126 | else if(name.equalsIgnoreCase("Sent")) { |
---|
127 | folder.setOid(Session.getSentFolderId()); |
---|
128 | } |
---|
129 | else if(name.equalsIgnoreCase("Spam")) { |
---|
130 | folder.setOid(Session.getSpamFolderId()); |
---|
131 | } |
---|
132 | else if(name.equalsIgnoreCase("Trash")) { |
---|
133 | folder.setOid(Session.getTrashFolderId()); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | pm.makePersistent(folder); |
---|
138 | } |
---|
139 | catch(IOException ex) { |
---|
140 | ServiceFault.fileSystemFailure() |
---|
141 | .setActor("createFolder") |
---|
142 | .setMessage("Filesystem create directory failure.") |
---|
143 | .addValue("parentFolderId", parentFolder.getOid()) |
---|
144 | .addValue("folderId", folder.getOid()) |
---|
145 | .addValue("folderPath", folder.getRelativePath()) |
---|
146 | .setCause(ex) |
---|
147 | .raise(); |
---|
148 | } |
---|
149 | break; |
---|
150 | |
---|
151 | case 1: |
---|
152 | folder = results.get(0); |
---|
153 | break; |
---|
154 | |
---|
155 | default: |
---|
156 | ServiceFault.moreThanOneFolderFound() |
---|
157 | .setActor("createFolder") |
---|
158 | .setMessage("More than one folder found.") |
---|
159 | .addValue("size", results.size()) |
---|
160 | .raise(); |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | return new TFolder(folder); |
---|
165 | } |
---|
166 | } |
---|