KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > deployment > api > EjbRelationDesc


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EjbRelationDesc.java,v 1.23 2004/09/27 14:50:09 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas_ejb.deployment.api;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.objectweb.jonas_ejb.deployment.xml.EjbRelation;
31 import org.objectweb.jonas_ejb.deployment.xml.EjbRelationshipRole;
32 import org.objectweb.jonas_ejb.deployment.xml.JonasEjbRelation;
33 import org.objectweb.jonas_ejb.deployment.xml.JonasEjbRelationshipRole;
34 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
35 import org.objectweb.util.monolog.api.Logger;
36
37 /**
38  * Class to hold meta-information related to an ejb-relation.
39  * @author Christophe Ney [cney@batisseurs.com] : Initial developer
40  * @author Helene Joanin on May 2003: code cleanup
41  * @author Helene Joanin on May 2003: complement for legacy first version
42  * @author Ph Durieux (may 2004): default names for relations and roles.
43  */

44
45 public class EjbRelationDesc {
46
47     private Logger logger = null;
48     private String JavaDoc name; // name of the relation
49
private String JavaDoc name1; // name of the relation role 1
50
private String JavaDoc name2; // name of the relation role 2
51
private EjbRelationshipRoleDesc relationshipRoleDesc1;
52     private EjbRelationshipRoleDesc relationshipRoleDesc2;
53     private String JavaDoc jdbcTableName = null;
54
55
56     // Zeus objects for the associated mapping information (needed in fillMappingInfo);
57
private EjbRelationshipRole role1;
58     private EjbRelationshipRole role2;
59     JonasEjbRelation jRel = null;
60     JonasEjbRelationshipRole jRsRole1 = null;
61     JonasEjbRelationshipRole jRsRole2 = null;
62
63
64     /**
65      * constructor to be used by parent node.
66      * @param er The described object EjbRelation
67      * @param logger The logger
68      * @throws DeploymentDescException thrown in error case.
69      */

70     public EjbRelationDesc(EjbRelation er, Logger logger) throws DeploymentDescException {
71
72         this.logger = logger;
73
74         role1 = er.getEjbRelationshipRole();
75         role2 = er.getEjbRelationshipRole2();
76
77         String JavaDoc cmr1 = "";
78         if (role1.getCmrField() != null) {
79             cmr1 = role1.getCmrField().getCmrFieldName();
80         }
81         String JavaDoc cmr2 = "";
82         if (role2.getCmrField() != null) {
83             cmr2 = role2.getCmrField().getCmrFieldName();
84         }
85
86         // first role. A name is mandatory.
87
// If not set: choose the cmr name of the opposite role, if it exists.
88
name1 = role1.getEjbRelationshipRoleName();
89         if (name1 == null || name1.length() == 0) {
90             if (role2.getCmrField() != null) {
91                 name1 = cmr2;
92             } else {
93                 name1 = "role1"; // a default value
94
}
95             // We have changed the name, keep the new one.
96
role1.setEjbRelationshipRoleName(name1);
97         }
98
99         // second role. A name is mandatory.
100
// If not set: choose the cmr name of the opposite role, if it exists.
101
name2 = role2.getEjbRelationshipRoleName();
102         if (name2 == null || name2.length() == 0) {
103             if (role1.getCmrField() != null) {
104                 name2 = cmr1;
105             } else {
106                 name2 = "role2"; // a default value
107
}
108             // We have changed the name, keep the new one.
109
role2.setEjbRelationshipRoleName(name2);
110         }
111
112         // the two roles must have different names
113
if (name1.equals(name2)) {
114             throw new DeploymentDescException("Relation " + name + " have 2 roles with same name: " + name1);
115         }
116
117         // name of the relation. If not set, choose a combination of the 2 cmr names.
118
String JavaDoc ern = er.getEjbRelationName();
119         if (ern == null || ern.length() == 0) {
120             name = cmr2 + "-" + cmr1;
121         } else {
122             name = ern;
123         }
124     }
125
126     /**
127      * Finish initialisation
128      * @param jer The described object JonasEjbRelation. This param may be null.
129      * @throws DeploymentDescException in error case
130      */

131     public void setJonasInfo(JonasEjbRelation jer) throws DeploymentDescException {
132         // search the associated JonasEjbRelationshipRole of EjbRelationshipRole.
133
// They may not exist.
134
jRel = jer;
135         HashMap JavaDoc table = new HashMap JavaDoc();
136         if (jRel != null) {
137             for (Iterator JavaDoc i = jRel.getJonasEjbRelationshipRoleList().iterator(); i.hasNext();) {
138                 JonasEjbRelationshipRole jersr = (JonasEjbRelationshipRole) i.next();
139                 String JavaDoc rname = jersr.getEjbRelationshipRoleName();
140                 if (!rname.equals(name1) && !rname.equals(name2)) {
141                     throw new DeploymentDescException("Invalid relationship-role-name \"" + rname + "\" for relation \"" + name + "\" in jonas-ejb-jar.xml");
142                 }
143                 table.put(rname, jersr);
144             }
145         }
146         jRsRole1 = (JonasEjbRelationshipRole) table.get(name1);
147         jRsRole2 = (JonasEjbRelationshipRole) table.get(name2);
148
149         relationshipRoleDesc1 = new EjbRelationshipRoleDesc(this, name1, role1, jRsRole1, role2, true, logger);
150         relationshipRoleDesc2 = new EjbRelationshipRoleDesc(this, name2, role2, jRsRole2, role1, false, logger);
151
152         // Add the opposite CMR field for the relation XXu
153
// in order to implement the coherence.
154
boolean r1hf = relationshipRoleDesc1.hasCmrField();
155         boolean r2hf = relationshipRoleDesc2.hasCmrField();
156         EjbRelationshipRoleDesc nocmr = null;
157         EjbRelationshipRoleDesc cmr = null;
158         if (r1hf && !r2hf) {
159             nocmr = relationshipRoleDesc2;
160             cmr = relationshipRoleDesc1;
161         } else if (!r1hf && r2hf) {
162             nocmr = relationshipRoleDesc1;
163             cmr = relationshipRoleDesc2;
164         }
165         if (nocmr != null) {
166             // The relation is OXu, the role 'role' does not have cmr field
167
String JavaDoc cmrName = name;
168             // calculate a cmr field name with the relation name. The bad
169
// character are replaced by the character '_'.
170
for (int i = 0; i < cmrName.length(); i++) {
171                 char c = cmrName.charAt(i);
172                 if (!Character.isJavaIdentifierPart(c)) {
173                     cmrName = cmrName.replace(c, '_');
174                 }
175             }
176             cmrName = "jonasCMR" + cmrName;
177             // Add the cmr, no type is specified because the added cmr field
178
// is mono valued.
179
nocmr.setCmrFieldName(cmrName);
180             nocmr.setIsJOnASCmrField();
181             if (nocmr.isTargetMultiple()) {
182                 if (cmr.isTargetMultiple()) {
183                     nocmr.setCmrFieldType(cmr.cmrFieldType.getName());
184                 } else {
185                     nocmr.setCmrFieldType("java.util.Collection");
186                 }
187             }
188         }
189     }
190
191     /**
192      * Fills the mapping information of this relation with the values defined in jonas DD.
193      * @throws DeploymentDescException thrown in error case.
194      */

195     protected void fillMappingInfo() throws DeploymentDescException {
196         if (jRel != null) {
197             if (jRel.getJdbcTableName() != null) {
198                 if (jRel.getJdbcTableName().length() != 0) {
199                     jdbcTableName = jRel.getJdbcTableName();
200                 }
201             }
202             relationshipRoleDesc1.fillMappingInfo();
203             relationshipRoleDesc2.fillMappingInfo();
204         }
205     }
206
207     /**
208      * Fills the mapping information of this relation with default values,
209      * if the mapping information is not already initialized.
210      */

211     protected void fillMappingInfoWithDefault() {
212         if (!hasJdbcTable()) {
213             if (getRelationshipRole1().isTargetMultiple()
214                 && getRelationshipRole2().isTargetMultiple()) {
215                 // Many-Many: join table needed for the relation
216
jdbcTableName = getRelationshipRole1().getSourceBean().getAbstractSchemaName().toUpperCase()
217                     + "_" + getRelationshipRole2().getSourceBean().getAbstractSchemaName().toUpperCase();
218             }
219         }
220         if (!getRelationshipRole1().isSourceMultiple()
221             && !getRelationshipRole2().isSourceMultiple()) {
222             // One-One
223
if (!getRelationshipRole1().hasJdbcMapping()
224                 && !getRelationshipRole2().hasJdbcMapping()) {
225                 if (!getRelationshipRole1().isJOnASCmrField()
226                     && !getRelationshipRole2().isJOnASCmrField()) {
227                     // One <-> One: foreign keys in the source bean of the first RsRole defined
228
getRelationshipRole1().fillMappingInfoWithDefault();
229                 } else {
230                     if (!getRelationshipRole1().isJOnASCmrField()) {
231                         // One -> One: foreign keys in the source bean of RsRole1
232
getRelationshipRole1().fillMappingInfoWithDefault();
233                     } else {
234                         // One <- One: foreign keys in the target bean of RsRole1
235
getRelationshipRole2().fillMappingInfoWithDefault();
236                     }
237                 }
238             }
239         } else if (getRelationshipRole1().isSourceMultiple()
240                    && getRelationshipRole2().isSourceMultiple()) {
241             // Many-Many
242
getRelationshipRole1().fillMappingInfoWithDefault();
243             getRelationshipRole2().fillMappingInfoWithDefault();
244         } else {
245             // One-Many or Many-One
246
if (getRelationshipRole1().isSourceMultiple()) {
247                 // Many-One
248
getRelationshipRole1().fillMappingInfoWithDefault();
249             } else {
250                 // One-Many
251
getRelationshipRole2().fillMappingInfoWithDefault();
252             }
253         }
254     }
255
256     /**
257      * get the name of the relationship.
258      * @return the String name of the relationship.
259      */

260     public String JavaDoc getName() {
261         return name;
262     }
263
264     /**
265      * get the meta-information for the first relation-ship-role
266      * @return the EjbRelationshipRoleDesc for the first relation-ship-role
267      */

268     public EjbRelationshipRoleDesc getRelationshipRole1() {
269         return relationshipRoleDesc1;
270     }
271
272     /**
273      * get the meta-information for the second relation-ship-role
274      * @return the EjbRelationshipRoleDesc for the second relation-ship-role
275      */

276     public EjbRelationshipRoleDesc getRelationshipRole2() {
277         return relationshipRoleDesc2;
278     }
279
280     /**
281      * Is a table in the database is defined for this relation ?
282      * @return true if table name in the database is defined for this relation.
283      */

284     public boolean hasJdbcTable() {
285         return (jdbcTableName != null);
286     }
287
288     /**
289      * Return the table name in the database associated to this relation.
290      * @return the String name of the table associated to this relation.
291      */

292     public String JavaDoc getJdbcTableName() {
293         return jdbcTableName;
294     }
295
296     /**
297      * String representation of the object for test purpose
298      * @return String representation of this object
299      */

300     public String JavaDoc toString() {
301         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
302         ret.append("\ngetName()=" + getName());
303         if (hasJdbcTable()) {
304             ret.append("\ngetJdbcTableName() = " + getJdbcTableName());
305         }
306         ret.append("\ngetRelationshipRole1() = " + getRelationshipRole1());
307         ret.append("\ngetRelationshipRole2() = " + getRelationshipRole2());
308         return ret.toString();
309     }
310 }
311
Popular Tags