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 AddressListField |
---|
55 | extends Field |
---|
56 | { |
---|
57 | /* |
---|
58 | * "To", "Cc", "Bcc", "Reply-To", "Resent-To", "Resent-Cc", "Resent-Bcc" |
---|
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<AddressListField_Address> addressList = new ArrayList<AddressListField_Address>(); |
---|
66 | //***************************** |
---|
67 | |
---|
68 | public final List<AddressListField_Address> getAddressList() { |
---|
69 | return Collections.unmodifiableList(addressList); |
---|
70 | } |
---|
71 | |
---|
72 | public final int indexOf(AddressListField_Address address) { |
---|
73 | return addressList.indexOf(address); |
---|
74 | } |
---|
75 | |
---|
76 | public final void addAddress(AddressListField_Address address) { |
---|
77 | if(address != null) { |
---|
78 | address.setField(this); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | final void internal_addAddress(AddressListField_Address address) { |
---|
83 | addressList.add(address); |
---|
84 | } |
---|
85 | |
---|
86 | public final void removeAddress(AddressListField_Address address) { |
---|
87 | if((address != null) && (address.getField() == this)) { |
---|
88 | address.setField(null); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | final void internal_removeAddress(AddressListField_Address address) { |
---|
93 | addressList.remove(address); |
---|
94 | } |
---|
95 | |
---|
96 | //-------------------------------------------------------------------------- |
---|
97 | @Override |
---|
98 | final void dumpTree(StringBuilder sb, String pad) { |
---|
99 | int size = addressList.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 | if(i < (size - 1)) { |
---|
105 | sb.append(pad).append("+---"); |
---|
106 | addressList.get(i).dumpTree(sb, pad + "| "); |
---|
107 | } |
---|
108 | else { |
---|
109 | sb.append(pad).append("+---"); |
---|
110 | addressList.get(i).dumpTree(sb, pad + " "); |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | @Override |
---|
116 | final String toString(String pad) { |
---|
117 | String idx = (getEntity() == null) ? "" : ("[" + String.valueOf(getEntityIdx()) + "]"); |
---|
118 | return String.format( |
---|
119 | "AddressListField %1$s%n" |
---|
120 | + "%2$sjdoState: %3$s%n" |
---|
121 | + "%2$soid: %4$s%n" |
---|
122 | + "%2$shash: %5$x%n" |
---|
123 | + "%2$sname: %6$s%n" |
---|
124 | + "%2$svalid: %7$b" |
---|
125 | , idx |
---|
126 | , pad |
---|
127 | , JDOHelper.getObjectState(this) |
---|
128 | , getOid() |
---|
129 | , hashCode() |
---|
130 | , getName() |
---|
131 | , isValid()); |
---|
132 | } |
---|
133 | |
---|
134 | //-------------------------------------------------------------------------- |
---|
135 | public final String toJSONString() { |
---|
136 | return toJSONString(JSONMappingConvention.Mapped_Attributes); |
---|
137 | } |
---|
138 | |
---|
139 | public final String toJSONString(JSONMappingConvention convention) { |
---|
140 | StringWriter strWriter = new StringWriter(); |
---|
141 | |
---|
142 | try { |
---|
143 | AbstractXMLStreamWriter xmlWriter = null; |
---|
144 | if(convention.useMapped()) { |
---|
145 | MappedNamespaceConvention con = new MappedNamespaceConvention(JSONMappingConvention.mappedConfiguration); |
---|
146 | xmlWriter = new MappedXMLStreamWriter(con, strWriter); |
---|
147 | } |
---|
148 | else if(convention.useBadgerFish()) { |
---|
149 | xmlWriter = new BadgerFishXMLStreamWriter(strWriter); |
---|
150 | } |
---|
151 | |
---|
152 | SMOutputDocument doc = SMOutputFactory.createOutputDocument(xmlWriter); |
---|
153 | |
---|
154 | for(AddressListField_Address address : getAddressList()) { |
---|
155 | |
---|
156 | if(address instanceof AddressListField_Mailbox) { |
---|
157 | AddressListField_Mailbox mailbox = (AddressListField_Mailbox)address; |
---|
158 | SMOutputElement mailboxElement = doc.addElement("mailbox"); |
---|
159 | |
---|
160 | if(convention.useAttributes()) { |
---|
161 | mailboxElement.addAttribute("name", mailbox.getName()); |
---|
162 | mailboxElement.addAttribute("route", mailbox.getRoute()); |
---|
163 | mailboxElement.addAttribute("localPart", mailbox.getLocalPart()); |
---|
164 | mailboxElement.addAttribute("domain", mailbox.getDomain()); |
---|
165 | } |
---|
166 | else if(convention.useNestedElements()) { |
---|
167 | mailboxElement.addElement("name").addCharacters(mailbox.getName()); |
---|
168 | mailboxElement.addElement("route").addCharacters(mailbox.getRoute()); |
---|
169 | mailboxElement.addElement("localPart").addCharacters(mailbox.getLocalPart()); |
---|
170 | mailboxElement.addElement("domain").addCharacters(mailbox.getDomain()); |
---|
171 | } |
---|
172 | } |
---|
173 | else if(address instanceof AddressListField_Group) { |
---|
174 | AddressListField_Group group = (AddressListField_Group)address; |
---|
175 | SMOutputElement groupElement = doc.addElement("group"); |
---|
176 | |
---|
177 | if(convention.useAttributes()) { |
---|
178 | groupElement.addAttribute("name", group.getName()); |
---|
179 | } |
---|
180 | else if(convention.useNestedElements()) { |
---|
181 | groupElement.addElement("name").addCharacters(group.getName()); |
---|
182 | } |
---|
183 | |
---|
184 | for(AddressListField_Group_Mailbox mailbox : group.getMailboxList()) { |
---|
185 | SMOutputElement mailboxElement = groupElement.addElement("mailbox"); |
---|
186 | |
---|
187 | if(convention.useAttributes()) { |
---|
188 | mailboxElement.addAttribute("name", mailbox.getName()); |
---|
189 | mailboxElement.addAttribute("route", mailbox.getRoute()); |
---|
190 | mailboxElement.addAttribute("localPart", mailbox.getLocalPart()); |
---|
191 | mailboxElement.addAttribute("domain", mailbox.getDomain()); |
---|
192 | } |
---|
193 | else if(convention.useNestedElements()) { |
---|
194 | mailboxElement.addElement("name").addCharacters(mailbox.getName()); |
---|
195 | mailboxElement.addElement("route").addCharacters(mailbox.getRoute()); |
---|
196 | mailboxElement.addElement("localPart").addCharacters(mailbox.getLocalPart()); |
---|
197 | mailboxElement.addElement("domain").addCharacters(mailbox.getDomain()); |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | doc.closeRootAndWriter(); |
---|
203 | } |
---|
204 | catch(XMLStreamException ex) { |
---|
205 | log.error(ex); |
---|
206 | return null; |
---|
207 | } |
---|
208 | |
---|
209 | return strWriter.toString(); |
---|
210 | } |
---|
211 | } |
---|