/**
* MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface.
* Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
/******************************************************************************\
*
* This product was developed by
*
* SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO),
*
* a government company established under Brazilian law (5.615/70),
* at Department of Development of Porto Alegre.
*
\******************************************************************************/
package serpro.mailarchiver.service.web;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import javax.jdo.annotations.PersistenceAware;
import org.datanucleus.query.typesafe.TypesafeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import serpro.mailarchiver.domain.metaarchive.Folder;
import serpro.mailarchiver.domain.metaarchive.QFolder;
import serpro.mailarchiver.service.BaseService;
import serpro.mailarchiver.service.dto.TFolder;
import serpro.mailarchiver.service.find.FFolder;
import serpro.mailarchiver.session.Session;
import serpro.mailarchiver.util.Logger;
import serpro.mailarchiver.util.jdo.PersistenceManager;
import serpro.mailarchiver.util.transaction.WithReadWriteTx;
@PersistenceAware
public class DefaultCreateAbsoluteFolderOperation
extends BaseService
implements CreateAbsoluteFolderOperation
{
private static final Logger log = Logger.getLocalLogger();
@Autowired
private FFolder findFolder;
@WithReadWriteTx
@Override
public TFolder apply(String path) throws ServiceFault {
PersistenceManager pm = getPersistenceManager();
if(path.isEmpty()) {
ServiceFault.invalidFolderPath()
.setActor("createAbsoluteFolder")
.setMessage("New folder path is null or empty.")
.raise();
}
Folder folder = findFolder.byId("home");
if(folder == null) {
ServiceFault.folderNotFound()
.setActor("createAbsoluteFolder")
.setMessage("Home folder not found.")
.raise();
}
//TODO: aprimorar validação
String[] names = path.split("/|\\\\");
for(String name : names) {
if(name.isEmpty()) {
continue;
}
Folder parentFolder = folder;
TypesafeQuery tq = pm.newTypesafeQuery(Folder.class);
QFolder cand = QFolder.candidate();
List results = tq.filter(
cand.name.equalsIgnoreCase(name)
.and(cand.parent.eq(tq.parameter("p", Folder.class))))
.setParameter("p", parentFolder)
.orderBy(cand.name.asc())
.executeList();
switch(results.size()) {
case 0:
folder = new Folder();
folder.setName(name);
folder.setParent(parentFolder);
try {
Files.createDirectory(folder.getAbsolutePath());
if(parentFolder.isUserHomeFolder()) {
if(name.equalsIgnoreCase("Inbox")) {
folder.setOid(Session.getInboxFolderId());
}
else if(name.equalsIgnoreCase("Outbox")) {
folder.setOid(Session.getOutboxFolderId());
}
else if(name.equalsIgnoreCase("Drafts")) {
folder.setOid(Session.getDraftsFolderId());
}
else if(name.equalsIgnoreCase("Sent")) {
folder.setOid(Session.getSentFolderId());
}
else if(name.equalsIgnoreCase("Spam")) {
folder.setOid(Session.getSpamFolderId());
}
else if(name.equalsIgnoreCase("Trash")) {
folder.setOid(Session.getTrashFolderId());
}
}
pm.makePersistent(folder);
}
catch(IOException ex) {
ServiceFault.fileSystemFailure()
.setActor("createFolder")
.setMessage("Filesystem create directory failure.")
.addValue("parentFolderId", parentFolder.getOid())
.addValue("folderId", folder.getOid())
.addValue("folderPath", folder.getRelativePath())
.setCause(ex)
.raise();
}
break;
case 1:
folder = results.get(0);
break;
default:
ServiceFault.moreThanOneFolderFound()
.setActor("createFolder")
.setMessage("More than one folder found.")
.addValue("size", results.size())
.raise();
}
}
return new TFolder(folder);
}
}