You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by de...@apache.org on 2017/09/17 12:30:42 UTC

[myfaces-trinidad] 19/36: TRINIDAD-1613 - Add support for rowKey change

This is an automated email from the ASF dual-hosted git repository.

deki pushed a commit to branch 1.2.12.2-branch
in repository https://gitbox.apache.org/repos/asf/myfaces-trinidad.git

commit 29b53ead68980a8dd66fc53abe4a91487603858a
Author: Matthias Wessendorf <ma...@apache.org>
AuthorDate: Wed Feb 10 22:03:19 2010 +0000

    TRINIDAD-1613 - Add support for rowKey change
    
    thanks to Min Lu for her patch
---
 .../myfaces/trinidad/model/CollectionModel.java    |  66 ++++++++++-
 .../myfaces/trinidad/model/RowKeyChangeEvent.java  | 124 +++++++++++++++++++++
 .../trinidad/model/RowKeyChangeListener.java       |  33 ++++++
 3 files changed, 222 insertions(+), 1 deletion(-)

diff --git a/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/CollectionModel.java b/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/CollectionModel.java
index 4d9716b..bfb83e1 100644
--- a/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/CollectionModel.java
+++ b/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/CollectionModel.java
@@ -20,6 +20,7 @@ package org.apache.myfaces.trinidad.model;
 
 import java.util.Collections;
 import java.util.List;
+import java.util.ArrayList;
 import javax.faces.model.DataModel;
 
 /**
@@ -296,6 +297,68 @@ public abstract class CollectionModel extends DataModel
     }
   }
 
+  /**
+   * <p>
+   * Adds the listener to the Set of RowKeyChangeListeners on the Collection.
+   * </p>
+   * <p>
+   * The same listener instance may be added multiple times, but will only be called once per change.
+   * </p>
+   * <p>
+   * Since the Collection may have a lifetime longer than Request, the listener implementation 
+   * should take care not to maintain any references to objects only valid in the current Request.  
+   * For example, if a UIComponent wishes to listen on a Collection, the UIComponent cannot use a 
+   * listener that maintains a Java reference to the UIComponent instance because UIComponent 
+   * instances are only valid for the current request (this also precludes the use of a non-static 
+   * inner class for the listener).  Instead, the UIComponent would need to use an indirect 
+   * reference such as {@link ComponentReference} to dynamically find the correct instance to use.  
+   * In the case where the Collection has a short lifetime, the code that adds the listener needs to 
+   * ensure that it executes every time the Collection instance is reinstantiated.
+   * </p>
+   * @param listener The listener for RowKeyChangeEvents to add to the Collection 
+   * @see #removeRowKeyChangeListener
+   */
+  public void addRowKeyChangeListener(RowKeyChangeListener listener)
+  {
+    if(!_rowKeyChangeListeners.contains(listener))
+      _rowKeyChangeListeners.add(listener);
+  }
+
+  /**
+   * <p>
+   * Remove an existing listener from the Set of RowKeyChangeListeners on the Collection.
+   * </p>
+   * <p>
+   * The same listener instance may be removed multiple times wihtout failure.
+   * </p>
+   * 
+   * @param listener The listener for RowKeyChangeEvents to remove from the Collection
+   */
+  public void removeRowKeyChangeListener(RowKeyChangeListener listener)
+  {
+    _rowKeyChangeListeners.remove(listener);
+  }
+
+  /**
+   * Fire an existing RowKeyChangeEvent to any registered listeners.
+   * No event is fired if the given event's old and new row keys are equal and non-null.
+   * @param event  The RowKeyChangeEvent object.
+   */
+  protected void fireRowKeyChange(RowKeyChangeEvent event) 
+  {
+    Object oldRowKey = event.getOldRowKey();
+    Object newRowKey = event.getNewRowKey();
+    if (oldRowKey != null && newRowKey != null && oldRowKey.equals(newRowKey)) 
+    {
+      return;
+    }
+
+    for (RowKeyChangeListener listener: _rowKeyChangeListeners)
+    {
+      listener.onRowKeyChange(event);
+    }
+  }
+
   //
   // Below is the default implemenation for the LocalRowKeyIndex interface.  
   //
@@ -453,5 +516,6 @@ public abstract class CollectionModel extends DataModel
   {
     return LocalRowKeyIndex.LocalCachingStrategy.NONE;
   }
