/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */
package org.bodington.spring.easyedit;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bodington.server.PermissionDeniedException;
import org.bodington.server.events.EasyEditEvent;
import org.bodington.server.realm.Permission;
import org.bodington.server.resources.Resource;
import org.bodington.spring.Utils;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

public class ChangesController extends AbstractCommandController
{

    private String viewName;
    private int pageSize;
    
    public ChangesController() {
        setCommandClass(ChangesCommand.class);
    }

    /**
     * @return the pageSize
     */
    public int getPageSize()
    {
        return pageSize;
    }

    /**
     * @param pageSize the pageSize to set
     */
    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }

    protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception
    {
        ChangesCommand changeCommand = (ChangesCommand)command; 
        Resource resource = Utils.getResource(request);
        
        if (!resource.checkPermission(Permission.EDIT))
            throw new PermissionDeniedException(Permission.EDIT);
        
        int start = changeCommand.getPage() * getPageSize();
        int size = getPageSize() + 1;
        
        List events = Collections.list(EasyEditEvent.findEvents(resource, start, size));
        
        ModelAndView modelView = new ModelAndView(viewName);
        modelView.addObject(getCommandName(), command);
        if (events.size() == size)
        {
            events.remove(size - 1);
            modelView.addObject("next", Boolean.TRUE);
        }
        else
        {
            modelView.addObject("next", Boolean.FALSE);
        }
        modelView.addObject("events", events);
        modelView.addObject("resource", resource);
        
        return modelView;
        
    }


    public static class ChangesCommand {
        int page;

        /**
         * @return the page
         */
        public int getPage()
        {
            return page;
        }

        /**
         * @param page the page to set
         */
        public void setPage(int page)
        {
            this.page = page;
        }
    }


    /**
     * @return the viewName
     */
    public String getViewName()
    {
        return viewName;
    }

    /**
     * @param viewName the viewName to set
     */
    public void setViewName(String viewName)
    {
        this.viewName = viewName;
    }
    
    
}
