Multiple selection with ExtendedDataTable and EntityQuery
rlopez.rlopez.cnc.una.py Dec 21, 2010 6:02 AMHi.
I'm trying to use extendedDataTable instead of dataTable on an application generated by seam-gen. I followed the example in RichFaces Live Demo, but I can´t make the multiple selection work.
This is what I´m trying to do:
public interface BaseEntity {
public Object getKey();
}
public class ExtendedEntityQuery<T extends BaseEntity> extends EntityQuery<T> {
private static final long serialVersionUID = 1L;
private String sortMode = "multi";
private String selectionMode = "multi";
private Object tableState;
private Selection selection = new SimpleSelection();
private ExtendedTableDataModel<T> dataModel;
private List<T> selectedList = new ArrayList<T>();
public String getSortMode() {
return sortMode;
}
public void setSortMode(String sortMode) {
this.sortMode = sortMode;
}
public String getSelectionMode() {
return selectionMode;
}
public void setSelectionMode(String selectionMode) {
this.selectionMode = selectionMode;
}
public ExtendedEntityQuery() {
}
public void takeSelection(){
selectedList.clear();
Iterator<Object> iterator = getSelection().getKeys();
while (iterator.hasNext()) {
Object key = iterator.next();
selectedList.add(getDataModel().getObjectByKey(key));
}
}
public ExtendedTableDataModel<T> getDataModel() {
if (dataModel == null) {
dataModel = new ExtendedTableDataModel<T>(new DataProvider<T>(){
private static final long serialVersionUID = 1L;
public T getItemByKey(Object key) {
for(T c : getResultList()){
if (key.equals(getKey(c))){
return c;
}
}
return null;
}
public List<T> getItemsByRange(int firstRow, int endRow) {
return getResultList().subList(firstRow, endRow);
}
public Object getKey(T item) {
return item.getKey();
}
public int getRowCount() {
return getResultList().size();
}
});
}
return dataModel;
}
public Object getTableState() {
return tableState;
}
public void setTableState(Object tableState) {
this.tableState = tableState;
}
public Selection getSelection() {
return selection;
}
public void setSelection(Selection selection) {
this.selection = selection;
}
public List<T> getSelectedList() {
return selectedList;
}
public void setSelectedList(List<T> selectedList) {
this.selectedList = selectedList;
}
}
As you can see is just a copy-paste of the ExtendeTableBean.java (from the RF example) but using generics.
The code generated by seam is:
@Name("departamentoList")
@Scope(ScopeType.PAGE)
public class DepartamentoList extends ExtendedEntityQuery<Departamento> {
private static final long serialVersionUID = -3548296613352342917L;
private static final String EJBQL = "select departamento from Departamento departamento";
private static final String[] RESTRICTIONS = {"blah, blah"};
private Departamento departamento = new Departamento();
public DepartamentoList() {
setEjbql(EJBQL);
setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
setMaxResults(20);
setOrderColumn("codigo");
}
public Departamento getDepartamento() {
return departamento;
}
}
I changed the scope to PAGE and extended from ExtendedEntityQuery.
My view is:
<h:form>
<rich:extendedDataTable width="786px" height="200px"
value="#{departamentoList.dataModel}"
var="_departamento"
sortMode="#{departamentoList.sortMode}"
selectionMode="#{departamentoList.selectionMode}"
tableState="#{departamentoList.tableState}"
selection="#{departamentoList.selection}"
enableContextMenu="false"
rendered="#{not empty departamentoList.resultList}"
noDataLabel="No existen datos">
<rich:column sortable="true" width="70px"
sortBy="#{_departamento.codigo}"
filterBy="#{_departamento.codigo}"
filterEvent="onkeyup"
label="Código">
<f:facet name="header">Código</f:facet>
<h:outputText value="#{_departamento.codigo}"/>
</rich:column>
<rich:column sortable="true" width="700px"
sortBy="#{_departamento.descripcion}"
filterBy="#{_departamento.descripcion}"
filterEvent="onkeyup"
label="Descripción">
<f:facet name="header">Descripción</f:facet>
<h:outputText value="#{_departamento.descripcion}"/>
</rich:column>
<a:support reRender="selectiontable"
action="#{departamentoList.takeSelection}"
event="onselectionchange" />
</rich:extendedDataTable>
<s:div styleClass="actionButtons" rendered="#{empty from}">
<s:button view="/DepartamentoEdit.xhtml"
id="create" propagation="none" value="Nuevo Departamento">
<f:param name="departamentoId"/>
</s:button>
</s:div>
<rich:dataTable value="#{departamentoList.selectedList}"
var="sel" id="selectiontable">
<rich:column>
<h:outputText value="#{sel.codigo}" />
</rich:column>
<rich:column>
<h:outputText value="#{sel.descripcion}" />
</rich:column>
</rich:dataTable>
</h:form>
SimpleSelection.getKeys() always returns the index of the selected rows instead of the keys of the selected entities. I don´t know how to make it work, if anyone have an example of how to do this it would be very helpfull for me.
Thanks in advance