Author: abelevich
Date: 2007-05-28 14:11:25 -0400 (Mon, 28 May 2007)
New Revision: 899
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/snapshot/
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/snapshot/DataModelSnapshot.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/snapshot/ObjectLocator.java
Log:
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/snapshot/DataModelSnapshot.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/snapshot/DataModelSnapshot.java
(rev 0)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/snapshot/DataModelSnapshot.java 2007-05-28
18:11:25 UTC (rev 899)
@@ -0,0 +1,274 @@
+/*
+ * Copyright
+ * Copyright (c) Exadel,Inc. 2006
+ * All rights reserved.
+ *
+ * History
+ * $Source:
/cvs-master/intralinks-jsf-comps/components/data-view-grid/src/component/com/exadel/jsf/model/snapshot/DataModelSnapshot.java,v
$
+ * $Revision: 1.14 $
+ */
+
+package org.richfaces.model.snapshot;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.faces.context.FacesContext;
+
+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;
+import org.richfaces.model.BufferedSequenceRange;
+import org.richfaces.model.ScrollableGridDataModel2;
+import org.richfaces.model.GridDataModelFeatures;
+import org.richfaces.model.GridRange;
+import org.richfaces.model.IOExceptionWrapper;
+import org.richfaces.model.IterationBounds;
+
+
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class DataModelSnapshot extends SerializableDataModel implements
+ GridDataModelFeatures {
+
+ private static final Log log = LogFactory.getLog(DataModelSnapshot.class);
+
+ private static final long serialVersionUID = -4868285117998717494L;
+
+ private int rowIndex;
+
+ private IterationBounds bounds;
+
+ private int rowCount;
+
+ private Serializable[] keys;
+
+ private transient ObjectLocator locator;
+
+ private transient Object dataObjects[];
+
+// private transient Selection selection;
+
+ public DataModelSnapshot() {
+ }
+
+ public DataModelSnapshot(final ScrollableGridDataModel2 model,
+ BufferedSequenceRange range) {
+
+ locator = model.getObjectLocator();
+
+ if (locator == null) {
+ throw new NullPointerException("Object Locator can not be null");
+ }
+
+
+ rowCount = model.getRowCount();
+
+ bounds = range.getIterationBounds();
+
+ //keys = new Serializable[bounds.size()];
+ final List kiz = new ArrayList();
+
+ try {
+ model.walk(FacesContext.getCurrentInstance(), new DataVisitor() {
+ public void process(FacesContext context, Object rowKey, Object argument) throws
IOException {
+ model.setRowKey(rowKey);
+ Object data = model.getRowData();
+ Serializable key = locator.getObjectId(data);
+ kiz.add(key);
+ }
+ },
+ range,
+ null);
+ } catch (IOException e) {
+ log.error("Failed to initialize data cache", e);
+ }
+
+ keys = (Serializable[]) kiz.toArray(new Serializable[kiz.size()]);
+
+ }
+
+ public ObjectLocator getObjectLocator() {
+ return locator;
+ }
+
+ public int getRowCount() {
+ return rowCount;
+ }
+
+ public Object getRowData() {
+ int indx = getRowIndex() - bounds.getStartRow();
+ if (dataObjects != null && indx >= 0 && indx <
dataObjects.length) {
+ if(log.isDebugEnabled()) {
+ log.debug("Found data " + indx);
+ }
+ return dataObjects[indx];
+ }
+ if(log.isDebugEnabled()) {
+ log.debug("No data loaded " + indx );
+ }
+ return null;
+ }
+
+ public int getRowIndex() {
+ return rowIndex;
+ }
+
+ public Object getRowKey() {
+ int index = getRowIndex();
+ if (index == -1) {
+ return null;
+ }
+
+ return new Integer(index);
+ }
+
+ public Collection getSelectedObjects() {
+// initStorage();
+// return SelectionExtractor.extractSelection(this);
+ return null;
+ }
+//
+// public Selection getSelection() {
+// return selection;
+// }
+
+ /**
+ * @see
org.ajax4jsf.ajax.repeat.ExtendedDataModel#getSerializableModel(org.ajax4jsf.ajax.repeat.Range)
+ */
+ public SerializableDataModel getSerializableModel(Range range) {
+ return null;
+ }
+
+ public Object getWrappedData() {
+ return dataObjects;
+ }
+
+ private void initStorage() {
+ if (dataObjects == null && locator != null) {
+ if (keys == null) {
+ log.error("No identifiers were stored - where are they?");
+ return;
+ }
+ List keyz = Arrays.asList(keys);
+ Collection objectz = locator.findObjects(keyz);
+ Map objectMapping = new HashMap();
+ for (Iterator iter = objectz.iterator(); iter.hasNext();) {
+ Object dataObject = iter.next();
+ Serializable key = locator.getObjectId(dataObject);
+ if (key != null) {
+ objectMapping.put(key, dataObject);
+ }
+ }
+ dataObjects = new Object[keys.length];
+ for (int i = 0; i < keys.length; i++) {
+ dataObjects[i] = objectMapping.get(keys[i]);
+ }
+ }
+ }
+
+ /**
+ * @see javax.faces.model.DataModel#isRowAvailable()
+ */
+ public boolean isRowAvailable() {
+ return bounds.within(rowIndex);
+ }
+
+ /**
+ * @see com.exadel.jsf.model.BufferedSequenceModel#reset()
+ */
+ public void reset() {
+// selection = null;
+ locator = null;
+ }
+
+ public void setObjectLocator(ObjectLocator locator) {
+ this.locator = locator;
+ }
+
+ public void setRowCount(int rowCount) {
+ this.rowCount = rowCount;
+ }
+
+ public void setRowIndex(int rowIndex) {
+ this.rowIndex = rowIndex;
+ }
+
+ public void setRowKey(Object key) {
+ if (key == null) {
+ setRowIndex(-1);
+ } else {
+ setRowIndex(((Integer) key).intValue());
+ }
+
+ }
+
+// public void setSelection(Selection selection) {
+// this.selection = selection;
+// //new Throwable().printStackTrace();
+// }
+
+ /**
+ * @see javax.faces.model.DataModel#setWrappedData(java.lang.Object)
+ */
+ public void setWrappedData(Object data) {
+ dataObjects = (Object[]) data;
+ }
+
+ public void update() {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void walk(final FacesContext context, final DataVisitor visitor,
+ Range range, Object argument) throws IOException {
+
+ log.debug("DataModelSnapshot.walk()");
+
+ if (rowCount == 0) {
+ return;
+ }
+
+ GridRange sequenceRange = (GridRange) range;
+
+ if (sequenceRange instanceof BufferedSequenceRange && ((BufferedSequenceRange)
sequenceRange).isReadOnly()) {
+
+ } else {
+ initStorage();
+ }
+
+ IterationBounds bounds = sequenceRange.getIterationBounds();
+
+ try {
+
+ bounds.iterate(new IterationBounds.RowVisitor() {
+
+ public void visit(int rowIndex) {
+ Integer rowKey = new Integer(rowIndex);
+ try {
+ visitor.process(context, rowKey, rowKey);
+ } catch (IOException e) {
+ throw new IOExceptionWrapper(e);
+ }
+ }
+
+ });
+ } catch (IOExceptionWrapper e) {
+ throw e.getWrappedException();
+ }
+
+ }
+
+}
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/snapshot/ObjectLocator.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/snapshot/ObjectLocator.java
(rev 0)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/snapshot/ObjectLocator.java 2007-05-28
18:11:25 UTC (rev 899)
@@ -0,0 +1,37 @@
+/*
+ * Copyright
+ * Copyright (c) Exadel,Inc. 2006
+ * All rights reserved.
+ *
+ * History
+ * $Source:
/cvs-master/intralinks-jsf-comps/components/data-view-grid/src/component/com/exadel/jsf/model/snapshot/ObjectLocator.java,v
$
+ * $Revision: 1.3 $
+ */
+
+package org.richfaces.model.snapshot;
+
+import java.io.Serializable;
+import java.util.Collection;
+
+/**
+ * Base interface for data locator object responsible
+ * for both getting Identifiers from objects and finding objects by their identifiers.
+ * <li>Identifiers must be {@link Serializable}</li>
+ * @author Maksim Kaszynski
+ */
+public interface ObjectLocator {
+
+ /**
+ * get identifier from specific object.
+ * @param object
+ * @return
+ */
+ public Serializable getObjectId(Object object);
+
+ /**
+ * find objects by their identifiers.
+ * @param ids
+ * @return
+ */
+ public Collection findObjects(Collection ids);
+}
Show replies by date