package edu.northwestern.bioinformatics.studycalendar.web;

import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.validation.BindException;
import org.springframework.beans.factory.annotation.Required;

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

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

import edu.northwestern.bioinformatics.studycalendar.domain.Role;
import edu.northwestern.bioinformatics.studycalendar.domain.Subject;
import edu.northwestern.bioinformatics.studycalendar.dao.SubjectDao;
import edu.northwestern.bioinformatics.studycalendar.utils.accesscontrol.AccessControl;

/**
 * @author Padmaja Vedula
 */
@AccessControl(roles = Role.SUBJECT_COORDINATOR)
public class NewSubjectController extends PscSimpleFormController {
    private SubjectDao subjectDao;

    public NewSubjectController() {
        setCommandClass(NewSubjectCommand.class);
        setFormView("createSubject");
        setSuccessView("assignSubject");
    }

    protected void initBinder(HttpServletRequest request,
        ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(Date.class, getControllerTools().getDateEditor(true));
        super.initBinder(request, binder);
    }
    
    protected Map<String, Object> referenceData(HttpServletRequest httpServletRequest) throws Exception {
        Map<String, Object> refdata = new HashMap<String, Object>();
        //can be probably loaded from a properties file ??
        Map<String, String> genders = new HashMap<String, String>();
        genders.put("Female", "Female");
        genders.put("Male", "Male");
        refdata.put("genders", genders);
        refdata.put("action", "New");
        refdata.put("studyId", ServletRequestUtils.getIntParameter(httpServletRequest, "id"));
        return refdata;
    }

    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object oCommand, BindException errors) throws Exception {
        NewSubjectCommand subjectCommand = (NewSubjectCommand) oCommand;
        Subject subject = subjectCommand.createSubject();
        subjectDao.save(subject);

        Map<String, Object> model = errors.getModel();
        model.put("subject", subject);
        return new ModelAndView(new RedirectView(getSuccessView()), "id", ServletRequestUtils.getIntParameter(request, "id"));
        //return new ModelAndView(new RedirectView(getSuccessView()), "newSubject", subject);
    }

    ////// CONFIGURATION

    @Required
    public void setSubjectDao(SubjectDao subjectDao) {
        this.subjectDao = subjectDao;
    }
}

@@ -916,16 +916,60 @@
 a%0D%0A */%0D%0A
+// TODO: this class needs to be cleaned up%0D%0A
 @AccessC
@@ -2497,94 +2497,86 @@
     
-Map%3CString, Object%3E model = errors.getModel();%0D%0A        model.put(%22subject%22, subject);
+// TODO: this is bad -- the redirect view should be full-path context-relative
 %0D%0A  
@@ -2642,18 +2642,21 @@
 ew()), %22
-id
+study
 %22, Servl

Update redirect back to assignSubject for new param names