1 25 package org.objectweb.jonas_ejb.deployment.api; 26 27 import java.util.HashMap ; 28 import java.util.Iterator ; 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 44 45 public class EjbRelationDesc { 46 47 private Logger logger = null; 48 private String name; private String name1; private String name2; private EjbRelationshipRoleDesc relationshipRoleDesc1; 52 private EjbRelationshipRoleDesc relationshipRoleDesc2; 53 private String jdbcTableName = null; 54 55 56 private EjbRelationshipRole role1; 58 private EjbRelationshipRole role2; 59 JonasEjbRelation jRel = null; 60 JonasEjbRelationshipRole jRsRole1 = null; 61 JonasEjbRelationshipRole jRsRole2 = null; 62 63 64 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 cmr1 = ""; 78 if (role1.getCmrField() != null) { 79 cmr1 = role1.getCmrField().getCmrFieldName(); 80 } 81 String cmr2 = ""; 82 if (role2.getCmrField() != null) { 83 cmr2 = role2.getCmrField().getCmrFieldName(); 84 } 85 86 name1 = role1.getEjbRelationshipRoleName(); 89 if (name1 == null || name1.length() == 0) { 90 if (role2.getCmrField() != null) { 91 name1 = cmr2; 92 } else { 93 name1 = "role1"; } 95 role1.setEjbRelationshipRoleName(name1); 97 } 98 99 name2 = role2.getEjbRelationshipRoleName(); 102 if (name2 == null || name2.length() == 0) { 103 if (role1.getCmrField() != null) { 104 name2 = cmr1; 105 } else { 106 name2 = "role2"; } 108 role2.setEjbRelationshipRoleName(name2); 110 } 111 112 if (name1.equals(name2)) { 114 throw new DeploymentDescException("Relation " + name + " have 2 roles with same name: " + name1); 115 } 116 117 String ern = er.getEjbRelationName(); 119 if (ern == null || ern.length() == 0) { 120 name = cmr2 + "-" + cmr1; 121 } else { 122 name = ern; 123 } 124 } 125 126 131 public void setJonasInfo(JonasEjbRelation jer) throws DeploymentDescException { 132 jRel = jer; 135 HashMap table = new HashMap (); 136 if (jRel != null) { 137 for (Iterator i = jRel.getJonasEjbRelationshipRoleList().iterator(); i.hasNext();) { 138 JonasEjbRelationshipRole jersr = (JonasEjbRelationshipRole) i.next(); 139 String 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 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 String cmrName = name; 168 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 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 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 211 protected void fillMappingInfoWithDefault() { 212 if (!hasJdbcTable()) { 213 if (getRelationshipRole1().isTargetMultiple() 214 && getRelationshipRole2().isTargetMultiple()) { 215 jdbcTableName = getRelationshipRole1().getSourceBean().getAbstractSchemaName().toUpperCase() 217 + "_" + getRelationshipRole2().getSourceBean().getAbstractSchemaName().toUpperCase(); 218 } 219 } 220 if (!getRelationshipRole1().isSourceMultiple() 221 && !getRelationshipRole2().isSourceMultiple()) { 222 if (!getRelationshipRole1().hasJdbcMapping() 224 && !getRelationshipRole2().hasJdbcMapping()) { 225 if (!getRelationshipRole1().isJOnASCmrField() 226 && !getRelationshipRole2().isJOnASCmrField()) { 227 getRelationshipRole1().fillMappingInfoWithDefault(); 229 } else { 230 if (!getRelationshipRole1().isJOnASCmrField()) { 231 getRelationshipRole1().fillMappingInfoWithDefault(); 233 } else { 234 getRelationshipRole2().fillMappingInfoWithDefault(); 236 } 237 } 238 } 239 } else if (getRelationshipRole1().isSourceMultiple() 240 && getRelationshipRole2().isSourceMultiple()) { 241 getRelationshipRole1().fillMappingInfoWithDefault(); 243 getRelationshipRole2().fillMappingInfoWithDefault(); 244 } else { 245 if (getRelationshipRole1().isSourceMultiple()) { 247 getRelationshipRole1().fillMappingInfoWithDefault(); 249 } else { 250 getRelationshipRole2().fillMappingInfoWithDefault(); 252 } 253 } 254 } 255 256 260 public String getName() { 261 return name; 262 } 263 264 268 public EjbRelationshipRoleDesc getRelationshipRole1() { 269 return relationshipRoleDesc1; 270 } 271 272 276 public EjbRelationshipRoleDesc getRelationshipRole2() { 277 return relationshipRoleDesc2; 278 } 279 280 284 public boolean hasJdbcTable() { 285 return (jdbcTableName != null); 286 } 287 288 292 public String getJdbcTableName() { 293 return jdbcTableName; 294 } 295 296 300 public String toString() { 301 StringBuffer ret = new StringBuffer (); 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 |