All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.adminfaces.persistence.util.AdminDataModel Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package com.github.adminfaces.persistence.util;

import com.github.adminfaces.persistence.model.AdminSort;
import com.github.adminfaces.persistence.model.Filter;
import com.github.adminfaces.persistence.model.PersistenceEntity;
import com.github.adminfaces.persistence.service.CrudService;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import java.util.List;
import java.util.Map;

public class AdminDataModel extends LazyDataModel {

    private CrudService crudService;
    private Filter filter;
    private boolean keepFiltersInSession;


    public AdminDataModel(CrudService crudService, Filter filter) {
        this(crudService, filter, true);
    }

    public AdminDataModel(CrudService crudService, Filter filter, boolean keepFiltersInSession) {
        this.crudService = crudService;
        this.filter = filter;
        this.keepFiltersInSession = keepFiltersInSession;
    }

    @Override
    public List load(int first, int pageSize, String sortField, SortOrder sortOrder,
                        Map filters) {
        AdminSort order = null;
        if (sortOrder != null) {
            order = sortOrder.equals(SortOrder.ASCENDING) ? AdminSort.ASCENDING
                    : sortOrder.equals(SortOrder.DESCENDING) ? AdminSort.DESCENDING
                    : AdminSort.UNSORTED;
        }

        if (filters == null || filters.isEmpty() && keepFiltersInSession) {
            filters = filter.getParams();
        }

        filter.setFirst(first).setPageSize(pageSize)
                .setSortField(sortField).setAdminSort(order)
                .setParams(filters);
        List list = crudService.paginate(filter);
        setRowCount(crudService.count(filter).intValue());
        return list;
    }

    @Override
    public int getRowCount() {
        return super.getRowCount();
    }

    @Override
    public T getRowData(String key) {
        List list = (List) this.getWrappedData();
        if(list != null && !list.isEmpty()) {
            for (T t : list) {
                if (key.equals(t.getId().toString())) {
                    return t;
                }
            }
        }
        return null;
    }

    public void setFilter(Filter filter) {
        this.filter = filter;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy