Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.vaadin.addons.lazyquerycontainer;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;

import java.io.Serializable;
import java.util.AbstractList;
Expand Down Expand Up @@ -88,7 +89,7 @@ public T get(final int index) {
if (index < 0 || index >= lazyQueryView.size()) {
throw new IndexOutOfBoundsException();
}
final T itemId = (T) lazyQueryView.getItem(index).getItemProperty(idPropertyId).getValue();
final T itemId = getItemId(lazyQueryView.getItem(index));
// Do not put added item ids to id index map and make sure that
// existing item indexes start from 0 i.e. ignore added items as they
// are compensated for in indexOf method.
Expand All @@ -99,6 +100,17 @@ public T get(final int index) {
return itemId;
}

private T getItemId(Item item) {
final T itemId;
if(item instanceof BeanItem
&& LazyQueryDefinition.ID_PROPERTY_ID_BEAN_SELF.equals(idPropertyId)) {
itemId = (T) ((BeanItem)item).getBean();
}else {
itemId = (T) item.getItemProperty(idPropertyId).getValue();
}
return itemId;
}

/**
* {@inheritDoc}
*/
Expand All @@ -116,7 +128,7 @@ public int indexOf(final Object o) {
// Brute force added items first. There should only be a few.
final List<Item> addedItems = lazyQueryView.getAddedItems();
for (int i = 0; i < addedItems.size(); i++) {
if (o.equals(addedItems.get(i).getItemProperty(idPropertyId).getValue())) {
if (o.equals(getItemId(addedItems.get(i)))) {
return i;
}
}
Expand All @@ -126,7 +138,7 @@ public int indexOf(final Object o) {
}
// Switching to brute forcing.
for (int i = addedItems.size(); i < lazyQueryView.size(); i++) {
if (o.equals(lazyQueryView.getItem(i).getItemProperty(idPropertyId).getValue())) {
if (o.equals(getItemId(lazyQueryView.getItem(i)))) {
return i;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class LazyQueryDefinition implements QueryDefinition, Serializable {
* Java serialization version UID.
*/
private static final long serialVersionUID = 1L;
/**
* The ID of the ID property if the items of the container are BeanItems
*/
public static final String ID_PROPERTY_ID_BEAN_SELF = "org.vaadin.addons.lazyquerycontainer.LazyQueryDefinition.ID_PROPERTY_ID_BEAN_SELF";
/**
* Lust of property IDs included in this QueryDefinition.
*/
Expand Down