Package jease.cms.web.user

Source Code of jease.cms.web.user.TableModel

/*
    Copyright (C) 2011 maik.jablonski@jease.org

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package jease.cms.web.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import jease.cms.domain.User;
import jease.cms.service.Users;
import jease.cms.web.servlet.JeaseSessionListener;
import jfix.util.I18N;
import jfix.zk.ObjectTableModel;
import jfix.zk.Sessions;

public class TableModel extends ObjectTableModel<User> {

  public User newObject() {
    if (Sessions.get(User.class).isAdministrator()) {
      return new User();
    } else {
      return null;
    }
  }

  public String[] getColumns() {
    return new String[] { I18N.get("Username"), I18N.get("Name"), I18N.get("Role"),
        I18N.get("Session"), I18N.get("Roots") };
  }

  public int[] getProportions() {
    return new int[] { 2, 3, 2, 2, 6 };
  }

  public List<User> getList() {
    return Users.queryModifiableByUser(Sessions.get(User.class));
  }

  public Object getValue(User user, int column) {
    switch (column) {
    case 0:
      return user.getLogin();
    case 1:
      return user.getName();
    case 2:
      return user.getRole();
    case 3:
      long lastAccessedTime = getLastAccessedTime(user);
      return lastAccessedTime != 0 ? new Date(lastAccessedTime) : "";
    case 4:
      return user.getRoots();
    }
    return "";
  }

  private long getLastAccessedTime(User user) {
    long lastAccessedTime = 0;
    for (HttpSession session : JeaseSessionListener.getSessions()) {
      if (user.equals(session.getAttribute(user.getClass().toString()))
          && lastAccessedTime < session.getLastAccessedTime()) {
        lastAccessedTime = session.getLastAccessedTime();
      }
    }
    return lastAccessedTime;
  }
}
TOP

Related Classes of jease.cms.web.user.TableModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.