<commit_before>package edu.umdearborn.astronomyapp.controller;

import static edu.umdearborn.astronomyapp.util.constants.UrlConstants.REST_PATH_PREFIX;
import static edu.umdearborn.astronomyapp.util.constants.UrlConstants.STUDENT_PATH;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import edu.umdearborn.astronomyapp.entity.CourseUser;
import edu.umdearborn.astronomyapp.entity.ModuleGroup;
import edu.umdearborn.astronomyapp.service.AclService;
import edu.umdearborn.astronomyapp.service.GroupService;
import edu.umdearborn.astronomyapp.util.json.JsonDecorator;

@RestController
@RequestMapping(REST_PATH_PREFIX)
@SessionAttributes("courseUser")
public class ModuleGroupController {

  private static final Logger logger = LoggerFactory.getLogger(ModuleGroupController.class);

  private AclService   acl;
  private GroupService groupService;

  public ModuleGroupController(AclService acl, GroupService groupService) {
    this.acl = acl;
    this.groupService = groupService;
  }

  @RequestMapping(value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group",
      method = POST)
  public ModuleGroup createGroup(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId,
      @RequestParam(name = "courseUserId") String courseUserId, Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));

    return groupService.createGroup(courseUserId, moduleId);
  }

  @RequestMapping(value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group", method = GET)
  public JsonDecorator<ModuleGroup> getGroup(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId,
      @RequestParam(name = "courseUserId") String courseUserId, Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));

    ModuleGroup group = groupService.getGroup(courseUserId, moduleId);
    
    if (group != null) {
      JsonDecorator<ModuleGroup> json = new JsonDecorator<>();
      json.setPayload(group);
      json.addProperty("members", groupService.getUsersInGroup(group.getId()));
      
      logger.debug("Returning group and members");
      return json;
    }
    
    logger.debug("Not in group");
    
    return null;
    
  }

  @RequestMapping(value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group/{groupId}",
      method = POST)
  public List<CourseUser> joinGroup(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId, @PathVariable("groupId") String groupId,
      @RequestParam(name = "courseUserId") String courseUserId, @RequestBody CourseUser courseUser,
      Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));
    acl.enforceInGroup(courseUser.getId(), groupId);

    return groupService.joinGroup(courseUser.getId(), moduleId, groupId);
  }

  @RequestMapping(
      value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group/{groupId}/checkin",
      method = GET)
  public List<String> getCheckinStatus(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId, @PathVariable("groupId") String groupId,
      @RequestParam(name = "courseUserId") String courseUserId, HttpSession session,
      Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));
    acl.enforceGroupInCourse(groupId, courseId);
    acl.enforceInGroup(courseUserId, groupId);

    List<String> checkin = getCheckinSessionAttribute(session, groupId, courseUserId);

    return checkin;
  }

  @RequestMapping(
      value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group/{groupId}/checkin",
      method = POST)
  public List<String> getCheckin(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId, @PathVariable("groupId") String groupId,
      @RequestParam(name = "courseUserId") String courseUserId,
      @RequestBody Map<String, String> checkinUser, HttpSession session, Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));
    acl.enforceGroupInCourse(groupId, courseId);
    acl.enforceInGroup(courseUserId, groupId);

    List<String> checkin = getCheckinSessionAttribute(session, groupId, courseUserId);

    CourseUser user =
        groupService.checkin(checkinUser.get("email"), checkinUser.get("password"), groupId);

    if (user != null && !checkin.contains(user.getId())) {
      checkin.add(user.getId());
      session.setAttribute(groupId, checkin);
    }

    return checkin;
  }

  @RequestMapping(value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group/{groupId}",
      method = GET)
  public List<CourseUser> getGroupRoster(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId, @PathVariable("groupId") String groupId,
      @RequestParam(name = "courseUserId") String courseUserId, HttpSession session,
      Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));
    acl.enforceGroupInCourse(groupId, courseId);
    acl.enforceInGroup(courseUserId, groupId);

    return groupService.getUsersInGroup(groupId);
  }

  @RequestMapping(
      value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group/{groupId}/lock",
      method = GET)
  public boolean hasLock(@PathVariable("courseId") String courseId,<commit_msg>added property for group if they can edit module<commit_after>package edu.umdearborn.astronomyapp.controller;

import static edu.umdearborn.astronomyapp.util.constants.UrlConstants.REST_PATH_PREFIX;
import static edu.umdearborn.astronomyapp.util.constants.UrlConstants.STUDENT_PATH;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import edu.umdearborn.astronomyapp.entity.CourseUser;
import edu.umdearborn.astronomyapp.entity.ModuleGroup;
import edu.umdearborn.astronomyapp.service.AclService;
import edu.umdearborn.astronomyapp.service.GroupService;
import edu.umdearborn.astronomyapp.util.json.JsonDecorator;

@RestController
@RequestMapping(REST_PATH_PREFIX)
@SessionAttributes("courseUser")
public class ModuleGroupController {

  private static final Logger logger = LoggerFactory.getLogger(ModuleGroupController.class);

  private AclService   acl;
  private GroupService groupService;

  public ModuleGroupController(AclService acl, GroupService groupService) {
    this.acl = acl;
    this.groupService = groupService;
  }

  @RequestMapping(value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group",
      method = POST)
  public ModuleGroup createGroup(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId,
      @RequestParam(name = "courseUserId") String courseUserId, Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));

    return groupService.createGroup(courseUserId, moduleId);
  }

  @RequestMapping(value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group", method = GET)
  public JsonDecorator<ModuleGroup> getGroup(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId,
      @RequestParam(name = "courseUserId") String courseUserId, HttpSession session,
      Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));

    ModuleGroup group = groupService.getGroup(courseUserId, moduleId);

    if (group != null) {
      JsonDecorator<ModuleGroup> json = new JsonDecorator<>();
      json.setPayload(group);
      json.addProperty("members", groupService.getUsersInGroup(group.getId()));

      json.addProperty("isModuleEditable", groupService.hasLock(group.getId(),
          getCheckinSessionAttribute(session, group.getId(), courseUserId)));

      logger.debug("Returning group and members and if editable");
      return json;
    }

    logger.debug("Not in group");

    return null;

  }

  @RequestMapping(value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group/{groupId}",
      method = POST)
  public List<CourseUser> joinGroup(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId, @PathVariable("groupId") String groupId,
      @RequestParam(name = "courseUserId") String courseUserId, @RequestBody CourseUser courseUser,
      Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));
    acl.enforceInGroup(courseUser.getId(), groupId);

    return groupService.joinGroup(courseUser.getId(), moduleId, groupId);
  }

  @RequestMapping(
      value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group/{groupId}/checkin",
      method = GET)
  public JsonDecorator<List<String>> getCheckinStatus(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId, @PathVariable("groupId") String groupId,
      @RequestParam(name = "courseUserId") String courseUserId, HttpSession session,
      Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));
    acl.enforceGroupInCourse(groupId, courseId);
    acl.enforceInGroup(courseUserId, groupId);

    List<String> checkin = getCheckinSessionAttribute(session, groupId, courseUserId);
    
    JsonDecorator<List<String>> json = new JsonDecorator<>();
    json.setPayload(checkin);
    json.addProperty("isModuleEditable", groupService.hasLock(groupId,
        getCheckinSessionAttribute(session, groupId, courseUserId)));

    return json;
  }

  @RequestMapping(
      value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group/{groupId}/checkin",
      method = POST)
  public JsonDecorator<List<String>> checkin(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId, @PathVariable("groupId") String groupId,
      @RequestParam(name = "courseUserId") String courseUserId,
      @RequestBody Map<String, String> checkinUser, HttpSession session, Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));
    acl.enforceGroupInCourse(groupId, courseId);
    acl.enforceInGroup(courseUserId, groupId);

    List<String> checkin = getCheckinSessionAttribute(session, groupId, courseUserId);

    CourseUser user =
        groupService.checkin(checkinUser.get("email"), checkinUser.get("password"), groupId);

    if (user != null && !checkin.contains(user.getId())) {
      checkin.add(user.getId());
      session.setAttribute(groupId, checkin);
    }
    
    JsonDecorator<List<String>> json = new JsonDecorator<>();
    json.setPayload(checkin);
    json.addProperty("isModuleEditable", groupService.hasLock(groupId,
        getCheckinSessionAttribute(session, groupId, courseUserId)));

    return json;
  }

  @RequestMapping(value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group/{groupId}",
      method = GET)
  public List<CourseUser> getGroupRoster(@PathVariable("courseId") String courseId,
      @PathVariable("moduleId") String moduleId, @PathVariable("groupId") String groupId,
      @RequestParam(name = "courseUserId") String courseUserId, HttpSession session,
      Principal principal) {

    acl.enforceInCourse(principal.getName(), courseId, courseUserId);
    acl.enforceIsCourseRole(principal.getName(), courseId,
        Arrays.asList(CourseUser.CourseRole.STUDENT));
    acl.enforceGroupInCourse(groupId, courseId);
    acl.enforceInGroup(courseUserId, groupId);

    return groupService.getUsersInGroup(groupId);
  }

  @RequestMapping(
      value = STUDENT_PATH + "/course/{courseId}/module/{moduleId}/group/{groupId}/lock",
      method = GET)
  public boolean hasLock(@PathVariable("courseId") String courseId,
{'fraction_non_alphanumeric': 0.11620283183371696, 'fraction_numerical': 0.0002789983957592244, 'mean_word_length': 5.169535283993115, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '262113', 'n_tokens_mistral': 4271, 'n_tokens_neox': 4147, 'n_words': 719}