| 1 | package edu.ucsb.cs156.frontiers.services; | |
| 2 | ||
| 3 | import com.opencsv.bean.HeaderColumnNameMappingStrategy; | |
| 4 | import com.opencsv.bean.StatefulBeanToCsv; | |
| 5 | import com.opencsv.bean.StatefulBeanToCsvBuilder; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 7 | import edu.ucsb.cs156.frontiers.models.CourseStaffDTO; | |
| 8 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 9 | import java.io.IOException; | |
| 10 | import java.io.Writer; | |
| 11 | import java.util.Comparator; | |
| 12 | import java.util.List; | |
| 13 | import java.util.Locale; | |
| 14 | import java.util.Map; | |
| 15 | import java.util.stream.Collectors; | |
| 16 | import java.util.stream.StreamSupport; | |
| 17 | import org.springframework.beans.factory.annotation.Autowired; | |
| 18 | import org.springframework.stereotype.Service; | |
| 19 | ||
| 20 | @Service | |
| 21 | public class CourseStaffDTOService { | |
| 22 | ||
| 23 | @Autowired private CourseStaffRepository courseStaffRepository; | |
| 24 | ||
| 25 | private static final List<String> CSV_COLUMN_ORDER = | |
| 26 | List.of( | |
| 27 | "id", | |
| 28 | "courseId", | |
| 29 | "userId", | |
| 30 | "firstName", | |
| 31 | "lastName", | |
| 32 | "email", | |
| 33 | "orgStatus", | |
| 34 | "githubId", | |
| 35 | "githubLogin", | |
| 36 | "role"); | |
| 37 | ||
| 38 | /** | |
| 39 | * This method gets a list of CourseStaffDTOs based on the courseId. | |
| 40 | * | |
| 41 | * @param courseId id of the course | |
| 42 | * @return a list of CourseStaffDTOs | |
| 43 | */ | |
| 44 | public List<CourseStaffDTO> getCourseStaffDTOs(Long courseId) { | |
| 45 | Iterable<CourseStaff> matchedStaff = courseStaffRepository.findByCourseId(courseId); | |
| 46 | ||
| 47 |
1
1. getCourseStaffDTOs : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/services/CourseStaffDTOService::getCourseStaffDTOs → KILLED |
return StreamSupport.stream(matchedStaff.spliterator(), false) |
| 48 | .map(CourseStaffDTO::new) | |
| 49 | .collect(Collectors.toList()); | |
| 50 | } | |
| 51 | ||
| 52 | public StatefulBeanToCsv<CourseStaffDTO> getStatefulBeanToCSV(Writer writer) throws IOException { | |
| 53 | HeaderColumnNameMappingStrategy<CourseStaffDTO> strategy = | |
| 54 | new HeaderColumnNameMappingStrategy<>(); | |
| 55 |
1
1. getStatefulBeanToCSV : removed call to com/opencsv/bean/HeaderColumnNameMappingStrategy::setType → KILLED |
strategy.setType(CourseStaffDTO.class); |
| 56 | ||
| 57 | Map<String, Integer> positionByColumn = | |
| 58 | StreamSupport.stream(CSV_COLUMN_ORDER.spliterator(), false) | |
| 59 | .collect( | |
| 60 | Collectors.toMap( | |
| 61 |
1
1. lambda$getStatefulBeanToCSV$0 : replaced return value with "" for edu/ucsb/cs156/frontiers/services/CourseStaffDTOService::lambda$getStatefulBeanToCSV$0 → KILLED |
column -> column.toUpperCase(Locale.US), CSV_COLUMN_ORDER::indexOf)); |
| 62 | ||
| 63 | Comparator<String> headerOrder = | |
| 64 | Comparator.comparingInt( | |
| 65 | column -> | |
| 66 |
1
1. lambda$getStatefulBeanToCSV$1 : replaced int return with 0 for edu/ucsb/cs156/frontiers/services/CourseStaffDTOService::lambda$getStatefulBeanToCSV$1 → KILLED |
positionByColumn.getOrDefault(column.toUpperCase(Locale.US), Integer.MAX_VALUE)); |
| 67 |
1
1. getStatefulBeanToCSV : removed call to com/opencsv/bean/HeaderColumnNameMappingStrategy::setColumnOrderOnWrite → KILLED |
strategy.setColumnOrderOnWrite(headerOrder); |
| 68 | ||
| 69 |
1
1. getStatefulBeanToCSV : replaced return value with null for edu/ucsb/cs156/frontiers/services/CourseStaffDTOService::getStatefulBeanToCSV → KILLED |
return new StatefulBeanToCsvBuilder<CourseStaffDTO>(writer) |
| 70 | .withMappingStrategy(strategy) | |
| 71 | .build(); | |
| 72 | } | |
| 73 | } | |
Mutations | ||
| 47 |
1.1 |
|
| 55 |
1.1 |
|
| 61 |
1.1 |
|
| 66 |
1.1 |
|
| 67 |
1.1 |
|
| 69 |
1.1 |