Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Elements to Lesson class #24

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions src/main/java/org/bytedream/untis4j/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ private <T extends BaseResponse> T requestSender(UntisUtils.Method method, Respo

/**
* Checks if the same request is still in the {@link com.github.benmanes.caffeine.cache.LoadingCache} and if not, the request is sent to the server.
*
*
*
*
* @param method the POST method
* @param params params you want to send with the request
* @param action lambda expression that gets called if the {@code method} is not in the cache manager
Expand Down Expand Up @@ -711,10 +711,26 @@ public SchoolYears.SchoolYearObject getCurrentSchoolYear() throws IOException {
* @since 1.0
*/
public Timetable getTimetable(LocalDate start, LocalDate end, UntisUtils.ElementType elementType, int id) throws IOException {
HashMap<String, String> params = UntisUtils.localDateToParams(start, end);

params.put("type", String.valueOf(elementType.getElementType()));
params.put("id", String.valueOf(id));
if (end.isBefore(start)) {
throw new DateTimeException("The end date must end after or on the same day as the start date");
}
Map<String, ?> element = new HashMap<>(){{
put("type", elementType.getElementType());
put("id", id);
}};
Map<String, ?> options = new HashMap<>() {{
put("startDate", start.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
put("endDate", end.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
put("element", element);
put("onlyBaseTimetable", "False");
put("showInfo","True");
put("showSubstText", "True");
put("showLsText", "True");
put("showLsNumber", "True");
put("showStudentgroup", "True");
}};
Map<String, Map<String, ?>> params = new HashMap<>();
params.put("options", options);

return requestSender(UntisUtils.Method.GETTIMETABLE, params, response -> {
JSONObject jsonResponse = response.getResponse();
Expand Down Expand Up @@ -841,7 +857,23 @@ public Timetable getTimetable(LocalDate start, LocalDate end, UntisUtils.Element
String activityType = null;
if (timetableInfos.has("activityType")) activityType = timetableInfos.getString("activityType");

timetable.add(new Timetable.Lesson(LocalDate.parse(String.valueOf(timetableInfos.getInt("date")), DateTimeFormatter.ofPattern("yyyyMMdd")),
String info = null;
if (timetableInfos.has("info")) info = timetableInfos.getString("info");

String substText = null;
if (timetableInfos.has("substText")) substText = timetableInfos.getString("substText");

String lsText = null;
if (timetableInfos.has("lstext")) lsText = timetableInfos.getString("lstext");

Integer lsNumber = null;
if (timetableInfos.has("lsnumber")) lsNumber = timetableInfos.getInt("lsnumber");

String studentGroup = null;
if (timetableInfos.has("sg")) studentGroup = timetableInfos.getString("sg");

timetable.add(new Timetable.Lesson(
LocalDate.parse(String.valueOf(timetableInfos.getInt("date")), DateTimeFormatter.ofPattern("yyyyMMdd")),
startTime,
endTime,
timeUnits.findByStartTime(startTime),
Expand All @@ -854,7 +886,13 @@ public Timetable getTimetable(LocalDate start, LocalDate end, UntisUtils.Element
subjects,
originalSubjects,
code,
activityType));
activityType,
info,
substText,
lsText,
lsNumber,
studentGroup
));
}

return timetable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,45 @@ public ArrayList<String> getActivityTypes() {
return activityTypes;
}

public ArrayList<String> getInfos() {
ArrayList<String> infos = new ArrayList<>();

this.stream().map(Lesson::getInfo).forEach(infos::add);

return infos;
}

public ArrayList<String> getSubstTexts() {
ArrayList<String> substText = new ArrayList<>();

this.stream().map(Lesson::getSubstText).forEach(substText::add);

return substText;
}

public ArrayList<String> getLsTexts() {
ArrayList<String> lsTexts = new ArrayList<>();

this.stream().map(Lesson::getLsText).forEach(lsTexts::add);

return lsTexts;
}

public ArrayList<Integer> getLsNumbers() {
ArrayList<Integer> lsNumbers = new ArrayList<>();

this.stream().map(Lesson::getLsNumber).forEach(lsNumbers::add);

return lsNumbers;
}
public ArrayList<String> getStudentGroups() {
ArrayList<String> studentGroups = new ArrayList<>();

this.stream().map(Lesson::getStudentGroup).forEach(studentGroups::add);

return studentGroups;
}

/**
* Class to get information about a lesson
*
Expand All @@ -741,6 +780,11 @@ public static class Lesson extends ResponseObject {
private final Subjects originalSubjects;
private final UntisUtils.LessonCode code;
private final String activityType;
private final String info;
private final String substText;
private final String lsText;
private final Integer lsNumber;
private final String studentGroup;

/**
* Initialize the {@link Lesson} class
Expand All @@ -759,6 +803,11 @@ public static class Lesson extends ResponseObject {
* @param originalSubjects the original subjects if the subject has changed
* @param code code of the lesson (normally null, {@link UntisUtils.LessonCode#CANCELLED} if the lesson is cancelled, {@code UntisUtils.LessonCode.IRREGULAR} if e.g. a lesson has been moved
* @param activityType type of the lesson
* @param info info text of the lesson
* @param substText substitution text
* @param lsText ls text of the lesson
* @param lsNumber ls Number of the lesson
* @param studentGroup name of the studentGroup
* @since 1.0
*/
public Lesson(LocalDate date,
Expand All @@ -774,7 +823,12 @@ public Lesson(LocalDate date,
Subjects subjects,
Subjects originalSubjects,
UntisUtils.LessonCode code,
String activityType) {
String activityType,
String info,
String substText,
String lsText,
Integer lsNumber,
String studentGroup) {
this.date = date;
this.startTime = startTime;
this.endTime = endTime;
Expand All @@ -789,6 +843,11 @@ public Lesson(LocalDate date,
this.originalSubjects = originalSubjects;
this.code = code;
this.activityType = activityType;
this.info = info;
this.substText = substText;
this.lsText = lsText;
this.lsNumber = lsNumber;
this.studentGroup = studentGroup;
}

/**
Expand Down Expand Up @@ -931,6 +990,21 @@ public String getActivityType() {
return activityType;
}

public String getInfo() {
return info;
}
public String getSubstText() {
return substText;
}
public String getLsText() {
return lsText;
}
public Integer getLsNumber() {
return lsNumber;
}
public String getStudentGroup() {
return studentGroup;
}
/**
* Returns a json parsed string with all information
*
Expand All @@ -954,6 +1028,11 @@ public String toString() {
classAsMap.put("startTime", startTime.format(DateTimeFormatter.ofPattern("HHmm")));
classAsMap.put("endTime", endTime.format(DateTimeFormatter.ofPattern("HHmm")));
classAsMap.put("activityType", activityType);
classAsMap.put("info", info);
classAsMap.put("substText", substText);
classAsMap.put("lsText", lsText);
classAsMap.put("lsNumber", lsNumber);
classAsMap.put("studentGroup", studentGroup);
return new JSONObject(classAsMap).toString();
}
}
Expand Down