| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import com.opencsv.CSVReader; | |
| 4 | import com.opencsv.exceptions.CsvException; | |
| 5 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 7 | import edu.ucsb.cs156.frontiers.enums.OrgStatus; | |
| 8 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 9 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 10 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 11 | import edu.ucsb.cs156.frontiers.services.UpdateUserService; | |
| 12 | import io.swagger.v3.oas.annotations.Operation; | |
| 13 | import io.swagger.v3.oas.annotations.Parameter; | |
| 14 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 15 | import java.io.BufferedInputStream; | |
| 16 | import java.io.IOException; | |
| 17 | import java.io.InputStream; | |
| 18 | import java.io.InputStreamReader; | |
| 19 | import java.util.ArrayList; | |
| 20 | import java.util.List; | |
| 21 | import java.util.Map; | |
| 22 | import lombok.extern.slf4j.Slf4j; | |
| 23 | import org.springframework.beans.factory.annotation.Autowired; | |
| 24 | import org.springframework.dao.DataIntegrityViolationException; | |
| 25 | import org.springframework.http.HttpStatus; | |
| 26 | import org.springframework.http.ResponseEntity; | |
| 27 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 28 | import org.springframework.web.bind.annotation.PostMapping; | |
| 29 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 30 | import org.springframework.web.bind.annotation.RequestParam; | |
| 31 | import org.springframework.web.bind.annotation.RestController; | |
| 32 | import org.springframework.web.multipart.MultipartFile; | |
| 33 | import org.springframework.web.server.ResponseStatusException; | |
| 34 | ||
| 35 | @Tag(name = "CourseStaff") | |
| 36 | @RequestMapping("/api/coursestaff") | |
| 37 | @RestController("CourseStaffCSVController") | |
| 38 | @Slf4j | |
| 39 | public class CourseStaffCSVController extends ApiController { | |
| 40 | ||
| 41 | @Autowired private CourseStaffRepository courseStaffRepository; | |
| 42 | ||
| 43 | @Autowired private CourseRepository courseRepository; | |
| 44 | ||
| 45 | @Autowired private UpdateUserService updateUserService; | |
| 46 | ||
| 47 | public static final String STAFF_CSV_HEADERS = "firstName,lastName,email"; | |
| 48 | ||
| 49 | @Operation(summary = "Upload Course Staff from CSV") | |
| 50 | @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") | |
| 51 | @PostMapping( | |
| 52 | value = "/upload/csv", | |
| 53 | consumes = {"multipart/form-data"}) | |
| 54 | public ResponseEntity<Map<String, Object>> uploadStaffCSV( | |
| 55 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 56 | @Parameter(name = "file") @RequestParam("file") MultipartFile file) | |
| 57 | throws IOException, CsvException { | |
| 58 | ||
| 59 | Course course = | |
| 60 | courseRepository | |
| 61 | .findById(courseId) | |
| 62 |
1
1. lambda$uploadStaffCSV$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::lambda$uploadStaffCSV$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId.toString())); |
| 63 | ||
| 64 | int count = 0; | |
| 65 | List<String> duplicates = new ArrayList<>(); | |
| 66 | ||
| 67 | try (InputStream inputStream = new BufferedInputStream(file.getInputStream()); | |
| 68 | InputStreamReader reader = new InputStreamReader(inputStream); | |
| 69 | CSVReader csvReader = new CSVReader(reader)) { | |
| 70 | ||
| 71 | String[] headers = csvReader.readNext(); | |
| 72 |
1
1. uploadStaffCSV : negated conditional → KILLED |
if (headers == null) { |
| 73 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "CSV file is empty"); | |
| 74 | } | |
| 75 | ||
| 76 |
1
1. uploadStaffCSV : removed call to edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::validateHeaders → KILLED |
validateHeaders(headers); |
| 77 | ||
| 78 | String[] row; | |
| 79 |
1
1. uploadStaffCSV : negated conditional → KILLED |
while ((row = csvReader.readNext()) != null) { |
| 80 |
2
1. uploadStaffCSV : changed conditional boundary → KILLED 2. uploadStaffCSV : negated conditional → KILLED |
if (row.length < 3) { |
| 81 | throw new ResponseStatusException( | |
| 82 | HttpStatus.BAD_REQUEST, | |
| 83 | String.format( | |
| 84 | "CSV row does not have enough columns. Expected at least 3, got %d", row.length)); | |
| 85 | } | |
| 86 | ||
| 87 | String staffEmail = row[2].trim(); | |
| 88 |
1
1. uploadStaffCSV : negated conditional → KILLED |
if (courseStaffRepository.findByEmailAndCourse(staffEmail, course).isPresent()) { |
| 89 | duplicates.add(staffEmail); | |
| 90 | continue; | |
| 91 | } | |
| 92 | ||
| 93 | CourseStaff courseStaff = | |
| 94 | CourseStaff.builder() | |
| 95 | .firstName(row[0].trim()) | |
| 96 | .lastName(row[1].trim()) | |
| 97 | .email(staffEmail) | |
| 98 | .course(course) | |
| 99 | .build(); | |
| 100 | ||
| 101 |
1
1. uploadStaffCSV : negated conditional → KILLED |
if (course.getInstallationId() != null) { |
| 102 |
1
1. uploadStaffCSV : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED |
courseStaff.setOrgStatus(OrgStatus.JOINCOURSE); |
| 103 | } else { | |
| 104 |
1
1. uploadStaffCSV : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED |
courseStaff.setOrgStatus(OrgStatus.PENDING); |
| 105 | } | |
| 106 | ||
| 107 | CourseStaff savedCourseStaff; | |
| 108 | try { | |
| 109 | savedCourseStaff = courseStaffRepository.save(courseStaff); | |
| 110 | } catch (DataIntegrityViolationException exception) { | |
| 111 | // A concurrent upload may have saved this staff member after our check. | |
| 112 |
1
1. uploadStaffCSV : negated conditional → KILLED |
if (courseStaffRepository.findByEmailAndCourse(staffEmail, course).isPresent()) { |
| 113 | duplicates.add(staffEmail); | |
| 114 | continue; | |
| 115 | } | |
| 116 | throw exception; | |
| 117 | } | |
| 118 |
1
1. uploadStaffCSV : removed call to edu/ucsb/cs156/frontiers/services/UpdateUserService::attachUserToCourseStaff → KILLED |
updateUserService.attachUserToCourseStaff(savedCourseStaff); |
| 119 |
1
1. uploadStaffCSV : Changed increment from 1 to -1 → KILLED |
count++; |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 |
1
1. uploadStaffCSV : negated conditional → KILLED |
if (!duplicates.isEmpty()) { |
| 124 |
1
1. uploadStaffCSV : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::uploadStaffCSV → KILLED |
return ResponseEntity.status(HttpStatus.CONFLICT) |
| 125 | .body(Map.of("count", count, "duplicates", duplicates)); | |
| 126 | } | |
| 127 |
1
1. uploadStaffCSV : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseStaffCSVController::uploadStaffCSV → KILLED |
return ResponseEntity.ok(Map.of("count", count)); |
| 128 | } | |
| 129 | ||
| 130 | static void validateHeaders(String[] headers) { | |
| 131 | String[] expectedHeaders = STAFF_CSV_HEADERS.split(","); | |
| 132 |
2
1. validateHeaders : changed conditional boundary → KILLED 2. validateHeaders : negated conditional → KILLED |
if (headers.length < expectedHeaders.length) { |
| 133 | throw new ResponseStatusException( | |
| 134 | HttpStatus.BAD_REQUEST, | |
| 135 | String.format( | |
| 136 | "CSV headers do not match expected format. Expected: %s", STAFF_CSV_HEADERS)); | |
| 137 | } | |
| 138 |
2
1. validateHeaders : changed conditional boundary → KILLED 2. validateHeaders : negated conditional → KILLED |
for (int i = 0; i < expectedHeaders.length; i++) { |
| 139 |
1
1. validateHeaders : negated conditional → KILLED |
if (!expectedHeaders[i].trim().equalsIgnoreCase(headers[i].trim())) { |
| 140 | throw new ResponseStatusException( | |
| 141 | HttpStatus.BAD_REQUEST, | |
| 142 | String.format( | |
| 143 | "CSV headers do not match expected format. Expected: %s", STAFF_CSV_HEADERS)); | |
| 144 | } | |
| 145 | } | |
| 146 | } | |
| 147 | } | |
Mutations | ||
| 62 |
1.1 |
|
| 72 |
1.1 |
|
| 76 |
1.1 |
|
| 79 |
1.1 |
|
| 80 |
1.1 2.2 |
|
| 88 |
1.1 |
|
| 101 |
1.1 |
|
| 102 |
1.1 |
|
| 104 |
1.1 |
|
| 112 |
1.1 |
|
| 118 |
1.1 |
|
| 119 |
1.1 |
|
| 123 |
1.1 |
|
| 124 |
1.1 |
|
| 127 |
1.1 |
|
| 132 |
1.1 2.2 |
|
| 138 |
1.1 2.2 |
|
| 139 |
1.1 |