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.EjbRelationshipRole; 31 import org.objectweb.jonas_ejb.deployment.xml.ForeignKeyJdbcMapping; 32 import org.objectweb.jonas_ejb.deployment.xml.JonasEjbRelationshipRole; 33 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 import org.objectweb.util.monolog.api.Logger; 36 37 44 public class EjbRelationshipRoleDesc { 45 46 private Logger logger = null; 47 48 51 public static final byte INDEX = 1; 52 53 57 public static final byte SOURCE = 2; 58 59 63 public static final byte TARGET = 4; 64 65 68 public static final byte OOU = 0; 69 70 public static final byte OOB = 1; 71 72 public static final byte OMU = 2; 73 74 public static final byte OMB = OMU + OOB; 75 76 public static final byte MOU = 4; 77 78 public static final byte MOB = MOU + OOB; 79 80 public static final byte MMU = OMU + MOU; 81 82 public static final byte MMB = MMU + OOB; 83 84 private String rsrName; 85 86 private String ejbSourceName; 87 88 private EjbRelationDesc ejbRelationDesc; 89 90 private EntityCmp2Desc sourceEntityCmp2Desc; 91 92 private boolean isSourceMultiple; 93 94 private EntityCmp2Desc targetEntityCmp2Desc; 95 96 private boolean isTargetMultiple; 97 98 private boolean isSlave; 99 100 protected String cmrFieldName = null; 101 102 protected Class cmrFieldType = null; 103 104 protected boolean isJOnASCMR = false; 105 106 private byte relationType = -1; 107 108 private boolean mustCascade; 109 110 private JonasEjbRelationshipRole jSourceRsRole = null; 114 115 private HashMap foreignKeyMap = new HashMap (); 118 119 private boolean hasJdbcMapping = false; 120 121 131 public EjbRelationshipRoleDesc(EjbRelationDesc rd, String name, EjbRelationshipRole role, 132 JonasEjbRelationshipRole jrole, EjbRelationshipRole opposite, boolean isSlave, Logger logger) 133 throws DeploymentDescException { 134 135 this.logger = logger; 136 ejbRelationDesc = rd; 137 this.isSlave = isSlave; 138 rsrName = name; 139 mustCascade = opposite.isCascadeDelete(); 140 ejbSourceName = role.getRelationshipRoleSource().getEjbName(); 141 142 if (opposite.getMultiplicity().equalsIgnoreCase("Many")) { 144 isTargetMultiple = true; 145 if (role.isCascadeDelete()) { 146 throw new DeploymentDescException("Cascade delete not allowed for relationshipRole for relationship '" 147 + rd.getName() + "(because opposite role has a multiplicity of Many)"); 148 } 149 } else if (opposite.getMultiplicity().equalsIgnoreCase("One")) { 150 isTargetMultiple = false; 151 } else { 152 throw new DeploymentDescException("Invalid multiplicity value for relationshipRole for relationship '" 153 + rd.getName() + "'(must be One or Many)"); 154 } 155 if (role.getMultiplicity().equalsIgnoreCase("Many")) { 156 isSourceMultiple = true; 157 } else if (role.getMultiplicity().equalsIgnoreCase("One")) { 158 isSourceMultiple = false; 159 } else { 160 throw new DeploymentDescException("Invalid multiplicity value for relationshipRole for relationship '" 161 + rd.getName() + "'(must be One or Many)"); 162 } 163 164 if (role.getCmrField() != null) { 166 setCmrFieldName(role.getCmrField().getCmrFieldName()); 167 if (isTargetMultiple) { 168 String type = role.getCmrField().getCmrFieldType(); 169 if (type == null) { 170 throw new DeploymentDescException( 171 "You must specify a cmr-field-type in case where the relation is 'Many' in the cmr-field '" 172 + cmrFieldName + "' of bean " + ejbSourceName); 173 } 174 setCmrFieldType(type); 175 } 176 } 177 178 jSourceRsRole = jrole; 180 } 181 182 187 protected void fillMappingInfo() throws DeploymentDescException { 188 if (logger.isLoggable(BasicLevel.DEBUG)) { 189 logger.log(BasicLevel.DEBUG, "" + (jSourceRsRole != null) + " for " + rsrName); 190 } 191 if (jSourceRsRole != null) { 192 for (Iterator i = jSourceRsRole.getForeignKeyJdbcMappingList().iterator(); i.hasNext();) { 193 ForeignKeyJdbcMapping fkMapping = (ForeignKeyJdbcMapping) i.next(); 194 String fkc = fkMapping.getForeignKeyJdbcName(); 195 String kc = null; 196 if (fkMapping.getKeyJdbcName() != null) { 197 kc = fkMapping.getKeyJdbcName(); 198 } 199 if (kc == null) { 200 if (targetEntityCmp2Desc.hasSimplePkField()) { 205 kc = ((FieldJdbcDesc) targetEntityCmp2Desc.getSimplePkField()).getJdbcFieldName(); 206 } else { 207 throw new DeploymentDescException( 209 "key-jdbc-name must be provided for foreign-key-jdbc-mapping " + fkc 210 + " of relation-ship role " + rsrName + "of relation " 211 + ejbRelationDesc.getName()); 212 } 213 } 214 if (logger.isLoggable(BasicLevel.DEBUG)) { 215 logger.log(BasicLevel.DEBUG, "explicit fk mapping = " + fkc + " for " + kc); 216 } 217 foreignKeyMap.put(kc, fkc); 218 } 219 hasJdbcMapping = true; 220 } 221 } 222 223 227 protected void fillMappingInfoWithDefault() { 228 if (logger.isLoggable(BasicLevel.DEBUG)) { 229 logger.log(BasicLevel.DEBUG, "" + hasJdbcMapping); 230 } 231 if (!hasJdbcMapping) { 232 if (targetEntityCmp2Desc.hasSimplePkField()) { 233 String fn = targetEntityCmp2Desc.getSimplePkFieldName(); 235 FieldJdbcDesc fd = (FieldJdbcDesc) targetEntityCmp2Desc.getCmpFieldDesc(fn); 236 String kc = fd.getJdbcFieldName(); 237 String fkc = targetEntityCmp2Desc.getAbstractSchemaName() + "_" + kc; 238 if (logger.isLoggable(BasicLevel.DEBUG)) { 239 logger.log(BasicLevel.DEBUG, "default fk mapping = " + fkc + " for " + kc); 240 } 241 foreignKeyMap.put(kc, fkc); 242 } else { 243 for (Iterator i = targetEntityCmp2Desc.getCmpFieldDescIterator(); i.hasNext();) { 245 FieldJdbcDesc fd = (FieldJdbcDesc) i.next(); 246 String kc = fd.getJdbcFieldName(); 247 String fkc = targetEntityCmp2Desc.getAbstractSchemaName() + "_" + kc; 248 if (logger.isLoggable(BasicLevel.DEBUG)) { 249 logger.log(BasicLevel.DEBUG, "default fk mapping = " + fkc + " for " + kc); 250 } 251 foreignKeyMap.put(kc, fkc); 252 } 253 } 254 hasJdbcMapping = true; 255 } 256 } 257 258 262 public String getName() { 263 return rsrName; 264 } 265 266 protected void setCmrFieldName(String name) throws DeploymentDescException { 267 cmrFieldName = name; 268 } 269 270 protected void setCmrFieldType(String type) throws DeploymentDescException { 271 try { 272 cmrFieldType = Class.forName(type); 273 } catch (ClassNotFoundException e) { 274 throw new DeploymentDescException("class name not found for cmr-field " + cmrFieldName + " of bean " 275 + ejbSourceName, e); 276 } 277 if (!(cmrFieldType.getName().equals("java.util.Collection") || cmrFieldType.getName().equals("java.util.Set"))) { 278 throw new DeploymentDescException("value of cmr-field-type " + cmrFieldName + " of bean " + ejbSourceName 279 + " should be java.util.Set or java.util.Collection if set"); 280 } 281 } 282 283 286 protected void setIsJOnASCmrField() { 287 isJOnASCMR = true; 288 } 289 290 294 protected void setSourceBean(EntityCmp2Desc led) { 295 sourceEntityCmp2Desc = led; 296 } 297 298 302 protected void setTargetBean(EntityCmp2Desc led) { 303 targetEntityCmp2Desc = led; 304 if (cmrFieldType == null) { 305 cmrFieldType = led.getLocalClass(); 306 } 307 } 308 309 313 public EjbRelationDesc getRelation() { 314 return ejbRelationDesc; 315 } 316 317 321 public EjbRelationshipRoleDesc getOppositeRelationshipRole() { 322 EjbRelationshipRoleDesc res = ejbRelationDesc.getRelationshipRole1(); 323 if (res == this) { 324 return ejbRelationDesc.getRelationshipRole2(); 325 } else { 326 return res; 327 } 328 } 329 330 335 public String getSourceBeanName() { 336 return ejbSourceName; 337 } 338 339 344 public EntityCmp2Desc getSourceBean() { 345 return sourceEntityCmp2Desc; 346 } 347 348 354 public EntityCmp2Desc getTargetBean() { 355 return targetEntityCmp2Desc; 356 } 357 358 362 public boolean isSourceMultiple() { 363 return isSourceMultiple; 364 } 365 366 371 public boolean isTargetMultiple() { 372 return isTargetMultiple; 373 } 374 375 379 public boolean mustCascade() { 380 return mustCascade; 381 } 382 383 389 public boolean isJOnASCmrField() { 390 return isJOnASCMR; 391 } 392 393 398 public boolean hasCmrField() { 400 return cmrFieldName != null; 401 } 402 403 407 public String getCmrFieldName() { 408 return cmrFieldName; 409 } 410 411 416 public Class getCmrFieldType() { 417 return cmrFieldType; 418 } 419 420 424 public byte getRelationType() { 425 if (relationType == -1) { 426 relationType = OOU; 427 EjbRelationshipRoleDesc rsr2 = getOppositeRelationshipRole(); 428 if (rsr2.hasCmrField() && hasCmrField()) { 429 relationType += OOB; 430 } 431 if (isTargetMultiple()) { 432 relationType += OMU; 433 } 434 if (rsr2.isTargetMultiple()) { 435 relationType += MOU; 436 } 437 } 438 return relationType; 439 } 440 441 445 public boolean hasJdbcMapping() { 446 return hasJdbcMapping; 447 } 448 449 453 public boolean isSlave() { 454 return isSlave; 455 } 456 457 463 public String getForeignKeyJdbcName(String jdbcFieldName) { 464 return (String ) foreignKeyMap.get(jdbcFieldName); 465 } 466 467 471 public String toString() { 472 StringBuffer ret = new StringBuffer (); 473 ret.append("\ngetName() = " + getName()); 474 ret.append("\ngetRelation().getName() = " + getRelation().getName()); 475 ret.append("\ngetOppositeRelationshipRole().getName() = " + getOppositeRelationshipRole().getName()); 476 ret.append("\ngetSourceBeanName() = " + getSourceBeanName()); 477 ret.append("\ngetTargetBean().getName() = " + getTargetBean().getEjbName()); 478 ret.append("\nisSourceMultiple() = " + isSourceMultiple()); 479 ret.append("\nisTargetMultiple() = " + isTargetMultiple()); 480 ret.append("\nmustCascade() = " + mustCascade()); 481 ret.append("\nisJOnASCmrField() = " + isJOnASCmrField()); 482 ret.append("\ngetCmrFieldName() = " + getCmrFieldName()); 483 ret.append("\ngetCmrFieldType() = " + getCmrFieldType()); 484 ret.append("\ngetRelationType() = " + getRelationType()); 485 if (hasJdbcMapping()) { 486 for (Iterator i = foreignKeyMap.keySet().iterator(); i.hasNext();) { 487 String key = (String ) i.next(); 488 String fkey = (String ) foreignKeyMap.get(key); 489 ret.append("\ngetForeignKeyJdbcName(" + key + ")=" + fkey); 490 } 491 } 492 return ret.toString(); 493 } 494 495 } | Popular Tags |