source: contrib/MailArchiver/sources/src/serpro/mailarchiver/domain/metaarchive/MailboxField.java @ 6785

Revision 6785, 5.7 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
30package serpro.mailarchiver.domain.metaarchive;
31
32import java.io.StringWriter;
33
34import javax.jdo.JDOHelper;
35import javax.jdo.annotations.NotPersistent;
36import javax.jdo.annotations.PersistenceCapable;
37import javax.xml.stream.XMLStreamException;
38
39import org.codehaus.jettison.AbstractXMLStreamWriter;
40import org.codehaus.jettison.badgerfish.BadgerFishXMLStreamWriter;
41import org.codehaus.jettison.mapped.MappedNamespaceConvention;
42import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
43import org.codehaus.staxmate.SMOutputFactory;
44import org.codehaus.staxmate.out.SMOutputDocument;
45import org.codehaus.staxmate.out.SMOutputElement;
46
47import serpro.mailarchiver.util.JSONMappingConvention;
48import serpro.mailarchiver.util.Logger;
49
50@PersistenceCapable
51public 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}
Note: See TracBrowser for help on using the repository browser.