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.accesscontrol.attributes;
19   
20   import java.util.HashSet;
21   import java.util.Set;
22   
23   import javax.persistence.CascadeType;
24   import javax.persistence.Column;
25   import javax.persistence.Entity;
26   import javax.persistence.FetchType;
27   import javax.persistence.Id;
28   import javax.persistence.OneToMany;
29   
30   import org.jdom.Element;
31   import org.torweg.pulse.bundle.JDOMable;
32   
33   /**
34    * The base of the {@code AttributeRegistry}.
35    * <p>
36    * TODO: explain
37    * </p>
38    * 
39    * @author Thomas Weber
40    * @version $Revision: 1378 $
41    */
42   @Entity
43   public class AttributeRegistry implements JDOMable {
44   
45       /**
46        * The {@code id} of the {@code AttributeRegistry}.
47        */
48       @Id
49       @Column(unique = true, nullable = false, updatable = false)
50       private final long id = 1L;
51   
52       /**
53        * the root nodes of the {@code AttributeRegistry} tree.
54        */
55       @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
56       private Set<BundleAttribute> rootNodes = new HashSet<BundleAttribute>();
57   
58       /**
59        * builds a new empty {@code AttributeRegistry}.
60        */
61       public AttributeRegistry() {
62           super();
63       }
64   
65       /**
66        * set the set of {@code RegistryBundleNode}s of the
67        * {@code AttributeRegistry}.
68        * 
69        * @param n
70        *            the set of {@code RegistryBundleNode}s
71        */
72       public final void setRootNodes(final Set<BundleAttribute> n) {
73           if (n == null) {
74               throw new NullPointerException(
75                       "The root node set of AttributeRegistry must not be null.");
76           }
77           this.rootNodes = n;
78       }
79   
80       /**
81        * @return returns the root {@code RegistryBundleNodes} of the
82        *         {@code AttributeRegistry}
83        */
84       public final Set<BundleAttribute> getRootNodes() {
85           return this.rootNodes;
86       }
87   
88       /**
89        * adds a new {@code Bundle} to the {@code AttributeRegistry}.
90        * 
91        * @param n
92        *            the {@code Node} of the {@code Bundle} to be added
93        */
94       public final void addBundleNode(final BundleAttribute n) {
95           this.rootNodes.add(n);
96       }
97   
98       /**
99        * get the {@code RegistryBundleNode} for the named {@code Bundle}
100       * .
101       * 
102       * @param bundleName
103       *            the name of the {@code Bundle}
104       * @return the {@code RegistryBundleNode} for the named
105       *         {@code Bundle}, or {@code null}, if no matching
106       *         {@code RegistryBundleNode} could be found in the
107       *         {@code AttributeRegistry}
108       */
109      public final BundleAttribute getBundleNode(final String bundleName) {
110          for (BundleAttribute bundleNode : this.rootNodes) {
111              if (bundleNode.getName().equals(bundleName)) {
112                  return bundleNode;
113              }
114          }
115          return null;
116      }
117  
118      /**
119       * returns the primary key.
120       * 
121       * @return returns the {@code id} of the {@code AttributeRegistry}
122       */
123      public final long getId() {
124          return this.id;
125      }
126  
127      /**
128       * @return the root node and the first child level of the
129       *         {@code AttributeRegistry} as an {@code Element}
130       */
131      public final Element deserializeToJDOM() {
132          Element registry = new Element("AttributeRegistry");
133          for (BundleAttribute bundleNode : this.rootNodes) {
134              registry.addContent(bundleNode.deserializeToJDOM());
135          }
136          return registry;
137      }
138  
139  }
140