1 21 package oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors; 23 24 import java.util.Collection ; 25 import java.util.Map ; 26 import java.util.List ; 27 import java.util.Set ; 28 import java.util.StringTokenizer ; 29 30 import javax.persistence.MapKey; 31 import javax.persistence.OrderBy; 32 import javax.persistence.JoinTable; 33 34 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataLogger; 35 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataHelper; 36 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataConstants; 37 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataDescriptor; 38 39 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataJoinColumn; 40 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataJoinColumns; 41 42 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor; 43 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataAccessibleObject; 44 45 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.tables.MetadataJoinTable; 46 47 import oracle.toplink.essentials.internal.helper.ClassConstants; 48 import oracle.toplink.essentials.internal.helper.Helper; 49 import oracle.toplink.essentials.internal.helper.DatabaseField; 50 51 import oracle.toplink.essentials.internal.queryframework.CollectionContainerPolicy; 52 53 import oracle.toplink.essentials.mappings.CollectionMapping; 54 import oracle.toplink.essentials.mappings.ManyToManyMapping; 55 56 62 public abstract class CollectionAccessor extends RelationshipAccessor { 63 66 public CollectionAccessor(MetadataAccessibleObject accessibleObject, ClassAccessor classAccessor) { 67 super(accessibleObject, classAccessor); 68 } 69 70 74 protected void addManyToManyRelationKeyFields(MetadataJoinColumns joinColumns, ManyToManyMapping mapping, String defaultFieldName, MetadataDescriptor descriptor, boolean isSource) { 75 String PK_CTX, FK_CTX; 77 if (isSource) { 78 PK_CTX = MetadataLogger.SOURCE_PK_COLUMN; 79 FK_CTX = MetadataLogger.SOURCE_FK_COLUMN; 80 } else { 81 PK_CTX = MetadataLogger.TARGET_PK_COLUMN; 82 FK_CTX = MetadataLogger.TARGET_FK_COLUMN; 83 } 84 85 for (MetadataJoinColumn joinColumn : processJoinColumns(joinColumns, descriptor)) { 86 String defaultPKFieldName = descriptor.getPrimaryKeyFieldName(); 89 DatabaseField pkField = joinColumn.getPrimaryKeyField(); 90 pkField.setName(getName(pkField, defaultPKFieldName, PK_CTX)); 91 pkField.setTableName(descriptor.getPrimaryTableName()); 92 93 DatabaseField fkField = joinColumn.getForeignKeyField(); 102 String defaultFKFieldName = defaultFieldName + "_" + defaultPKFieldName; 103 fkField.setName(getName(fkField, defaultFKFieldName, FK_CTX)); 104 fkField.setTableName(mapping.getRelationTableQualifiedName()); 109 110 if (isSource) { 112 mapping.addSourceRelationKeyField(fkField, pkField); 113 } else { 114 mapping.addTargetRelationKeyField(fkField, pkField); 115 } 116 } 117 } 118 119 123 protected MetadataJoinTable getJoinTable() { 124 JoinTable joinTable = getAnnotation(JoinTable.class); 125 return new MetadataJoinTable(joinTable); 126 } 127 128 133 public String getMapKey() { 134 if (isAnnotationPresent(MapKey.class)) { 135 MapKey mapKey = getAnnotation(MapKey.class); 136 return mapKey.name(); 137 } else { 138 return ""; 139 } 140 } 141 142 147 public String getOrderBy() { 148 OrderBy orderBy = getAnnotation(OrderBy.class); 149 return orderBy.value(); 150 } 151 152 156 public boolean hasOrderBy() { 157 return isAnnotationPresent(OrderBy.class); 158 } 159 160 164 public boolean isMapCollectionAccessor() { 165 return getRawClass().equals(Map .class); 166 } 167 168 171 protected void populateCollectionMapping(CollectionMapping mapping, String context) { 172 mapping.setIsReadOnly(false); 173 mapping.setIsPrivateOwned(false); 174 mapping.setAttributeName(getAttributeName()); 175 176 setAccessorMethods(mapping); 178 179 processCascadeTypes(mapping); 181 182 setReferenceClass(getTargetEntity(), context); 184 mapping.setReferenceClassName(getReferenceClassName()); 185 186 processOrderBy(mapping); 188 189 String mapKey = processMapKey(mapping); 191 192 setIndirectionPolicy(mapping, mapKey); 196 } 197 198 202 protected void processJoinTable(MetadataJoinTable joinTable, ManyToManyMapping mapping) { 203 String sourceName = Helper.getShortClassName(getJavaClassName()).toUpperCase(); 205 String targetName = Helper.getShortClassName(getReferenceClassName()).toUpperCase(); 206 String defaultName = sourceName + "_" + targetName; 207 208 String name = getName(joinTable.getName(), defaultName, m_logger.JOIN_TABLE_NAME); 210 211 String catalog = getName(joinTable.getCatalog(), m_descriptor.getCatalog(), m_logger.JOIN_TABLE_CATALOG); 213 214 String schema = getName(joinTable.getSchema(), m_descriptor.getSchema(), m_logger.JOIN_TABLE_SCHEMA); 216 217 joinTable.setName(MetadataHelper.getFullyQualifiedTableName(name, catalog, schema)); 219 220 mapping.setRelationTable(joinTable.getDatabaseTable()); 222 223 String defaultSourceFieldName; 225 if (getReferenceDescriptor().hasManyToManyAccessorFor(getJavaClassName())) { 226 defaultSourceFieldName = getReferenceDescriptor().getManyToManyAccessor(getJavaClassName()).getAttributeName(); 227 } else { 228 defaultSourceFieldName = Helper.getShortClassName(getJavaClass().getName()); 229 } 230 addManyToManyRelationKeyFields(joinTable.getJoinColumns(), mapping, defaultSourceFieldName, m_descriptor, true); 231 232 String defaultTargetFieldName = getAttributeName(); 234 addManyToManyRelationKeyFields(joinTable.getInverseJoinColumns(), mapping, defaultTargetFieldName, getReferenceDescriptor(), false); 235 } 236 237 242 protected String processMapKey(CollectionMapping mapping) { 243 String mapKey = null; 244 245 if (isMapCollectionAccessor()) { 246 MetadataDescriptor referenceDescriptor = getReferenceDescriptor(); 247 String mapKeyValue = getMapKey(); 248 249 if (mapKeyValue.equals("") && referenceDescriptor.hasCompositePrimaryKey()) { 250 } else { 256 String fieldOrPropertyName = getName(mapKeyValue, referenceDescriptor.getIdAttributeName(), getLogger().MAP_KEY_ATTRIBUTE_NAME); 261 262 MetadataAccessor referenceAccessor = referenceDescriptor.getAccessorFor(fieldOrPropertyName); 264 265 if (referenceAccessor == null) { 266 m_validator.throwCouldNotFindMapKey(fieldOrPropertyName, referenceDescriptor.getJavaClass(), mapping); 267 } 268 269 mapKey = referenceAccessor.getName(); 270 } 271 } 272 273 return mapKey; 274 } 275 276 297 protected void processOrderBy(CollectionMapping mapping) { 298 if (hasOrderBy()) { 299 String orderBy = getOrderBy(); 300 MetadataDescriptor referenceDescriptor = getReferenceDescriptor(); 301 302 if (orderBy.equals("")) { 303 List <String > orderByAttributes = referenceDescriptor.getIdOrderByAttributeNames(); 305 306 if (referenceDescriptor.hasEmbeddedIdAttribute()) { 307 String embeddedIdAttributeName = referenceDescriptor.getEmbeddedIdAttributeName(); 308 309 for (String orderByAttribute : orderByAttributes) { 310 mapping.addAggregateOrderBy(embeddedIdAttributeName, orderByAttribute, false); 311 } 312 } else { 313 for (String orderByAttribute : orderByAttributes) { 314 mapping.addOrderBy(orderByAttribute, false); 315 } 316 } 317 } else { 318 StringTokenizer commaTokenizer = new StringTokenizer (orderBy, ","); 319 320 while (commaTokenizer.hasMoreTokens()) { 321 StringTokenizer spaceTokenizer = new StringTokenizer (commaTokenizer.nextToken()); 322 String propertyOrFieldName = spaceTokenizer.nextToken(); 323 MetadataAccessor referenceAccessor = referenceDescriptor.getAccessorFor(propertyOrFieldName); 324 325 if (referenceAccessor == null) { 326 m_validator.throwInvalidOrderByValue(getJavaClass(), propertyOrFieldName, referenceDescriptor.getJavaClass(), getName()); 327 } 328 329 String attributeName = referenceAccessor.getAttributeName(); 330 String ordering = (spaceTokenizer.hasMoreTokens()) ? spaceTokenizer.nextToken() : MetadataConstants.ASCENDING; 331 332 if (referenceAccessor.isEmbedded()) { 333 for (String orderByAttributeName : referenceDescriptor.getOrderByAttributeNames()) { 334 mapping.addAggregateOrderBy(attributeName, orderByAttributeName, ordering.equals(MetadataConstants.DESCENDING)); 335 } 336 } else { 337 mapping.addOrderBy(attributeName, ordering.equals(MetadataConstants.DESCENDING)); 338 } 339 } 340 } 341 } 342 } 343 344 350 protected void setIndirectionPolicy(CollectionMapping mapping, String mapKey) { 351 Class rawClass = getRawClass(); 352 353 if (usesIndirection()) { 354 if (rawClass == Map .class) { 355 mapping.useTransparentMap(mapKey); 356 } else if (rawClass == List .class) { 357 mapping.useTransparentList(); 358 } else if (rawClass == Collection .class) { 359 mapping.useTransparentCollection(); 360 mapping.setContainerPolicy(new CollectionContainerPolicy(ClassConstants.IndirectList_Class)); 361 } else if (rawClass == Set .class) { 362 mapping.useTransparentSet(); 363 } else { 364 } 366 } else { 367 mapping.dontUseIndirection(); 368 369 if (rawClass == Map .class) { 370 mapping.useMapClass(java.util.Hashtable .class, mapKey); 371 } else if (rawClass == Set .class) { 372 mapping.useCollectionClass(java.util.HashSet .class); 373 } else { 374 mapping.useCollectionClass(java.util.Vector .class); 375 } 376 } 377 } 378 } 379 | Popular Tags |