001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.metadata;
023:
024: import java.util.Iterator;
025:
026: import org.w3c.dom.Element;
027: import org.jboss.deployment.DeploymentException;
028:
029: /**
030: * Represents one ejb-relation element found in the ejb-jar.xml
031: * file's relationships elements.
032: *
033: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
034: * @version $Revision: 57209 $
035: */
036: public class RelationMetaData extends MetaData {
037: /** Name of the relation. Loaded from the ejb-relation-name element. */
038: private String relationName;
039:
040: /**
041: * The left relationship role. Loaded from an ejb-relationship-role.
042: * Left/right assignment is completely arbitrary.
043: */
044: private RelationshipRoleMetaData left;
045:
046: /**
047: * The right relationship role. Loaded from an ejb-relationship-role.
048: * Left/right assignment is completely arbitrary.
049: */
050: private RelationshipRoleMetaData right;
051:
052: /**
053: * Gets the relation name.
054: * Relation name is loaded from the ejb-relation-name element.
055: */
056: public String getRelationName() {
057: return relationName;
058: }
059:
060: /**
061: * Gets the left relationship role.
062: * The relationship role is loaded from an ejb-relationship-role.
063: * Left/right assignment is completely arbitrary.
064: */
065: public RelationshipRoleMetaData getLeftRelationshipRole() {
066: return left;
067: }
068:
069: /**
070: * Gets the right relationship role.
071: * The relationship role is loaded from an ejb-relationship-role.
072: * Left/right assignment is completely arbitrary.
073: */
074: public RelationshipRoleMetaData getRightRelationshipRole() {
075: return right;
076: }
077:
078: public RelationshipRoleMetaData getOtherRelationshipRole(
079: RelationshipRoleMetaData role) {
080:
081: if (left == role) {
082: return right;
083: } else if (right == role) {
084: return left;
085: } else {
086: throw new IllegalArgumentException(
087: "Specified role is not the left "
088: + "or right role. role=" + role);
089: }
090: }
091:
092: public void importEjbJarXml(Element element)
093: throws DeploymentException {
094: // name - treating empty values as not specified
095: relationName = getOptionalChildContent(element,
096: "ejb-relation-name");
097: if ("".equals(relationName)) {
098: relationName = null;
099: }
100:
101: // left role
102: Iterator iter = getChildrenByTagName(element,
103: "ejb-relationship-role");
104: if (iter.hasNext()) {
105: left = new RelationshipRoleMetaData(this );
106: left.importEjbJarXml((Element) iter.next());
107: } else {
108: throw new DeploymentException(
109: "Expected 2 ejb-relationship-role "
110: + "roles but found none");
111: }
112:
113: // right role
114: if (iter.hasNext()) {
115: right = new RelationshipRoleMetaData(this );
116: right.importEjbJarXml((Element) iter.next());
117: } else {
118: throw new DeploymentException(
119: "Expected 2 ejb-relationship-role "
120: + "but only found one");
121: }
122:
123: // assure there are only two ejb-relationship-role elements
124: if (iter.hasNext()) {
125: throw new DeploymentException(
126: "Expected only 2 ejb-relationship-"
127: + "role but found more then 2");
128: }
129:
130: // JBossCMP needs ejb-relation-name if jbosscmp-jdbc.xml is used to map relationships.
131: if (relationName == null) {
132: // generate unique name, we can't rely on ejb-relationship-role-name being unique
133: relationName = left.getEntityName()
134: + (left.getCMRFieldName() == null ? "" : "_"
135: + left.getCMRFieldName())
136: + "-"
137: + right.getEntityName()
138: + (right.getCMRFieldName() == null ? "" : "_"
139: + right.getCMRFieldName());
140: }
141:
142: // assure that the left role and right role do not have the same name
143: String leftName = left.getRelationshipRoleName();
144: String rightName = right.getRelationshipRoleName();
145: if (leftName != null && leftName.equals(rightName)) {
146: throw new DeploymentException(
147: "ejb-relationship-role-name must be "
148: + "unique in ejb-relation: ejb-relationship-role-name is "
149: + leftName);
150: }
151:
152: // verify cascade delete
153: if (left.isCascadeDelete() && right.isMultiplicityMany()) {
154: throw new DeploymentException(
155: "cascade-delete is only allowed in "
156: + "ejb-relationship-role where the other role has a "
157: + "multiplicity One");
158: }
159: if (right.isCascadeDelete() && left.isMultiplicityMany()) {
160: throw new DeploymentException(
161: "cascade-delete is only allowed in "
162: + "ejb-relationship-role where the other role has a "
163: + "multiplicity One");
164: }
165: }
166: }
|