1 21 package oracle.toplink.essentials.descriptors; 23 24 import java.util.*; 25 import oracle.toplink.essentials.exceptions.QueryException; 26 import oracle.toplink.essentials.internal.descriptors.ObjectBuilder; 27 import oracle.toplink.essentials.internal.descriptors.OptimisticLockingPolicy; 28 import oracle.toplink.essentials.internal.helper.DatabaseField; 29 import oracle.toplink.essentials.mappings.DatabaseMapping; 30 import oracle.toplink.essentials.descriptors.ClassDescriptor; 31 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 32 import oracle.toplink.essentials.queryframework.FetchGroup; 33 import oracle.toplink.essentials.queryframework.FetchGroupTracker; 34 import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery; 35 36 52 public class FetchGroupManager { 53 private Map fetchGroups = null; 55 56 private FetchGroup defaultFetchGroup; 58 59 private ClassDescriptor descriptor; 61 62 65 public FetchGroupManager() { 66 } 67 68 71 public void addFetchGroup(FetchGroup group) { 72 getFetchGroups().put(group.getName(), group); 74 } 75 76 79 public Map getFetchGroups() { 80 if (fetchGroups == null) { 81 fetchGroups = new HashMap(2); 83 } 84 85 return fetchGroups; 86 } 87 88 102 public FetchGroup getDefaultFetchGroup() { 103 return defaultFetchGroup; 104 } 105 106 109 public FetchGroup getFetchGroup(String groupName) { 110 return (FetchGroup)getFetchGroups().get(groupName); 111 } 112 113 127 public void setDefaultFetchGroup(FetchGroup newDefaultFetchGroup) { 128 defaultFetchGroup = newDefaultFetchGroup; 129 } 130 131 136 public boolean isPartialObject(Object domainObject) { 137 if (domainObject != null) { 138 FetchGroup fetchGroupInCache = ((FetchGroupTracker)domainObject).getFetchGroup(); 139 140 return (fetchGroupInCache != null); 142 } 143 return false; 144 } 145 146 150 public boolean isObjectValidForFetchGroup(Object object, FetchGroup fetchGroup) { 151 FetchGroup groupInObject = ((FetchGroupTracker)object).getFetchGroup(); 152 return (groupInObject == null) || groupInObject.isSupersetOf(fetchGroup); 153 } 154 155 160 public boolean shouldWriteInto(Object cachedObject, Object clone) { 161 if (isPartialObject(clone)) { 162 FetchGroup fetchGroupInSrc = ((FetchGroupTracker)cachedObject).getFetchGroup(); 163 FetchGroup fetchGroupInTarg = ((FetchGroupTracker)clone).getFetchGroup(); 164 165 return (!((fetchGroupInTarg == null) || fetchGroupInTarg.isSupersetOf(fetchGroupInSrc)) || ((FetchGroupTracker)cachedObject).shouldRefreshFetchGroup()); 168 } 169 return false; 170 } 171 172 176 public void writePartialIntoClones(Object partialObject, Object workingClone, UnitOfWorkImpl uow) { 177 FetchGroup fetchGroupInClone = ((FetchGroupTracker)workingClone).getFetchGroup(); 178 FetchGroup fetchGroupInObject = ((FetchGroupTracker)partialObject).getFetchGroup(); 179 Object backupClone = uow.getBackupClone(workingClone); 180 181 if (((FetchGroupTracker)partialObject).shouldRefreshFetchGroup()) { 183 refreshFetchGroupIntoClones(partialObject, workingClone, backupClone, fetchGroupInObject, fetchGroupInClone, uow); 185 } else { revertDataIntoUnfetchedAttributesOfClones(partialObject, workingClone, backupClone, fetchGroupInObject, fetchGroupInClone, uow); 188 } 189 190 fetchGroupInObject = unionFetchGroups(fetchGroupInObject, fetchGroupInClone); 192 setObjectFetchGroup(workingClone, fetchGroupInObject); 194 setObjectFetchGroup(backupClone, fetchGroupInObject); 195 } 196 197 201 private void refreshFetchGroupIntoClones(Object cachedObject, Object workingClone, Object backupClone, FetchGroup fetchGroupInObject, FetchGroup fetchGroupInClone, UnitOfWorkImpl uow) { 202 Vector mappings = descriptor.getMappings(); 203 boolean isObjectPartial = (fetchGroupInObject != null); 204 Set fetchedAttributes = isObjectPartial ? fetchGroupInObject.getAttributes() : null; 205 for (int index = 0; index < mappings.size(); index++) { 206 DatabaseMapping mapping = (DatabaseMapping)mappings.get(index); 207 if ((!isObjectPartial) || ((fetchedAttributes != null) && fetchedAttributes.contains(mapping.getAttributeName()))) { 208 mapping.buildClone(cachedObject, workingClone, uow, null); 210 mapping.buildClone(workingClone, backupClone, uow, null); 211 } 212 } 213 } 214 215 218 private void revertDataIntoUnfetchedAttributesOfClones(Object cachedObject, Object workingClone, Object backupClone, FetchGroup fetchGroupInObject, FetchGroup fetchGroupInClone, UnitOfWorkImpl uow) { 219 if (isObjectValidForFetchGroup(workingClone, fetchGroupInObject)) { 221 return; 224 } 225 Vector mappings = descriptor.getMappings(); 226 227 Set fetchedAttributesClone = fetchGroupInClone.getAttributes(); 229 for (int index = 0; index < mappings.size(); index++) { 230 DatabaseMapping mapping = (DatabaseMapping)mappings.get(index); 231 232 if (isAttributeFetched(cachedObject, mapping.getAttributeName()) && (!fetchedAttributesClone.contains(mapping.getAttributeName()))) { 234 mapping.buildClone(cachedObject, workingClone, uow, null); 236 mapping.buildClone(workingClone, backupClone, uow, null); 237 } 238 } 239 } 240 241 245 public void copyFetchGroupInto(Object source, Object target) { 246 if (isPartialObject(source)) { 247 ((FetchGroupTracker)target).setFetchGroup(((FetchGroupTracker)source).getFetchGroup()); 248 } 249 } 250 251 255 public void unionFetchGroupIntoObject(Object source, FetchGroup newFetchGroup) { 256 FetchGroupTracker tracker = (FetchGroupTracker)source; 257 tracker.setFetchGroup(unionFetchGroups(tracker.getFetchGroup(), newFetchGroup)); 258 } 259 260 264 public FetchGroup unionFetchGroups(FetchGroup first, FetchGroup second) { 265 if ((first == null) || (second == null)) { 266 return null; 267 } 268 269 if (first.isSupersetOf(second)) { 271 return first; 272 } else if (second.isSupersetOf(first)) { 273 return second; 274 } 275 276 StringBuffer unionGroupName = new StringBuffer (first.getName()); 278 unionGroupName.append("_"); 279 unionGroupName.append(second.getName()); 280 FetchGroup unionFetchGroup = new FetchGroup(unionGroupName.toString()); 281 unionFetchGroup.addAttributes(first.getAttributes()); 282 unionFetchGroup.addAttributes(second.getAttributes()); 283 return unionFetchGroup; 284 } 285 286 290 public void reset(Object source) { 291 ((FetchGroupTracker)source).resetFetchGroup(); 292 } 293 294 298 public void setObjectFetchGroup(Object source, FetchGroup fetchGroup) { 299 if (descriptor.getFetchGroupManager() != null) { 300 ((FetchGroupTracker)source).setFetchGroup(fetchGroup); 301 } 302 } 303 304 308 public void setRefreshOnFetchGroupToObject(Object source, boolean shouldRefreshOnFetchgroup) { 309 ((FetchGroupTracker)source).setShouldRefreshFetchGroup(shouldRefreshOnFetchgroup); 310 } 311 312 315 public boolean isAttributeFetched(Object object, String attributeName) { 316 FetchGroup fetchgroup = ((FetchGroupTracker)object).getFetchGroup(); 317 return (fetchgroup == null) || (fetchgroup.getAttributes().contains(attributeName)); 318 } 319 320 324 public ClassDescriptor getDescriptor() { 325 return descriptor; 326 } 327 328 332 public ClassDescriptor getClassDescriptor() { 333 return getDescriptor(); 334 } 335 336 339 public void setDescriptor(ClassDescriptor descriptor) { 340 this.descriptor = descriptor; 341 } 342 343 348 public void prepareQueryWithFetchGroup(ObjectLevelReadQuery query) { 349 query.initializeFetchGroup(); 351 if ((query.getFetchGroup() == null) || query.getFetchGroup().hasFetchGroupAttributeExpressions()) { 352 return; 354 } else { 355 if (query.isReportQuery()) { 356 throw QueryException.fetchGroupNotSupportOnReportQuery(); 358 } 359 if (query.hasPartialAttributeExpressions()) { 360 throw QueryException.fetchGroupNotSupportOnPartialAttributeReading(); 362 } 363 } 364 Set attributes = query.getFetchGroup().getAttributes(); 365 ObjectBuilder builder = query.getDescriptor().getObjectBuilder(); 366 367 Iterator pkMappingIter = builder.getPrimaryKeyMappings().iterator(); 369 370 while (pkMappingIter.hasNext()) { 371 DatabaseMapping pkMapping = (DatabaseMapping)pkMappingIter.next(); 372 DatabaseField pkField = pkMapping.getField(); 373 374 attributes.add(pkMapping.getAttributeName()); 376 } 377 378 OptimisticLockingPolicy lockingPolicy = getDescriptor().getOptimisticLockingPolicy(); 380 if (query.shouldMaintainCache() && (lockingPolicy != null)) { 381 lockingPolicy.prepareFetchGroupForReadQuery(query.getFetchGroup(), query); 382 } 383 384 Iterator attrIter = attributes.iterator(); 386 while (attrIter.hasNext()) { 387 String attrName = (String )attrIter.next(); 388 DatabaseMapping mapping = builder.getMappingForAttributeName(attrName); 389 if (mapping == null) { 390 throw QueryException.fetchGroupAttributeNotMapped(attrName); 392 } 393 394 if (mapping.isCollectionMapping()) { 396 query.getFetchGroup().addFetchGroupAttribute(query.getExpressionBuilder().anyOf(attrName)); 397 } else { 398 query.getFetchGroup().addFetchGroupAttribute(query.getExpressionBuilder().get(attrName)); 399 } 400 } 401 } 402 403 407 public Object clone() { 408 Object object = null; 409 try { 410 object = super.clone(); 411 } catch (Exception exception) { 412 ; 413 } 414 return object; 415 } 416 } 417 | Popular Tags |