1 23 24 29 30 package com.sun.jdo.spi.persistence.support.ejb.model.util; 31 32 import java.util.*; 33 34 import com.sun.jdo.api.persistence.mapping.ejb.AbstractNameMapper; 35 36 import com.sun.enterprise.deployment.*; 37 38 49 public abstract class NameMapper extends AbstractNameMapper 50 { 51 private EjbBundleDescriptor _bundleDescriptor; 52 private Map _generatedRelToInverseRelMap; 53 private Map _relToInverseGeneratedRelMap; 54 55 59 protected NameMapper (EjbBundleDescriptor bundleDescriptor) 60 { 61 _bundleDescriptor = bundleDescriptor; 62 initGeneratedRelationshipMaps(); 63 } 64 65 private void initGeneratedRelationshipMaps () 66 { 67 EjbBundleDescriptor bundleDescriptor = getBundleDescriptor(); 68 Set relationships = bundleDescriptor.getRelationships(); 69 70 _generatedRelToInverseRelMap = new HashMap(); 71 _relToInverseGeneratedRelMap = new HashMap(); 72 73 if (relationships != null) 77 { 78 Iterator iterator = relationships.iterator(); 79 List generatedRels = new ArrayList(); 80 int counter = 0; 81 82 while (iterator.hasNext()) 84 { 85 RelationshipDescriptor relationship = 86 (RelationshipDescriptor)iterator.next(); 87 88 if (relationship.getSource().getCMRField() == null) 89 generatedRels.add(relationship); 90 91 if (relationship.getSink().getCMRField() == null) 92 generatedRels.add(relationship); 93 } 94 95 iterator = generatedRels.iterator(); 97 while (iterator.hasNext()) 98 { 99 RelationshipDescriptor relationship = 100 (RelationshipDescriptor)iterator.next(); 101 RelationRoleDescriptor source = relationship.getSource(); 102 String sourceEjbName = source.getOwner().getName(); 103 String sourceCMRField = source.getCMRField(); 104 boolean sourceIsNull = (sourceCMRField == null); 105 RelationRoleDescriptor sink = relationship.getSink(); 106 String sinkEjbName = sink.getOwner().getName(); 107 String ejbName = (sourceIsNull ? sourceEjbName : sinkEjbName); 108 String otherEjbName = 109 (sourceIsNull ? sinkEjbName : sourceEjbName); 110 List ejbField = Arrays.asList(new String []{otherEjbName, 111 (sourceIsNull ? sink.getCMRField() : sourceCMRField)}); 112 PersistenceDescriptor pDescriptor = ((EjbCMPEntityDescriptor) 113 bundleDescriptor.getEjbByName(ejbName)). 114 getPersistenceDescriptor(); 115 List generatedField = null; 116 String uniqueName = null; 117 118 do 121 { 122 counter++; 123 uniqueName = GENERATED_CMR_FIELD_PREFIX + counter; 124 } while (hasField(pDescriptor, uniqueName)); 125 126 generatedField = 127 Arrays.asList(new String []{ejbName, uniqueName}); 128 _generatedRelToInverseRelMap.put(generatedField, ejbField); 129 _relToInverseGeneratedRelMap.put(ejbField, generatedField); 130 } 131 } 132 } 133 134 protected Map getGeneratedFieldsMap () 135 { 136 return _generatedRelToInverseRelMap; 137 } 138 protected Map getInverseFieldsMap () { return _relToInverseGeneratedRelMap; } 139 140 private boolean hasField (PersistenceDescriptor persistenceDescriptor, 142 String fieldName) 143 { 144 Class fieldType = null; 145 146 try 147 { 148 fieldType = persistenceDescriptor.getTypeFor(fieldName); 149 } 150 catch (RuntimeException e) 151 { 152 } 154 155 return (fieldType != null); 156 } 157 158 163 public EjbBundleDescriptor getBundleDescriptor () 164 { 165 return _bundleDescriptor; 166 } 167 168 173 abstract public EjbCMPEntityDescriptor getDescriptorForEjbName ( 174 String name); 175 176 180 public int getKeyClassTypeForEjbName (String name) 181 { 182 String keyClass = getKeyClassForEjbName(name); 183 184 if (!"java.lang.Object".equals(keyClass)) { 186 EjbCMPEntityDescriptor descriptor = getDescriptorForEjbName(name); 187 188 return ((descriptor.getPrimaryKeyFieldDesc() != null) ? 189 PRIMARY_KEY_FIELD : USER_DEFINED_KEY_CLASS); 190 } 191 192 return UNKNOWN_KEY_CLASS; 193 } 194 195 200 abstract public String getEjbNameForAbstractSchema (String schemaName); 201 202 207 abstract public String getConcreteBeanClassForEjbName (String name); 208 } 209 | Popular Tags |