Author: julien(a)jboss.com
Date: 2007-12-18 12:34:47 -0500 (Tue, 18 Dec 2007)
New Revision: 9361
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AbstractUIObject.java
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/UIObjectList.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectRef.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/ConstraintViolationException.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockException.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java
Removed:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/DuplicateStateException.java
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/UIController.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/PresentationContextImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContainerImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIPageImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIPortalImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIWindowImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/NoSuchStateException.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StateException.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/UIModelTester.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java
Log:
- isolate model state management in a package
- implement destroy and move (partially)
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-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/client/controller/AjaxUIController.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -150,7 +150,7 @@
//TODO: set proper status
//TODO: set proper parent reference
- List<UIObject> windows = page.getChildren();
+ List<? extends UIObject> windows = page.getChildren();
List clientWindows = new ArrayList();
for(int i=0; i<windows.size(); i++)
{
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-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/client/controller/UIController.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -336,7 +336,7 @@
if(uiObject instanceof UIPage)
{
UIPage page = (UIPage)uiObject;
- List<UIObject> windows = page.getChildren();
+ List<? extends UIObject> windows = page.getChildren();
for(int i=0; i<windows.size(); i++)
{
UIObject cour = windows.get(i);
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/PresentationContextImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/PresentationContextImpl.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/PresentationContextImpl.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -23,7 +23,7 @@
package org.jboss.portal.presentation.impl;
import org.jboss.portal.presentation.client.PresentationContext;
-import org.jboss.portal.presentation.impl.model.UIContextImpl;
+import org.jboss.portal.presentation.impl.model.container.UIObjectContainer;
import
org.jboss.portal.presentation.impl.model.state.navigational.NavigationalStateContextImpl;
import org.jboss.portal.presentation.model.UIContext;
import org.jboss.portal.presentation.model.UIObject;
@@ -57,7 +57,7 @@
private PresentationServer presentationServer = null;
/** . */
- private UIContextImpl root;
+ private UIObjectContainer container;
public PresentationContextImpl(PresentationServer presentationServer, ServerInvocation
invocation)
{
@@ -67,7 +67,7 @@
public UIContext getUIContext()
{
- if (root == null)
+ if (container == null)
{
HttpSession session =
this.invocation.getServerContext().getClientRequest().getSession();
@@ -80,11 +80,11 @@
}
//
- root = new UIContextImpl(presentationServer.getStructuralStateManager(), nsc);
+ container = new
UIObjectContainer(presentationServer.getStructuralStateManager(), nsc);
}
//
- return root;
+ return container.getRoot();
}
/**
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContainerImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContainerImpl.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContainerImpl.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -25,17 +25,18 @@
import org.jboss.portal.presentation.model.UIContainer;
import org.jboss.portal.presentation.model.UIObject;
import org.jboss.portal.presentation.model.UIWindow;
-import org.jboss.portal.presentation.model.state.structural.StructuralState;
import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.impl.model.container.AbstractUIObject;
+import org.jboss.portal.presentation.impl.model.container.UIObjectContainer;
/**
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
*
*/
-public class UIContainerImpl extends UIObjectImpl implements UIContainer
+public class UIContainerImpl extends AbstractUIObject implements UIContainer
{
- public UIContainerImpl(UIContextImpl context, StructuralObject object)
+ public UIContainerImpl(UIObjectContainer context, StructuralObject object)
{
super(context, object);
}
Deleted:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -1,245 +0,0 @@
-/******************************************************************************
- * 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;
-
-import org.jboss.portal.presentation.model.ModelEvent;
-import org.jboss.portal.presentation.model.ModelListener;
-import org.jboss.portal.presentation.model.UIContainer;
-import org.jboss.portal.presentation.model.UIContext;
-import org.jboss.portal.presentation.model.UIObject;
-import org.jboss.portal.presentation.model.UIPage;
-import org.jboss.portal.presentation.model.UIPortal;
-import org.jboss.portal.presentation.model.UIWindow;
-import org.jboss.portal.presentation.model.state.navigational.NavigationalStateContext;
-import org.jboss.portal.presentation.model.state.structural.StructuralObject;
-import org.jboss.portal.presentation.model.state.structural.StructuralStateContext;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
- *
- */
-public final class UIContextImpl extends UIObjectImpl implements UIContext
-{
-
- /** . */
- private final Map<String, UIObjectImpl> universe = new HashMap<String,
UIObjectImpl>();
-
- /** . */
- protected final StructuralStateContext structuralStateContext;
-
- /** . */
- protected final NavigationalStateContext navigationalStateContext;
-
- /** . */
- protected final List<ModelListener> listeners;
-
- private static StructuralObject getRootState(StructuralStateContext loader)
- {
- return loader.load(loader.getRootId());
- }
-
- public UIContextImpl(StructuralStateContext structuralStateContext,
NavigationalStateContext navigationalStateContext)
- {
- super(getRootState(structuralStateContext));
-
- //
- this.structuralStateContext = structuralStateContext;
- this.navigationalStateContext = navigationalStateContext;
- this.listeners = new ArrayList<ModelListener>();
-
- // Put our self in the universe
- universe.put(getId(), this);
- }
-
- /**
- *
- */
- public UIObject getObject(String id)
- {
- return getObject(id, true);
- }
-
- void update(StructuralObject.Change change)
- {
- if (change instanceof StructuralObject.Update)
- {
- StructuralObject.Update update = (StructuralObject.Update)change;
- context.updateUniverse(update.getObject());
- }
- else if (change instanceof StructuralObject.Creation)
- {
- StructuralObject.Creation creation = (StructuralObject.Creation)change;
- context.updateUniverse(creation.getParent());
- context.updateUniverse(creation.getChild());
- }
- else
- {
- throw new AssertionError();
- }
- }
-
- UIObjectImpl getParent(StructuralObject structuralObject)
- {
- StructuralObject parentStructuralState =
context.structuralStateContext.loadParent(structuralObject);
- return context.updateUniverse(parentStructuralState);
- }
-
- List<UIObject> getChildren(StructuralObject structuralObject)
- {
- ArrayList<UIObject> children = new ArrayList<UIObject>();
- for (StructuralObject structuralChild :
context.structuralStateContext.loadChildren(structuralObject))
- {
- UIObjectImpl child = context.updateUniverse(structuralChild);
- children.add(child);
- }
- return children;
- }
-
- /**
- * Get an object from the universe. If the parameter
<code>loadIfAbsent</code> has the value <code>false</code>
- * and the object is not in the universe it will throw an
<code>AssertionError</code> otherwise it will return
- * the object.
- *
- * @param id the id of the object to obtain
- * @param loadIfAbsent load the object from the structural state context if it is not
present
- * @return the loaded object
- */
- private UIObjectImpl getObject(String id, boolean loadIfAbsent)
- {
- try
- {
- UIObjectImpl object = universe.get(id);
-
- //
- if (object != null)
- {
- return object;
- }
-
- //
- if (loadIfAbsent)
- {
- // Fetch the state of the UIObject in question
- StructuralObject structuralObject = this.structuralStateContext.load(id);
-
- //
- return updateUniverse(structuralObject);
- }
- else
- {
- throw new AssertionError("Should not be here");
- }
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
-
- private UIObjectImpl updateUniverse(StructuralObject structuralObject)
- {
- UIObjectImpl object = universe.get(structuralObject.getId());
-
- //
- if (object == null)
- {
- object = createObject(structuralObject);
-
- //
- universe.put(structuralObject.getId(), object);
- }
- else
- {
- object.update(structuralObject);
- }
-
- //
- return object;
- }
-
- private UIObjectImpl createObject(StructuralObject state)
- {
- Class type = state.getState().getType();
-
- //
- if(type == UIPortal.class)
- {
- return new UIPortalImpl(this, state);
- }
- else if(type == UIPage.class)
- {
- return new UIPageImpl(this, state);
- }
- else if(type == UIContainer.class)
- {
- return new UIContainerImpl(this, state);
- }
- else if(type == UIWindow.class)
- {
- return new UIWindowImpl(this, state);
- }
- else
- {
- throw new AssertionError();
- }
- }
-
- public void addModelListener(ModelListener listener)
- {
- if (listener == null)
- {
- throw new IllegalArgumentException();
- }
-
- //
- if (!listeners.contains(listener))
- {
- listeners.add(listener);
- }
- }
-
- protected <T extends UIObject> boolean isAllowedAsChild(Class<T> type)
- {
- return type == UIPortal.class;
- }
-
- protected void fireEvent(ModelEvent event)
- {
- for (ModelListener listener : listeners)
- {
- try
- {
- listener.onEvent(event);
- }
- catch (Exception ignore)
- {
- System.out.println("ignore = " + ignore);
- }
- }
- }
-}
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIContextImpl.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -0,0 +1,59 @@
+/******************************************************************************
+ * 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;
+
+import org.jboss.portal.presentation.impl.model.container.AbstractUIObject;
+import org.jboss.portal.presentation.impl.model.container.UIObjectContainer;
+import org.jboss.portal.presentation.model.UIObject;
+import org.jboss.portal.presentation.model.UIPortal;
+import org.jboss.portal.presentation.model.UIContext;
+import org.jboss.portal.presentation.model.ModelListener;
+import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class UIContextImpl extends AbstractUIObject implements UIContext
+{
+
+ public UIContextImpl(UIObjectContainer context, StructuralObject object)
+ {
+ super(context, object);
+ }
+
+ protected <T extends UIObject> boolean isAllowedAsChild(Class<T> type)
+ {
+ return type == UIPortal.class;
+ }
+
+ public UIObject getObject(String id)
+ {
+ return container.getObject(id);
+ }
+
+ public void addModelListener(ModelListener listener)
+ {
+ container.addModelListener(listener);
+ }
+}
Deleted:
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-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -1,583 +0,0 @@
-/******************************************************************************
- * 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;
-
-import org.jboss.portal.common.NotYetImplemented;
-import org.jboss.portal.presentation.model.StateScopeType;
-import org.jboss.portal.presentation.model.UIObject;
-import org.jboss.portal.presentation.model.event.StateChange;
-import org.jboss.portal.presentation.model.event.StateChangeEvent;
-import
org.jboss.portal.presentation.model.event.navigational.NavigationalStateModification;
-import org.jboss.portal.presentation.model.event.structural.StructuralStateModification;
-import org.jboss.portal.presentation.model.state.NoSuchStateException;
-import org.jboss.portal.presentation.model.state.StaleStateException;
-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;
-
-import java.io.Serializable;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- *
- */
-public abstract class UIObjectImpl implements UIObject, Serializable
-{
-
- /** . */
- private static final Map<String, String> EMPTY_STATE = Collections.emptyMap();
-
- /** . */
- protected final UIContextImpl context;
-
- /** . */
- private List<UIObject> children;
-
- /** . */
- private UIObjectImpl parent;
-
- /** This is used to assist with data needed during lazy loading, other state related
data etc... */
- private StructuralObject structuralObject;
-
- /** . */
- private Status status;
-
- public UIObjectImpl(UIContextImpl context, StructuralObject object)
- {
- this.structuralObject = object;
- this.context = context;
- this.children = null;
- this.parent = null;
- this.status = Status.VALID;
- }
-
- public UIObjectImpl(StructuralObject object)
- {
- this.structuralObject = object;
- this.context = (UIContextImpl)this;
- this.children = null;
- this.parent = null;
- this.status = Status.VALID;
- }
-
- protected final StructuralObject getStructuralObject()
- {
- return structuralObject;
- }
-
- //UIObject interface
implementation-----------------------------------------------------------------------------------------------------------------------------
-
- /**
- *
- */
- public String getId()
- {
- return structuralObject.getId();
- }
-
- /**
- * Attempt to cast the value argument to the provided type argument. If the value
argument type is assignable
- * to the provided type, the value is returned, otherwise if it is not or the value is
null, null is returned.
- *
- * todo: Move that to common package.
- *
- * @param value the value to cast
- * @param type the type to downcast
- * @return the casted value or null
- */
- private <T> T safeCast(Object value, Class<T> type)
- {
- if (value == null)
- {
- return null;
- }
- else
- {
- if (type.isAssignableFrom(value.getClass()))
- {
- return type.cast(value);
- }
- else
- {
- return null;
- }
- }
- }
-
- public Status getStatus()
- {
- return status;
- }
-
- void updateStatus(StateException e)
- {
- if (e instanceof NoSuchStateException)
- {
- status = Status.INVALID;
- }
- else if (e instanceof StaleStateException)
- {
- status = Status.STALE;
- }
- else
- {
- throw new AssertionError(e);
- }
- }
-
- public void validate()
- {
- try
- {
- context.structuralStateContext.validate(structuralObject);
- }
- catch (StateException e)
- {
- updateStatus(e);
- }
- }
-
- public void resolveConflicts()
- {
- switch (status)
- {
- case STALE:
- // Todo :-)
- throw new NotYetImplemented();
- case INVALID:
- // Cannot be resolved
- throw new IllegalStateException();
- case VALID:
- // Nothing to do
- break;
- }
- }
-
- public <T> T getProperty(StateScopeType scopeType, String propertyName,
Class<T> propertyType)
- {
- if (scopeType == null)
- {
- throw new IllegalArgumentException();
- }
- if (propertyName == null)
- {
- throw new IllegalArgumentException();
- }
- if (propertyType == null)
- {
- throw new IllegalArgumentException();
- }
- Object value;
- switch (scopeType)
- {
- case NAVIGATIONAL:
- value = context.navigationalStateContext.get(getId(), propertyName);
- break;
- case STRUCTURAL:
- value = structuralObject.getState().getProperties().get(propertyName);
- break;
- default:
- throw new AssertionError();
- }
- return safeCast(value, propertyType);
- }
-
- public Object getProperty(StateScopeType scopeType, String propertyName)
- {
- return getProperty(scopeType, propertyName, Object.class);
- }
-
- public <T> void setProperty(StateScopeType scopeType, String propertyName, T
propertyValue) throws StateChangeVetoException
- {
- if (scopeType == null)
- {
- throw new IllegalArgumentException();
- }
- if (propertyName == null)
- {
- throw new IllegalArgumentException();
- }
-
- //
- String id = getId();
-
- //
- switch (scopeType)
- {
- case NAVIGATIONAL:
- {
- // Have context process the change
- context.navigationalStateContext.set(id, propertyName, propertyValue);
-
- // Broadcast event
- NavigationalStateModification mod = new
NavigationalStateModification(propertyName, propertyValue);
- StateChange<NavigationalStateModification> change = new
StateChange<NavigationalStateModification>(id, mod);
- StateChangeEvent event = new StateChangeEvent(change);
- context.fireEvent(event);
- break;
- }
- case STRUCTURAL:
- {
- if (propertyValue instanceof String)
- {
- Map<String, String> changes = new HashMap<String, String>();
- changes.put(propertyName, (String)propertyValue);
-
- // Have context process change
- StructuralObject.Update update;
- try
- {
- update = context.structuralStateContext.update(structuralObject,
changes);
- }
- catch (StateException e)
- {
- validate();
-
- //
- throw e;
- }
-
- //
- context.update(update);
-
- // Broadcast event
- StructuralStateModification mod = new
StructuralStateModification.Update(changes);
- StateChange<StructuralStateModification> change = new
StateChange<StructuralStateModification>(id, mod);
- StateChangeEvent event = new StateChangeEvent(change);
- context.fireEvent(event);
- }
- else
- {
- throw new StateChangeVetoException("Structural property value must be
string value");
- }
- break;
- }
- default:
- throw new AssertionError();
- }
- }
-
- public UIObject getChild(String name)
- {
- for (UIObject child : getChildren())
- {
- if (child.getName().equals(name))
- {
- return child;
- }
- }
- return null;
- }
-
- /**
- *
- */
- public String getName()
- {
- return structuralObject.getState().getName();
- }
-
- /**
- *
- */
- public final UIObject getParent()
- {
- if (this instanceof UIContextImpl)
- {
- return null;
- }
-
- //
- if (parent == null)
- {
- parent = context.getParent(structuralObject);
- }
-
- //
- return parent;
- }
-
- /**
- *
- */
- public final List<UIObject> getChildren()
- {
- if (children == null)
- {
- children = context.getChildren(structuralObject);
- }
- return children;
- }
-
- public <T extends UIObject> T createChild(String name, Class<T> type)
throws IllegalArgumentException
- {
- StructuralObject.Creation creation;
- try
- {
- creation = context.structuralStateContext.create(structuralObject, type, name,
EMPTY_STATE);
- }
- catch (StateException e)
- {
- updateStatus(e);
-
- //
- throw e;
- }
-
- //
- context.update(creation);
-
- //
- StructuralObject child = creation.getChild();
-
- // Eventing
- StructuralStateModification mod = new StructuralStateModification.Creation(type,
name, EMPTY_STATE);
- StateChange<StructuralStateModification> change = new
StateChange<StructuralStateModification>(child.getId(), mod);
- StateChangeEvent event = new StateChangeEvent(change);
- context.fireEvent(event);
-
- //
- return type.cast(context.getObject(child.getId()));
- }
-
- void update(StructuralObject object)
- {
- structuralObject = object;
- parent = null;
- children = null;
- }
-
- protected abstract <T extends UIObject> boolean isAllowedAsChild(Class<T>
type);
-
- /**
- *
- */
-// public <T extends UIObject> List<T> getChildren(Class<T> type)
-// {
-// try
-// {
-// List<T> filteredList = new ArrayList<T>();
-//
-// //Making sure all children are fully loaded
-// List<UIObject> allChildren = this.getChildren();
-//
-// Class<T> implementationClass = this.getImplementationClass(type);
-// if(allChildren != null)
-// {
-// for(int i=0; i<allChildren.size(); i++)
-// {
-// UIObject child = allChildren.get(i);
-// if(implementationClass == child.getClass())
-// {
-// filteredList.add((T)child);
-// }
-// }
-// }
-// return filteredList;
-// }
-// catch(Exception e)
-// {
-// throw new RuntimeException(e);
-// }
-// throw new UnsupportedOperationException("todo");
-// }
-
-
-
- /**
- *
- */
-// public <T extends UIObject> T createChild(String name, Class<T> type)
throws IllegalArgumentException
-// {
-// if(!this.isAllowedAsChild(type))
-// {
-// throw new IllegalArgumentException(type.getName()+" cannot be added as a
child to this parent");
-// }
-// try
-// {
-// T child = this.getImplementationClass(type).newInstance();
-//
-// //Name
-// ((UIObjectImpl)child).name = name;
-//
-// //Id
-// ((UIObjectImpl)child).id = this.generateChildId(name);
-//
-// //State
-// ((UIObjectImpl)child).state =
this.getUIContext().getStructuralStateManager().load(((UIObjectImpl)child).id);
-//
-// this.children.add(child);
-//
-// return child;
-// }
-// catch(Exception e)
-// {
-// throw new RuntimeException(e);
-// }
-// }
-//
-// /**
-// *
-// */
-// public void removeChild(String name)
-// {
-// //Make sure all children are loaded
-// this.getChildren();
-// if(this.children != null)
-// {
-// for(int i=0; i<this.children.size(); i++)
-// {
-// UIObject child = this.children.get(i);
-// if(child.getName().equals(name))
-// {
-// this.children.remove(child);
-// break;
-// }
-// }
-// }
-// }
-
- //-----UIObjectImpl implementation specific
methods------------------------------------------------------------------------------------------------------------
-// protected void setState(UIContext uiContext, ObjectState state, boolean loadParent)
-// {
-// //Name
-// this.name = state.getName();
-//
-// //Parent..load using parentId from state being used
-// if(loadParent && state.getParentId() != null &&
state.getParentId().trim().length()>0)
-// {
-// ObjectState parentState =
uiContext.getStructuralStateManager().load(state.getParentId());
-// this.parent = uiContext.getObject(state.getParentId(),
parentState.getType());
-// }
-//
-// //Id
-// this.id = this.generateId();
-//
-// //State
-// this.state = state;
-// }
-
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
-
-// /**
-// *
-// * @return
-// */
-// private String generateId()
-// {
-// String newId = null;
-//
-// String myName = "";
-// if(this.getName() != null && this.getName().trim().length()>0)
-// {
-// myName = this.getName();
-// }
-//
-// //Generate a new id concatenating its entire path in the UIObject tree
-// StringBuffer buffer = new StringBuffer();
-// if(this.getParent() != null && !(this.getParent() instanceof UIContext))
-// {
-//
buffer.append("/"+this.getParent().getId().substring(1)+"/");
-// if(myName.trim().length()>0)
-// {
-// buffer.append(myName);
-// }
-// newId = buffer.toString();
-// }
-// else
-// {
-// buffer.append("/");
-// if(myName.trim().length()>0)
-// {
-// buffer.append(myName);
-// }
-// newId = buffer.toString();
-// }
-//
-// return newId;
-// }
-
-/*
- private String generateChildId(String childName)
- {
- String newId = null;
- String myName = "";
- if(this.getName() != null && this.getName().trim().length()>0)
- {
- myName = this.getName();
- }
-
- //Generate a new id concatenating its entire path in the UIObject tree
- StringBuffer buffer = new StringBuffer();
- if(this.getParent() != null && !(this.getParent() instanceof UIContext))
- {
-
buffer.append("/"+this.getParent().getId().substring(1)+"/");
- if(myName.trim().length()>0)
- {
- buffer.append(myName+"/");
- }
- buffer.append(childName);
- newId = buffer.toString();
- }
- else
- {
- buffer.append("/");
- if(myName.trim().length()>0)
- {
- buffer.append(myName+"/");
- }
- buffer.append(childName);
- newId = buffer.toString();
- }
-
- return newId;
- }
-*/
-
-/*
- private UIContext getUIContext()
- {
- UIContext uiContext = null;
-
- //Make sure this UIObject is not the root UIContext object
- if(this.getParent() == null)
- {
- return (UIContext)this;
- }
-
- //Move up the tree to the UIContext and get the ModelLoader
- UIObject parent = this.getParent();
- while(!(parent instanceof UIContext))
- {
- parent = parent.getParent();
- }
- uiContext = (UIContext)parent;
-
- return uiContext;
- }
-*/
-}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIPageImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIPageImpl.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIPageImpl.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -26,17 +26,18 @@
import org.jboss.portal.presentation.model.UIContainer;
import org.jboss.portal.presentation.model.UIPage;
import org.jboss.portal.presentation.model.UIWindow;
-import org.jboss.portal.presentation.model.state.structural.StructuralState;
import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.impl.model.container.AbstractUIObject;
+import org.jboss.portal.presentation.impl.model.container.UIObjectContainer;
/**
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
*
*/
-public class UIPageImpl extends UIObjectImpl implements UIPage
+public class UIPageImpl extends AbstractUIObject implements UIPage
{
- public UIPageImpl(UIContextImpl context, StructuralObject object)
+ public UIPageImpl(UIObjectContainer context, StructuralObject object)
{
super(context, object);
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIPortalImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIPortalImpl.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIPortalImpl.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -25,17 +25,18 @@
import org.jboss.portal.presentation.model.UIObject;
import org.jboss.portal.presentation.model.UIPortal;
import org.jboss.portal.presentation.model.UIPage;
-import org.jboss.portal.presentation.model.state.structural.StructuralState;
import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.impl.model.container.AbstractUIObject;
+import org.jboss.portal.presentation.impl.model.container.UIObjectContainer;
/**
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
*
*/
-public class UIPortalImpl extends UIObjectImpl implements UIPortal
+public class UIPortalImpl extends AbstractUIObject implements UIPortal
{
- public UIPortalImpl(UIContextImpl context, StructuralObject object)
+ public UIPortalImpl(UIObjectContainer context, StructuralObject object)
{
super(context, object);
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIWindowImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIWindowImpl.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIWindowImpl.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -27,17 +27,18 @@
import org.jboss.portal.presentation.model.StateScopeType;
import org.jboss.portal.presentation.model.UIObject;
import org.jboss.portal.presentation.model.UIWindow;
-import org.jboss.portal.presentation.model.state.structural.StructuralState;
import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.impl.model.container.AbstractUIObject;
+import org.jboss.portal.presentation.impl.model.container.UIObjectContainer;
/**
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
*
*/
-public class UIWindowImpl extends UIObjectImpl implements UIWindow
+public class UIWindowImpl extends AbstractUIObject implements UIWindow
{
- public UIWindowImpl(UIContextImpl context, StructuralObject object)
+ public UIWindowImpl(UIObjectContainer context, StructuralObject object)
{
super(context, object);
}
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AbstractUIObject.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AbstractUIObject.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AbstractUIObject.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -0,0 +1,377 @@
+/******************************************************************************
+ * 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.common.NotYetImplemented;
+import org.jboss.portal.presentation.model.StateScopeType;
+import org.jboss.portal.presentation.model.UIObject;
+import org.jboss.portal.presentation.model.event.StateChange;
+import org.jboss.portal.presentation.model.event.StateChangeEvent;
+import
org.jboss.portal.presentation.model.event.navigational.NavigationalStateModification;
+import org.jboss.portal.presentation.model.event.structural.StructuralStateModification;
+import org.jboss.portal.presentation.model.state.NoSuchStateException;
+import org.jboss.portal.presentation.model.state.StaleStateException;
+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;
+import org.jboss.portal.presentation.impl.model.container.UIObjectContainer;
+import org.jboss.portal.presentation.impl.model.container.UIObjectList;
+
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ *
+ */
+public abstract class AbstractUIObject implements UIObject, Serializable
+{
+
+ /** . */
+ private static final Map<String, String> EMPTY_STATE = Collections.emptyMap();
+
+ /** . */
+ protected final UIObjectContainer container;
+
+ /** . */
+ final UIObjectList children;
+
+ /** . */
+ final UIObjectRef parent;
+
+ /** This is used to assist with data needed during lazy loading, other state related
data etc... */
+ StructuralObject structuralObject;
+
+ /** . */
+ Status status;
+
+ public AbstractUIObject(UIObjectContainer container, StructuralObject object)
+ {
+ this.structuralObject = object;
+ this.container = container;
+ this.children = new UIObjectList(this);
+ this.parent = new UIObjectRef();
+ this.status = Status.VALID;
+ }
+
+ protected final StructuralObject getStructuralObject()
+ {
+ return structuralObject;
+ }
+
+ // UIObject interface
implementation-----------------------------------------------------------------------------------------------------------------------------
+
+ /**
+ *
+ */
+ public String getId()
+ {
+ return structuralObject.getId();
+ }
+
+ /**
+ * Attempt to cast the value argument to the provided type argument. If the value
argument type is assignable
+ * to the provided type, the value is returned, otherwise if it is not or the value is
null, null is returned.
+ *
+ * todo: Move that to common package.
+ *
+ * @param value the value to cast
+ * @param type the type to downcast
+ * @return the casted value or null
+ */
+ private <T> T safeCast(Object value, Class<T> type)
+ {
+ if (value == null)
+ {
+ return null;
+ }
+ else
+ {
+ if (type.isAssignableFrom(value.getClass()))
+ {
+ return type.cast(value);
+ }
+ else
+ {
+ return null;
+ }
+ }
+ }
+
+ public Status getStatus()
+ {
+ return status;
+ }
+
+ void updateStatus(StateException e)
+ {
+ if (e instanceof NoSuchStateException)
+ {
+ status = Status.INVALID;
+ }
+ else if (e instanceof StaleStateException)
+ {
+ status = Status.STALE;
+ }
+ else
+ {
+ throw new AssertionError(e);
+ }
+ }
+
+ public void validate()
+ {
+ try
+ {
+ container.structuralStateContext.validate(structuralObject);
+ }
+ catch (StateException e)
+ {
+ updateStatus(e);
+ }
+ }
+
+ public void resolveConflicts()
+ {
+ switch (status)
+ {
+ case STALE:
+ // Todo :-)
+ throw new NotYetImplemented();
+ case INVALID:
+ // Cannot be resolved
+ throw new IllegalStateException();
+ case VALID:
+ // Nothing to do
+ break;
+ }
+ }
+
+ public <T> T getProperty(StateScopeType scopeType, String propertyName,
Class<T> propertyType)
+ {
+ if (scopeType == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (propertyName == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (propertyType == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ Object value;
+ switch (scopeType)
+ {
+ case NAVIGATIONAL:
+ value = container.navigationalStateContext.get(getId(), propertyName);
+ break;
+ case STRUCTURAL:
+ value = structuralObject.getState().getProperties().get(propertyName);
+ break;
+ default:
+ throw new AssertionError();
+ }
+ return safeCast(value, propertyType);
+ }
+
+ public Object getProperty(StateScopeType scopeType, String propertyName)
+ {
+ return getProperty(scopeType, propertyName, Object.class);
+ }
+
+ public <T> void setProperty(StateScopeType scopeType, String propertyName, T
propertyValue) throws StateChangeVetoException
+ {
+ if (scopeType == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (propertyName == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ String id = getId();
+
+ //
+ switch (scopeType)
+ {
+ case NAVIGATIONAL:
+ {
+ // Have context process the change
+ container.navigationalStateContext.set(id, propertyName, propertyValue);
+
+ // Broadcast event
+ NavigationalStateModification mod = new
NavigationalStateModification(propertyName, propertyValue);
+ StateChange<NavigationalStateModification> change = new
StateChange<NavigationalStateModification>(id, mod);
+ StateChangeEvent event = new StateChangeEvent(change);
+ container.fireEvent(event);
+ break;
+ }
+ case STRUCTURAL:
+ {
+ if (propertyValue instanceof String)
+ {
+ Map<String, String> changes = new HashMap<String, String>();
+ changes.put(propertyName, (String)propertyValue);
+
+ // Have context process change
+ StructuralObject.Update update;
+ try
+ {
+ update = container.structuralStateContext.update(structuralObject,
changes);
+ }
+ catch (StateException e)
+ {
+ validate();
+
+ //
+ throw e;
+ }
+
+ //
+ container.update(update);
+
+ // Broadcast event
+ StructuralStateModification mod = new
StructuralStateModification.Update(changes);
+ StateChange<StructuralStateModification> change = new
StateChange<StructuralStateModification>(id, mod);
+ StateChangeEvent event = new StateChangeEvent(change);
+ container.fireEvent(event);
+ }
+ else
+ {
+ throw new StateChangeVetoException("Structural property value must be
string value");
+ }
+ break;
+ }
+ default:
+ throw new AssertionError();
+ }
+ }
+
+ public AbstractUIObject getChild(String name)
+ {
+ for (AbstractUIObject child : getChildren())
+ {
+ if (child.getName().equals(name))
+ {
+ return child;
+ }
+ }
+ return null;
+ }
+
+ /**
+ *
+ */
+ public String getName()
+ {
+ return structuralObject.getState().getName();
+ }
+
+ /**
+ *
+ */
+ public final UIObject getParent()
+ {
+ if (!parent.loaded)
+ {
+ parent.object = container.getParent(structuralObject);
+ parent.loaded = true;
+ }
+
+ //
+ return parent.object;
+ }
+
+ /**
+ *
+ */
+ public final List<AbstractUIObject> getChildren()
+ {
+ return children;
+ }
+
+ public <T extends UIObject> T createChild(String name, Class<T> type)
throws IllegalArgumentException
+ {
+ StructuralObject.Creation creation;
+ try
+ {
+ creation = container.structuralStateContext.create(structuralObject, type, name,
EMPTY_STATE);
+ }
+ catch (StateException e)
+ {
+ updateStatus(e);
+
+ //
+ throw e;
+ }
+
+ //
+ container.update(creation);
+
+ //
+ StructuralObject child = creation.getChild();
+
+ // Eventing
+ StructuralStateModification mod = new StructuralStateModification.Creation(type,
name, EMPTY_STATE);
+ StateChange<StructuralStateModification> change = new
StateChange<StructuralStateModification>(child.getId(), mod);
+ StateChangeEvent event = new StateChangeEvent(change);
+ container.fireEvent(event);
+
+ //
+ return type.cast(container.getObject(child.getId()));
+ }
+
+ public void destroyChild(String name) throws IllegalArgumentException, StateException
+ {
+ if (name == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ // Get the named child
+ AbstractUIObject namedChild = getChild(name);
+
+ //
+ if (namedChild == null)
+ {
+ throw new IllegalArgumentException("No such child with name " +
name);
+ }
+
+ // Destroy the child
+ StructuralObject.Destruction destruction =
container.structuralStateContext.destroy(namedChild.structuralObject);
+
+ //
+ container.update(destruction);
+ }
+
+ protected abstract <T extends UIObject> boolean isAllowedAsChild(Class<T>
type);
+
+}
Added:
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
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectContainer.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -0,0 +1,285 @@
+/******************************************************************************
+ * 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.model.ModelEvent;
+import org.jboss.portal.presentation.model.ModelListener;
+import org.jboss.portal.presentation.model.UIContainer;
+import org.jboss.portal.presentation.model.UIObject;
+import org.jboss.portal.presentation.model.UIPage;
+import org.jboss.portal.presentation.model.UIPortal;
+import org.jboss.portal.presentation.model.UIWindow;
+import org.jboss.portal.presentation.model.UIContext;
+import org.jboss.portal.presentation.model.state.navigational.NavigationalStateContext;
+import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.model.state.structural.StructuralStateContext;
+import org.jboss.portal.presentation.impl.model.UIPortalImpl;
+import org.jboss.portal.presentation.impl.model.UIPageImpl;
+import org.jboss.portal.presentation.impl.model.UIContainerImpl;
+import org.jboss.portal.presentation.impl.model.UIWindowImpl;
+import org.jboss.portal.presentation.impl.model.UIContextImpl;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ *
+ */
+public final class UIObjectContainer
+{
+
+ /** . */
+ private final Map<String, AbstractUIObject> universe = new HashMap<String,
AbstractUIObject>();
+
+ /** . */
+ protected final StructuralStateContext structuralStateContext;
+
+ /** . */
+ protected final NavigationalStateContext navigationalStateContext;
+
+ /** . */
+ protected final List<ModelListener> listeners;
+
+ /** . */
+ protected final UIContext root;
+
+ public UIObjectContainer(StructuralStateContext structuralStateContext,
NavigationalStateContext navigationalStateContext)
+ {
+ // Put our self in the universe
+ String rootId = structuralStateContext.getRootId();
+ StructuralObject rootObject = structuralStateContext.load(rootId);
+ UIContextImpl root = (UIContextImpl)update(rootObject);
+
+ //
+ this.structuralStateContext = structuralStateContext;
+ this.navigationalStateContext = navigationalStateContext;
+ this.listeners = new ArrayList<ModelListener>();
+ this.root = root;
+ }
+
+ public UIContext getRoot()
+ {
+ return root;
+ }
+
+ public UIObject getObject(String id)
+ {
+ return getObject(id, true);
+ }
+
+ void update(StructuralObject.Change change)
+ {
+ if (change instanceof StructuralObject.Update)
+ {
+ StructuralObject.Update update = (StructuralObject.Update)change;
+ update(update.getObject());
+ }
+ else if (change instanceof StructuralObject.Creation)
+ {
+ StructuralObject.Creation creation = (StructuralObject.Creation)change;
+ update(creation.getParent());
+ update(creation.getChild());
+ }
+ else if (change instanceof StructuralObject.Destruction)
+ {
+ StructuralObject.Destruction destruction =
(StructuralObject.Destruction)change;
+
+ //
+ update(destruction.getParent());
+
+ //
+ for (String id : destruction.getIds())
+ {
+ AbstractUIObject o = universe.remove(id);
+
+ //
+ if (o != null)
+ {
+ o.status = UIObject.Status.INVALID;
+ }
+ }
+ }
+ else
+ {
+ throw new AssertionError();
+ }
+ }
+
+ AbstractUIObject getParent(StructuralObject structuralObject)
+ {
+ StructuralObject parentStructuralState =
structuralStateContext.loadParent(structuralObject);
+
+ //
+ if (parentStructuralState == null)
+ {
+ return null;
+ }
+
+ //
+ return update(parentStructuralState);
+ }
+
+ List<AbstractUIObject> getChildren(StructuralObject structuralObject)
+ {
+ ArrayList<AbstractUIObject> children = new
ArrayList<AbstractUIObject>();
+
+ //
+ for (StructuralObject structuralChild :
structuralStateContext.loadChildren(structuralObject))
+ {
+ AbstractUIObject child = update(structuralChild);
+ children.add(child);
+ }
+
+ //
+ return children;
+ }
+
+ /**
+ * Get an object from the universe. If the parameter
<code>loadIfAbsent</code> has the value <code>false</code>
+ * and the object is not in the universe it will throw an
<code>AssertionError</code> otherwise it will return
+ * the object.
+ *
+ * @param id the id of the object to obtain
+ * @param loadIfAbsent load the object from the structural state context if it is not
present
+ * @return the loaded object
+ */
+ private AbstractUIObject getObject(String id, boolean loadIfAbsent)
+ {
+ try
+ {
+ AbstractUIObject context = universe.get(id);
+
+ //
+ if (context != null)
+ {
+ return context;
+ }
+
+ //
+ if (loadIfAbsent)
+ {
+ // Fetch the state of the UIObject in question
+ StructuralObject structuralObject = this.structuralStateContext.load(id);
+
+ //
+ return update(structuralObject);
+ }
+ else
+ {
+ throw new AssertionError("Should not be here");
+ }
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private AbstractUIObject update(StructuralObject structuralObject)
+ {
+ AbstractUIObject context = universe.get(structuralObject.getId());
+
+ //
+ if (context == null)
+ {
+ context = createObject(structuralObject);
+
+ //
+ universe.put(structuralObject.getId(), context);
+ }
+ else
+ {
+ context.structuralObject = structuralObject;
+
+ //
+ context.parent.invalidate();
+ context.children.invalidate();
+ }
+
+ //
+ return context;
+ }
+
+ private AbstractUIObject createObject(StructuralObject state)
+ {
+ Class type = state.getState().getType();
+
+ //
+ if(type == UIContext.class)
+ {
+ return new UIContextImpl(this, state);
+ }
+ else if(type == UIPortal.class)
+ {
+ return new UIPortalImpl(this, state);
+ }
+ else if(type == UIPage.class)
+ {
+ return new UIPageImpl(this, state);
+ }
+ else if(type == UIContainer.class)
+ {
+ return new UIContainerImpl(this, state);
+ }
+ else if(type == UIWindow.class)
+ {
+ return new UIWindowImpl(this, state);
+ }
+ else
+ {
+ throw new AssertionError();
+ }
+ }
+
+ public void addModelListener(ModelListener listener)
+ {
+ if (listener == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ if (!listeners.contains(listener))
+ {
+ listeners.add(listener);
+ }
+ }
+
+ protected void fireEvent(ModelEvent event)
+ {
+ for (ModelListener listener : listeners)
+ {
+ try
+ {
+ listener.onEvent(event);
+ }
+ catch (Exception ignore)
+ {
+ System.out.println("ignore = " + ignore);
+ }
+ }
+ }
+}
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectList.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectList.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectList.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -0,0 +1,69 @@
+/******************************************************************************
+ * 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 java.util.AbstractList;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class UIObjectList extends AbstractList<AbstractUIObject>
+{
+
+ /** . */
+ private List<AbstractUIObject> content;
+
+ /** . */
+ private final AbstractUIObject context;
+
+ public UIObjectList(AbstractUIObject context)
+ {
+ this.context = context;
+ this.content = null;
+ }
+
+ public AbstractUIObject get(int i)
+ {
+ if (content == null)
+ {
+ content = context.container.getChildren(context.structuralObject);
+ }
+ return content.get(i);
+ }
+
+ public int size()
+ {
+ if (content == null)
+ {
+ content = context.container.getChildren(context.structuralObject);
+ }
+ return content.size();
+ }
+
+ void invalidate()
+ {
+ content = null;
+ }
+}
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectRef.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectRef.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/UIObjectRef.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -0,0 +1,49 @@
+/******************************************************************************
+ * 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;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class UIObjectRef
+{
+
+ /** . */
+ AbstractUIObject object;
+
+ /** . */
+ boolean loaded;
+
+ public UIObjectRef()
+ {
+ object = null;
+ loaded = false;
+ }
+
+ void invalidate()
+ {
+ loaded = false;
+ object = null;
+ }
+}
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-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -143,26 +143,22 @@
* @throws IllegalArgumentException if the name is null, already exists or this kind
of object does not accept children of the specified type
* @throws StateException any state exception
*/
- public <T extends UIObject> T createChild(String name, Class<T> type)
throws IllegalArgumentException, StateException;
+ <T extends UIObject> T createChild(String name, Class<T> type) throws
IllegalArgumentException, StateException;
/**
* Returns the children of this object.
*
* @return the list of children
*/
- List<UIObject> getChildren();
+ List<? extends UIObject> getChildren();
/**
+ * Destroy a named child.
*
- * @param name
+ * @param name the name of the child to destroy
+ * @throws IllegalArgumentException if the name is null or the child does not exist
+ * @throws StateException any state exception
*/
-// public void removeChild(String name);
+ void destroyChild(String name) throws IllegalArgumentException, StateException;
- /**
- *
- * @param <T>
- * @param type
- * @return
- */
-// <T extends UIObject> List<T> getChildren(Class<T> type);
}
Copied:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/ConstraintViolationException.java
(from rev 9357,
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/DuplicateStateException.java)
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/ConstraintViolationException.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/ConstraintViolationException.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -0,0 +1,49 @@
+/******************************************************************************
+ * 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.model.state;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class ConstraintViolationException extends StateException
+{
+ public ConstraintViolationException()
+ {
+ }
+
+ public ConstraintViolationException(String s)
+ {
+ super(s);
+ }
+
+ public ConstraintViolationException(String s, Throwable throwable)
+ {
+ super(s, throwable);
+ }
+
+ public ConstraintViolationException(Throwable throwable)
+ {
+ super(throwable);
+ }
+}
\ No newline at end of file
Deleted:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/DuplicateStateException.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/DuplicateStateException.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/DuplicateStateException.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -1,31 +0,0 @@
-/******************************************************************************
- * 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.model.state;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 630 $
- */
-public class DuplicateStateException extends StateException
-{
-}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/NoSuchStateException.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/NoSuchStateException.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/NoSuchStateException.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -28,4 +28,22 @@
*/
public class NoSuchStateException extends StateException
{
+ public NoSuchStateException()
+ {
+ }
+
+ public NoSuchStateException(String s)
+ {
+ super(s);
+ }
+
+ public NoSuchStateException(String s, Throwable throwable)
+ {
+ super(s, throwable);
+ }
+
+ public NoSuchStateException(Throwable throwable)
+ {
+ super(throwable);
+ }
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StateException.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StateException.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StateException.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -26,7 +26,7 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 630 $
*/
-public abstract class StateException extends RuntimeException
+public class StateException extends RuntimeException
{
public StateException()
{
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-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockModelTestCase.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -26,16 +26,17 @@
import org.jboss.portal.presentation.model.UIContext;
import org.jboss.portal.presentation.model.UIPortal;
import org.jboss.portal.presentation.model.state.StaleStateException;
+import org.jboss.portal.presentation.model.state.StateException;
import org.jboss.portal.presentation.model.state.structural.StructuralObject;
import org.jboss.portal.presentation.model.state.structural.StructuralState;
import org.jboss.portal.presentation.model.state.structural.StructuralStateContext;
import org.jboss.portal.presentation.test.model.state.structural.MockModel;
import org.jboss.portal.presentation.test.model.state.structural.MockObject;
+import org.jboss.portal.presentation.test.model.state.structural.MockException;
+import org.jboss.portal.presentation.test.model.state.structural.MockModelImpl;
import java.util.Collections;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
/**
* Test that the mock model we are using behaves in an expected manner
@@ -54,7 +55,7 @@
protected void setUp() throws Exception
{
- model = new MockModel();
+ model = new MockModelImpl();
ssc = model.getStructuralStateContext();
}
@@ -120,7 +121,7 @@
assertEquals(Collections.singletonMap("foo", "bar"),
rootState1_1.getProperties());
}
- public void testUpdateProperty()
+ public void testUpdateProperty() throws MockException
{
MockObject mockRoot = model.getRoot();
String rootId = mockRoot.getId();
@@ -176,29 +177,20 @@
{
try
{
- ssc.loadParent(object);
+ ssc.validate(object);
fail();
}
- catch (StaleStateException ignore)
+ catch (StateException ignore)
{
}
- try
- {
- ssc.loadChildren(object);
- fail();
- }
- catch (StaleStateException ignore)
- {
- }
}
private void assertNotStale(StructuralObject object)
{
- ssc.loadParent(object);
- ssc.loadChildren(object);
+ ssc.validate(object);
}
- public void testAddChild()
+ public void testAddChild() throws MockException
{
MockObject mockRoot = model.getRoot();
int rootV0 = mockRoot.getVersion();
@@ -261,4 +253,171 @@
assertEquals(rootId, root.getId());
}
+ public void testDestroy() throws MockException
+ {
+ MockObject mockRoot = model.getRoot();
+ MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PORTAL);
+ MockObject mockBar = mockFoo.addChild("bar", MockObject.Type.PAGE);
+ MockObject mockJuu = mockFoo.addChild("juu", MockObject.Type.PAGE);
+ int rootV0 = mockRoot.getVersion();
+ int fooV0 = mockFoo.getVersion();
+ int barV0 = mockBar.getVersion();
+ String fooId = mockFoo.getId();
+ String barId = mockBar.getId();
+ String juuId = mockJuu.getId();
+ StructuralObject foo0 = ssc.load(fooId);
+ StructuralObject bar0 = ssc.load(barId);
+ StructuralObject juu0 = ssc.load(juuId);
+
+ //
+ model.destroy(mockJuu);
+
+ //
+ StructuralObject foo1 = ssc.load(fooId);
+ StructuralObject bar1 = ssc.load(barId);
+ StructuralObject juu1 = ssc.load(juuId);
+ int rootV1 = mockRoot.getVersion();
+ int fooV1 = mockFoo.getVersion();
+ int barV1 = mockBar.getVersion();
+
+ //
+ assertStale(juu0);
+ assertStale(foo0);
+ assertNotStale(bar0);
+ assertNull(juu1);
+ assertNotStale(foo1);
+ assertNotStale(bar1);
+ assertEquals(Collections.singletonList(mockFoo), mockRoot.getChildren());
+ assertEquals(Collections.singletonList(mockBar), mockFoo.getChildren());
+ assertTrue(mockFoo.isValid());
+ assertTrue(mockBar.isValid());
+ assertFalse(mockJuu.isValid());
+ assertTrue(rootV1 == rootV0);
+ assertTrue(fooV1 > fooV0);
+ assertTrue(barV1 == barV0);
+
+ //
+ model.destroy(mockFoo);
+
+ int rootV2 = mockRoot.getVersion();
+ StructuralObject foo2 = ssc.load(fooId);
+ StructuralObject bar2 = ssc.load(barId);
+ StructuralObject juu2 = ssc.load(juuId);
+
+ //
+ assertStale(juu0);
+ assertStale(foo0);
+ assertStale(bar0);
+ assertNull(juu1);
+ assertStale(foo1);
+ assertStale(bar1);
+ assertNull(foo2);
+ assertNull(bar2);
+ assertNull(juu2);
+ assertEquals(Collections.emptyList(), mockRoot.getChildren());
+ assertFalse(mockFoo.isValid());
+ assertFalse(mockBar.isValid());
+ assertFalse(mockJuu.isValid());
+ assertTrue(rootV2 > rootV1);
+ }
+
+ public void testDestroyInvalidates() throws MockException
+ {
+ MockObject mockRoot = model.getRoot();
+ MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
+
+ //
+ model.destroy(mockFoo);
+
+ //
+ assertEquals(false, mockFoo.isValid());
+ try
+ {
+ mockFoo.addChild("juu", MockObject.Type.PAGE);
+ }
+ catch (MockException e)
+ {
+ assertEquals(MockException.ErrorCode.INVALID_OBJECT, e.getCode());
+ }
+ try
+ {
+ model.destroy(mockFoo);
+ }
+ catch (MockException e)
+ {
+ assertEquals(MockException.ErrorCode.INVALID_OBJECT, e.getCode());
+ }
+ try
+ {
+ mockFoo.setPropertyValue("blah", "bluh");
+ }
+ catch (MockException e)
+ {
+ assertEquals(MockException.ErrorCode.INVALID_OBJECT, e.getCode());
+ }
+ }
+
+ public void testMove() throws MockException
+ {
+ MockObject mockRoot = model.getRoot();
+
+ MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
+ MockObject mockBar = mockRoot.addChild("bar", MockObject.Type.PAGE);
+
+ mockFoo.move(mockBar);
+
+ assertSame(mockBar, mockFoo.getParent());
+ assertEquals(Collections.singletonList(mockFoo), mockBar.getChildren());
+ assertEquals(Collections.singletonList(mockBar), mockRoot.getChildren());
+ }
+
+ public void testMoveViolatesConstraint() throws MockException
+ {
+ MockObject mockRoot = model.getRoot();
+
+ MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
+ MockObject mockBar = mockRoot.addChild("bar", MockObject.Type.PAGE);
+ MockObject mockJuu = mockBar.addChild("juu", MockObject.Type.PAGE);
+
+ //
+ try
+ {
+ mockRoot.move(mockRoot);
+ }
+ catch (MockException e)
+ {
+ assertEquals(MockException.ErrorCode.CONSTRAINT_VIOLATION, e.getCode());
+ }
+
+ //
+ try
+ {
+ mockRoot.move(mockFoo);
+ }
+ catch (MockException e)
+ {
+ assertEquals(MockException.ErrorCode.CONSTRAINT_VIOLATION, e.getCode());
+ }
+
+ //
+ try
+ {
+ mockBar.move(mockBar);
+ }
+ catch (MockException e)
+ {
+ assertEquals(MockException.ErrorCode.CONSTRAINT_VIOLATION, e.getCode());
+ }
+
+ //
+ try
+ {
+ mockBar.move(mockJuu);
+ }
+ catch (MockException e)
+ {
+ assertEquals(MockException.ErrorCode.CONSTRAINT_VIOLATION, e.getCode());
+ }
+ }
+
}
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-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -25,7 +25,7 @@
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
-import org.jboss.portal.presentation.impl.model.UIContextImpl;
+import org.jboss.portal.presentation.impl.model.container.UIObjectContainer;
import
org.jboss.portal.presentation.impl.model.state.navigational.NavigationalStateContextImpl;
import org.jboss.portal.presentation.model.StateScopeType;
import org.jboss.portal.presentation.model.UIContext;
@@ -38,6 +38,8 @@
import org.jboss.portal.presentation.model.state.StateChangeVetoException;
import org.jboss.portal.presentation.test.model.state.structural.MockModel;
import org.jboss.portal.presentation.test.model.state.structural.MockObject;
+import org.jboss.portal.presentation.test.model.state.structural.MockException;
+import org.jboss.portal.presentation.test.model.state.structural.MockModelImpl;
import java.util.Collections;
import java.util.HashMap;
@@ -70,7 +72,7 @@
*/
protected void setUp() throws Exception
{
- this.model = new MockModel();
+ this.model = new MockModelImpl();
}
/**
@@ -179,7 +181,7 @@
private UIContext createContext()
{
- return new UIContextImpl(model.getStructuralStateContext(), new
NavigationalStateContextImpl());
+ return new UIObjectContainer(model.getStructuralStateContext(), new
NavigationalStateContextImpl()).getRoot();
}
public void testGetPropertyThrowsIAE()
@@ -214,7 +216,7 @@
UIContext context = createContext();
//
- List<UIObject> children = context.getChildren();
+ List<? extends UIObject> children = context.getChildren();
assertNotNull(children);
assertEquals(2, children.size());
@@ -288,12 +290,12 @@
Assert.assertEquals(null, object.getProperty(scopeType, propertyName,
Blah.class)); }
}
- public void testUpdatePropertyNonExistingObject()
+ public void testUpdatePropertyNonExistingObject() throws MockException
{
UIContext context = createContext();
MockObject mockFoo = model.getRoot().addChild("foo",
MockObject.Type.PORTAL);
UIPortal foo = (UIPortal)context.getObject(mockFoo.getId());
- model.destroy(mockFoo.getId());
+ model.destroy(mockFoo);
//
try
@@ -306,7 +308,7 @@
}
}
- public void testUpdateExistingMutableStructuralProperty()
+ public void testUpdateExistingMutableStructuralProperty() throws MockException
{
MockObject mockFoo = model.getRoot().addChild("foo",
MockObject.Type.PORTAL);
String fooId = mockFoo.getId();
@@ -338,7 +340,7 @@
eventAssert.assertEmpty();
}
- public void testUpdateExistingNonMutableStructuralProperty()
+ public void testUpdateExistingNonMutableStructuralProperty() throws MockException
{
MockObject mockFoo = model.getRoot().addChild("foo",
MockObject.Type.PORTAL);
String fooId = mockFoo.getId();
@@ -377,7 +379,7 @@
eventAssert.assertEmpty();
}
- public void testUpdateNonExistingMutableStructuralProperty()
+ public void testUpdateNonExistingMutableStructuralProperty() throws MockException
{
MockObject mockFoo = model.getRoot().addChild("foo",
MockObject.Type.PORTAL);
String fooId = mockFoo.getId();
@@ -408,7 +410,7 @@
eventAssert.assertEmpty();
}
- public void testUpdateNonExistingNonMutableStructuralProperty()
+ public void testUpdateNonExistingNonMutableStructuralProperty() throws MockException
{
MockObject mockFoo = model.getRoot().addChild("foo",
MockObject.Type.PORTAL);
mockFoo.setPropertyBehavior("foo", MockObject.Failure.veto());
@@ -446,7 +448,7 @@
eventAssert.assertEmpty();
}
- public void testUpdateNavigationalProperty()
+ public void testUpdateNavigationalProperty() throws MockException
{
MockObject mockFoo = model.getRoot().addChild("foo",
MockObject.Type.PORTAL);
UIContext context = createContext();
@@ -476,7 +478,7 @@
context.setProperty(StateScopeType.STRUCTURAL, "foo", "bar2");
}
- public void testConcurrentStructuralPropertyDetection()
+ public void testConcurrentStructuralPropertyDetection() throws MockException
{
UIContext context = createContext();
@@ -503,23 +505,38 @@
EventAssert eventAssert = new EventAssert();
context.addModelListener(eventAssert);
+ //
+ List<? extends UIObject> rootChildren = context.getChildren();
+ assertNotNull(rootChildren);
+ assertEquals(0, rootChildren.size());
assertNull(context.getChild("foo"));
+ //
UIPortal portal = context.createChild("foo", UIPortal.class);
-
assertNotNull(portal);
-
assertSame(context, portal.getParent());
-
assertSame(portal, context.getChild("foo"));
-
+ assertEquals(1, rootChildren.size());
+ assertSame(portal, rootChildren.iterator().next());
eventAssert.next(portal.getId(), new
StructuralStateModification.Creation(UIPortal.class, "foo", new
HashMap<String, String>()));
}
- public void testBlah()
+ public void testDestroyChild() throws Exception
{
+ MockObject mockFoo = model.getRoot().addChild("foo",
MockObject.Type.PORTAL);
+ MockObject mockBar = model.getRoot().addChild("bar",
MockObject.Type.PORTAL);
+ MockObject mockJuu = mockFoo.addChild("juu", MockObject.Type.PAGE);
+ UIContext context = createContext();
+ UIPortal foo = (UIPortal)context.getChild("foo");
+ UIPage juu = (UIPage)foo.getChild("juu");
+ context.destroyChild("foo");
+
+ List<? extends UIObject> rootChildren = context.getChildren();
+
+
+
}
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/UIModelTester.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/UIModelTester.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/UIModelTester.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -24,10 +24,10 @@
import junit.framework.TestCase;
import org.apache.log4j.Logger;
-import org.jboss.portal.presentation.impl.model.UIContextImpl;
+import org.jboss.portal.presentation.impl.model.container.UIObjectContainer;
import
org.jboss.portal.presentation.impl.model.state.navigational.NavigationalStateContextImpl;
import org.jboss.portal.presentation.model.UIContext;
-import org.jboss.portal.presentation.test.model.state.structural.MockModel;
+import org.jboss.portal.presentation.test.model.state.structural.MockModelImpl;
/**
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
@@ -43,7 +43,7 @@
*/
protected void setUp() throws Exception
{
- this.uiContext = new UIContextImpl(new MockModel().getStructuralStateContext(), new
NavigationalStateContextImpl());
+ this.uiContext = new UIObjectContainer(new
MockModelImpl().getStructuralStateContext(), new
NavigationalStateContextImpl()).getRoot();
}
/**
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockException.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockException.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockException.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -0,0 +1,56 @@
+/******************************************************************************
+ * 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.test.model.state.structural;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class MockException extends Exception
+{
+
+ /** . */
+ private ErrorCode code;
+
+ public MockException(ErrorCode code)
+ {
+ this.code = code;
+ }
+
+ public MockException(String s, ErrorCode code)
+ {
+ super(s);
+ this.code = code;
+ }
+
+ public enum ErrorCode
+ {
+ INVALID_OBJECT,
+ CONSTRAINT_VIOLATION
+ }
+
+ public ErrorCode getCode()
+ {
+ return code;
+ }
+}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -22,196 +22,19 @@
******************************************************************************/
package org.jboss.portal.presentation.test.model.state.structural;
-import org.jboss.portal.presentation.model.UIContext;
-import org.jboss.portal.presentation.model.UIObject;
-import org.jboss.portal.presentation.model.UIPage;
-import org.jboss.portal.presentation.model.UIPortal;
-import org.jboss.portal.presentation.model.UIWindow;
-import org.jboss.portal.presentation.model.state.NoSuchStateException;
-import org.jboss.portal.presentation.model.state.StaleStateException;
-import org.jboss.portal.presentation.model.state.StateChangeVetoException;
-import org.jboss.portal.presentation.model.state.StateException;
-import
org.jboss.portal.presentation.model.state.structural.AbstractStructuralStateContext;
-import org.jboss.portal.presentation.model.state.structural.StructuralObject;
import org.jboss.portal.presentation.model.state.structural.StructuralStateContext;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 630 $
*/
-public class MockModel
+public interface MockModel
{
- /** . */
- private int generator = 0;
+ StructuralStateContext getStructuralStateContext();
- /** . */
- private final Map<String, MockObjectImpl> universe = new HashMap<String,
MockObjectImpl>();
+ MockObject getRoot();
- /** . */
- private final MockObjectImpl root = createObject(MockObject.Type.CONTEXT,
"");
+ void destroy(MockObject object) throws MockException;
- /** . */
- private static final Map<Class<? extends UIObject>, MockObject.Type> map;
-
- static
- {
- map = new HashMap<Class<? extends UIObject>, MockObject.Type>();
- map.put(UIContext.class, MockObject.Type.CONTEXT);
- map.put(UIPage.class, MockObject.Type.PAGE);
- map.put(UIPortal.class, MockObject.Type.PORTAL);
- map.put(UIWindow.class, MockObject.Type.WINDOW);
- }
-
- public StructuralStateContext getStructuralStateContext()
- {
- return structuralStateContext;
- }
-
- public MockObject getRoot()
- {
- return root;
- }
-
- public void destroy(String objectId)
- {
- MockObjectImpl object = universe.remove(objectId);
-
- //
- object.destroy();
- }
-
- MockObjectImpl createObject(MockObject.Type type, String name)
- {
- MockObjectImpl mockObject = new MockObjectImpl(this, name, type, "" +
generator++);
- universe.put(mockObject.getId(), mockObject);
- return mockObject;
- }
-
- private final StructuralStateContext structuralStateContext = new
AbstractStructuralStateContext()
- {
- public StructuralObject load(String objectId) throws IllegalArgumentException
- {
- if (objectId == null)
- {
- throw new IllegalArgumentException();
- }
- MockObjectImpl object = universe.get(objectId);
- return object != null ? object.takeSnapshot() : null;
- }
-
- public String getRootId()
- {
- return root.getId();
- }
-
- public void validate(StructuralObject object) throws StateException
- {
- getValidMockObject(object);
- }
-
- public StructuralObject.Update update(StructuralObject object, Map<String,
String> changes) throws StateChangeVetoException
- {
- MockObject mockObject = getValidMockObject(object);
-
- //
- for (Map.Entry<String, String> entry : changes.entrySet())
- {
- String propertyName = entry.getKey();
- MockObject.UpdateBehavior behavior =
mockObject.getPropertyBehavior(propertyName);
- if (behavior instanceof MockObject.Veto)
- {
- throw new StateChangeVetoException("Cannot modify non behavior
property");
- }
- else if (behavior instanceof MockObject.Failure)
- {
- MockObject.Failure failure = (MockObject.Failure)behavior;
- failure.throwAs(IllegalArgumentException.class).
- throwAs(StateChangeVetoException.class).
- throwAs(StateException.class);
- }
- else
- {
- mockObject.setPropertyValue(propertyName, entry.getValue());
- }
- }
-
- //
- return new StructuralObject.Update(mockObject.takeSnapshot());
- }
-
- public StructuralObject.Creation create(StructuralObject parent, Class<? extends
UIObject> classType, String name, Map<String, String> properties) throws
StateException, IllegalArgumentException
- {
- MockObject mockParent = getValidMockObject(parent);
-
- //
- MockObject.Type type = map.get(classType);
-
- //
- MockObject mockChild = mockParent.addChild(name, type, properties);
-
- //
- return new StructuralObject.Creation(mockParent.takeSnapshot(),
mockChild.takeSnapshot());
- }
-
- public List<StructuralObject> loadChildren(StructuralObject parent)
- {
- MockObject mockParent = getValidMockObject(parent);
-
- //
- List<StructuralObject> tmp = new ArrayList<StructuralObject>();
- for (MockObject mockChild : mockParent.getChildren())
- {
- tmp.add(mockChild.takeSnapshot());
- }
- return tmp;
- }
-
- public StructuralObject loadParent(StructuralObject child)
- {
- MockObject mockchild = getValidMockObject(child);
- MockObject mockParent = mockchild.getParent();
- return mockParent != null ? mockParent.takeSnapshot() : null;
- }
-
- private MockObject getValidMockObject(StructuralObject object)
- {
- if (object == null)
- {
- throw new IllegalArgumentException("No null object accepted");
- }
-
- //
- StructuralObjectImpl structuralObject = (StructuralObjectImpl)object;
-
- //
- MockObjectImpl mockObject = universe.get(structuralObject.getId());
-
- //
- if (mockObject == null)
- {
- throw new NoSuchStateException();
- }
-
- //
- if (!mockObject.isValid())
- {
- throw new StaleStateException();
- }
-
- //
- if (mockObject.getVersion() != structuralObject.version)
- {
- throw new StaleStateException("Version has changed obtained=" +
mockObject.getVersion() + " wanted=" + structuralObject.version);
- }
-
- //
- return mockObject;
- }
- };
}
Copied:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java
(from rev 9357,
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java)
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -0,0 +1,306 @@
+/******************************************************************************
+ * 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.test.model.state.structural;
+
+import org.jboss.portal.presentation.model.UIContext;
+import org.jboss.portal.presentation.model.UIObject;
+import org.jboss.portal.presentation.model.UIPage;
+import org.jboss.portal.presentation.model.UIPortal;
+import org.jboss.portal.presentation.model.UIWindow;
+import org.jboss.portal.presentation.model.state.NoSuchStateException;
+import org.jboss.portal.presentation.model.state.StaleStateException;
+import org.jboss.portal.presentation.model.state.StateChangeVetoException;
+import org.jboss.portal.presentation.model.state.StateException;
+import org.jboss.portal.presentation.model.state.ConstraintViolationException;
+import
org.jboss.portal.presentation.model.state.structural.AbstractStructuralStateContext;
+import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.model.state.structural.StructuralStateContext;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class MockModelImpl implements MockModel
+{
+
+ /** . */
+ private int generator = 0;
+
+ /** . */
+ private final Map<String, MockObjectImpl> universe = new HashMap<String,
MockObjectImpl>();
+
+ /** . */
+ private final MockObjectImpl root = createObject(MockObject.Type.CONTEXT,
"");
+
+ /** . */
+ private static final Map<Class<? extends UIObject>, MockObject.Type> map;
+
+ static
+ {
+ map = new HashMap<Class<? extends UIObject>, MockObject.Type>();
+ map.put(UIContext.class, MockObject.Type.CONTEXT);
+ map.put(UIPage.class, MockObject.Type.PAGE);
+ map.put(UIPortal.class, MockObject.Type.PORTAL);
+ map.put(UIWindow.class, MockObject.Type.WINDOW);
+ }
+
+ public StructuralStateContext getStructuralStateContext()
+ {
+ return structuralStateContext;
+ }
+
+ public MockObject getRoot()
+ {
+ return root;
+ }
+
+ public void destroy(MockObject object) throws MockException
+ {
+ destroy((MockObjectImpl)object, new ArrayList<String>());
+ }
+
+ void destroy(MockObjectImpl object, List<String> ids) throws MockException
+ {
+ universe.remove(object.getId());
+
+ // Remove children recursively
+ for (MockObjectImpl child : object.getChildren())
+ {
+ destroy(child, ids);
+ }
+
+ //
+ object.destroy();
+
+ //
+ ids.add(object.getId());
+ }
+
+ MockObjectImpl createObject(MockObject.Type type, String name)
+ {
+ MockObjectImpl mockObject = new MockObjectImpl(this, name, type, "" +
generator++);
+ universe.put(mockObject.getId(), mockObject);
+ return mockObject;
+ }
+
+ private final StructuralStateContext structuralStateContext = new
AbstractStructuralStateContext()
+ {
+ public StructuralObject load(String objectId) throws IllegalArgumentException
+ {
+ if (objectId == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ MockObjectImpl object = universe.get(objectId);
+
+ //
+ return object != null ? object.takeSnapshot() : null;
+ }
+
+ public String getRootId()
+ {
+ return root.getId();
+ }
+
+ public void validate(StructuralObject object) throws StateException
+ {
+ getValidMockObject(object);
+ }
+
+ public StructuralObject.Update update(StructuralObject object, Map<String,
String> changes) throws StateChangeVetoException
+ {
+ MockObject mockObject = getValidMockObject(object);
+
+ //
+ for (Map.Entry<String, String> entry : changes.entrySet())
+ {
+ String propertyName = entry.getKey();
+ MockObject.UpdateBehavior behavior =
mockObject.getPropertyBehavior(propertyName);
+ if (behavior instanceof MockObject.Veto)
+ {
+ throw new StateChangeVetoException("Cannot modify non behavior
property");
+ }
+ else if (behavior instanceof MockObject.Failure)
+ {
+ MockObject.Failure failure = (MockObject.Failure)behavior;
+ failure.throwAs(IllegalArgumentException.class).
+ throwAs(StateChangeVetoException.class).
+ throwAs(StateException.class);
+ }
+ else
+ {
+ try
+ {
+ mockObject.setPropertyValue(propertyName, entry.getValue());
+ }
+ catch (MockException e)
+ {
+ throw new AssertionError();
+ }
+ }
+ }
+
+ //
+ return new StructuralObject.Update(mockObject.takeSnapshot());
+ }
+
+ public StructuralObject.Creation create(StructuralObject parent, Class<? extends
UIObject> classType, String name, Map<String, String> properties) throws
StateException, IllegalArgumentException
+ {
+ MockObject mockParent = getValidMockObject(parent);
+
+ //
+ MockObject.Type type = map.get(classType);
+
+ //
+ try
+ {
+ MockObject mockChild = mockParent.addChild(name, type, properties);
+
+ //
+ return new StructuralObject.Creation(mockParent.takeSnapshot(),
mockChild.takeSnapshot());
+ }
+ catch (MockException e)
+ {
+ switch (e.getCode())
+ {
+ case CONSTRAINT_VIOLATION:
+ throw new ConstraintViolationException();
+ default:
+ throw new AssertionError();
+ }
+ }
+ }
+
+ public StructuralObject.Destruction destroy(StructuralObject object) throws
StateException, IllegalArgumentException
+ {
+ MockObjectImpl mockObject = getValidMockObject(object);
+
+ //
+ MockObjectImpl parent = mockObject.getParent();
+
+ //
+ List<String> ids = new ArrayList<String>();
+
+ //
+ try
+ {
+ MockModelImpl.this.destroy(mockObject, ids);
+ }
+ catch (MockException e)
+ {
+ // It is kind of unexpected since we are destroying something valid
+ throw new StateException(e);
+ }
+
+ //
+ return new StructuralObject.Destruction(parent.takeSnapshot(), ids);
+ }
+
+ public StructuralObject.Move move(StructuralObject source, StructuralObject
destination) throws StateException, IllegalArgumentException
+ {
+ MockObject mockSource = getValidMockObject(source);
+ MockObject mockDestination = getValidMockObject(destination);
+
+ //
+ try
+ {
+ mockSource.move(mockDestination);
+
+ //
+ return new StructuralObject.Move(mockSource.takeSnapshot(),
mockSource.takeSnapshot());
+ }
+ catch (MockException e)
+ {
+ switch (e.getCode())
+ {
+ default:
+ throw new AssertionError();
+ case CONSTRAINT_VIOLATION:
+ throw new ConstraintViolationException();
+ }
+ }
+ }
+
+ public List<StructuralObject> loadChildren(StructuralObject parent)
+ {
+ MockObject mockParent = getValidMockObject(parent);
+
+ //
+ List<StructuralObject> tmp = new ArrayList<StructuralObject>();
+ for (MockObject mockChild : mockParent.getChildren())
+ {
+ tmp.add(mockChild.takeSnapshot());
+ }
+ return tmp;
+ }
+
+ public StructuralObject loadParent(StructuralObject child) throws StateException
+ {
+ MockObject mockchild = getValidMockObject(child);
+ MockObject mockParent = mockchild.getParent();
+ return mockParent != null ? mockParent.takeSnapshot() : null;
+ }
+
+ private MockObjectImpl getValidMockObject(StructuralObject object) throws
StateException
+ {
+ if (object == null)
+ {
+ throw new IllegalArgumentException("No null object accepted");
+ }
+
+ //
+ StructuralObjectImpl structuralObject = (StructuralObjectImpl)object;
+
+ //
+ MockObjectImpl mockObject = universe.get(structuralObject.getId());
+
+ //
+ if (mockObject == null)
+ {
+ throw new NoSuchStateException();
+ }
+
+ //
+ if (!mockObject.isValid())
+ {
+ throw new StaleStateException();
+ }
+
+ //
+ if (mockObject.getVersion() != structuralObject.version)
+ {
+ throw new StaleStateException("Version has changed obtained=" +
mockObject.getVersion() + " wanted=" + structuralObject.version);
+ }
+
+ //
+ return mockObject;
+ }
+ };
+}
\ No newline at end of file
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -102,25 +102,27 @@
int getVersion();
- String getPropertyValue(String propertyName);
+ String getPropertyValue(String propertyName) throws IllegalArgumentException;
- void setPropertyBehavior(String propertyName, UpdateBehavior propertyBehavior);
+ void setPropertyBehavior(String propertyName, UpdateBehavior propertyBehavior) throws
IllegalArgumentException;
- UpdateBehavior getPropertyBehavior(String propertyName);
+ UpdateBehavior getPropertyBehavior(String propertyName) throws
IllegalArgumentException;
- void setPropertyValue(String propertyName, String propertyValue);
+ void setPropertyValue(String propertyName, String propertyValue) throws
MockException;
Set<String> getPropertyNames();
- MockObject addChild(String name, Type type, Map<String, String> state);
+ MockObject addChild(String name, Type type, Map<String, String> state) throws
IllegalArgumentException, MockException;
- MockObject addChild(String name, Type type);
+ MockObject addChild(String name, Type type) throws IllegalArgumentException,
MockException;
MockObject getParent();
- List<MockObject> getChildren();
+ List<? extends MockObject> getChildren();
boolean isValid();
StructuralObject takeSnapshot();
+
+ void move(MockObject destination) throws IllegalArgumentException, MockException;
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java 2007-12-18
01:32:23 UTC (rev 9360)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java 2007-12-18
17:34:47 UTC (rev 9361)
@@ -23,6 +23,7 @@
package org.jboss.portal.presentation.test.model.state.structural;
import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.model.state.ConstraintViolationException;
import java.util.ArrayList;
import java.util.Collections;
@@ -61,7 +62,7 @@
private final LinkedHashMap<String, MockObjectImpl> children;
/** . */
- private final MockModel model;
+ private final MockModelImpl model;
/** . */
private MockObjectImpl parent;
@@ -72,7 +73,7 @@
/** The version id. */
int version;
- MockObjectImpl(MockModel model, String name, MockObject.Type type, String id)
+ MockObjectImpl(MockModelImpl model, String name, MockObject.Type type, String id)
{
this.model = model;
this.name = name;
@@ -107,40 +108,46 @@
public String getPropertyValue(String propertyName)
{
+ if (name == null)
+ {
+ throw new IllegalArgumentException();
+ }
return propertyValues.get(propertyName);
}
- public MockObject getParent()
+ public MockObjectImpl getParent()
{
return parent;
}
- public List<MockObject> getChildren()
+ public List<MockObjectImpl> getChildren()
{
- return Collections.unmodifiableList(new
ArrayList<MockObject>(children.values()));
+ return Collections.unmodifiableList(new
ArrayList<MockObjectImpl>(children.values()));
}
- public MockObject addChild(String name, MockObject.Type type)
+ public MockObjectImpl addChild(String name, MockObject.Type type) throws
MockException
{
return addChild(name, type, EMPTY_STATE);
}
public Set<String> getPropertyNames()
{
- return propertyValues.keySet();
+ return Collections.unmodifiableSet(propertyValues.keySet());
}
- public MockObject addChild(String name, Type type, Map<String, String> state)
+ public MockObjectImpl addChild(String name, Type type, Map<String, String>
state) throws MockException
{
+ if (!valid)
+ {
+ throw new MockException(MockException.ErrorCode.INVALID_OBJECT);
+ }
if (name == null)
{
throw new IllegalArgumentException();
}
-
- //
if (children.containsKey(name))
{
- throw new IllegalArgumentException();
+ throw new MockException(MockException.ErrorCode.CONSTRAINT_VIOLATION);
}
//
@@ -183,11 +190,19 @@
public UpdateBehavior getPropertyBehavior(String propertyName)
{
+ if (propertyName == null)
+ {
+ throw new IllegalArgumentException();
+ }
return propertyBehaviors.get(propertyName);
}
- public void setPropertyValue(String propertyName, String propertyValue)
+ public void setPropertyValue(String propertyName, String propertyValue) throws
MockException
{
+ if (!valid)
+ {
+ throw new MockException(MockException.ErrorCode.INVALID_OBJECT);
+ }
if (propertyName == null)
{
throw new IllegalArgumentException();
@@ -215,14 +230,55 @@
return new StructuralObjectImpl(id, version, new StructuralStateImpl(type.clazz,
name, new HashMap<String, String>(propertyValues)));
}
- void destroy()
+ public void move(MockObject destination) throws IllegalStateException, MockException
{
- // Remove children recursively
- for (String childId : new ArrayList<String>(children.keySet()))
+ move((MockObjectImpl)destination);
+ }
+
+ private void move(MockObjectImpl destination) throws MockException
+ {
+ if (!valid)
{
- model.destroy(childId);
+ throw new MockException(MockException.ErrorCode.INVALID_OBJECT);
}
+ if (destination == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ // The source cannot an ancestor of the destination
+ for (MockObject current = destination;current != null;current =
current.getParent())
+ {
+ if (current == this)
+ {
+ throw new MockException(MockException.ErrorCode.CONSTRAINT_VIOLATION);
+ }
+ }
+
+ // No duplicate child name
+ for (MockObjectImpl child : destination.children.values())
+ {
+ if (name.equals(child.name))
+ {
+ throw new MockException(MockException.ErrorCode.CONSTRAINT_VIOLATION);
+ }
+ }
+
+ //
+ parent.children.remove(id);
+ destination.children.put(id, this);
+ parent = destination;
+ version++;
+ destination.version++;
+ }
+
+ public void destroy() throws MockException
+ {
+ if (!valid)
+ {
+ throw new MockException(MockException.ErrorCode.INVALID_OBJECT);
+ }
+
// Detach from parent
if (parent != null)
{