Author: julien(a)jboss.com
Date: 2007-12-27 12:39:08 -0500 (Thu, 27 Dec 2007)
New Revision: 9392
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/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/test/model/ModelTestCase.java
Log:
encapsulate more code in the AssociationContext class
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:00:47 UTC (rev 9391)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AssociationContext.java 2007-12-27
17:39:08 UTC (rev 9392)
@@ -29,6 +29,7 @@
import java.util.List;
import java.util.ArrayList;
import java.util.AbstractList;
+import java.util.Iterator;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -37,30 +38,77 @@
public class AssociationContext
{
+ /** . */
ObjectContext owner;
+ /** . */
final ManyToOne parent = new ManyToOne();
+ /** . */
final OneToMany children = new OneToMany();
-
class ManyToOne
{
/** . */
- boolean loaded;
+ private boolean loaded;
/** . */
- UIContainerObject related;
+ private UIContainerObject related;
private ManyToOne()
{
}
- UIContainerObject get()
+ boolean isLoaded()
{
+ return loaded;
+ }
+
+ void setLoadedRelated(UIContainerObject newParent)
+ {
+ if (newParent == null)
+ {
+ throw new IllegalArgumentException();
+ }
if (!loaded)
{
+ throw new IllegalStateException("Cannot set parent of non loaded
association");
+ }
+ if (newParent != null)
+ {
+ related = newParent;
+ }
+ else
+ {
+ related = null;
+ loaded = false;
+ }
+ }
+
+ UIContainerObject getLoadedRelated()
+ {
+ if (!loaded)
+ {
+ throw new IllegalStateException("Cannot set parent of non loaded
association");
+ }
+ return related;
+ }
+
+ void clearLoadedRelated()
+ {
+ if (!loaded)
+ {
+ throw new IllegalStateException("Cannot clear parent of non loaded
association");
+ }
+ related = null;
+ loaded = false;
+ }
+
+ UIContainerObject getRelated()
+ {
+ if (!loaded)
+ {
StructuralObject parentSO =
owner.container.structuralStateContext.loadParent(owner.structuralObject);
// If null it is the root so nothing is done
@@ -107,10 +155,10 @@
{
/** . */
- final ProxyList list;
+ private final ProxyList list;
/** . */
- List<UIContainerObject> relateds;
+ private List<UIContainerObject> relateds;
private OneToMany()
{
@@ -118,8 +166,64 @@
this.relateds = null;
}
- List<UIContainerObject> get()
+ boolean isLoaded()
{
+ return relateds != null;
+ }
+
+ void addLoadedRelated(UIContainerObject related)
+ {
+ if (relateds == null)
+ {
+ throw new IllegalStateException("Not loaded");
+ }
+ if (related == null)
+ {
+ throw new IllegalArgumentException("No null child accepted");
+ }
+ for (UIContainerObject r : relateds)
+ {
+ if (related.getId().equals(r.getId()))
+ {
+ throw new IllegalStateException("Cannot add duplicate");
+ }
+ }
+ relateds.add(related);
+ }
+
+ void setLoadedRelateds(List<UIContainerObject> relateds)
+ {
+ if (relateds == null)
+ {
+ throw new IllegalStateException("Not loaded");
+ }
+ }
+
+ void removeLoadedRelated(String relatedId)
+ {
+ if (relateds == null)
+ {
+ throw new IllegalStateException("Not loaded");
+ }
+ if (relatedId == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ for (Iterator<UIContainerObject> i = relateds.iterator();i.hasNext();)
+ {
+ UIContainerObject child = i.next();
+
+ //
+ if (relatedId.equals(child.getId()))
+ {
+ i.remove();
+ break;
+ }
+ }
+ }
+
+ List<UIContainerObject> getRelateds()
+ {
return list;
}
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
17:00:47 UTC (rev 9391)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ManagedObject.java 2007-12-27
17:39:08 UTC (rev 9392)
@@ -289,7 +289,7 @@
}
//
- return context.associationContext.parent.get();
+ return context.associationContext.parent.getRelated();
}
public final List<UIContainerObject> getChildren()
@@ -300,7 +300,7 @@
}
//
- return context.associationContext.children.get();
+ return context.associationContext.children.getRelateds();
}
public final <T extends UIObject> T createChild(String name, Class<T>
type) throws IllegalArgumentException
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
17:00:47 UTC (rev 9391)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectContainer.java 2007-12-27
17:39:08 UTC (rev 9392)
@@ -39,7 +39,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Iterator;
/**
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
@@ -156,7 +155,7 @@
parentContext.structuralObject = creation.getParent();
//
- if (parentContext.associationContext.children.relateds != null)
+ if (parentContext.associationContext.children.isLoaded())
{
UIContainerObject child = create(creation.getChild());
@@ -164,7 +163,7 @@
put(child);
//
- parentContext.associationContext.children.relateds.add(child);
+ parentContext.associationContext.children.addLoadedRelated(child);
}
}
else if (change instanceof StructuralObject.Destruction)
@@ -183,19 +182,13 @@
parentContext.structuralObject = destruction.getParent();
//
- if (parentContext.associationContext.children.relateds != null)
+ if (parentContext.associationContext.children.isLoaded())
{
- for (Iterator<UIContainerObject> i =
parentContext.associationContext.children.relateds.iterator();i.hasNext();)
- {
- UIContainerObject child = i.next();
+ // Get the id which is the destroyed object
+ String destroyedId = destruction.getIds().get(destruction.getIds().size()
- 1);
- //
- if (destruction.getIds().contains(child.getId()))
- {
- i.remove();
- break;
- }
- }
+ //
+
parentContext.associationContext.children.removeLoadedRelated(destroyedId);
}
}
@@ -230,19 +223,9 @@
parentContext.structuralObject = move.getParent();
// Remove the child if the relationship is loaded on this side
- if (parentContext.associationContext.children.relateds != null)
+ if (parentContext.associationContext.children.isLoaded())
{
- for (Iterator<UIContainerObject> i =
parentContext.associationContext.children.relateds.iterator();i.hasNext();)
- {
- UIContainerObject child = i.next();
-
- //
- if (child.getId().equals(move.getSource().getId()))
- {
- i.remove();
- break;
- }
- }
+
parentContext.associationContext.children.removeLoadedRelated(move.getSource().getId());
}
}
@@ -255,7 +238,7 @@
destinationContext.structuralObject = move.getDestination();
//
- if (destinationContext.associationContext.children.relateds != null)
+ if (destinationContext.associationContext.children.isLoaded())
{
// Now we really need to add the source as we load collections entirely
if (source == null)
@@ -267,7 +250,7 @@
}
//
- destinationContext.associationContext.children.relateds.add(source);
+ destinationContext.associationContext.children.addLoadedRelated(source);
}
}
@@ -280,16 +263,15 @@
sourceContext.structuralObject = move.getSource();
//
- if (sourceContext.associationContext.parent.loaded)
+ if (sourceContext.associationContext.parent.isLoaded())
{
if (destination != null)
{
- sourceContext.associationContext.parent.related = destination;
+ sourceContext.associationContext.parent.setLoadedRelated(destination);
}
else
{
- sourceContext.associationContext.parent.related = null;
- sourceContext.associationContext.parent.loaded = false;
+ sourceContext.associationContext.parent.clearLoadedRelated();
}
}
}
@@ -389,13 +371,13 @@
// Continue only if it is valid
if (context.isValid())
{
- boolean loaded = context.associationContext.children.relateds != null;
+ boolean loaded = context.associationContext.children.isLoaded();
// Loading children will never make the current object invalid
// but it could make some already loaded children invalid
if (scope.enterChildren(object, loaded))
{
- for (UIContainerObject child :
context.associationContext.children.get())
+ for (UIContainerObject child :
context.associationContext.children.getRelateds())
{
ObjectContext childContext = (ObjectContext)child.getContext();
@@ -429,7 +411,7 @@
// Update the structural state
if (!context.structuralObject.compareTo(refresh.getObject()))
{
- boolean loaded = context.associationContext.children.relateds !=
null;
+ boolean loaded = context.associationContext.children.isLoaded();
//
if (loaded)
@@ -450,15 +432,15 @@
ObjectContext addedContext =
(ObjectContext)added.getContext();
// Set the parent as stale if needed
- if (addedContext.associationContext.parent.loaded)
+ if (addedContext.associationContext.parent.isLoaded())
{
- ObjectContext addedParentContext =
(ObjectContext)addedContext.associationContext.parent.related.getContext();
+ ObjectContext addedParentContext =
(ObjectContext)addedContext.associationContext.parent.getLoadedRelated().getContext();
// Mark the parent as stale
addedParentContext.status = UIObject.Status.STALE;
//
- addedParentContext.associationContext.parent.related =
get(context.structuralObject.getId());
+
addedParentContext.associationContext.parent.setLoadedRelated(get(context.structuralObject.getId()));
}
}
else
@@ -515,7 +497,7 @@
// Update state
context.structuralObject = refresh.getObject();
context.status = UIObject.Status.VALID;
- context.associationContext.children.relateds = children;
+ context.associationContext.children.setLoadedRelateds(children);
}
}
}
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
17:00:47 UTC (rev 9391)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-12-27
17:39:08 UTC (rev 9392)
@@ -45,7 +45,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.HashSet;
-import java.util.Set;
import java.util.Map;
/**
Show replies by date