/**
* 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.domain.metaarchive;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.jdo.JDOHelper;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import org.springframework.beans.factory.annotation.Autowired;
import serpro.mailarchiver.domain.BaseObject;
import serpro.mailarchiver.util.ArchiveVisitor;
import serpro.mailarchiver.util.Logger;
import serpro.mailarchiver.util.UserAppConfig;
@PersistenceCapable
public class Folder
extends BaseObject
implements Comparable
{
@NotPersistent
private static final Logger log = Logger.getLocalLogger();
//**** P E R S I S T E N T ****
private String oid;
private String name;
private Folder parent;
private Integer parentIdx;
private ArrayList children = new ArrayList();
private ArrayList messages = new ArrayList();
//*****************************
@Override
public void jdoPreDelete() {
super.jdoPreDelete();
setParent(null);
}
public final boolean isUserHomeFolder() {
return oid.endsWith("_home");
}
public final boolean isInboxSpecialFolder() {
return oid.endsWith("_inbox");
}
public final boolean isOutboxSpecialFolder() {
return oid.endsWith("_outbox");
}
public final boolean isDraftsSpecialFolder() {
return oid.endsWith("_drafts");
}
public final boolean isSentSpecialFolder() {
return oid.endsWith("_sent");
}
public final boolean isSpamSpecialFolder() {
return oid.endsWith("_spam");
}
public final boolean isTrashSpecialFolder() {
return oid.endsWith("_trash");
}
public final boolean isSpecialFolder() {
return
isInboxSpecialFolder()
|| isOutboxSpecialFolder()
|| isDraftsSpecialFolder()
|| isSentSpecialFolder()
|| isSpamSpecialFolder()
|| isTrashSpecialFolder();
}
public final String getExternalOid() {
if(isUserHomeFolder()) {
return "home";
}
else if(isInboxSpecialFolder()) {
return "inbox";
}
else if(isOutboxSpecialFolder()) {
return "outbox";
}
else if(isDraftsSpecialFolder()) {
return "drafts";
}
else if(isSentSpecialFolder()) {
return "sent";
}
else if(isSpamSpecialFolder()) {
return "spam";
}
else if(isTrashSpecialFolder()) {
return "trash";
}
else {
return oid;
}
}
public final String getOid() {
return oid;
}
public final void setOid(String oid) {
this.oid = oid;
}
public final String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
}
//--------------------------------------------------------------------------
public final Folder getParent() {
return parent;
}
public final void setParent(Folder parent) {
if(this.parent != null) {
this.parent.internal_removeChild(this);
}
this.parent = parent;
if(this.parent != null) {
this.parent.internal_addChild(this);
}
}
public final int getParentIdx() {
return (parentIdx != null) ? parentIdx
: (getParent() != null) ? getParent().indexOf(this)
: -1;
}
//--------------------------------------------------------------------------
public final List getChildren() {
return Collections.unmodifiableList(children);
}
public final int indexOf(Folder folder) {
return children.indexOf(folder);
}
public final void addChild(Folder folder) {
if(folder != null) {
folder.setParent(this);
}
}
final void internal_addChild(Folder folder) {
children.add(folder);
}
public final void removeChild(Folder folder) {
if((folder != null) && (folder.getParent() == this)) {
folder.setParent(null);
}
}
final void internal_removeChild(Folder folder) {
children.remove(folder);
}
//--------------------------------------------------------------------------
public final List getMessages() {
return Collections.unmodifiableList(messages);
}
public final int indexOf(Message message) {
return messages.indexOf(message);
}
public final void addMessage(Message message) {
if(message != null) {
message.setFolder(this);
}
}
final void internal_addMessage(Message message) {
messages.add(message);
}
public final void removeMessage(Message message) {
if((message != null) && (message.getFolder() == this)) {
message.setFolder(null);
}
}
final void internal_removeMessage(Message message) {
messages.remove(message);
}
//--------------------------------------------------------------------------
public final void visitArchive(ArchiveVisitor visitor) {
if(visitor.isQuitted()) {
return;
}
visitor.visitFolder(this);
if(visitor.isQuitted()) {
return;
}
for(Message message : getMessages()) {
visitor.visitMessage(message);
if(visitor.isQuitted()) {
return;
}
}
for(Folder folder : getChildren()) {
folder.visitArchive(visitor);
if(visitor.isQuitted()) {
return;
}
}
}
public final void visitArchiveDepthFirst(ArchiveVisitor visitor) {
if(visitor.isQuitted()) {
return;
}
for(Folder folder : getChildren()) {
folder.visitArchiveDepthFirst(visitor);
if(visitor.isQuitted()) {
return;
}
}
for(Message message : getMessages()) {
visitor.visitMessage(message);
if(visitor.isQuitted()) {
return;
}
}
visitor.visitFolder(this);
}
//--------------------------------------------------------------------------
@Override
public String toString() {
return String.format(
"Folder%n"
+ "%1$sjdoState: %2$s%n"
+ "%1$soid: %3$s%n"
+ "%1$shash: %4$x%n"
+ "%1$srelativePath: %5$s"
, " "
, JDOHelper.getObjectState(this)
, getOid()
, hashCode()
, getRelativePath());
}
//--------------------------------------------------------------------------
public final Path getRelativePath() {
if(getParent() == null) {
return Paths.get("");
}
else {
return getParent().getRelativePath().resolve(getName());
}
}
public final Path getQualifiedPath() {
if(getParent() == null) {
return Paths.get(getName());
}
else {
return getParent().getQualifiedPath().resolve(getName());
}
}
@Autowired
@NotPersistent
private UserAppConfig userAppConfig;
public final Path getAbsolutePath() {
return userAppConfig.SERVER.getArchiveDir()
.resolve("mail")
.resolve(getQualifiedPath());
}
//--------------------------------------------------------------------------
@Override
public int compareTo(Folder other) {
return priorizeOrder().compareTo(other.priorizeOrder());
}
private String priorizeOrder() {
String path = getRelativePath().toString();
if(isUserHomeFolder()) {
path = " " + path;
}
else if(isInboxSpecialFolder()) {
path = " " + path;
}
else if(isOutboxSpecialFolder()) {
path = " " + path;
}
else if(isDraftsSpecialFolder()) {
path = " " + path;
}
else if(isSentSpecialFolder()) {
path = " " + path;
}
else if(isSpamSpecialFolder()) {
path = " " + path;
}
else if(isTrashSpecialFolder()) {
path = " " + path;
}
return path.toUpperCase();
}
}