1    /*
2     * Copyright 2008 :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.admin;
19   
20   import java.util.HashMap;
21   import java.util.HashSet;
22   import java.util.Map;
23   import java.util.Set;
24   import java.util.TimeZone;
25   
26   import org.jdom.Content;
27   import org.jdom.Element;
28   import org.torweg.pulse.bundle.Result;
29   import org.torweg.pulse.site.content.AbstractBasicContent;
30   import org.torweg.pulse.site.content.AbstractBasicVariant;
31   import org.torweg.pulse.site.content.Attachment;
32   
33   /**
34    * the result for the {@code AbstractBasicContentEditor}.
35    * 
36    * @author Daniel Dietz
37    * @version $Revision: 1425 $
38    */
39   public class AbstractBasicContentEditorResult implements Result {
40   
41       /**
42        * the {@code AbstractBasicContent} of the result.
43        */
44       private AbstractBasicContent content;
45   
46       /**
47        * The variant of the result.
48        */
49       private AbstractBasicVariant<?> variant;
50   
51       /**
52        * a map of attributes to be added to the result.
53        */
54       private final Map<String, String> attributes = new HashMap<String, String>();
55   
56       /**
57        * the {@code Content}s to be added to the result.
58        */
59       private final Set<Content> contents = new HashSet<Content>();
60   
61       /**
62        * the mode of the result.
63        * 
64        * <p>
65        * used as flag for xsl:choose:<br/>
66        * <ul type="disc">
67        * <li>mode=1: with ajax.AbstractBasicContent.Attachments.xsl will use js
68        * that builds up file-browser-panel for attachments-editor.</li>
69        * <li>mode=2: with ajax.AbstractBasicContent.Attachments.xsl will use
70        * html/js that builds up attachments-editor for a attachment of a content.</li>
71        * </ul>
72        * </p>
73        */
74       private Integer mode;
75   
76       /**
77        * the attachment of the result.
78        */
79       private Attachment attachment;
80   
81       /**
82        * Flag indicating whether to de-serialise time-zones for the result.
83        */
84       private boolean addTimeZones = false;
85   
86       /**
87        * @return the result as {@code Element}.
88        * 
89        * @see org.torweg.pulse.bundle.JDOMable#deserializeToJDOM()
90        */
91       public Element deserializeToJDOM() {
92           Element result = new Element("result").setAttribute("class", this
93                   .getClass().getCanonicalName());
94   
95           // add content
96           if (this.content != null) {
97               result.addContent(this.content.deserializeToJDOM());
98           }
99   
100          // add variant
101          if (this.variant != null) {
102              result.addContent(this.variant.deserializeToJDOM());
103          }
104  
105          // add attributes
106          for (Map.Entry<String, String> entry : this.attributes.entrySet()) {
107              result.setAttribute(entry.getKey(), entry.getValue());
108          }
109  
110          // add contents
111          for (Content c : this.contents) {
112              result.addContent(c.detach());
113          }
114  
115          // add attachment
116          if (this.attachment != null) {
117              result.addContent(this.attachment.deserializeToJDOM());
118          }
119  
120          // set mode
121          if (this.mode != null) {
122              result.setAttribute("mode", this.mode.toString());
123          }
124  
125          // add time-zones for reference duration editor
126          if (this.addTimeZones) {
127              Element timeZones = new Element("time-zones");
128              for (String tz : TimeZone.getAvailableIDs()) {
129                  timeZones.addContent(new Element("time-zone").setText(tz));
130              }
131              result.addContent(timeZones);
132          }
133  
134          return result;
135      }
136  
137      /**
138       * sets the content of the result.
139       * 
140       * @param c
141       *            the {@code AbstractBasicContent} to be set
142       */
143      public final void setContent(final AbstractBasicContent c) {
144          this.content = c;
145      }
146  
147      /**
148       * sets the variant of the result.
149       * 
150       * @param v
151       *            the {@code AbstractBasicVariant} to be set
152       */
153      public final void setVariant(final AbstractBasicVariant<?> v) {
154          this.variant = v;
155      }
156  
157      /**
158       * adds a attribute to the root-container of this result.
159       * 
160       * @param attributeName
161       *            the name of the attribute
162       * @param attributeValue
163       *            the value of the attribute
164       */
165      public final void addAttribute(final String attributeName,
166              final String attributeValue) {
167          this.attributes.put(attributeName, attributeValue);
168      }
169  
170      /**
171       * adds a {@code Content} of this result.
172       * 
173       * @param jDomContent
174       *            the {@code Content} to add
175       */
176      public final void addContent(final Content jDomContent) {
177          this.contents.add(jDomContent);
178      }
179  
180      /**
181       * sets the mode for the result.
182       * 
183       * @param i
184       *            the mode to set
185       */
186      public final void setMode(final int i) {
187          this.mode = i;
188      }
189  
190      /**
191       * sets the {@code Attachement} for the result.
192       * 
193       * @param att
194       *            the {@code Attachment} to set
195       */
196      public final void setAttachment(final Attachment att) {
197          this.attachment = att;
198      }
199  
200      /**
201       * Flag indicating whether to de-serialise time-zones for the result.
202       */
203      public final void addTimeZones() {
204          this.addTimeZones = true;
205      }
206  
207  }
208