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.map;
19   
20   import java.util.Locale;
21   
22   import javax.persistence.Basic;
23   import javax.persistence.CascadeType;
24   import javax.persistence.Column;
25   import javax.persistence.Entity;
26   import javax.persistence.FetchType;
27   import javax.persistence.GeneratedValue;
28   import javax.persistence.Id;
29   import javax.persistence.OneToOne;
30   
31   import org.hibernate.annotations.Index;
32   import org.jdom.Element;
33   import org.torweg.pulse.bundle.JDOMable;
34   
35   /**
36    * The base of the site's table of contents.
37    * 
38    * @author Thomas Weber
39    * @version $Revision: 1404 $
40    * @see SitemapNode
41    */
42   @Entity
43   public class Sitemap implements JDOMable {
44   
45       /**
46        * The {@code id} of the {@code Sitemap}.
47        */
48       @Id
49       @GeneratedValue
50       @Column(unique = true, nullable = false, updatable = false)
51       private long id;
52   
53       /**
54        * The {@code Locale} of the {@code Sitemap}.
55        */
56       @Basic
57       @Index(name = "idx_locale")
58       @Column(nullable = false, unique = true)
59       private Locale locale;
60   
61       /**
62        * the root of the {@code Sitemap} tree.
63        */
64       @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
65       // @Column(nullable = false, unique = true)
66       private SitemapNode rootNode;
67   
68       /**
69        * no-args constructor for Hibernate<sup>TM</sup>.
70        */
71       @Deprecated
72       protected Sitemap() {
73           super();
74       }
75   
76       /**
77        * builds a new {@code Sitemap} for the given {@code Locale} with
78        * the given {@code SitemapNode} as the root node.
79        * 
80        * @param l
81        *            the {@code Locale} of the {@code Sitemap}
82        * @param root
83        *            the root {@code SitemapNode} of the {@code Sitemap}
84        */
85       public Sitemap(final Locale l, final SitemapNode root) {
86           if (!root.getLocale().equals(l)) {
87               throw new IllegalArgumentException(
88                       "The Locale of the root node must"
89                               + " match the Locale of the Sitemap.");
90           }
91           this.locale = l;
92           this.rootNode = root;
93       }
94   
95       /**
96        * set the root {@code SitemapNode} of the {@code Sitemap}.
97        * 
98        * @param n
99        *            the root {@code SitemapNode}
100       */
101      public final void setRootNode(final SitemapNode n) {
102          if (n == null) {
103              throw new NullPointerException(
104                      "The root node of Sitemap must not be null.");
105          }
106          this.rootNode = n;
107      }
108  
109      /**
110       * @return returns the root {@code SitemapNode} of the
111       *         {@code Sitemap}
112       */
113      public final SitemapNode getRootNode() {
114          return this.rootNode;
115      }
116  
117      /**
118       * set the {@code Locale} of the {@code Sitemap}.
119       * 
120       * @param l
121       *            the {@code Locale}
122       */
123      public final void setLocale(final Locale l) {
124          if (l == null) {
125              throw new NullPointerException(
126                      "The Locale of Sitemap must not be null.");
127          }
128          this.locale = l;
129      }
130  
131      /**
132       * @return returns the {@code Locale} of the {@code Sitemap}
133       */
134      public final Locale getLocale() {
135          return this.locale;
136      }
137  
138      /**
139       * returns the primary key.
140       * 
141       * @return returns the {@code id} of the {@code Sitemap}
142       */
143      public final long getId() {
144          return this.id;
145      }
146  
147      /**
148       * @return the root node and the first child level of the
149       *         {@code Sitemap} as an {@code Element}
150       */
151      public final Element deserializeToJDOM() {
152          Element sitemap = new Element("Sitemap");
153          sitemap.setAttribute("locale", this.locale.toString());
154          sitemap.addContent(this.rootNode.deserializeToJDOM());
155          return sitemap;
156      }
157  
158  }
159