/*
 * ProActive Parallel Suite(TM):
 * The Open Source library for parallel and distributed
 * Workflows & Scheduling, Orchestration, Cloud Automation
 * and Big Data Analysis on Enterprise Grids & Clouds.
 *
 * Copyright (c) 2007 - 2017 ActiveEon
 * Contact: contact@activeeon.com
 *
 * This library 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: version 3 of
 * the License.
 *
 * 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/>.
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 */
package org.ow2.proactive_grid_cloud_portal.scheduler.client.controller;

import org.ow2.proactive_grid_cloud_portal.scheduler.client.model.JobsPaginationModel;
import org.ow2.proactive_grid_cloud_portal.scheduler.client.view.JobsPaginationView;

import com.smartgwt.client.widgets.layout.Layout;


/**
 * Controller for the jobs pagination logic.
 * @author the activeeon team.
 *
 */
public class JobsPaginationController extends PaginationController<JobsPaginationModel> {

    private JobsController itemsController;

    private JobsPaginationView view;

    public JobsPaginationController(JobsController jobsController) {
        super(new JobsPaginationModel());
        this.itemsController = jobsController;
        jobsController.getModel().setPaginationModel(this.model);
    }

    public void fetch(boolean silentFetch) {
        this.itemsController.fetchJobs(silentFetch);
    }

    public Layout buildView() {
        this.view = new JobsPaginationView(itemsController);
        return this.view.build();
    }

    @Override
    public void firstPage() {
        model.setFetchData(null, null, false);
        this.fetch(false);
    }

    @Override
    public void lastPage() {
        model.setFetchData(null, null, true);
        this.fetch(false);
    }

    @Override
    public void nextPage() {
        consoleLog("Total Items: " + model.getTotalItems());
        consoleLog("Page Size: " + model.getPageSize());
        model.setPage(model.getPage() + 1);
        model.setFetchData(null, model.getCurrentStartCursor(), false);
        this.fetch(false);
    }

    native void consoleLog(String message) /*-{
                                           console.log( "me:" + message );
                                           }-*/;

    @Override
    public void previousPage() {
        int curPage = model.getPage();
        consoleLog("currentPage: " + curPage);
        if (curPage == 0)
            return;
        model.setPage(Math.max(0, curPage - 1));
        model.setFetchData(model.getCurrentEndCursor(), null, true);
        this.fetch(false);
    }

    @Override
    public boolean hasPrevious() {
        //GraphQL fetches the jobs starting from the oldest
        return model.hasNextPage();
    }

    @Override
    public boolean hasNext() {
        //GraphQL fetches the jobs starting from the oldest
        return model.hasPreviousPage();
    }

    public JobsPaginationModel getModel() {
        return model;
    }
}

@@ -2341,126 +2341,8 @@
 ) %7B%0A
-        consoleLog(%22Total Items: %22 + model.getTotalItems());%0A        consoleLog(%22Page Size: %22 + model.getPageSize());%0A
     
@@ -2491,181 +2491,8 @@
  %7D%0A%0A
-    native void consoleLog(String message) /*-%7B%0A                                           console.log( %22me:%22 + message );%0A                                           %7D-*/;%0A%0A
     
@@ -2577,55 +2577,8 @@
 ();%0A
-        consoleLog(%22currentPage: %22 + curPage);%0A
     

clean code