KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > RelationshipRoleMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.metadata;
23
24 import org.w3c.dom.Element JavaDoc;
25 import org.jboss.deployment.DeploymentException;
26
27 /**
28  * Represents one ejb-relationship-role element found in the ejb-jar.xml
29  * file's ejb-relation elements.
30  *
31  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
32  * @version $Revision: 58196 $
33  */

34 public class RelationshipRoleMetaData extends MetaData {
35    // one is one
36
private static int ONE = 1;
37    // and two is many :)
38
private static int MANY = 2;
39    
40    /**
41     * Role name
42     */

43    private String JavaDoc relationshipRoleName;
44    
45    /**
46     * The relation to which the role belongs.
47     */

48     private RelationMetaData relationMetaData;
49    
50    /**
51     * Multiplicity of role, ONE or MANY.
52     */

53    private int multiplicity;
54    
55    /**
56     * Should this entity be deleted when related entity is deleted.
57     */

58    private boolean cascadeDelete;
59    
60    /**
61     * Name of the entity that has this role.
62     */

63    private String JavaDoc entityName;
64    
65    /**
66     * Name of the entity's cmr field for this role.
67     */

68    private String JavaDoc cmrFieldName;
69    
70    /**
71     * Type of the cmr field (i.e., collection or set)
72     */

73    private String JavaDoc cmrFieldType;
74
75    public RelationshipRoleMetaData(RelationMetaData relationMetaData) {
76       this.relationMetaData = relationMetaData;
77    }
78    
79    /**
80     * Gets the relationship role name
81     */

82    public String JavaDoc getRelationshipRoleName() {
83       return relationshipRoleName;
84    }
85
86    /**
87     * Gets the relation meta data to which the role belongs.
88     * @returns the relation to which the relationship role belongs
89     */

90    public RelationMetaData getRelationMetaData() {
91       return relationMetaData;
92    }
93    
94    /**
95     * Gets the related role's metadata
96     */

97    public RelationshipRoleMetaData getRelatedRoleMetaData() {
98       return relationMetaData.getOtherRelationshipRole(this);
99    }
100    
101    /**
102     * Checks if the multiplicity is one.
103     */

104    public boolean isMultiplicityOne() {
105       return multiplicity == ONE;
106    }
107    
108    /**
109     * Checks if the multiplicity is many.
110     */

111    public boolean isMultiplicityMany() {
112       return multiplicity == MANY;
113    }
114    
115    /**
116     * Should this entity be deleted when related entity is deleted.
117     */

118    public boolean isCascadeDelete() {
119       return cascadeDelete;
120    }
121    
122    /**
123     * Gets the name of the entity that has this role.
124     */

125    public String JavaDoc getEntityName() {
126       return entityName;
127    }
128    
129    /**
130     * Gets the name of the entity's cmr field for this role.
131     */

132    public String JavaDoc getCMRFieldName() {
133       return cmrFieldName;
134    }
135    
136    /**
137     * Gets the type of the cmr field (i.e., collection or set)
138     */

139    public String JavaDoc getCMRFieldType() {
140       return cmrFieldType;
141    }
142
143    public void setRelationshipRoleName(String JavaDoc relationshipRoleName)
144    {
145       this.relationshipRoleName = relationshipRoleName;
146    }
147
148    public void setMultiplicity(String JavaDoc multiplicity)
149    {
150       if("One".equals(multiplicity))
151       {
152          this.multiplicity = ONE;
153       }
154       else if("Many".equals(multiplicity))
155       {
156          this.multiplicity = MANY;
157       }
158       else
159       {
160          throw new IllegalStateException JavaDoc("multiplicity must be exactaly 'One' " +
161                "or 'Many' but is " + multiplicity + "; this is case " +
162                "sensitive");
163       }
164    }
165
166    public void setCascadeDelete(boolean cascadeDelete)
167    {
168       this.cascadeDelete = cascadeDelete;
169    }
170
171    public void setEntityName(String JavaDoc entityName)
172    {
173       this.entityName = entityName;
174    }
175
176    public void setCmrFieldName(String JavaDoc cmrFieldName)
177    {
178       this.cmrFieldName = cmrFieldName;
179    }
180
181    public void setCmrFieldType(String JavaDoc cmrFieldType)
182    {
183       this.cmrFieldType = cmrFieldType;
184    }
185
186    public void importEjbJarXml (Element JavaDoc element) throws DeploymentException {
187       // ejb-relationship-role-name?
188
relationshipRoleName =
189             getOptionalChildContent(element, "ejb-relationship-role-name");
190
191       // multiplicity
192
String JavaDoc multiplicityString =
193             getUniqueChildContent(element, "multiplicity");
194       if("One".equals(multiplicityString)) {
195          multiplicity = ONE;
196       } else if("Many".equals(multiplicityString)) {
197          multiplicity = MANY;
198       } else {
199          throw new DeploymentException("multiplicity must be exactaly 'One' " +
200                "or 'Many' but is " + multiplicityString + "; this is case " +
201                "sensitive");
202       }
203
204       // cascade-delete?
205
if(getOptionalChild(element, "cascade-delete") != null) {
206          cascadeDelete = true;
207       }
208
209       // relationship-role-source
210
Element JavaDoc relationshipRoleSourceElement =
211             getUniqueChild(element, "relationship-role-source");
212       entityName =
213             getUniqueChildContent(relationshipRoleSourceElement, "ejb-name");
214
215       // cmr-field?
216
Element JavaDoc cmrFieldElement = getOptionalChild(element, "cmr-field");
217       if(cmrFieldElement != null) {
218          // cmr-field-name
219
cmrFieldName =
220                getUniqueChildContent(cmrFieldElement, "cmr-field-name");
221
222          // cmr-field-type?
223
cmrFieldType =
224                getOptionalChildContent(cmrFieldElement, "cmr-field-type");
225          if(cmrFieldType != null &&
226                !cmrFieldType.equals("java.util.Collection") &&
227                !cmrFieldType.equals("java.util.Set")) {
228
229             throw new DeploymentException("cmr-field-type should be " +
230                   "java.util.Collection or java.util.Set but is " +
231                   cmrFieldType);
232          }
233       }
234
235       // JBossCMP needs ejb-relationship-role-name if jbosscmp-jdbc.xml is used to map relationships
236
if(relationshipRoleName == null)
237       {
238          relationshipRoleName = entityName + (cmrFieldName == null ? "" : "_" + cmrFieldName);
239       }
240    }
241 }
242
Popular Tags