Author: julien(a)jboss.com
Date: 2007-12-29 17:56:28 -0500 (Sat, 29 Dec 2007)
New Revision: 9405
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/RelationshipContext.java
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/ObjectContext.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectContainer.java
Log:
rename Associationcontext to RelationshipContext for more clarity
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-29
22:51:43 UTC (rev 9404)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ManagedObject.java 2007-12-29
22:56:28 UTC (rev 9405)
@@ -27,7 +27,6 @@
import org.jboss.portal.presentation.model.event.state.StateChange;
import org.jboss.portal.presentation.model.event.state.StateChangeEvent;
import
org.jboss.portal.presentation.model.event.state.navigational.NavigationalStateModification;
-import
org.jboss.portal.presentation.model.event.state.structural.StructuralStateModification;
import org.jboss.portal.presentation.model.state.StateChangeVetoException;
import org.jboss.portal.presentation.model.state.StateException;
import org.jboss.portal.presentation.model.state.structural.StructuralObject;
@@ -35,7 +34,6 @@
import java.util.Collections;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.Collection;
@@ -274,7 +272,7 @@
context.checkAccess();
//
- return context.associationContext.parent.getRelated();
+ return context.relationshipContext.parent.getRelated();
}
public final Collection<UIContainerObject> getChildren()
@@ -282,7 +280,7 @@
context.checkAccess();
//
- return context.associationContext.children.getRelateds();
+ return context.relationshipContext.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/ObjectContext.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ObjectContext.java 2007-12-29
22:51:43 UTC (rev 9404)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ObjectContext.java 2007-12-29
22:56:28 UTC (rev 9405)
@@ -52,7 +52,7 @@
final ManagedObject managedObject;
/** The associations. */
- AssociationContext associationContext;
+ RelationshipContext relationshipContext;
/** What we know from the structural context. */
StructuralObject structuralObject;
Copied:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/RelationshipContext.java
(from rev 9400,
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/RelationshipContext.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/RelationshipContext.java 2007-12-29
22:56:28 UTC (rev 9405)
@@ -0,0 +1,568 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.presentation.impl.model.container;
+
+import org.jboss.portal.presentation.impl.model.container.spi.UIContainerObject;
+import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.model.state.StateException;
+import org.jboss.portal.presentation.model.UIObject;
+
+import java.util.Iterator;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Collection;
+import java.util.AbstractSet;
+import java.util.ArrayList;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+final class RelationshipContext
+{
+
+ /** . */
+ ObjectContext owner;
+
+ /** . */
+ final ManyToOne parent = new ManyToOne()
+ {
+ protected StructuralObject doLoad()
+ {
+ return
owner.container.structuralStateContext.loadParent(owner.structuralObject);
+ }
+ protected ObjectContext getOwner()
+ {
+ return owner;
+ }
+ protected OneToMany getOneToMany(UIContainerObject related)
+ {
+ return ((ObjectContext)related.getContext()).relationshipContext.children;
+ }
+ };
+
+ /** . */
+ final OneToMany children = new OneToMany()
+ {
+ protected Collection<StructuralObject> doLoad()
+ {
+ return
owner.container.structuralStateContext.loadChildren(owner.structuralObject);
+ }
+ protected ObjectContext getOwner()
+ {
+ return owner;
+ }
+ protected ManyToOne getManyToOne(UIContainerObject related)
+ {
+ return ((ObjectContext)related.getContext()).relationshipContext.parent;
+ }
+ };
+
+ static abstract class ManyToOne
+ {
+
+ /** . */
+ private boolean loaded;
+
+ /** . */
+ private UIContainerObject related;
+
+ /** The context pointing at us via a OneToMany. */
+ private Set<ObjectContext> refs;
+
+ private ManyToOne()
+ {
+ this.loaded = false;
+ this.related = null;
+ this.refs = new HashSet<ObjectContext>();
+ }
+
+ Set<ObjectContext> getReferences()
+ {
+ return refs;
+ }
+
+ 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");
+ }
+
+ // Downgrade the related side
+ if (related != null)
+ {
+ detach();
+ }
+
+ //
+ if (newParent != null)
+ {
+ attach(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");
+ }
+
+ // Downgrade realted
+ detach();
+
+ //
+ loaded = false;
+ }
+
+ UIContainerObject getRelated()
+ {
+ ObjectContext owner = getOwner();
+
+ //
+ owner.checkAccess();
+
+ //
+ if (!loaded)
+ {
+ load();
+ }
+
+ //
+ owner.checkAccess();
+
+ //
+ return related;
+ }
+
+ /**
+ * Loads the related side.
+ *
+ * @return the loaded structural object
+ * @throws StateException if the load operation cannot be achieved
+ */
+ protected abstract StructuralObject doLoad() throws StateException;
+
+ 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
+ * of the related side is succesful the association is updated.
+ */
+ private void load()
+ {
+ ObjectContext owner = getOwner();
+
+ //
+ try
+ {
+ StructuralObject relatedSO = doLoad();
+
+ // If null it is the root so nothing is done
+ if (relatedSO != null)
+ {
+ UIContainerObject related = owner.container.get(relatedSO);
+
+ //
+ if (related != null)
+ {
+ attach(related);
+
+ //
+ ObjectContext parentContext = (ObjectContext)related.getContext();
+
+ //
+ if (!relatedSO.compareTo(parentContext.structuralObject))
+ {
+ parentContext.status = UIObject.Status.STALE;
+ }
+ }
+ else
+ {
+ related = owner.container.create(relatedSO);
+
+ //
+ owner.container.attach(related);
+
+ //
+ attach(related);
+ }
+ }
+
+ // Set as loaded
+ this.loaded = true;
+ }
+ catch (StateException e)
+ {
+ owner.updateStatus(e);
+ }
+ }
+ }
+
+ static abstract class OneToMany
+ {
+
+ /** . */
+ private final LazySet list;
+
+ /** . */
+ private boolean loaded;
+
+ /** . */
+ private Set<UIContainerObject> relateds;
+
+ /** The contexts pointing at us via a ManyToOne. */
+ private Set<ObjectContext> refs;
+
+ private OneToMany()
+ {
+ this.list = new LazySet();
+ this.loaded = false;
+ this.relateds = new HashSet<UIContainerObject>();
+ this.refs = new HashSet<ObjectContext>();
+ }
+
+ boolean isLoaded()
+ {
+ return loaded;
+ }
+
+ void addLoadedRelated(UIContainerObject related)
+ {
+ if (!isLoaded())
+ {
+ 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");
+ }
+ }
+
+ //
+ attach(related);
+ }
+
+ void setLoadedRelateds(Set<UIContainerObject> relateds)
+ {
+ if (!isLoaded())
+ {
+ throw new IllegalStateException("Not loaded");
+ }
+
+ //
+ for (UIContainerObject related : new
ArrayList<UIContainerObject>(this.relateds))
+ {
+ detach(related);
+ }
+
+ //
+ for (UIContainerObject related : relateds)
+ {
+ attach(related);
+ }
+ }
+
+ UIContainerObject removeLoadedRelated(String relatedId)
+ {
+ if (!isLoaded())
+ {
+ throw new IllegalStateException("Not loaded");
+ }
+ if (relatedId == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ for (UIContainerObject related : relateds)
+ {
+ if (relatedId.equals(related.getId()))
+ {
+ detach(related);
+ return related;
+ }
+ }
+
+ //
+ throw new AssertionError("BUG");
+ }
+
+ Collection<UIContainerObject> getRelateds()
+ {
+ return list;
+ }
+
+ Set<ObjectContext> getReferences()
+ {
+ return refs;
+ }
+
+ /**
+ * Load the related side.
+ *
+ * @return the list of structural objects
+ * @throws StateException if the load operation cannot be achieved
+ */
+ protected abstract Collection<StructuralObject> doLoad() throws
StateException;
+
+ protected abstract ObjectContext getOwner();
+
+ protected abstract ManyToOne getManyToOne(UIContainerObject related);
+
+ private void detach(UIContainerObject related)
+ {
+ ManyToOne manyToOne = getManyToOne(related);
+
+ //
+ ObjectContext owner = getOwner();
+
+ //
+ if (!manyToOne.refs.contains(owner))
+ {
+ throw new AssertionError("BUG");
+ }
+
+ //
+ manyToOne.refs.remove(owner);
+ relateds.remove(related);
+ }
+
+ private void attach(UIContainerObject related)
+ {
+ ManyToOne manyToOne = getManyToOne(related);
+
+ //
+ ObjectContext owner = getOwner();
+
+ //
+ if (manyToOne.refs.contains(owner))
+ {
+ throw new AssertionError("Context already referenced by the provided
object");
+ }
+
+ //
+ manyToOne.refs.add(owner);
+ relateds.add(related);
+ }
+
+ private void load()
+ {
+ ObjectContext owner = getOwner();
+
+ //
+ try
+ {
+ for (StructuralObject relatedSO : doLoad())
+ {
+ UIContainerObject related = owner.container.get(relatedSO);
+
+ //
+ if (related != null)
+ {
+ ObjectContext relatedContext = (ObjectContext)related.getContext();
+
+ //
+ if (!relatedContext.structuralObject.compareTo(relatedSO))
+ {
+ relatedContext.status = UIObject.Status.STALE;
+ }
+ }
+ else
+ {
+ related = owner.container.create(relatedSO);
+
+ //
+ owner.container.attach(related);
+ }
+
+ //
+ attach(related);
+ }
+
+ //
+ this.loaded = true;
+ }
+ catch (StateException e)
+ {
+ owner.updateStatus(e);
+ }
+ }
+
+ private class LazySet extends AbstractSet<UIContainerObject>
+ {
+
+ public Iterator<UIContainerObject> iterator()
+ {
+ final ObjectContext owner = getOwner();
+
+ //
+ owner.checkAccess();
+
+ // Load the entire relationship
+ if (!isLoaded())
+ {
+ load();
+ }
+
+ //
+ owner.checkAccess();
+
+ //
+ 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()
+ {
+ ObjectContext owner = getOwner();
+
+ //
+ owner.checkAccess();
+
+ //
+ if (!isLoaded())
+ {
+ load();
+ }
+
+ //
+ owner.checkAccess();
+
+ //
+ return relateds.size();
+ }
+
+ public String toString()
+ {
+ return "ProxyList[" + getOwner() + "]";
+ }
+ }
+ }
+}
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-29
22:51:43 UTC (rev 9404)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectContainer.java 2007-12-29
22:56:28 UTC (rev 9405)
@@ -37,14 +37,11 @@
import org.jboss.portal.presentation.model.state.StateException;
import org.jboss.portal.presentation.impl.model.container.spi.UIContainerObject;
import org.jboss.portal.presentation.impl.model.container.spi.UIObjectFactory;
-import org.jboss.portal.common.NotYetImplemented;
import java.util.ArrayList;
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>
@@ -98,11 +95,11 @@
// Create context
ObjectContext objectContext = new ObjectContext(this, object, structuralObject);
- AssociationContext associationContext = new AssociationContext();
+ RelationshipContext relationshipContext = new RelationshipContext();
// Wire
- objectContext.associationContext = associationContext;
- associationContext.owner = objectContext;
+ objectContext.relationshipContext = relationshipContext;
+ relationshipContext.owner = objectContext;
// Contextualize
object.setContext(objectContext);
@@ -170,7 +167,7 @@
parentContext.structuralObject = creation.getParent();
//
- if (parentContext.associationContext.children.isLoaded())
+ if (parentContext.relationshipContext.children.isLoaded())
{
UIContainerObject child = create(creation.getChild());
@@ -178,7 +175,7 @@
attach(child);
//
- parentContext.associationContext.children.addLoadedRelated(child);
+ parentContext.relationshipContext.children.addLoadedRelated(child);
}
// Eventing
@@ -205,13 +202,13 @@
parentContext.structuralObject = destruction.getParent();
//
- if (parentContext.associationContext.children.isLoaded())
+ if (parentContext.relationshipContext.children.isLoaded())
{
// Get the id which is the destroyed object
String destroyedId = destruction.getIds().get(destruction.getIds().size()
- 1);
//
-
parentContext.associationContext.children.removeLoadedRelated(destroyedId);
+
parentContext.relationshipContext.children.removeLoadedRelated(destroyedId);
}
}
@@ -251,9 +248,9 @@
parentContext.structuralObject = move.getParent();
// Remove the child if the relationship is loaded on this side
- if (parentContext.associationContext.children.isLoaded())
+ if (parentContext.relationshipContext.children.isLoaded())
{
-
parentContext.associationContext.children.removeLoadedRelated(move.getSource().getId());
+
parentContext.relationshipContext.children.removeLoadedRelated(move.getSource().getId());
}
}
@@ -266,7 +263,7 @@
destinationContext.structuralObject = move.getDestination();
//
- if (destinationContext.associationContext.children.isLoaded())
+ if (destinationContext.relationshipContext.children.isLoaded())
{
// Now we really need to add the source as we load collections entirely
if (source == null)
@@ -278,7 +275,7 @@
}
//
- destinationContext.associationContext.children.addLoadedRelated(source);
+ destinationContext.relationshipContext.children.addLoadedRelated(source);
}
}
@@ -291,15 +288,15 @@
sourceContext.structuralObject = move.getSource();
//
- if (sourceContext.associationContext.parent.isLoaded())
+ if (sourceContext.relationshipContext.parent.isLoaded())
{
if (destination != null)
{
- sourceContext.associationContext.parent.setLoadedRelated(destination);
+
sourceContext.relationshipContext.parent.setLoadedRelated(destination);
}
else
{
- sourceContext.associationContext.parent.clearLoadedRelated();
+ sourceContext.relationshipContext.parent.clearLoadedRelated();
}
}
}
@@ -405,13 +402,13 @@
// Continue only if it is valid
if (context.isValid())
{
- boolean loaded = context.associationContext.children.isLoaded();
+ boolean loaded = context.relationshipContext.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.getRelateds())
+ for (UIContainerObject child :
context.relationshipContext.children.getRelateds())
{
ObjectContext childContext = (ObjectContext)child.getContext();
@@ -452,7 +449,7 @@
}
//
- boolean loaded = context.associationContext.children.isLoaded();
+ boolean loaded = context.relationshipContext.children.isLoaded();
boolean refreshChildren = scope.enterChildren(object, loaded);
// Update the structural state
@@ -471,15 +468,15 @@
ObjectContext addedContext = (ObjectContext)added.getContext();
// Find parent pointing at us and set as dirty
- for (ObjectContext addedParentContext :
addedContext.associationContext.parent.getReferences())
+ for (ObjectContext addedParentContext :
addedContext.relationshipContext.parent.getReferences())
{
addedParentContext.status = UIObject.Status.STALE;
}
// Update parent if it was loaded
- if (addedContext.associationContext.parent.isLoaded())
+ if (addedContext.relationshipContext.parent.isLoaded())
{
- addedContext.associationContext.parent.setLoadedRelated(object);
+
addedContext.relationshipContext.parent.setLoadedRelated(object);
}
}
else
@@ -491,7 +488,7 @@
}
//
- context.associationContext.children.addLoadedRelated(added);
+ context.relationshipContext.children.addLoadedRelated(added);
}
//
@@ -509,7 +506,7 @@
// Removals
for (String removedId : refresh.getRemovedChildren())
{
- UIContainerObject removed =
context.associationContext.children.removeLoadedRelated(removedId);
+ UIContainerObject removed =
context.relationshipContext.children.removeLoadedRelated(removedId);
//
detach(removed);
@@ -524,7 +521,7 @@
// Recursively refresh children
if (refreshChildren)
{
- for (UIContainerObject child :
context.associationContext.children.getRelateds())
+ for (UIContainerObject child :
context.relationshipContext.children.getRelateds())
{
refresh(child, scope);
}
@@ -545,7 +542,7 @@
ObjectContext context = (ObjectContext)object.getContext();
//
- for (UIContainerObject child : context.associationContext.children.getRelateds())
+ for (UIContainerObject child : context.relationshipContext.children.getRelateds())
{
detach(child);
}