/**
* 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.seam;
import java.io.Serializable;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jboss.seam.annotations.In;
import org.ajax4jsf.model.ExtendedDataModel;
/**
*
* @author Dave Oxley <dave@daveoxley.co.uk>
*/
public abstract class TablePageBean<T> implements Serializable {
@In
private HttpServletRequest httpRequest;
@In
private HttpServletResponse httpResponse;
private ExtendedDataModel<T> dataModel;
private final String tableStateCookieName = getClass().getName() + "TableState";
private String tableState = null;
public ExtendedDataModel<T> getDataModel() {
return dataModel;
}
public void setDataModel(ExtendedDataModel<T> dataModel) {
this.dataModel = dataModel;
}
public boolean isPaginated() {
return dataModel.getRowCount() > 20;
}
public String getTableState() {
if (tableState == null){
//try to get state from cookies
Cookie[] cookies = httpRequest.getCookies();
if (cookies != null){
for (Cookie c : cookies){
if (c.getName().equals(tableStateCookieName)){
tableState = c.getValue();
break;
}
}
}
}
return tableState;
}
public void setTableState(String tableState) {
this.tableState = tableState;
//save state in cookies
Cookie stateCookie = new Cookie(tableStateCookieName, this.tableState);
stateCookie.setMaxAge(30 * 24 * 60 * 60);
httpResponse.addCookie(stateCookie);
}
}