-  
+
+  private List<RowKeyChangeListener> _rowKeyChangeListeners = new ArrayList<RowKeyChangeListener>(3);    
 }
diff --git a/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/RowKeyChangeEvent.java b/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/RowKeyChangeEvent.java
new file mode 100644
index 0000000..7433c44
--- /dev/null
+++ b/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/RowKeyChangeEvent.java
@@ -0,0 +1,124 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.myfaces.trinidad.model;
+
+import java.util.EventObject;
+
+/**
+ * Event that is generated when RowKey changes.
+ * This event is fired when a row is updated, inserted or deleted
+ * The oldRowKey or new RowKey can be NULL
+ * 
+ * The serialization behavior depends on the implementation of the collectionModel that consumes 
+ * the RowKeyChangeEvent. If the collectionModel implementation is serializable and keeps track 
+ * of row key change events that it recevies, then these events are serialized with the model; 
+ * otherwise, the events are not serialized
+ */
+public class RowKeyChangeEvent extends EventObject 
+{
+  /**
+   * The operation that triggers row key to change
+   */
+  public enum Cause
+  {
+     UPDATE,
+     INSERT,
+     DELETE
+  };
+  
+  /**
+  * Creates a new RowKeyChangeEvent
+  * @param source    the source of the event
+  * @param oldRowKey the old RowKey.
+  * @param newRowKey the new RowKey.
+  * @param cause the operation that triggers this event
+  */  
+  public RowKeyChangeEvent(CollectionModel source, Object oldRowKey, Object newRowKey, Cause cause)
+  {
+    super(source);
+
+    _oldRowKey = oldRowKey;
+    _newRowKey = newRowKey;
+    _cause = cause;
+  }
+   
+  /**
+  * retrieve the old RowKey from the event
+  * @return the old RowKey of the event.
+  */
+  public Object getOldRowKey()
+  {
+    return _oldRowKey;
+  }
+  
+  /**
+  * retrieve the new row key from the event
+  * @return the new row key of the event.
+  */
+  public Object getNewRowKey()
+  {
+    return _newRowKey;
+  }
+  
+  public Cause getCause()
+  {
+    return _cause;
+  }
+  
+  @Override
+  public boolean equals(Object o)
+  {
+    if (o == this)
+      return true;
+    else if (!(o instanceof RowKeyChangeEvent))
+      return false;
+    else
+    {
+      RowKeyChangeEvent otherEvent = (RowKeyChangeEvent)o;
+
+      return _oldRowKey.equals(otherEvent._oldRowKey) &&
+             _newRowKey.equals(otherEvent._newRowKey) &&
+             _cause == otherEvent._cause;
+    }    
+  }
+
+  @Override
+  public int hashCode()
+  {
+      int hashCode = 17;
+      hashCode = 31 * hashCode + _oldRowKey.hashCode();
+      hashCode = 31 * hashCode + _newRowKey.hashCode(); 
+      hashCode = 31 * hashCode + _cause.hashCode();
+      return hashCode;
+  }
+  
+  @Override
+  public String toString()
+  {
+    return super.toString() +
+           "[oldRowKey=" + _oldRowKey.toString() +
+           ", newRowKey=" + _newRowKey.toString() + 
+           ", cause=" + _cause.toString() + "]";
+  }
+  
+  private final Object _oldRowKey;
+  private final Object _newRowKey;
+  private final Cause _cause;
+  private static final long serialVersionUID = 1L;
+}
diff --git a/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/RowKeyChangeListener.java b/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/RowKeyChangeListener.java
new file mode 100644
index 0000000..bd1e10e
--- /dev/null
+++ b/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/RowKeyChangeListener.java
@@ -0,0 +1,33 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.myfaces.trinidad.model;
+
+/**
+ * Listener for RowKeyChangeEvent.
+ */
+public interface RowKeyChangeListener
+{
+  /**
+   * Called by CollectionModel to inform a RowKeyChangeListener that a RowKey has changed
+   * The old row key or the new row key can be null, it is expected that listener implementation
+   * to handle these situations with best efforts, for example, clear and rebuild component
+   * state cache to recover.
+   */
+  public void onRowKeyChange(RowKeyChangeEvent event); 
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@myfaces.apache.org" <co...@myfaces.apache.org>.