[6785] | 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 | |
---|
| 30 | package serpro.mailarchiver.domain.metaarchive; |
---|
| 31 | |
---|
| 32 | import java.io.StringWriter; |
---|
| 33 | import java.util.ArrayList; |
---|
| 34 | import java.util.Collections; |
---|
| 35 | import java.util.List; |
---|
| 36 | |
---|
| 37 | import javax.jdo.JDOHelper; |
---|
| 38 | import javax.jdo.annotations.NotPersistent; |
---|
| 39 | import javax.jdo.annotations.PersistenceCapable; |
---|
| 40 | import javax.xml.stream.XMLStreamException; |
---|
| 41 | |
---|
| 42 | import org.codehaus.jettison.AbstractXMLStreamWriter; |
---|
| 43 | import org.codehaus.jettison.badgerfish.BadgerFishXMLStreamWriter; |
---|
| 44 | import org.codehaus.jettison.mapped.MappedNamespaceConvention; |
---|
| 45 | import org.codehaus.jettison.mapped.MappedXMLStreamWriter; |
---|
| 46 | import org.codehaus.staxmate.SMOutputFactory; |
---|
| 47 | import org.codehaus.staxmate.out.SMOutputDocument; |
---|
| 48 | import org.codehaus.staxmate.out.SMOutputElement; |
---|
| 49 | |
---|
| 50 | import serpro.mailarchiver.util.JSONMappingConvention; |
---|
| 51 | import serpro.mailarchiver.util.Logger; |
---|
| 52 | |
---|
| 53 | @PersistenceCapable |
---|
| 54 | public class MailboxListField |
---|
| 55 | extends Field |
---|
| 56 | { |
---|
| 57 | /* |
---|
| 58 | * "From", "Resent-From" |
---|
| 59 | */ |
---|
| 60 | |
---|
| 61 | @NotPersistent |
---|
| 62 | private static final Logger log = Logger.getLocalLogger(); |
---|
| 63 | |
---|
| 64 | //**** P E R S I S T E N T **** |
---|
| 65 | private ArrayList<MailboxListField_Mailbox> mailboxList = new ArrayList<MailboxListField_Mailbox>(); |
---|
| 66 | //***************************** |
---|
| 67 | |
---|
| 68 | public final List<MailboxListField_Mailbox> getMailboxList() { |
---|
| 69 | return Collections.unmodifiableList(mailboxList); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | public final int indexOf(MailboxListField_Mailbox mailbox) { |
---|
| 73 | return mailboxList.indexOf(mailbox); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | public final void addMailbox(MailboxListField_Mailbox mailbox) { |
---|
| 77 | if(mailbox != null) { |
---|
| 78 | mailbox.setField(this); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | final void internal_addMailbox(MailboxListField_Mailbox mailbox) { |
---|
| 83 | mailboxList.add(mailbox); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | public final void removeMailbox(MailboxListField_Mailbox mailbox) { |
---|
| 87 | if((mailbox != null) && (mailbox.getField() == this)) { |
---|
| 88 | mailbox.setField(null); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | final void internal_removeMailbox(MailboxListField_Mailbox mailbox) { |
---|
| 93 | mailboxList.remove(mailbox); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | //-------------------------------------------------------------------------- |
---|
| 97 | @Override |
---|
| 98 | final void dumpTree(StringBuilder sb, String pad) { |
---|
| 99 | int size = mailboxList.size(); |
---|
| 100 | sb.append(toString(pad + ((size > 0) ? "| " : " "))); |
---|
| 101 | for(int i = 0; i < size; i++) { |
---|
| 102 | sb.append("\n") |
---|
| 103 | .append(pad).append("|\n") |
---|
| 104 | .append(pad).append("+---"); |
---|
| 105 | if(i < (size - 1)) { |
---|
| 106 | mailboxList.get(i).dumpTree(sb, pad + "| "); |
---|
| 107 | } |
---|
| 108 | else { |
---|
| 109 | mailboxList.get(i).dumpTree(sb, pad + " "); |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | @Override |
---|
| 115 | final String toString(String pad) { |
---|
| 116 | String idx = (getEntity() == null) ? "" : ("[" + String.valueOf(getEntityIdx()) + "]"); |
---|
| 117 | return String.format( |
---|
| 118 | "MailboxListField %1$s%n" |
---|
| 119 | + "%2$sjdoState: %3$s%n" |
---|
| 120 | + "%2$soid: %4$s%n" |
---|
| 121 | + "%2$shash: %5$x%n" |
---|
| 122 | + "%2$sname: %6$s%n" |
---|
| 123 | + "%2$svalid: %7$b" |
---|
| 124 | , idx |
---|
| 125 | , pad |
---|
| 126 | , JDOHelper.getObjectState(this) |
---|
| 127 | , getOid() |
---|
| 128 | , hashCode() |
---|
| 129 | , getName() |
---|
| 130 | , isValid()); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | //-------------------------------------------------------------------------- |
---|
| 134 | public final String toJSONString() { |
---|
| 135 | return toJSONString(JSONMappingConvention.Mapped_Attributes); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | public final String toJSONString(JSONMappingConvention convention) { |
---|
| 139 | StringWriter strWriter = new StringWriter(); |
---|
| 140 | |
---|
| 141 | try { |
---|
| 142 | AbstractXMLStreamWriter xmlWriter = null; |
---|
| 143 | if(convention.useMapped()) { |
---|
| 144 | MappedNamespaceConvention con = new MappedNamespaceConvention(JSONMappingConvention.mappedConfiguration); |
---|
| 145 | xmlWriter = new MappedXMLStreamWriter(con, strWriter); |
---|
| 146 | } |
---|
| 147 | else if(convention.useBadgerFish()) { |
---|
| 148 | xmlWriter = new BadgerFishXMLStreamWriter(strWriter); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | SMOutputDocument doc = SMOutputFactory.createOutputDocument(xmlWriter); |
---|
| 152 | |
---|
| 153 | for(MailboxListField_Mailbox mailbox : getMailboxList()) { |
---|
| 154 | SMOutputElement mailboxElement = doc.addElement("mailbox"); |
---|
| 155 | |
---|
| 156 | if(convention.useAttributes()) { |
---|
| 157 | mailboxElement.addAttribute("name", mailbox.getName()); |
---|
| 158 | mailboxElement.addAttribute("route", mailbox.getRoute()); |
---|
| 159 | mailboxElement.addAttribute("localPart", mailbox.getLocalPart()); |
---|
| 160 | mailboxElement.addAttribute("domain", mailbox.getDomain()); |
---|
| 161 | } |
---|
| 162 | else if(convention.useNestedElements()) { |
---|
| 163 | mailboxElement.addElement("name").addCharacters(mailbox.getName()); |
---|
| 164 | mailboxElement.addElement("route").addCharacters(mailbox.getRoute()); |
---|
| 165 | mailboxElement.addElement("localPart").addCharacters(mailbox.getLocalPart()); |
---|
| 166 | mailboxElement.addElement("domain").addCharacters(mailbox.getDomain()); |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | doc.closeRootAndWriter(); |
---|
| 170 | } |
---|
| 171 | catch(XMLStreamException ex) { |
---|
| 172 | log.error(ex, ex.getMessage()); |
---|
| 173 | return null; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | return strWriter.toString(); |
---|
| 177 | } |
---|
| 178 | } |
---|