Author: maksimkaszynski
Date: 2007-06-12 09:53:13 -0400 (Tue, 12 Jun 2007)
New Revision: 1137
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/DataModelCache.java
Log:
added server-side caching
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/DataModelCache.java
===================================================================
--- trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/DataModelCache.java
(rev 0)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/DataModelCache.java 2007-06-12
13:53:13 UTC (rev 1137)
@@ -0,0 +1,170 @@
+/**
+ *
+ */
+package org.richfaces.model;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.faces.context.FacesContext;
+import javax.faces.model.DataModelListener;
+
+import org.ajax4jsf.ajax.repeat.DataVisitor;
+import org.ajax4jsf.ajax.repeat.Range;
+import org.ajax4jsf.ajax.repeat.SerializableDataModel;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class DataModelCache extends GridDataModel {
+
+ private static Log log = LogFactory.getLog(DataModelCache.class);
+
+ private static class DataRange {
+ private int startRow;
+ private int endRow;
+ private SortOrder sortOrder;
+ private List loadedData;
+
+ public DataRange(int startRow, int endRow, SortOrder sortOrder,
+ List loadedData) {
+ super();
+ this.startRow = startRow;
+ this.endRow = endRow;
+ this.sortOrder = sortOrder;
+ this.loadedData = loadedData;
+ }
+
+ public boolean match(int s, int e, SortOrder sortOrder) {
+ return s == startRow
+ && e == endRow
+ && sortOrdersMatch(sortOrder, this.sortOrder);
+ }
+
+ private boolean sortOrdersMatch(SortOrder sortOrder1, SortOrder sortOrder2) {
+ boolean result = sortOrder1 == sortOrder2;
+
+ if (sortOrder1 != null && sortOrder2 != null) {
+ result = sortOrder1.equals(sortOrder2);
+ }
+
+ return result;
+ }
+
+ }
+
+ private Map secondaryMapping = new HashMap();
+
+ private DataRange dataRange;
+
+ private GridDataModel gridDataModel;
+
+
+ public DataModelCache(GridDataModel gridDataModel) {
+ super();
+ this.gridDataModel = gridDataModel;
+
+ if (log.isTraceEnabled()) {
+ log.trace("initializing with " + gridDataModel);
+ }
+ }
+
+ public void addDataModelListener(DataModelListener listener) {
+ gridDataModel.addDataModelListener(listener);
+ }
+
+ public DataModelListener[] getDataModelListeners() {
+ return gridDataModel.getDataModelListeners();
+ }
+
+ public Object getObjectById(Object id) {
+
+ if (log.isDebugEnabled()) {
+ log.debug("Trying to get object by id" + id);
+ }
+
+ Object cached = secondaryMapping.get(id);
+
+ if (cached == null) {
+
+ if (log.isDebugEnabled()) {
+ log.debug("Cache miss " + id + " falling back to original
model");
+ }
+
+ cached = gridDataModel.getObjectById(id);
+ secondaryMapping.put(id, cached);
+
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("At last found element " + cached);
+ }
+
+ return cached;
+ }
+
+ public int getRowCount() {
+ return gridDataModel.getRowCount();
+ }
+
+ public Object getRowData() {
+ return gridDataModel.getRowData();
+ }
+
+ public int getRowIndex() {
+ return gridDataModel.getRowIndex();
+ }
+
+ public Object getRowKey() {
+ return gridDataModel.getRowKey();
+ }
+
+ public SerializableDataModel getSerializableModel(Range range) {
+ return gridDataModel.getSerializableModel(range);
+ }
+
+ public Object getWrappedData() {
+ return gridDataModel.getWrappedData();
+ }
+
+ public boolean isRowAvailable() {
+ return secondaryMapping.containsKey(getRowKey()) || gridDataModel.isRowAvailable();
+ }
+
+ public List loadData(int startRow, int endRow, SortOrder sortOrder) {
+ if (dataRange == null || !dataRange.match(startRow, endRow, sortOrder)) {
+ List data = gridDataModel.loadData(startRow, endRow, sortOrder);
+ dataRange = new DataRange(startRow, endRow, sortOrder, data);
+ }
+ return dataRange.loadedData;
+ }
+
+ public void removeDataModelListener(DataModelListener listener) {
+ gridDataModel.removeDataModelListener(listener);
+ }
+
+ public void setRowIndex(int arg0) {
+ gridDataModel.setRowIndex(arg0);
+ }
+
+ public void setRowKey(Object key) {
+ gridDataModel.setRowKey(key);
+ }
+
+ public void setWrappedData(Object arg0) {
+ gridDataModel.setWrappedData(arg0);
+ }
+
+
+ public void walk(FacesContext context, DataVisitor visitor, Range range,
+ Object argument) throws IOException {
+ // TODO Auto-generated method stub
+ super.walk(context, visitor, range, argument);
+ }
+
+}