-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1c2df0
commit 12e850e
Showing
33 changed files
with
730 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...in/java/amu/zhcet/core/admin/dean/registration/course/floated/FloatCoursesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package amu.zhcet.core.admin.dean.registration.course.floated; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
@Controller | ||
@RequestMapping("/admin/dean/float") | ||
public class FloatCoursesController { | ||
|
||
@GetMapping | ||
public String floatCourse(Model model) { | ||
model.addAttribute("page_title", "Float Courses"); | ||
model.addAttribute("page_subtitle", "Float courses using CSV"); | ||
model.addAttribute("page_description", "Upload courses CSV to float courses in current session"); | ||
|
||
return "dean/float-course"; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...hcet/core/admin/dean/registration/course/floated/FloatedCourseRegistrationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,32 @@ | ||
package amu.zhcet.core.admin.dean.registration.course.floated; | ||
|
||
import amu.zhcet.storage.csv.neo.Confirmation; | ||
import amu.zhcet.storage.csv.neo.Result; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequestMapping("/api/test") | ||
public class FloatedCourseRegistrationController { | ||
|
||
private final FloatedCourseRegistrationService floatedCourseRegistrationService; | ||
|
||
public FloatedCourseRegistrationController(FloatedCourseRegistrationService floatedCourseRegistrationService) { | ||
this.floatedCourseRegistrationService = floatedCourseRegistrationService; | ||
} | ||
|
||
@PostMapping | ||
public Result<FloatedCourseUpload> test(@RequestParam(required = false) MultipartFile file) throws IOException { | ||
return floatedCourseRegistrationService.parse(file); | ||
} | ||
|
||
@PostMapping("confirm") | ||
public Confirmation testConfirm(@RequestBody ItemState itemState) throws IOException { | ||
return floatedCourseRegistrationService.confirm(itemState); | ||
} | ||
|
||
} |
157 changes: 157 additions & 0 deletions
157
...u/zhcet/core/admin/dean/registration/course/floated/FloatedCourseRegistrationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,161 @@ | ||
package amu.zhcet.core.admin.dean.registration.course.floated; | ||
|
||
import amu.zhcet.data.config.ConfigurationService; | ||
import amu.zhcet.data.course.Course; | ||
import amu.zhcet.data.course.CourseLite; | ||
import amu.zhcet.data.course.CourseRepository; | ||
import amu.zhcet.data.course.floated.FloatedCourse; | ||
import amu.zhcet.data.course.floated.FloatedCourseLite; | ||
import amu.zhcet.data.course.floated.FloatedCourseLiteImpl; | ||
import amu.zhcet.data.course.floated.FloatedCourseRepository; | ||
import amu.zhcet.storage.csv.neo.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.transaction.Transactional; | ||
import java.io.IOException; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Service | ||
public class FloatedCourseRegistrationService { | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CourseResponse { | ||
private String code; | ||
private String title; | ||
private String department; | ||
} | ||
|
||
private final FloatedCourseRepository floatedCourseRepository; | ||
private final CourseRepository courseRepository; | ||
|
||
private final FileStorageCsvParser floatedCourseCsvParser; | ||
|
||
private final ModelMapper modelMapper; | ||
|
||
public FloatedCourseRegistrationService(FloatedCourseRepository floatedCourseRepository, CourseRepository courseRepository, FileStorageCsvParser floatedCourseCsvParser, ModelMapper modelMapper) { | ||
this.floatedCourseRepository = floatedCourseRepository; | ||
this.courseRepository = courseRepository; | ||
this.floatedCourseCsvParser = floatedCourseCsvParser; | ||
this.modelMapper = modelMapper; | ||
} | ||
|
||
public List<FloatedCourseLite> get(Collection<String> codes) { | ||
String defaultSessionCode = ConfigurationService.getDefaultSessionCode(); | ||
|
||
return floatedCourseRepository.getBySessionAndCourse_CodeIn(defaultSessionCode, codes); | ||
} | ||
|
||
public Result<FloatedCourseUpload> parse(MultipartFile file) throws IOException { | ||
Result<FloatedCourseUpload> uploadResult = floatedCourseCsvParser.parse(FloatedCourseUpload.class, file); | ||
|
||
if (uploadResult.isParsed() && uploadResult.getCsv().isSuccessful()) { | ||
return getWrappedResult(uploadResult); | ||
} | ||
|
||
return uploadResult; | ||
} | ||
|
||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
public static class FloatedCourseConfirmation extends Confirmation { | ||
private Collection<FloatedCourseLite> floated; | ||
|
||
public FloatedCourseConfirmation(boolean success, String message, Collection<FloatedCourseLite> floated) { | ||
super(success, message); | ||
this.floated = floated; | ||
} | ||
} | ||
|
||
@Transactional | ||
public Confirmation confirm(ItemState itemState) { | ||
List<Wrapper<CourseResponse>> wrappers = getWrappedCourses(new LinkedHashSet<>(itemState.getItems())); | ||
State newState = State.fromWrappers(wrappers); | ||
|
||
boolean matchingState = newState.isMatching(itemState.getState()); | ||
log.debug("Is new state same as old state? {}", matchingState); | ||
|
||
if (!matchingState) { | ||
return new Confirmation(false, "Something went wrong! Please try again"); | ||
} else { | ||
log.debug("Floating course requests {}", wrappers); | ||
Set<String> validCodes = wrappers.stream() | ||
.filter(item -> item.getMessage() == null || item.getMessage().getType() == Type.SUCCESS) | ||
.map(item -> item.getItem().getCode()) | ||
.collect(Collectors.toCollection(LinkedHashSet::new)); | ||
|
||
log.debug("Floating courses {}", validCodes); | ||
List<Course> courses = courseRepository.findAllByCodeIn(validCodes); | ||
List<FloatedCourse> floatedCourses = courses.stream() | ||
.map(course -> new FloatedCourse(ConfigurationService.getDefaultSessionCode(), course)) | ||
.collect(Collectors.toList()); | ||
Iterable<FloatedCourse> floated = floatedCourseRepository.saveAll(floatedCourses); | ||
|
||
List<FloatedCourseLite> floatedCourseLites = new ArrayList<>(); | ||
floated.forEach(floatedCourse -> { | ||
floatedCourseLites.add(modelMapper.map(floatedCourse, FloatedCourseLiteImpl.class)); | ||
}); | ||
|
||
return new FloatedCourseConfirmation(true, "Courses floated successfully", floatedCourseLites); | ||
} | ||
} | ||
|
||
private WrappedResult<FloatedCourseUpload, CourseResponse> getWrappedResult(Result<FloatedCourseUpload> uploadResult) { | ||
Set<String> codes = uploadResult.getCsv().getItems().stream() | ||
.map(FloatedCourseUpload::getCourse) | ||
.collect(Collectors.toCollection(LinkedHashSet::new)); | ||
|
||
List<Wrapper<CourseResponse>> wrappers = getWrappedCourses(codes); | ||
|
||
State state = State.fromWrappers(wrappers); | ||
log.debug("Floated Course State {}", state); | ||
log.debug("Is valid state? {}", state.isValid()); | ||
return new WrappedResult<>(uploadResult, wrappers, state); | ||
} | ||
|
||
private List<Wrapper<CourseResponse>> getWrappedCourses(Set<String> codes) { | ||
List<FloatedCourseLite> floatedCourseLites = get(codes); | ||
List<CourseLite> courses = courseRepository.getByCodeIn(codes); | ||
|
||
return codes.stream().map(item -> { | ||
Optional<FloatedCourseLite> floatedCourseLiteOptional = floatedCourseLites.stream() | ||
.filter(floatedCourse -> floatedCourse.getCourse().getCode().equals(item)) | ||
.findAny(); | ||
|
||
if (floatedCourseLiteOptional.isPresent()) { | ||
FloatedCourseLite floatedCourseLite = floatedCourseLiteOptional.get(); | ||
CourseResponse courseResponse = new CourseResponse(floatedCourseLite.getCourse().getCode(), floatedCourseLite.getCourse().getTitle(), floatedCourseLite.getCourse().getDepartment().getName()); | ||
return new Wrapper<>(courseResponse, Message.warning("Course is already floated")); | ||
} | ||
|
||
Optional<CourseLite> courseLiteOptional = courses.stream() | ||
.filter(courseLite -> courseLite.getCode().equals(item)) | ||
.findAny(); | ||
|
||
if (courseLiteOptional.isPresent()) { | ||
CourseLite courseLite = courseLiteOptional.get(); | ||
|
||
|
||
CourseResponse courseResponse = new CourseResponse(courseLite.getCode(), courseLite.getTitle(), courseLite.getDepartment().getName()); | ||
if (!courseLite.isActive()) { | ||
return new Wrapper<>(courseResponse, Message.error("Course is inactive")); | ||
} else { | ||
return new Wrapper<>(courseResponse); | ||
} | ||
} else { | ||
return new Wrapper<>(new CourseResponse(item, null, null), Message.error("Course does not exist")); | ||
} | ||
}).collect(Collectors.toList()); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/amu/zhcet/core/admin/dean/registration/course/floated/FloatedCourseUpload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package amu.zhcet.core.admin.dean.registration.course.floated; | ||
|
||
import com.j256.simplecsv.common.CsvColumn; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public class FloatedCourseUpload { | ||
@CsvColumn | ||
private String course; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/amu/zhcet/core/admin/dean/registration/course/floated/ItemState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package amu.zhcet.core.admin.dean.registration.course.floated; | ||
|
||
import amu.zhcet.storage.csv.neo.State; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public class ItemState { | ||
private List<String> items; | ||
private State state; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
package amu.zhcet.data.course; | ||
|
||
public class CourseLite { | ||
import amu.zhcet.data.department.DepartmentLite; | ||
|
||
public interface CourseLite { | ||
String getCode(); | ||
String getTitle(); | ||
boolean isActive(); | ||
DepartmentLite getDepartment(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package amu.zhcet.data.course; | ||
|
||
import amu.zhcet.data.department.DepartmentLiteImpl; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class CourseLiteImpl implements CourseLite { | ||
private String code; | ||
private String title; | ||
private boolean active; | ||
private DepartmentLiteImpl department; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
src/main/java/amu/zhcet/data/course/floated/FloatedCourseLite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
package amu.zhcet.data.course.floated; | ||
|
||
public class FloatedCourseLite { | ||
import amu.zhcet.data.course.CourseLite; | ||
|
||
public interface FloatedCourseLite { | ||
String getSession(); | ||
CourseLite getCourse(); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/amu/zhcet/data/course/floated/FloatedCourseLiteImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package amu.zhcet.data.course.floated; | ||
|
||
import amu.zhcet.data.course.CourseLiteImpl; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class FloatedCourseLiteImpl implements FloatedCourseLite { | ||
private String session; | ||
private CourseLiteImpl course; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package amu.zhcet.data.department; | ||
|
||
public class LightDepartment { | ||
public interface DepartmentLite { | ||
String getCode(); | ||
String getName(); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/amu/zhcet/data/department/DepartmentLiteImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package amu.zhcet.data.department; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class DepartmentLiteImpl implements DepartmentLite { | ||
private String code; | ||
private String name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.