/**
* 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.io.StringWriter;
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 javax.xml.stream.XMLStreamException;
import org.codehaus.jettison.AbstractXMLStreamWriter;
import org.codehaus.jettison.badgerfish.BadgerFishXMLStreamWriter;
import org.codehaus.jettison.mapped.MappedNamespaceConvention;
import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
import org.codehaus.staxmate.SMOutputFactory;
import org.codehaus.staxmate.out.SMOutputDocument;
import org.codehaus.staxmate.out.SMOutputElement;
import serpro.mailarchiver.util.JSONMappingConvention;
import serpro.mailarchiver.util.Logger;
@PersistenceCapable
public class MailboxListField
extends Field
{
/*
* "From", "Resent-From"
*/
@NotPersistent
private static final Logger log = Logger.getLocalLogger();
//**** P E R S I S T E N T ****
private ArrayList mailboxList = new ArrayList();
//*****************************
public final List getMailboxList() {
return Collections.unmodifiableList(mailboxList);
}
public final int indexOf(MailboxListField_Mailbox mailbox) {
return mailboxList.indexOf(mailbox);
}
public final void addMailbox(MailboxListField_Mailbox mailbox) {
if(mailbox != null) {
mailbox.setField(this);
}
}
final void internal_addMailbox(MailboxListField_Mailbox mailbox) {
mailboxList.add(mailbox);
}
public final void removeMailbox(MailboxListField_Mailbox mailbox) {
if((mailbox != null) && (mailbox.getField() == this)) {
mailbox.setField(null);
}
}
final void internal_removeMailbox(MailboxListField_Mailbox mailbox) {
mailboxList.remove(mailbox);
}
//--------------------------------------------------------------------------
@Override
final void dumpTree(StringBuilder sb, String pad) {
int size = mailboxList.size();
sb.append(toString(pad + ((size > 0) ? "| " : " ")));
for(int i = 0; i < size; i++) {
sb.append("\n")
.append(pad).append("|\n")
.append(pad).append("+---");
if(i < (size - 1)) {
mailboxList.get(i).dumpTree(sb, pad + "| ");
}
else {
mailboxList.get(i).dumpTree(sb, pad + " ");
}
}
}
@Override
final String toString(String pad) {
String idx = (getEntity() == null) ? "" : ("[" + String.valueOf(getEntityIdx()) + "]");
return String.format(
"MailboxListField %1$s%n"
+ "%2$sjdoState: %3$s%n"
+ "%2$soid: %4$s%n"
+ "%2$shash: %5$x%n"
+ "%2$sname: %6$s%n"
+ "%2$svalid: %7$b"
, idx
, pad
, JDOHelper.getObjectState(this)
, getOid()
, hashCode()
, getName()
, isValid());
}
//--------------------------------------------------------------------------
public final String toJSONString() {
return toJSONString(JSONMappingConvention.Mapped_Attributes);
}
public final String toJSONString(JSONMappingConvention convention) {
StringWriter strWriter = new StringWriter();
try {
AbstractXMLStreamWriter xmlWriter = null;
if(convention.useMapped()) {
MappedNamespaceConvention con = new MappedNamespaceConvention(JSONMappingConvention.mappedConfiguration);
xmlWriter = new MappedXMLStreamWriter(con, strWriter);
}
else if(convention.useBadgerFish()) {
xmlWriter = new BadgerFishXMLStreamWriter(strWriter);
}
SMOutputDocument doc = SMOutputFactory.createOutputDocument(xmlWriter);
for(MailboxListField_Mailbox mailbox : getMailboxList()) {
SMOutputElement mailboxElement = doc.addElement("mailbox");
if(convention.useAttributes()) {
mailboxElement.addAttribute("name", mailbox.getName());
mailboxElement.addAttribute("route", mailbox.getRoute());
mailboxElement.addAttribute("localPart", mailbox.getLocalPart());
mailboxElement.addAttribute("domain", mailbox.getDomain());
}
else if(convention.useNestedElements()) {
mailboxElement.addElement("name").addCharacters(mailbox.getName());
mailboxElement.addElement("route").addCharacters(mailbox.getRoute());
mailboxElement.addElement("localPart").addCharacters(mailbox.getLocalPart());
mailboxElement.addElement("domain").addCharacters(mailbox.getDomain());
}
}
doc.closeRootAndWriter();
}
catch(XMLStreamException ex) {
log.error(ex, ex.getMessage());
return null;
}
return strWriter.toString();
}
}