/* ======================================================================
   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.homecontainer;

import java.util.HashMap;
import java.util.Map;

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

import org.bodington.server.BuildingServerException;
import org.bodington.server.BuildingSessionManagerImpl;
import org.bodington.server.HomeContainerSession;
import org.bodington.server.HomeSession;
import org.bodington.server.PermissionDeniedException;
import org.bodington.server.realm.User;
import org.bodington.spring.Utils;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class Signup extends SimpleFormController implements Validator
{
    
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
        throws Exception
    {
        HomeContainerSession session = (HomeContainerSession)Utils
            .getSession(request);
        User user = Utils.getUser(request);
                
        if (!session.hasHome(user))
        {
            try
            {
                session.createHome(user);
                Utils.saveMessage(request,
                    getMessageSourceAccessor().getMessage("homecontainer.signup.sucessful"));
                
               HomeSession homeSession = (HomeSession)BuildingSessionManagerImpl.getSession(session.findHome(user));
               homeSession.makePublic();
            }
            catch (PermissionDeniedException pde)
            {
                Utils.saveMessage(request,
                    getMessageSourceAccessor().getMessage("permission.required",
                        new Object[]{pde.getPermission()}));
            }
        }
        else
        {
            Utils.saveMessage(request, getMessageSourceAccessor().getMessage(
            "homecontainer.signup.exists"));
        }
        
        ModelAndView modelAndView =  onSubmit(command, errors);
        modelAndView.addObject("resource", session.findHome(user));
        return modelAndView;
    }
        
    protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws BuildingServerException
    {
        Map map = new HashMap();
        HomeContainerSession session = (HomeContainerSession)Utils.getSession(request);
        User user = Utils.getUser(request);
        String terms = session.getTerms(user);
        map.put("terms", terms);
        return map;
    }

    public boolean supports(Class clazz)
    {
        return SignupForm.class.isAssignableFrom(clazz);
    }

    public void validate(Object obj, Errors errors)
    {
        SignupForm form = (SignupForm)obj;
        if (!form.isAccepted())
        {
            errors.rejectValue("accepted", "homecontainer.accepted");
        }
    }

}
