1    /*
2     * Copyright 2007 :torweg free software group
3     * 
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.site.content;
19   
20   import java.io.Serializable;
21   import java.io.StringReader;
22   
23   import javax.persistence.Column;
24   import javax.persistence.Entity;
25   import javax.persistence.Lob;
26   import javax.persistence.ManyToOne;
27   import javax.xml.bind.annotation.XmlAccessOrder;
28   import javax.xml.bind.annotation.XmlAccessType;
29   import javax.xml.bind.annotation.XmlAccessorOrder;
30   import javax.xml.bind.annotation.XmlAccessorType;
31   import javax.xml.bind.annotation.XmlElement;
32   import javax.xml.bind.annotation.XmlRootElement;
33   
34   import net.sf.json.JSONObject;
35   
36   import org.jdom.Element;
37   import org.jdom.input.SAXBuilder;
38   import org.jdom.output.Format;
39   import org.jdom.output.XMLOutputter;
40   import org.torweg.pulse.bundle.JDOMable;
41   import org.torweg.pulse.util.INameable;
42   import org.torweg.pulse.util.entity.AbstractNamableEntity;
43   import org.torweg.pulse.vfs.VirtualFile;
44   
45   /**
46    * represents an {@code Attachment} to a {@code Content}, where
47    * {@code Attachments} are entities for downloads.
48    * 
49    * @author Thomas Weber, Daniel Dietz
50    * @version $Revision: 1398 $
51    * 
52    */
53   @Entity
54   @XmlRootElement(name = "attachment")
55   @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
56   @XmlAccessorType(XmlAccessType.FIELD)
57   public class Attachment extends AbstractNamableEntity implements INameable,
58           Serializable, JDOMable {
59   
60       /**
61        * serialVersionUID.
62        */
63       private static final long serialVersionUID = 7032977743892025164L;
64   
65       /**
66        * a description of the {@code Attachment} in deserialised XML.
67        */
68       @Lob
69       @Column(length = 8192, nullable = true)
70       @XmlElement(name = "description")
71       private String description;
72   
73       /**
74        * the {@code VirtualFile} associated with the {@code Attachment}.
75        */
76       @ManyToOne
77       @XmlElement(name = "file")
78       private VirtualFile file;
79   
80       /**
81        * for Hibernate<sup>TM</sup>.
82        */
83       @Deprecated
84       public Attachment() {
85           super();
86       }
87   
88       /**
89        * creates a new {@code Attachment} with the given virtual file and
90        * name.
91        * 
92        * @param vf
93        *            the file
94        * @param n
95        *            the name
96        */
97       public Attachment(final VirtualFile vf, final String n) {
98           super();
99           if (vf == null) {
100              throw new NullPointerException(
101                      "Cannot create an attachment without a file: null");
102          }
103          this.file = vf;
104          if (n != null) {
105              setName(n);
106          }
107      }
108  
109      /**
110       * creates a new {@code Attachment} with the given virtual file, name
111       * and description.
112       * 
113       * @param vf
114       *            the file
115       * @param n
116       *            the name
117       * @param desc
118       *            the description (may be {@code null})
119       */
120      public Attachment(final VirtualFile vf, final String n, final Element desc) {
121          this(vf, n);
122          if (desc != null) {
123              setDescription(desc);
124          }
125      }
126  
127      /**
128       * returns the {@code VirtualFile} of the {@code Attachment}.
129       * 
130       * @return the {@code VirtualFile} of the {@code Attachment}
131       */
132      public final VirtualFile getVirtualFile() {
133          return this.file;
134      }
135  
136      /**
137       * sets the {@code VirtualFile} of the {@code Attachment}.
138       * 
139       * @param f
140       *            the file to be set
141       */
142      public final void setVirtualFile(final VirtualFile f) {
143          this.file = f;
144      }
145  
146      /**
147       * get the description of the {@code Attachment}.
148       * 
149       * @return the description of the {@code Attachment}
150       */
151      public final Element getDescription() {
152          try {
153              return new SAXBuilder().build(new StringReader(description))
154                      .detachRootElement();
155          } catch (Exception e) {
156              return null;
157          }
158      }
159  
160      /**
161       * get the description of the {@code Attachment} as string.
162       * 
163       * @return the description of the {@code Attachment}
164       */
165      public final String getDescriptionText() {
166          return this.description;
167      }
168  
169      /**
170       * sets the description of the {@code Attachment}.
171       * 
172       * @param desc
173       *            the description
174       */
175      public final void setDescription(final Element desc) {
176          XMLOutputter xmlOut = new XMLOutputter();
177          xmlOut.setFormat(Format.getRawFormat());
178          this.description = xmlOut.outputString(desc);
179      }
180  
181      /**
182       * @see JDOMable
183       * @return a JDOM representation of the {@code Attachment}
184       */
185      public final Element deserializeToJDOM() {
186          Element attachment = new Element("Attachment").setAttribute("class",
187                  this.getClass().getCanonicalName()).setAttribute("id",
188                  getId().toString());
189          attachment.addContent(new Element("name").setText(getName()));
190          if (this.description != null) {
191              attachment.addContent(new Element("description")
192                      .addContent(getDescription()));
193          }
194          attachment.addContent(this.file.deserializeToJDOM());
195          return attachment;
196      }
197  
198      /**
199       * returns a JSON-representation of the {@code Attachment}.
200       * 
201       * @return a JSON-representation of the {@code Attachment}
202       */
203      public final JSONObject toJSON() {
204          JSONObject att = new JSONObject();
205          att.put("id", getId());
206          att.put("clazz", getClass().getCanonicalName());
207          att.put("text", getName());
208          att.put("uiProvider", "AttachmentTreeNodeUI");
209          att.put("leaf", "true");
210          att.put("hasChildren", "false");
211          att.put("expandable", "false");
212          att.put("file", this.file.toJSON());
213          return att;
214      }
215  }
216