1    /*
2     * Copyright 2006 :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.util.HashMap;
21   import java.util.List;
22   import java.util.Locale;
23   import java.util.Map;
24   
25   import javax.persistence.Basic;
26   import javax.persistence.Column;
27   import javax.persistence.Entity;
28   import javax.xml.bind.annotation.XmlAccessOrder;
29   import javax.xml.bind.annotation.XmlAccessType;
30   import javax.xml.bind.annotation.XmlAccessorOrder;
31   import javax.xml.bind.annotation.XmlAccessorType;
32   import javax.xml.bind.annotation.XmlElement;
33   import javax.xml.bind.annotation.XmlRootElement;
34   import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
35   
36   import net.sf.json.JSONObject;
37   
38   import org.hibernate.LazyInitializationException;
39   import org.jdom.Element;
40   import org.torweg.pulse.service.request.ServiceRequest;
41   import org.torweg.pulse.site.LocaleMatchingException;
42   import org.torweg.pulse.util.entity.Node;
43   import org.torweg.pulse.util.xml.bind.LocaleXmlAdapter;
44   
45   /**
46    * a subdivision of the {@code ContentRegistry} representing a {@code Locale}.
47    * These {@code RegistryLocaleNode}s are used internally for structuring
48    * purposes.
49    * 
50    * @author Daniel Dietz
51    * @version $Revision: 2071 $
52    * 
53    */
54   @Entity
55   @XmlRootElement(name = "registry-locale-node")
56   @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
57   @XmlAccessorType(XmlAccessType.FIELD)
58   public class RegistryLocaleNode extends AbstractRegistryNode {
59   
60       /**
61        * The serialVersionUID.
62        */
63       private static final long serialVersionUID = -239149009194228466L;
64       /**
65        * the {@code Locale}.
66        */
67       @Basic
68       @Column(nullable = false)
69       @XmlElement(name = "locale")
70       @XmlJavaTypeAdapter(value = LocaleXmlAdapter.class)
71       private Locale locale;
72   
73       /**
74        * default constructor for Hibernate<sup>TM</sup>.
75        */
76       @Deprecated
77       protected RegistryLocaleNode() {
78           super();
79       }
80   
81       /**
82        * creates a new {@code RegistryLocaleNode} with the given {@code Locale}.
83        * 
84        * @param l
85        *            the {@code Locale} of the node
86        */
87       public RegistryLocaleNode(final Locale l) {
88           super();
89           setLocale(l);
90           setName(l.toString());
91       }
92   
93       /**
94        * sets the {@code Locale} for the {@code RegistryLocaleNode}.
95        * 
96        * @param l
97        *            the {@code Locale} to be set
98        */
99       public final void setLocale(final Locale l) {
100          if (getParent() instanceof RegistryLocaleNode) {
101              RegistryLocaleNode parent = (RegistryLocaleNode) getParent();
102              if (!parent.getLocale().equals(l)) {
103                  throw new LocaleMatchingException("The locale " + l
104                          + " does not match the parent's locale "
105                          + parent.getLocale());
106              }
107          }
108          this.locale = l;
109      }
110  
111      /**
112       * @return the locale
113       */
114      public final Locale getLocale() {
115          return locale;
116      }
117  
118      /**
119       * adds a child to the children list at the given position.
120       * <p>
121       * If the child is an instance of {@code RegistryLocaleNode}, its {@code
122       * Locale} is checked to equal the {@code RegistryLocaleNode}'s {@code
123       * Locale}.
124       * </p>
125       * 
126       * @param pos
127       *            the position
128       * @param n
129       *            the node
130       * @see Node#addChild(int, Node)
131       */
132      @Override
133      public void addChild(final int pos, final Node n) {
134          if (n instanceof RegistryLocaleNode) {
135              RegistryLocaleNode node = (RegistryLocaleNode) n;
136              if (!node.getLocale().equals(getLocale())) {
137                  throw new LocaleMatchingException("The locale "
138                          + node.getLocale() + " does not match our locale "
139                          + getLocale());
140              }
141          }
142          super.addChild(pos, n);
143      }
144  
145      /**
146       * adds a child to the children list.
147       * <p>
148       * If the child is an instance of {@code RegistryLocaleNode}, its {@code
149       * Locale} is checked to equal the {@code RegistryLocaleNode}'s {@code
150       * Locale}.
151       * </p>
152       * 
153       * @param n
154       *            the node
155       * @see Node#addChild(Node)
156       */
157      @Override
158      public void addChild(final Node n) {
159          if (n instanceof RegistryLocaleNode) {
160              RegistryLocaleNode node = (RegistryLocaleNode) n;
161              if (!node.getLocale().equals(getLocale())) {
162                  throw new LocaleMatchingException("The locale "
163                          + node.getLocale() + " does not match our locale "
164                          + getLocale());
165              }
166          }
167          super.addChild(n);
168      }
169  
170      /**
171       * sets the children list.
172       * <p>
173       * If a child is an instance of {@code RegistryLocaleNode}, its {@code
174       * Locale} is checked to equal the {@code RegistryLocaleNode}'s {@code
175       * Locale}.
176       * </p>
177       * 
178       * @param <E>
179       *            extends {@code Node}
180       * @param nodeList
181       *            the list of children
182       * @see Node#setChildren(List)
183       */
184      @Override
185      public <E extends Node> void setChildren(final List<E> nodeList) {
186          for (Node n : nodeList) {
187              if (n instanceof RegistryLocaleNode) {
188                  RegistryLocaleNode node = (RegistryLocaleNode) n;
189                  if (!node.getLocale().equals(getLocale())) {
190                      throw new LocaleMatchingException("The locale "
191                              + node.getLocale() + " does not match our locale "
192                              + getLocale());
193                  }
194              }
195          }
196          super.setChildren(nodeList);
197      }
198  
199      /**
200       * serializes the {@code RegistryLocaleNode} to JDOM.
201       * 
202       * @param req
203       *            the current request
204       * @return an {@code Element} representing the {@code RegistryBundleNode}.
205       */
206      @Override
207      public Element deserializeToJDOM(final ServiceRequest req) {
208          Element jdom = new Element("RegistryLocaleNode").setAttribute("locale",
209                  getName()).setAttribute("id", getId().toString()).setAttribute(
210                  "bundle", getBundle().getName()).setAttribute("type",
211                  "AbstractRegistryNode").setAttribute("name",
212                  this.locale.toString()).setAttribute("disabledActions",
213                  getDisabledActions()).setAttribute("hasChildren",
214                  ((Boolean) this.hasChildren()).toString());
215          Element actions = new Element("actions");
216          if (req != null) {
217              for (Map.Entry<String, String> entry : getActionURLs(req)
218                      .entrySet()) {
219                  actions.addContent(new Element(entry.getKey()).setText(entry
220                          .getValue()));
221              }
222          }
223          jdom.addContent(actions);
224          try {
225              for (Node n : getChildren()) {
226                  AbstractRegistryNode arn = (AbstractRegistryNode) n;
227                  jdom.addContent(arn.deserializeToJDOM(req));
228              }
229          } catch (LazyInitializationException e) {
230              LOGGER.trace(
231                      "RegistryLocaleNode.LazyInitializationException @ {} : {}",
232                      getId(), getName());
233          }
234  
235          return jdom;
236      }
237  
238      /**
239       * returns {@code MOVE.EDIT.REMOVE:ENABLE_CREATEFOLDER}.
240       * 
241       * @return {@code MOVE.EDIT.REMOVE:ENABLE_CREATEFOLDER}
242       */
243      @Override
244      public String getDisabledActions() {
245          return "MOVE.EDIT.REMOVE:ENABLE_CREATEFOLDER";
246      }
247  
248      /**
249       * returns the {@code createFolder} action URL.
250       * 
251       * @param req
252       *            the current request
253       * @return the {@code createFolder} action URL
254       */
255      @Override
256      public Map<String, String> getActionURLs(final ServiceRequest req) {
257          Map<String, String> actions = new HashMap<String, String>();
258          actions.put("create", getBundle().getViewTypes(this).getCreateView()
259                  .getCommand(req.getCommand().createCopy(false))
260                  .addHttpParameter("contentNodeId", this.getId().toString())
261                  .toCommandURL(req));
262          actions.put("expandEdit", getBundle().getViewTypes(this)
263                  .getExpandEditView().getCommand(
264                          req.getCommand().createCopy(false)).addHttpParameter(
265                          "contentNodeId", this.getId().toString()).toCommandURL(
266                          req));
267          return actions;
268      }
269  
270      /**
271       * /** returns a {@code JSONObject} representation of the {@code
272       * RegistryBundleNode}.
273       * 
274       * @return a {@code JSONObject} representation of the {@code
275       *         RegistryBundleNode}
276       */
277      @Override
278      public JSONObject toJSON() {
279          JSONObject nodeObject = super.toJSON();
280          nodeObject.put("locale", getLocale().toString());
281          nodeObject.put("country", getLocale().getCountry().toLowerCase());
282          nodeObject.put("expandable", true);
283          return nodeObject;
284      }
285  
286  }
287