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

Revision 6785, 5.7 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.util.List;
35
36import javax.jdo.annotations.PersistenceAware;
37
38import org.datanucleus.query.typesafe.TypesafeQuery;
39
40import org.springframework.beans.factory.annotation.Autowired;
41
42import serpro.mailarchiver.domain.metaarchive.Folder;
43import serpro.mailarchiver.domain.metaarchive.QFolder;
44import serpro.mailarchiver.service.BaseService;
45import serpro.mailarchiver.service.dto.TFolder;
46import serpro.mailarchiver.service.find.FFolder;
47import serpro.mailarchiver.session.Session;
48import serpro.mailarchiver.util.Logger;
49import serpro.mailarchiver.util.jdo.PersistenceManager;
50import serpro.mailarchiver.util.transaction.WithReadWriteTx;
51
52@PersistenceAware
53public 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}
Note: See TracBrowser for help on using the repository browser.