package edu.northwestern.bioinformatics.studycalendar.web.template;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.mvc.Controller;
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.Map;
import java.util.HashMap;

import edu.northwestern.bioinformatics.studycalendar.domain.Study;
import edu.northwestern.bioinformatics.studycalendar.domain.StudySite;
import edu.northwestern.bioinformatics.studycalendar.domain.PlannedCalendar;
import edu.northwestern.bioinformatics.studycalendar.domain.Epoch;
import edu.northwestern.bioinformatics.studycalendar.dao.StudyDao;
import edu.northwestern.bioinformatics.studycalendar.dao.SiteDao;
import edu.northwestern.bioinformatics.studycalendar.dao.StudySiteDao;
import edu.northwestern.bioinformatics.studycalendar.web.ControllerTools;

/**
 * @author Rhett Sutphin
 */
public class NewStudyController implements Controller {
    private StudyDao studyDao;
    private SiteDao siteDao;
    private StudySiteDao studySiteDao;

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Study study = new Study();
        study.setName("New study");
        study.setPlannedCalendar(new PlannedCalendar());
        study.getPlannedCalendar().addEpoch(Epoch.create("New epoch"));

        studyDao.save(study);

        //// XXX: TEMPORARY until there's an interface for setting up sites & assigning studies to them
        StudySite ss = new StudySite();
        ss.setStudy(study);
        ss.setSite(siteDao.getDefaultSite());
        studySiteDao.save(ss);

        return ControllerTools.redirectToCalendarTemplate(study.getId());
    }

    ////// CONFIGURATION

    @Required
    public void setStudyDao(StudyDao studyDao) {
        this.studyDao = studyDao;
    }

    @Required
    public void setSiteDao(SiteDao siteDao) {
        this.siteDao = siteDao;
    }

    @Required
    public void setStudySiteDao(StudySiteDao studySiteDao) {
        this.studySiteDao = studySiteDao;
    }
}

@@ -1057,41 +1057,314 @@
 ls;%0A
-%0A/**%0A * @author Rhett Sutphin%0A */
+import edu.northwestern.bioinformatics.studycalendar.utils.accesscontrol.AccessControl;%0Aimport edu.northwestern.bioinformatics.studycalendar.utils.accesscontrol.StudyCalendarProtectionGroup;%0A%0A/**%0A * @author Rhett Sutphin%0A */%0A@AccessControl(protectionGroups = StudyCalendarProtectionGroup.STUDY_COORDINATOR)
 %0Apub

Add AccessControl annotation