<commit_before>package edu.northwestern.bioinformatics.studycalendar.web.activity;

import edu.northwestern.bioinformatics.studycalendar.web.PscAbstractController;
import edu.northwestern.bioinformatics.studycalendar.dao.ActivityTypeDao;
import edu.northwestern.bioinformatics.studycalendar.dao.ActivityDao;
import edu.northwestern.bioinformatics.studycalendar.utils.breadcrumbs.DefaultCrumb;
import edu.northwestern.bioinformatics.studycalendar.utils.breadcrumbs.BreadcrumbContext;
import edu.northwestern.bioinformatics.studycalendar.domain.ActivityType;
import edu.northwestern.bioinformatics.studycalendar.domain.Activity;
import edu.northwestern.bioinformatics.studycalendar.service.ActivityTypeService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.ServletRequestUtils;
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 java.util.List;

/**
 * @author <NAME>
 */
public class AddEditActivityTypeController extends PscAbstractController {
    private ActivityTypeDao activityTypeDao;
    private ActivityDao activityDao;
    private ActivityTypeService activityTypeService;

    public AddEditActivityTypeController() {
        setCrumb(new DefaultCrumb("Activity types"));
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        if ("POST".equals(request.getMethod())) {
            String activityTypeName = ServletRequestUtils.getRequiredStringParameter(request, "activityTypeName");
            String action = ServletRequestUtils.getRequiredStringParameter(request, "action");

            if (action.toLowerCase().equals("save")){
                Integer activityTypeId = ServletRequestUtils.getRequiredIntParameter(request, "activityTypeId");
                ActivityType at = activityTypeDao.getById(activityTypeId);
                at.setName(activityTypeName);
                activityTypeDao.save(at);
            } else if (action.toLowerCase().equals("add")) {
                ActivityType at = new ActivityType(activityTypeName);
                if (activityTypeDao.getByName(activityTypeName) == null) {
                    activityTypeDao.save(at);
                }
            } else if (action.toLowerCase().equals("delete")) {<commit_msg>Add access control to the AddEditActivityTypeController. Only Study Admin able to access Activity Tab. Related to #550, r3291.<commit_after>package edu.northwestern.bioinformatics.studycalendar.web.activity;

import edu.northwestern.bioinformatics.studycalendar.web.PscAbstractController;
import edu.northwestern.bioinformatics.studycalendar.dao.ActivityTypeDao;
import edu.northwestern.bioinformatics.studycalendar.dao.ActivityDao;
import edu.northwestern.bioinformatics.studycalendar.utils.breadcrumbs.DefaultCrumb;
import edu.northwestern.bioinformatics.studycalendar.utils.breadcrumbs.BreadcrumbContext;
import edu.northwestern.bioinformatics.studycalendar.utils.accesscontrol.AccessControl;
import edu.northwestern.bioinformatics.studycalendar.domain.ActivityType;
import edu.northwestern.bioinformatics.studycalendar.domain.Activity;
import edu.northwestern.bioinformatics.studycalendar.domain.Role;
import edu.northwestern.bioinformatics.studycalendar.service.ActivityTypeService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.ServletRequestUtils;
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 java.util.List;

/**
 * @author <NAME>
 */
@AccessControl(roles = Role.STUDY_ADMIN)
public class AddEditActivityTypeController extends PscAbstractController {
    private ActivityTypeDao activityTypeDao;
    private ActivityDao activityDao;
    private ActivityTypeService activityTypeService;

    public AddEditActivityTypeController() {
        setCrumb(new DefaultCrumb("Activity types"));
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        if ("POST".equals(request.getMethod())) {
            String activityTypeName = ServletRequestUtils.getRequiredStringParameter(request, "activityTypeName");
            String action = ServletRequestUtils.getRequiredStringParameter(request, "action");

            if (action.toLowerCase().equals("save")){
                Integer activityTypeId = ServletRequestUtils.getRequiredIntParameter(request, "activityTypeId");
                ActivityType at = activityTypeDao.getById(activityTypeId);
                at.setName(activityTypeName);
                activityTypeDao.save(at);
            } else if (action.toLowerCase().equals("add")) {
                ActivityType at = new ActivityType(activityTypeName);
                if (activityTypeDao.getByName(activityTypeName) == null) {
                    activityTypeDao.save(at);
                }
            } else if (action.toLowerCase().equals("delete")) {
{'fraction_non_alphanumeric': 0.09034792368125702, 'fraction_numerical': 0.001309390198279087, 'mean_word_length': 4.546680497925311, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7595638', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1356, 'n_words': 231}