Author: julien(a)jboss.com
Date: 2007-12-27 19:39:56 -0500 (Thu, 27 Dec 2007)
New Revision: 9399
Modified:
branches/presentation/core-presentation/src/main/org/jboss/portal/core/presentation/model/StructuralStateContextImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/client/controller/AjaxUIController.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/client/controller/UIController.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java
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/ManagedObject.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectContainer.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralStateContext.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockModelTestCase.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java
Log:
- replace children list by collection (which provide more flexibility)
- started to add backward references tracking between one2many and many2one associations
Modified:
branches/presentation/core-presentation/src/main/org/jboss/portal/core/presentation/model/StructuralStateContextImpl.java
===================================================================
---
branches/presentation/core-presentation/src/main/org/jboss/portal/core/presentation/model/StructuralStateContextImpl.java 2007-12-27
22:00:40 UTC (rev 9398)
+++
branches/presentation/core-presentation/src/main/org/jboss/portal/core/presentation/model/StructuralStateContextImpl.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -115,7 +115,7 @@
}
- public List<StructuralObject> loadChildren(StructuralObject parent)
+ public Collection<StructuralObject> loadChildren(StructuralObject parent)
{
// Get the PortalObject corresponding to this objectId from the
PortalObjectContainer
PortalObject portalObject =
this.portalObjectContainer.getObject(PortalObjectId.parse(parent.getId(),
PortalObjectPath.CANONICAL_FORMAT));
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/client/controller/AjaxUIController.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/client/controller/AjaxUIController.java 2007-12-27
22:00:40 UTC (rev 9398)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/client/controller/AjaxUIController.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -48,6 +48,7 @@
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.ArrayList;
+import java.util.Collection;
/**
* The UIServer is responsible for generating the output that is sent back to the client
based on the Portal state of a particular Portal Request
@@ -150,11 +151,10 @@
//TODO: set proper status
//TODO: set proper parent reference
- List<? extends UIObject> windows = page.getChildren();
+ Collection<? extends UIObject> windows = page.getChildren();
List clientWindows = new ArrayList();
- for(int i=0; i<windows.size(); i++)
+ for(UIObject cour : windows)
{
- UIObject cour = windows.get(i);
if(cour instanceof UIWindow)
{
UIWindow window = (UIWindow)cour;
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/client/controller/UIController.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/client/controller/UIController.java 2007-12-27
22:00:40 UTC (rev 9398)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/client/controller/UIController.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -48,6 +48,7 @@
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;
+import java.util.Collection;
/**
* The UIServer is responsible for generating the output that is sent back to the client
based on the Portal state of a particular Portal Request
@@ -336,10 +337,9 @@
if(uiObject instanceof UIPage)
{
UIPage page = (UIPage)uiObject;
- List<? extends UIObject> windows = page.getChildren();
- for(int i=0; i<windows.size(); i++)
+ Collection<? extends UIObject> windows = page.getChildren();
+ for(UIObject cour : windows)
{
- UIObject cour = windows.get(i);
if(cour instanceof UIWindow)
{
UIWindow window = (UIWindow)cour;
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java 2007-12-27
22:00:40 UTC (rev 9398)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -29,7 +29,7 @@
import org.jboss.portal.presentation.impl.model.container.spi.UIContainerObject;
import org.jboss.portal.presentation.impl.model.container.spi.UIObjectContext;
-import java.util.List;
+import java.util.Collection;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -114,7 +114,7 @@
return context.getManagedObject().getParent();
}
- public List<? extends UIObject> getChildren()
+ public Collection<? extends UIObject> getChildren()
{
return context.getManagedObject().getChildren();
}
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
22:00:40 UTC (rev 9398)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AssociationContext.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -27,10 +27,11 @@
import org.jboss.portal.presentation.model.state.StateException;
import org.jboss.portal.presentation.model.UIObject;
-import java.util.List;
-import java.util.ArrayList;
-import java.util.AbstractList;
import java.util.Iterator;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Collection;
+import java.util.AbstractSet;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -53,12 +54,16 @@
{
return owner;
}
+ protected OneToMany getOneToMany(UIContainerObject related)
+ {
+ return ((ObjectContext)related.getContext()).associationContext.children;
+ }
};
/** . */
final OneToMany children = new OneToMany()
{
- protected List<StructuralObject> doLoad()
+ protected Collection<StructuralObject> doLoad()
{
return
owner.container.structuralStateContext.loadChildren(owner.structuralObject);
}
@@ -66,6 +71,10 @@
{
return owner;
}
+ protected ManyToOne getManyToOne(UIContainerObject related)
+ {
+ return ((ObjectContext)related.getContext()).associationContext.parent;
+ }
};
static abstract class ManyToOne
@@ -77,8 +86,14 @@
/** . */
private UIContainerObject related;
+ /** . */
+ private UIContainerObject ref;
+
private ManyToOne()
{
+ this.loaded = false;
+ this.related = null;
+ this.ref = null;
}
boolean isLoaded()
@@ -97,16 +112,16 @@
throw new IllegalStateException("Cannot set parent of non loaded
association");
}
- // Update the related side
+ // Downgrade the related side
if (related != null)
{
-
+ detach();
}
//
if (newParent != null)
{
- related = newParent;
+ attach(newParent);
}
else
{
@@ -130,10 +145,34 @@
{
throw new IllegalStateException("Cannot clear parent of non loaded
association");
}
- related = null;
+
+ // Downgrade realted
+ detach();
+
+ //
loaded = false;
}
+ UIContainerObject getRelated()
+ {
+ ObjectContext owner = getOwner();
+
+ //
+ owner.checkAccess();
+
+ //
+ if (!loaded)
+ {
+ load();
+ }
+
+ //
+ owner.checkAccess();
+
+ //
+ return related;
+ }
+
/**
* Loads the related side.
*
@@ -144,6 +183,52 @@
protected abstract ObjectContext getOwner();
+ protected abstract OneToMany getOneToMany(UIContainerObject related);
+
+ private void attach(UIContainerObject related)
+ {
+ if (this.related != null)
+ {
+ throw new AssertionError("BUG");
+ }
+
+ //
+ OneToMany oneToMany = getOneToMany(related);
+ ObjectContext owner = getOwner();
+
+ //
+ if (oneToMany.refs.contains(owner))
+ {
+ throw new AssertionError("BUG");
+ }
+
+ //
+ oneToMany.refs.add(owner);
+ this.related = related;
+ }
+
+ private void detach()
+ {
+ if (related == null)
+ {
+ throw new AssertionError("BUG");
+ }
+
+ //
+ OneToMany oneToMany = getOneToMany(related);
+ ObjectContext owner = getOwner();
+
+ //
+ if (!oneToMany.refs.contains(owner))
+ {
+ throw new AssertionError("BUG");
+ }
+
+ //
+ oneToMany.refs.remove(owner);
+ this.related = null;
+ }
+
/**
* Attempt for loading the related side. If a failure occurs during while loading
the related side
* the load operation is aborted and the status of the association owner is updated
accordingly. If the loading
@@ -166,7 +251,7 @@
//
if (related != null)
{
- this.related = related;
+ attach(related);
//
ObjectContext parentContext = (ObjectContext)related.getContext();
@@ -185,7 +270,7 @@
owner.container.attach(related);
//
- this.related = related;
+ attach(related);
}
}
@@ -197,45 +282,29 @@
owner.updateStatus(e);
}
}
-
- UIContainerObject getRelated()
- {
- ObjectContext owner = getOwner();
-
- //
- owner.checkAccess();
-
- //
- if (!loaded)
- {
- load();
- }
-
- //
- owner.checkAccess();
-
- //
- return related;
- }
}
static abstract class OneToMany
{
/** . */
- private final ProxyList list;
+ private final LazySet list;
/** . */
private boolean loaded;
/** . */
- private List<UIContainerObject> relateds;
+ private Set<UIContainerObject> relateds;
+ /** . */
+ private Set<ObjectContext> refs;
+
private OneToMany()
{
- this.list = new ProxyList();
+ this.list = new LazySet();
this.loaded = false;
- this.relateds = new ArrayList<UIContainerObject>();
+ this.relateds = new HashSet<UIContainerObject>();
+ this.refs = new HashSet<ObjectContext>();
}
boolean isLoaded()
@@ -263,7 +332,7 @@
relateds.add(related);
}
- void setLoadedRelateds(List<UIContainerObject> relateds)
+ void setLoadedRelateds(Set<UIContainerObject> relateds)
{
if (!isLoaded())
{
@@ -295,7 +364,7 @@
}
}
- List<UIContainerObject> getRelateds()
+ Collection<UIContainerObject> getRelateds()
{
return list;
}
@@ -306,10 +375,12 @@
* @return the list of structural objects
* @throws StateException if the load operation cannot be achieved
*/
- protected abstract List<StructuralObject> doLoad() throws StateException;
+ protected abstract Collection<StructuralObject> doLoad() throws
StateException;
protected abstract ObjectContext getOwner();
+ protected abstract ManyToOne getManyToOne(UIContainerObject related);
+
private void load()
{
ObjectContext owner = getOwner();
@@ -317,7 +388,7 @@
//
try
{
- ArrayList<UIContainerObject> relateds = new
ArrayList<UIContainerObject>();
+ Set<UIContainerObject> relateds = new
HashSet<UIContainerObject>();
//
for (StructuralObject relatedSO : doLoad())
@@ -357,11 +428,12 @@
}
}
- private class ProxyList extends AbstractList<UIContainerObject>
+ private class LazySet extends AbstractSet<UIContainerObject>
{
- public UIContainerObject get(int i)
+
+ public Iterator<UIContainerObject> iterator()
{
- ObjectContext owner = getOwner();
+ final ObjectContext owner = getOwner();
//
owner.checkAccess();
@@ -376,7 +448,35 @@
owner.checkAccess();
//
- return relateds.get(i);
+ final Iterator<UIContainerObject> iterator = relateds.iterator();
+
+ //
+ return new Iterator<UIContainerObject>()
+ {
+ public boolean hasNext()
+ {
+ owner.checkAccess();
+
+ //
+ return iterator.hasNext();
+ }
+
+ public UIContainerObject next()
+ {
+ owner.checkAccess();
+
+ //
+ return iterator.next();
+ }
+
+ public void remove()
+ {
+ owner.checkAccess();
+
+ //
+ throw new UnsupportedOperationException();
+ }
+ };
}
public int size()
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ManagedObject.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ManagedObject.java 2007-12-27
22:00:40 UTC (rev 9398)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ManagedObject.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -37,6 +37,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Collection;
/**
* Implement base fonctionnality of the <code>UIObject</code> interface.
@@ -283,7 +284,7 @@
return context.associationContext.parent.getRelated();
}
- public final List<UIContainerObject> getChildren()
+ public final Collection<UIContainerObject> getChildren()
{
context.checkAccess();
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectContainer.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectContainer.java 2007-12-27
22:00:40 UTC (rev 9398)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectContainer.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -39,6 +39,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
+import java.util.HashSet;
/**
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
@@ -419,7 +421,7 @@
boolean refreshChildren = scope.enterChildren(object, loaded);
// The new list of children
- List<UIContainerObject> children = new
ArrayList<UIContainerObject>();
+ Set<UIContainerObject> children = new
HashSet<UIContainerObject>();
// Take care of the added children
for (StructuralObject addedSO : refresh.getAddedChildren())
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java 2007-12-27
22:00:40 UTC (rev 9398)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -25,7 +25,7 @@
import org.jboss.portal.presentation.model.state.StateException;
import org.jboss.portal.presentation.model.state.ConstraintViolationException;
-import java.util.List;
+import java.util.Collection;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -183,7 +183,7 @@
*
* @return the list of children
*/
- List<? extends UIObject> getChildren();
+ Collection<? extends UIObject> getChildren();
/**
* Destroy a named child.
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralStateContext.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralStateContext.java 2007-12-27
22:00:40 UTC (rev 9398)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralStateContext.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -29,6 +29,7 @@
import java.util.List;
import java.util.Map;
+import java.util.Collection;
/**
* The structural state context.
@@ -56,7 +57,7 @@
* @throws IllegalArgumentException if the provided object is null
* @throws StateException any useful exception that would make the object non valid
*/
- List<StructuralObject> loadChildren(StructuralObject parent) throws
IllegalArgumentException, StateException;
+ Collection<StructuralObject> loadChildren(StructuralObject parent) throws
IllegalArgumentException, StateException;
/**
* Load the parent of the specified object.
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockModelTestCase.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockModelTestCase.java 2007-12-27
22:00:40 UTC (rev 9398)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockModelTestCase.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -42,6 +42,7 @@
import java.util.HashMap;
import java.util.Set;
import java.util.Map;
+import java.util.Collection;
/**
* Test that the mock model we are using behaves in an expected manner
@@ -225,10 +226,10 @@
assertEquals(rootId, root.getId());
//
- List<StructuralObject> children = ssc.loadChildren(root);
+ Collection<StructuralObject> children = ssc.loadChildren(root);
assertNotNull(children);
assertEquals(1, children.size());
- StructuralObject foo = children.get(0);
+ StructuralObject foo = children.iterator().next();
assertNotNull(foo);
assertEquals(fooId, foo.getId());
StructuralState fooState = foo.getState();
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-12-27
22:00:40 UTC (rev 9398)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -43,10 +43,9 @@
import java.util.Collections;
import java.util.HashMap;
-import java.util.List;
import java.util.HashSet;
import java.util.Map;
-import java.util.ArrayList;
+import java.util.Collection;
/**
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
@@ -151,29 +150,40 @@
UIContext context = createContext();
//
- List<? extends UIObject> children = context.getChildren();
+ Collection<? extends UIObject> children = context.getChildren();
assertNotNull(children);
assertEquals(2, children.size());
+ Map childrenMap = toMap(children);
//
- UIPortal defaultPortal = (UIPortal)children.get(0);
+ UIPortal defaultPortal = (UIPortal)childrenMap.get(mockDefaultPortal.getId());
assertNotNull(defaultPortal);
assertSame(defaultPortal, context.getObject(defaultPortal.getId()));
assertEquals("defaultPortal", defaultPortal.getName());
assertEquals(mockDefaultPortal.getId(), defaultPortal.getId());
assertSame(context, defaultPortal.getParent());
- assertEquals(Collections.EMPTY_LIST, defaultPortal.getChildren());
+ assertEquals(0, defaultPortal.getChildren().size());
//
- UIPortal someOtherPortal = (UIPortal)children.get(1);
+ UIPortal someOtherPortal = (UIPortal)childrenMap.get(mockSomeOtherPortal.getId());
assertNotNull(someOtherPortal);
assertSame(someOtherPortal, context.getObject(someOtherPortal.getId()));
assertEquals("someOtherPortal", someOtherPortal.getName());
assertEquals(mockSomeOtherPortal.getId(), someOtherPortal.getId());
assertSame(context, someOtherPortal.getParent());
- assertEquals(Collections.EMPTY_LIST, defaultPortal.getChildren());
+ assertEquals(0, defaultPortal.getChildren().size());
}
+ Map<String, UIObject> toMap(Collection<? extends UIObject> objects)
+ {
+ Map<String, UIObject> map = new HashMap<String, UIObject>();
+ for (UIObject o : objects)
+ {
+ map.put(o.getId(), o);
+ }
+ return map;
+ }
+
public void testLoadChildrenFromStale() throws Exception
{
MockObject mockRoot = model.getRoot();
@@ -189,7 +199,7 @@
// Access children
try
{
- List<? extends UIObject> children = foo.getChildren();
+ Collection<? extends UIObject> children = foo.getChildren();
for (UIObject child : children)
{
}
@@ -244,7 +254,7 @@
// Access children
try
{
- List<? extends UIObject> children = foo.getChildren();
+ Collection<? extends UIObject> children = foo.getChildren();
for (UIObject child : children)
{
}
@@ -551,7 +561,7 @@
context.addModelListener(eventAssert);
//
- List<? extends UIObject> rootChildren = context.getChildren();
+ Collection<? extends UIObject> rootChildren = context.getChildren();
assertNotNull(rootChildren);
assertEquals(0, rootChildren.size());
assertNull(context.getChild("foo"));
@@ -581,7 +591,7 @@
// Load only the descendant
UIObject daa = context.getObject(daaId);
- List<? extends UIObject> daaChildren = daa.getChildren();
+ Collection<? extends UIObject> daaChildren = daa.getChildren();
// Destroy foo
context.destroyChild("foo");
@@ -618,9 +628,9 @@
UIObject foo = context.getChild("foo");
UIObject juu = foo.getChild("juu");
UIObject daa = juu.getChild("daa");
- List<? extends UIObject> fooChildren = foo.getChildren();
- List<? extends UIObject> juuChildren = juu.getChildren();
- List<? extends UIObject> daaChildren = daa.getChildren();
+ Collection<? extends UIObject> fooChildren = foo.getChildren();
+ Collection<? extends UIObject> juuChildren = juu.getChildren();
+ Collection<? extends UIObject> daaChildren = daa.getChildren();
//
foo.destroyChild("juu");
@@ -709,7 +719,7 @@
mockJuu.setPropertyValue("abc", "def");
// Now navigate
- List<? extends UIObject> fooChildren = foo.getChildren();
+ Collection<? extends UIObject> fooChildren = foo.getChildren();
//
assertNotNull(fooChildren);
@@ -739,7 +749,7 @@
UIObject juu = context.getObject(juuId);
// Now navigate
- List<? extends UIObject> fooChildren = foo.getChildren();
+ Collection<? extends UIObject> fooChildren = foo.getChildren();
//
assertNotNull(fooChildren);
@@ -806,12 +816,12 @@
//
UIObject foo = context.getChild("foo");
- List<? extends UIObject> fooChildren = foo.getChildren();
+ Collection<? extends UIObject> fooChildren = foo.getChildren();
UIObject fooJuu = foo.getChild("juu");
UIObject fooDaa = foo.getChild("daa");
UIObject bar = context.getChild("bar");
UIObject barDaa = bar.getChild("daa");
- List<? extends UIObject> barChildren = bar.getChildren();
+ Collection<? extends UIObject> barChildren = bar.getChildren();
//
fooJuu.move(bar);
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java 2007-12-27
22:00:40 UTC (rev 9398)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java 2007-12-28
00:39:56 UTC (rev 9399)
@@ -42,6 +42,7 @@
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
+import java.util.Collection;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -337,7 +338,7 @@
}
}
- public List<StructuralObject> loadChildren(StructuralObject parent)
+ public Collection<StructuralObject> loadChildren(StructuralObject parent)
{
MockObject mockParent = getValidMockObject(parent);