/** * 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 AddressListField extends Field { /* * "To", "Cc", "Bcc", "Reply-To", "Resent-To", "Resent-Cc", "Resent-Bcc" */ @NotPersistent private static final Logger log = Logger.getLocalLogger(); //**** P E R S I S T E N T **** private ArrayList addressList = new ArrayList(); //***************************** public final List getAddressList() { return Collections.unmodifiableList(addressList); } public final int indexOf(AddressListField_Address address) { return addressList.indexOf(address); } public final void addAddress(AddressListField_Address address) { if(address != null) { address.setField(this); } } final void internal_addAddress(AddressListField_Address address) { addressList.add(address); } public final void removeAddress(AddressListField_Address address) { if((address != null) && (address.getField() == this)) { address.setField(null); } } final void internal_removeAddress(AddressListField_Address address) { addressList.remove(address); } //-------------------------------------------------------------------------- @Override final void dumpTree(StringBuilder sb, String pad) { int size = addressList.size(); sb.append(toString(pad + ((size > 0) ? "| " : " "))); for(int i = 0; i < size; i++) { sb.append("\n") .append(pad).append("|\n"); if(i < (size - 1)) { sb.append(pad).append("+---"); addressList.get(i).dumpTree(sb, pad + "| "); } else { sb.append(pad).append("+---"); addressList.get(i).dumpTree(sb, pad + " "); } } } @Override final String toString(String pad) { String idx = (getEntity() == null) ? "" : ("[" + String.valueOf(getEntityIdx()) + "]"); return String.format( "AddressListField %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(AddressListField_Address address : getAddressList()) { if(address instanceof AddressListField_Mailbox) { AddressListField_Mailbox mailbox = (AddressListField_Mailbox)address; 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()); } } else if(address instanceof AddressListField_Group) { AddressListField_Group group = (AddressListField_Group)address; SMOutputElement groupElement = doc.addElement("group"); if(convention.useAttributes()) { groupElement.addAttribute("name", group.getName()); } else if(convention.useNestedElements()) { groupElement.addElement("name").addCharacters(group.getName()); } for(AddressListField_Group_Mailbox mailbox : group.getMailboxList()) { SMOutputElement mailboxElement = groupElement.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); return null; } return strWriter.toString(); } }