/**
* C-Nery - A home automation web application for C-Bus.
* Copyright (C) 2008,2009,2012 Dave Oxley <dave@daveoxley.co.uk>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.daveoxley.cnery.factories;
import com.workplacesystems.queuj.Process;
import com.workplacesystems.queuj.process.ProcessImplServer;
import com.workplacesystems.queuj.process.QueujFactory;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.FacesContext;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Transactional;
import org.ajax4jsf.model.DataVisitor;
import org.ajax4jsf.model.ExtendedDataModel;
import org.ajax4jsf.model.Range;
import org.ajax4jsf.model.SequenceRange;
/**
*
* @author dave
*/
@Name("processesFactory")
public class ProcessesFactory implements Serializable {
private ProcessImplServer<Integer> processImplServer = (ProcessImplServer<Integer>)QueujFactory.getProcessServer((String)null, null);
@Factory(value="processesDataModel",autoCreate=true,scope=ScopeType.EVENT)
@Transactional
public ExtendedDataModel<Process> updateProcesses() {
return new ExtendedDataModel<Process>() {
private Integer current_key = null;
private int firstRow = 0;
private List<Process> cache = new ArrayList<Process>();
@Override
public void setRowKey(Object o) {
this.current_key = (Integer)o;
}
@Override
public Object getRowKey() {
return current_key;
}
@Override
public void walk(FacesContext fc, DataVisitor dv, Range range, Object argument) {
firstRow = ((SequenceRange)range).getFirstRow();
int numberOfRows = ((SequenceRange)range).getRows();
int lastRow = numberOfRows > 0 ? firstRow + numberOfRows : processImplServer.size();
if (lastRow > processImplServer.size())
lastRow = processImplServer.size();
cache.clear();
for (Process<Integer> v : processImplServer.subList(firstRow, lastRow)) {
Integer key = v.getProcessKey();
cache.add(v);
dv.process(fc, key, argument);
}
}
@Override
public boolean isRowAvailable() {
return current_key != null;
}
@Override
public int getRowCount() {
return processImplServer.size();
}
@Override
public Process getRowData() {
if (current_key == null)
return null;
return new Process<Integer>(processImplServer.get(current_key));
}
@Override
public int getRowIndex() {
Process<Integer> value = getRowData();
if (value == null)
return -1;
return cache.indexOf(value) + firstRow;
}
@Override
public void setRowIndex(int rowIndex) {
Process<Integer> value = cache.get(rowIndex - firstRow);
if (value != null)
this.current_key = value.getProcessKey();
}
@Override
public Object getWrappedData() {
throw new UnsupportedOperationException();
}
@Override
public void setWrappedData(Object data) {
throw new UnsupportedOperationException();
}
};
}
}