1 23 24 29 30 package com.sun.jdo.spi.persistence.support.ejb.ejbc; 31 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Set ; 38 39 import com.sun.enterprise.deployment.CMRFieldInfo; 40 import com.sun.enterprise.deployment.EjbBundleDescriptor; 41 import com.sun.enterprise.deployment.PersistenceDescriptor; 42 import com.sun.enterprise.deployment.PersistentFieldInfo; 43 import com.sun.enterprise.deployment.RelationshipDescriptor; 44 import com.sun.enterprise.deployment.RelationRoleDescriptor; 45 46 import com.sun.enterprise.deployment.IASEjbCMPEntityDescriptor; 47 48 import com.sun.jdo.api.persistence.mapping.ejb.ConversionHelper; 49 50 import com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper; 51 52 58 public class EjbConversionHelper implements ConversionHelper { 59 60 private NameMapper nameMapper = null; 61 private EjbBundleDescriptor bundle = null; 62 private HashMap ejbDescMap = new HashMap (); 63 private HashMap ejbFieldMap = new HashMap (); 64 private HashMap ejbKeyMap = new HashMap (); 65 private HashMap ejbPerDescMap = new HashMap (); 66 private HashMap ejbRelMap = new HashMap (); 67 boolean generateFields = true; 68 boolean ensureValidation = true; 69 70 public EjbConversionHelper(NameMapper nameMapper) { 71 this.nameMapper = nameMapper; 72 this.bundle = nameMapper.getBundleDescriptor(); 73 74 Iterator iter = bundle.getEjbs().iterator(); 75 while (iter.hasNext()) { 76 Object desc = iter.next(); 77 if (desc instanceof IASEjbCMPEntityDescriptor) { 78 IASEjbCMPEntityDescriptor ejbDesc = 79 (IASEjbCMPEntityDescriptor)desc; 80 81 String ejbName = ejbDesc.getName(); 82 ejbDescMap.put(ejbName, ejbDesc); 84 85 PersistenceDescriptor pers = ejbDesc.getPersistenceDescriptor(); 87 ejbPerDescMap.put(ejbName, pers); 88 89 Collection pFields = ejbDesc.getPersistentFields(); 91 HashMap fieldMap = new HashMap (); 92 Iterator fIter = pFields.iterator(); 93 while (fIter.hasNext()) { 94 String fieldName = ((PersistentFieldInfo)fIter.next()).name; 95 fieldMap.put(fieldName, fieldName); 96 } 97 ejbFieldMap.put(ejbName, fieldMap); 98 99 List pseudoFields = nameMapper.getGeneratedRelationshipsForEjbName(ejbName); 101 Iterator pIter = pseudoFields.iterator(); 102 while (pIter.hasNext()) { 103 addField(ejbName, (String )pIter.next()); 104 } 105 106 Collection pKeys = ejbDesc.getPrimaryKeyFields(); 108 HashMap pKeyMap = new HashMap (); 109 Iterator kIter = pKeys.iterator(); 110 while (kIter.hasNext()) { 111 String fieldName = ((PersistentFieldInfo)kIter.next()).name; 112 pKeyMap.put(fieldName, fieldName); 113 } 114 ejbKeyMap.put(ejbName, pKeyMap); 115 } 116 } 117 118 Set rels = bundle.getRelationships(); 120 Iterator relIter = rels.iterator(); 121 while (relIter.hasNext()) { 122 RelationshipDescriptor rel = (RelationshipDescriptor)relIter.next(); 123 RelationRoleDescriptor source = rel.getSource(); 124 RelationRoleDescriptor sink = rel.getSink(); 125 126 String sourceEjbName = source.getOwner().getName(); 128 ArrayList sourceRels = (ArrayList )ejbRelMap.get(sourceEjbName); 129 if (sourceRels == null) { 130 sourceRels = new ArrayList (); 131 ejbRelMap.put(sourceEjbName, sourceRels); 132 } 133 sourceRels.add(rel); 134 135 String sourceCMRField = source.getCMRField(); 137 if (sourceCMRField != null) { 138 addField(sourceEjbName, sourceCMRField); 139 } 140 141 String sinkEjbName = sink.getOwner().getName(); 143 ArrayList sinkRels = (ArrayList )ejbRelMap.get(sinkEjbName); 144 if (sinkRels == null) { 145 sinkRels = new ArrayList (); 146 ejbRelMap.put(sinkEjbName, sinkRels); 147 } 148 sinkRels.add(rel); 149 150 String sinkCMRField = sink.getCMRField(); 152 if (sinkCMRField != null) { 153 addField(sinkEjbName, sinkCMRField); 154 } 155 } 156 } 157 158 160 public String getMappedClassName(String ejbName) { 161 return nameMapper.getPersistenceClassForEjbName(ejbName); 162 } 163 164 173 public boolean hasField(String ejbName, String fieldName) { 174 if (!generateFields && isGeneratedRelationship(ejbName, fieldName)) 175 return false; 176 else { 177 HashMap fieldMap = (HashMap )ejbFieldMap.get(ejbName); 178 return (fieldMap != null) ? 179 (fieldMap.get(fieldName) != null) : false; 180 } 181 } 182 183 191 public Object [] getFields(String ejbName) { 192 HashMap fieldMap = (HashMap )ejbFieldMap.get(ejbName); 193 if (fieldMap != null) { 194 List fields = new ArrayList (fieldMap.keySet()); 195 if (!generateFields) { 196 fields.removeAll(getGeneratedRelationships(ejbName)); 197 } 198 return fields.toArray(); 199 } 200 return null; 201 } 202 203 206 public boolean isKey(String ejbName, String fieldName, boolean candidate) { 207 HashMap keyMap = (HashMap )ejbKeyMap.get(ejbName); 208 return (keyMap != null) ? (keyMap.get(fieldName) != null) : false; 209 } 210 211 215 public String getRelationshipFieldType(String ejbName, String fieldName) { 216 if (isGeneratedRelationship(ejbName, fieldName)) { 217 return java.util.Collection .class.getName(); 218 } else { 219 PersistenceDescriptor pers = 220 (PersistenceDescriptor)ejbPerDescMap.get(ejbName); 221 return pers.getCMRFieldReturnType(fieldName); 222 } 223 } 224 225 229 public String getMultiplicity(String ejbName, String fieldName) { 230 RelationRoleDescriptor oppRole = getRelationRoleDescriptor(ejbName, 231 fieldName, false); 232 return (oppRole.getIsMany()) ? MANY : ONE; 233 } 234 235 public String getRelationshipFieldContent(String ejbName, String fieldName) { 236 RelationRoleDescriptor oppRole = getRelationRoleDescriptor(ejbName, 237 fieldName, false); 238 return oppRole.getOwner().getName(); 239 } 240 241 244 public String getInverseFieldName(String ejbName, String fieldName) { 245 RelationRoleDescriptor oppRole = getRelationRoleDescriptor(ejbName, 246 fieldName, false); 247 String inverseName = oppRole.getCMRField(); 248 249 if ((generateFields) && (inverseName == null)) 251 inverseName = nameMapper.getGeneratedFieldForEjbField( 252 ejbName, fieldName)[1]; 253 254 return inverseName; 255 } 256 257 265 public boolean applyDefaultUnknownPKClassStrategy(String ejbName) { 266 IASEjbCMPEntityDescriptor ejbDesc = 267 (IASEjbCMPEntityDescriptor)ejbDescMap.get(ejbName); 268 String keyClassName = ejbDesc.getPrimaryKeyClassName(); 269 return keyClassName != null && 270 keyClassName.equals(Object .class.getName()); 271 } 272 273 277 public String getGeneratedPKFieldName() { 278 return nameMapper.GENERATED_KEY_FIELD_NAME; 279 } 280 281 285 public String getGeneratedVersionFieldNamePrefix() { 286 return nameMapper.GENERATED_VERSION_FIELD_PREFIX; 287 } 288 289 public boolean relatedObjectsAreDeleted(String beanName, String fieldName) { 290 RelationRoleDescriptor oppRole = getRelationRoleDescriptor(beanName, fieldName, false); 291 return oppRole.getCascadeDelete(); 292 } 293 294 302 public boolean generateFields() { 303 return generateFields; 304 } 305 306 312 public void setGenerateFields(boolean generateFields) { 313 this.generateFields = generateFields; 314 } 315 316 321 public boolean ensureValidation() { 322 return ensureValidation; 323 } 324 325 330 public void setEnsureValidation(boolean isValidating) { 331 ensureValidation = isValidating; 332 } 333 334 343 344 public boolean isGeneratedField(String ejbName, String fieldName) { 345 return nameMapper.isGeneratedField(ejbName, fieldName); 346 } 347 348 public boolean isGeneratedRelationship(String ejbName, String fieldName) { 349 return nameMapper.isGeneratedEjbRelationship(ejbName, fieldName); 350 } 351 352 357 public List getGeneratedRelationships(String ejbName) { 358 return nameMapper.getGeneratedRelationshipsForEjbName(ejbName); 359 360 } 361 362 private RelationRoleDescriptor getRelationRoleDescriptor(String ejbName, 364 String cmrFieldName, boolean self) { 365 String myEjbName = ejbName; 366 String myCMRFieldName = cmrFieldName; 367 boolean myself = self; 368 if (isGeneratedRelationship(ejbName, cmrFieldName)) { 369 String [] nfPair = nameMapper.getEjbFieldForGeneratedField( 370 ejbName, cmrFieldName); 371 myEjbName = nfPair[0]; 372 myCMRFieldName = nfPair[1]; 373 myself = !self; 374 } 375 return getRealRelationRoleDescriptor(myEjbName, myCMRFieldName, myself); 376 } 377 378 private RelationRoleDescriptor getRealRelationRoleDescriptor( 379 String ejbName, String cmrFieldName, boolean self) { 380 ArrayList rels = (ArrayList )ejbRelMap.get(ejbName); 381 for (int i = 0; i < rels.size(); i++) { 382 RelationshipDescriptor rel = (RelationshipDescriptor)rels.get(i); 383 RelationRoleDescriptor source = rel.getSource(); 384 RelationRoleDescriptor sink = rel.getSink(); 385 if (ejbName.equals(source.getOwner().getName()) && 386 cmrFieldName.equals(source.getCMRField())) { 387 return (self) ? source : sink; 388 } else if (ejbName.equals(sink.getOwner().getName()) && 389 cmrFieldName.equals(sink.getCMRField())) { 390 return (self) ? sink : source; 391 } 392 } 393 throw new IllegalArgumentException (); 394 } 395 396 private void addField(String ejbName, String fieldName) { 397 HashMap fieldMap = (HashMap )ejbFieldMap.get(ejbName); 398 if (fieldMap == null) { 399 fieldMap = new HashMap (); 400 ejbFieldMap.put(ejbName, fieldMap); 401 } 402 fieldMap.put(fieldName, fieldName); 403 } 404 } 405 | Popular Tags |