Author: julien(a)jboss.com
Date: 2007-12-27 15:16:20 -0500 (Thu, 27 Dec 2007)
New Revision: 9393
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AssociationContext.java
Log:
improved state management of association context
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AssociationContext.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AssociationContext.java 2007-12-27
17:39:08 UTC (rev 9392)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AssociationContext.java 2007-12-27
20:16:20 UTC (rev 9393)
@@ -42,12 +42,24 @@
ObjectContext owner;
/** . */
- final ManyToOne parent = new ManyToOne();
+ final ManyToOne parent = new ManyToOne()
+ {
+ protected StructuralObject doLoad()
+ {
+ return
owner.container.structuralStateContext.loadParent(owner.structuralObject);
+ }
+ };
/** . */
- final OneToMany children = new OneToMany();
+ final OneToMany children = new OneToMany()
+ {
+ protected List<StructuralObject> doLoad()
+ {
+ return
owner.container.structuralStateContext.loadChildren(owner.structuralObject);
+ }
+ };
- class ManyToOne
+ abstract class ManyToOne
{
/** . */
@@ -105,75 +117,86 @@
loaded = false;
}
- UIContainerObject getRelated()
+ protected abstract StructuralObject doLoad();
+
+ private void load()
{
- if (!loaded)
+ StructuralObject relatedSO = doLoad();
+
+ // If null it is the root so nothing is done
+ if (relatedSO != null)
{
- StructuralObject parentSO =
owner.container.structuralStateContext.loadParent(owner.structuralObject);
+ UIContainerObject related = owner.container.get(relatedSO);
- // If null it is the root so nothing is done
- if (parentSO != null)
+ //
+ if (related != null)
{
- UIContainerObject parent = owner.container.get(parentSO);
+ this.related = related;
//
- if (parent != null)
+ ObjectContext parentContext = (ObjectContext)related.getContext();
+
+ //
+ if (!relatedSO.compareTo(parentContext.structuralObject))
{
- related = parent;
-
- //
- ObjectContext parentContext = (ObjectContext)parent.getContext();
-
- //
- if (!parentSO.compareTo(parentContext.structuralObject))
- {
- parentContext.status = UIObject.Status.STALE;
- }
+ parentContext.status = UIObject.Status.STALE;
}
- else
- {
- parent = owner.container.create(parentSO);
+ }
+ else
+ {
+ related = owner.container.create(relatedSO);
- //
- owner.container.put(parent);
+ //
+ owner.container.put(related);
- //
- related = parent;
- }
+ //
+ this.related = related;
}
}
- // Try to resolve the relationship
+ //
+ this.loaded = true;
+ }
+ UIContainerObject getRelated()
+ {
+ if (!loaded)
+ {
+ load();
+ }
//
return related;
}
}
- class OneToMany
+ abstract class OneToMany
{
/** . */
private final ProxyList list;
/** . */
+ private boolean loaded;
+
+ /** . */
private List<UIContainerObject> relateds;
private OneToMany()
{
this.list = new ProxyList();
- this.relateds = null;
+ this.loaded = false;
+ this.relateds = new ArrayList<UIContainerObject>();
}
boolean isLoaded()
{
- return relateds != null;
+ return loaded;
}
void addLoadedRelated(UIContainerObject related)
{
- if (relateds == null)
+ if (!isLoaded())
{
throw new IllegalStateException("Not loaded");
}
@@ -193,15 +216,16 @@
void setLoadedRelateds(List<UIContainerObject> relateds)
{
- if (relateds == null)
+ if (!isLoaded())
{
throw new IllegalStateException("Not loaded");
}
+ this.relateds = relateds;
}
void removeLoadedRelated(String relatedId)
{
- if (relateds == null)
+ if (!isLoaded())
{
throw new IllegalStateException("Not loaded");
}
@@ -227,40 +251,43 @@
return list;
}
- private List<UIContainerObject> getChildren(StructuralObject
structuralObject)
+ protected abstract List<StructuralObject> doLoad();
+
+ private void load()
{
- ArrayList<UIContainerObject> children = new
ArrayList<UIContainerObject>();
+ ArrayList<UIContainerObject> relateds = new
ArrayList<UIContainerObject>();
//
- for (StructuralObject childSO :
owner.container.structuralStateContext.loadChildren(structuralObject))
+ for (StructuralObject relatedSO : doLoad())
{
- UIContainerObject child = owner.container.get(childSO);
+ UIContainerObject related = owner.container.get(relatedSO);
//
- if (child != null)
+ if (related != null)
{
- ObjectContext childContext = (ObjectContext)child.getContext();
+ ObjectContext relatedContext = (ObjectContext)related.getContext();
//
- if (!childContext.structuralObject.compareTo(childSO))
+ if (!relatedContext.structuralObject.compareTo(relatedSO))
{
- childContext.status = UIObject.Status.STALE;
+ relatedContext.status = UIObject.Status.STALE;
}
}
else
{
- child = owner.container.create(childSO);
+ related = owner.container.create(relatedSO);
//
- owner.container.put(child);
+ owner.container.put(related);
}
//
- children.add(child);
+ relateds.add(related);
}
//
- return children;
+ this.relateds = relateds;
+ this.loaded = true;
}
private class ProxyList extends AbstractList<UIContainerObject>
@@ -273,9 +300,9 @@
}
// Load the entire relationship
- if (relateds == null)
+ if (!isLoaded())
{
- relateds = getChildren(owner.structuralObject);
+ load();
}
//
@@ -288,9 +315,9 @@
{
throw new IllegalStateException("Relationship cannot be used because
the owner " + owner + " is not valid");
}
- if (relateds == null)
+ if (!isLoaded())
{
- relateds = getChildren(owner.structuralObject);
+ load();
}
return relateds.size();
}
Show replies by date