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