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
Showing
12 changed files
with
158 additions
and
6 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
85 changes: 85 additions & 0 deletions
85
src/main/java/com/oskopek/studyguide/constraint/CourseEnrollmentEnrolledInConstraint.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,85 @@ | ||
package com.oskopek.studyguide.constraint; | ||
|
||
import com.oskopek.studyguide.constraint.event.StringMessageEvent; | ||
import com.oskopek.studyguide.model.CourseEnrollment; | ||
import com.oskopek.studyguide.model.Semester; | ||
import com.oskopek.studyguide.model.courses.EnrollableIn; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Checks if the course is enrolled only in the allowed semester. | ||
* | ||
* @see EnrollableIn | ||
*/ | ||
public class CourseEnrollmentEnrolledInConstraint extends CourseEnrollmentConstraint { | ||
|
||
private final String message = "constraint.enrolledInInvalid"; | ||
|
||
private final transient Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
/** | ||
* Private default constructor, needed by CDI. | ||
*/ | ||
protected CourseEnrollmentEnrolledInConstraint() { | ||
// needed for CDI | ||
} | ||
|
||
/** | ||
* Default constructor. | ||
* | ||
* @param enrollment the enrollment to check | ||
*/ | ||
public CourseEnrollmentEnrolledInConstraint(CourseEnrollment enrollment) { | ||
super(enrollment); | ||
} | ||
|
||
@Override | ||
public void validate() { | ||
EnrollableIn enrollableIn = getCourseEnrollment().getCourse().getEnrollableIn(); | ||
if (EnrollableIn.BOTH.equals(enrollableIn)) { | ||
fireFixedEvent(this); | ||
return; | ||
} | ||
Semester semester = getCourseEnrollment().getSemester(); | ||
int semesterIndex = getSemesterPlan().getSemesterList().indexOf(semester); | ||
if (semesterIndex < 0) { | ||
throw new IllegalStateException("Semester " + semester + " not found in semester plan " + semesterPlan); | ||
} | ||
|
||
semesterIndex += 1; // count from one | ||
switch (enrollableIn) { | ||
case BOTH: | ||
fireFixedEvent(this); | ||
return; | ||
case SUMMER: | ||
if (semesterIndex % 2 == 0) { | ||
fireFixedEvent(this); | ||
return; | ||
} | ||
break; | ||
case WINTER: | ||
if (semesterIndex % 2 == 1) { | ||
fireFixedEvent(this); | ||
return; | ||
} | ||
break; | ||
default: | ||
throw new IllegalStateException("Unknown enrollableIn: " + enrollableIn); | ||
} | ||
fireBrokenEvent(generateMessage(semesterIndex % 2 == 1 ? EnrollableIn.WINTER : EnrollableIn.SUMMER, | ||
enrollableIn), getCourseEnrollment()); | ||
} | ||
|
||
/** | ||
* Generates a message from the given parameters (localized). Used for populating the message of | ||
* {@link StringMessageEvent}s (usually upon breaking a constraint). | ||
* | ||
* @param enrolledIn the type of semester the course is currently enrolled in | ||
* @param enrollableIn the allowed type | ||
* @return the String to use as a message, localized | ||
*/ | ||
protected String generateMessage(EnrollableIn enrolledIn, EnrollableIn enrollableIn) { | ||
return String.format(messages.getString(message), enrolledIn, enrollableIn); | ||
} | ||
} |
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
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
41 changes: 40 additions & 1 deletion
41
src/main/java/com/oskopek/studyguide/model/courses/EnrollableIn.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,5 +1,44 @@ | ||
package com.oskopek.studyguide.model.courses; | ||
|
||
import java.util.ResourceBundle; | ||
|
||
/** | ||
* Semester types for constraining course enrollments. | ||
*/ | ||
public enum EnrollableIn { | ||
WINTER, SUMMER, BOTH | ||
|
||
/** | ||
* Winter/fall semester. Usually semesters with a semesterIndex % 2 == 1 (counted from 1). | ||
*/ | ||
WINTER, | ||
|
||
/** | ||
* Summer/spring semester. Usually semesters with a semesterIndex % 2 == 0 (counted from 1). | ||
*/ | ||
SUMMER, | ||
|
||
/** | ||
* Both of the above (covers all semesters). | ||
*/ | ||
BOTH; | ||
|
||
/** | ||
* Print a localized string representing the given value. | ||
* | ||
* @param messages the resource bundle to use | ||
* @return localized string representation | ||
* @throws IllegalStateException if this is not a known enrollable in type | ||
*/ | ||
public String print(ResourceBundle messages) { | ||
switch (this) { | ||
case WINTER: | ||
return messages.getString("semester.winterLong"); | ||
case SUMMER: | ||
return messages.getString("semester.summerLong"); | ||
case BOTH: | ||
return messages.getString("semester.bothLong"); | ||
default: | ||
throw new IllegalStateException("No known enrollable in: " + this); | ||
} | ||
} | ||
} |
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
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
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