KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
25
26 import org.w3c.dom.Element JavaDoc;
27 import org.jboss.deployment.DeploymentException;
28
29 /**
30  * Represents one ejb-relation element found in the ejb-jar.xml file's
31  * relationships elements.
32  *
33  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
34  * @author Scott.Stark@jboss.org
35  * @version $Revision: 58337 $
36  */

37 public class RelationMetaData extends MetaData
38 {
39    private String JavaDoc description;
40    private String JavaDoc displayName;
41    /** Name of the relation. Loaded from the ejb-relation-name element. */
42    private String JavaDoc relationName;
43
44    /**
45     * The left relationship role. Loaded from an ejb-relationship-role.
46     * Left/right assignment is completely arbitrary.
47     */

48    private RelationshipRoleMetaData left;
49
50    /**
51     * The right relationship role. Loaded from an ejb-relationship-role.
52     * Left/right assignment is completely arbitrary.
53     */

54    private RelationshipRoleMetaData right;
55
56    public String JavaDoc getDescription()
57    {
58       return description;
59    }
60
61    public void setDescription(String JavaDoc description)
62    {
63       this.description = description;
64    }
65
66    public String JavaDoc getDisplayName()
67    {
68       return displayName;
69    }
70
71    public void setDisplayName(String JavaDoc displayName)
72    {
73       this.displayName = displayName;
74    }
75
76    /**
77     * Gets the relation name. Relation name is loaded from the ejb-relation-name
78     * element.
79     */

80    public String JavaDoc getRelationName()
81    {
82       return relationName;
83    }
84
85    public void setRelationName(String JavaDoc name)
86    {
87       relationName = name;
88       if( relationName != null && relationName.length() == 0 )
89          relationName = null;
90    }
91
92    /**
93     * Gets the left relationship role. The relationship role is loaded from an
94     * ejb-relationship-role. Left/right assignment is completely arbitrary.
95     */

96    public RelationshipRoleMetaData getLeftRelationshipRole()
97    {
98       return left;
99    }
100
101    public void setLeftRelationshipRole(RelationshipRoleMetaData left)
102    {
103       this.left = left;
104    }
105
106    /**
107     * Gets the right relationship role. The relationship role is loaded from an
108     * ejb-relationship-role. Left/right assignment is completely arbitrary.
109     */

110    public RelationshipRoleMetaData getRightRelationshipRole()
111    {
112       return right;
113    }
114
115    public void setRightRelationshipRole(RelationshipRoleMetaData left)
116    {
117       this.right = left;
118    }
119
120    public RelationshipRoleMetaData getOtherRelationshipRole(RelationshipRoleMetaData role)
121    {
122       if (left == role)
123       {
124          return right;
125       }
126       else if (right == role)
127       {
128          return left;
129       }
130       else
131       {
132          throw new IllegalArgumentException JavaDoc("Specified role is not the left "
133                + "or right role. role=" + role);
134       }
135    }
136
137    public void importEjbJarXml(Element JavaDoc element) throws DeploymentException
138    {
139       // name - treating empty values as not specified
140
relationName = getOptionalChildContent(element, "ejb-relation-name");
141       if ("".equals(relationName))
142       {
143          relationName = null;
144       }
145
146       // left role
147
Iterator JavaDoc iter = getChildrenByTagName(element, "ejb-relationship-role");
148       if (iter.hasNext())
149       {
150          left = new RelationshipRoleMetaData(this);
151          left.importEjbJarXml((Element JavaDoc) iter.next());
152       } else
153       {
154          throw new DeploymentException("Expected 2 ejb-relationship-role "
155                + "roles but found none");
156       }
157
158       // right role
159
if (iter.hasNext())
160       {
161          right = new RelationshipRoleMetaData(this);
162          right.importEjbJarXml((Element JavaDoc) iter.next());
163       } else
164       {
165          throw new DeploymentException("Expected 2 ejb-relationship-role "
166                + "but only found one");
167       }
168
169       // assure there are only two ejb-relationship-role elements
170
if (iter.hasNext())
171       {
172          throw new DeploymentException("Expected only 2 ejb-relationship-"
173                + "role but found more then 2");
174       }
175
176       // JBossCMP needs ejb-relation-name if jbosscmp-jdbc.xml is used to map
177
// relationships.
178
if (relationName == null)
179       {
180          // generate unique name, we can't rely on ejb-relationship-role-name
181
// being unique
182
setDefaultRelationName();
183       }
184
185       // assure that the left role and right role do not have the same name
186
String JavaDoc leftName = left.getRelationshipRoleName();
187       String JavaDoc rightName = right.getRelationshipRoleName();
188       if (leftName != null && leftName.equals(rightName))
189       {
190          throw new DeploymentException("ejb-relationship-role-name must be "
191                + "unique in ejb-relation: ejb-relationship-role-name is "
192                + leftName);
193       }
194
195       // verify cascade delete
196
if (left.isCascadeDelete() && right.isMultiplicityMany())
197       {
198          throw new DeploymentException("cascade-delete is only allowed in "
199                + "ejb-relationship-role where the other role has a "
200                + "multiplicity One");
201       }
202       if (right.isCascadeDelete() && left.isMultiplicityMany())
203       {
204          throw new DeploymentException("cascade-delete is only allowed in "
205                + "ejb-relationship-role where the other role has a "
206                + "multiplicity One");
207       }
208    }
209
210    public void setDefaultRelationName()
211    {
212       relationName = left.getEntityName()
213             + (left.getCMRFieldName() == null ? "" : "_"
214                   + left.getCMRFieldName())
215             + "-"
216             + right.getEntityName()
217             + (right.getCMRFieldName() == null ? "" : "_"
218                   + right.getCMRFieldName());
219    }
220 }
221
Popular Tags