View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.web.jcr.rest.client.domain;
25  
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  import java.util.Properties;
30  
31  import net.jcip.annotations.Immutable;
32  
33  import org.modeshape.common.util.HashCode;
34  import org.modeshape.web.jcr.rest.client.RestClientI18n;
35  
36  /**
37   * The NodeType class is the business object for a ModeShape supported node type.
38   */
39  @Immutable
40  public class NodeType implements IModeShapeObject {
41  
42      // ===========================================================================================================================
43      // Fields
44      // ===========================================================================================================================
45  
46      /**
47       * The node type name.
48       */
49      private final String name;
50  
51      /**
52       * The workspace where this node type resides.
53       */
54      private final Workspace workspace;
55      
56      private Properties properties = null;
57      
58      private NodeType parentNodeType = null;
59      
60      private List<NodeType> childrenNodeType = null;
61      
62      private List<NodeType> propertyDefinitons = null;
63      
64      private List<NodeType> childNodeDefinitons = null;
65  
66      // ===========================================================================================================================
67      // Constructors
68      // ===========================================================================================================================
69  
70      /**
71       * Constructs a new <code>NodeType</code>.
72       * 
73       * @param name the node type name (never <code>null</code>)
74       * @param workspace the workspace where this node type resides (never <code>null</code>)
75       * @param properties which are the attributes defined for this node type (<code>nullable</code>)
76       * @throws IllegalArgumentException if the name or workspace argument is <code>null</code>
77       */
78      public NodeType( String name,
79      		Workspace workspace,
80      		Properties properties) {
81      	assert name != null;
82      	assert workspace != null;
83       	this.name = name;
84          this.workspace = workspace;
85          this.properties = (properties == null ? new Properties() : properties);  
86  
87       }
88  
89      // ===========================================================================================================================
90      // Methods
91      // ===========================================================================================================================
92  
93      /**
94       * {@inheritDoc}
95       * 
96       * @see java.lang.Object#equals(java.lang.Object)
97       */
98      @Override
99      public boolean equals( Object obj ) {
100         if (this == obj) return true;
101         if ((obj == null) || (getClass() != obj.getClass())) return false;
102 
103         NodeType otherNodeType = (NodeType)obj;
104         return (this.name.equals(otherNodeType.name) && this.workspace.equals(otherNodeType.workspace));
105     }
106 
107     /**
108      * {@inheritDoc}
109      * 
110      * @see org.modeshape.web.jcr.rest.client.domain.IModeShapeObject#getName()
111      */
112     public String getName() {
113         return this.name;
114     }
115 
116     /**
117      * @return the server where this workspace is located (never <code>null</code>)
118      */
119     public Workspace getWorkspace() {
120         return this.workspace;
121     }
122     
123     /**
124      * @return the node type attributes as a property set.
125      */
126     public Properties getProperties() {
127     	return this.properties;
128     }
129     
130     public void setProperties(Properties properties ){
131     	this.properties = properties;
132     }
133     
134     public String getProperty(String key) {
135     	return this.properties.getProperty(key);
136     }
137     
138     @SuppressWarnings("unchecked")
139 	public List<NodeType> getChildren() {
140     	return (List<NodeType>) (this.childrenNodeType != null ? this.childrenNodeType : Collections.emptyList());
141     }
142     
143     public void addChildNodeType(NodeType childNodeType) {
144 		if (this.childrenNodeType == null) this.childrenNodeType = new ArrayList<NodeType>();
145 		this.childrenNodeType.add(childNodeType);
146     	
147 		childNodeType.setParentNodeType(this);
148     }
149     
150     public void addPropertyDefinitionNodeType(NodeType propertyDefinitionNodeType) {
151 		if (this.propertyDefinitons == null) this.propertyDefinitons = new ArrayList<NodeType>();
152 		propertyDefinitons.add(propertyDefinitionNodeType);
153 
154 		propertyDefinitionNodeType.setParentNodeType(this);
155     }
156     
157     public void addChildNodeDefinitionNodeType(NodeType childNodeDefinitionNodeType) {
158 		if (this.childNodeDefinitons == null) this.childNodeDefinitons = new ArrayList<NodeType>();
159 		childNodeDefinitons.add(childNodeDefinitionNodeType);
160 
161 		childNodeDefinitionNodeType.setParentNodeType(this);
162     }
163     
164     @SuppressWarnings("unchecked")
165 	public List<NodeType> getPropertyDefinitions() {
166     	return (List<NodeType>) (this.propertyDefinitons != null ? this.propertyDefinitons : Collections.emptyList());
167     }
168     
169     @SuppressWarnings("unchecked")
170 	public List<NodeType> getChildNodeDefinitions() {
171     	return (List<NodeType>) (this.childNodeDefinitons != null ? this.childNodeDefinitons : Collections.emptyList());
172     }
173     
174     public NodeType getParentNodeType() {
175     	return this.parentNodeType;
176     }
177     
178     public void setParentNodeType(NodeType parent) {
179     	this.parentNodeType = parent;
180     }
181 
182 
183     /**
184      * {@inheritDoc}
185      * 
186      * @see org.modeshape.web.jcr.rest.client.domain.IModeShapeObject#getShortDescription()
187      */
188     public String getShortDescription() {
189         return RestClientI18n.nodeTypeShortDescription.text(this.name, this.workspace.getServer());
190     }
191 
192     /**
193      * {@inheritDoc}
194      * 
195      * @see java.lang.Object#hashCode()
196      */
197     @Override
198     public int hashCode() {
199         return HashCode.compute(this.name, this.workspace);
200     }
201 
202     /**
203      * {@inheritDoc}
204      * 
205      * @see java.lang.Object#toString()
206      */
207     @Override
208     public String toString() {
209         return getShortDescription();
210     }
211     
212 }