Search in sources :

Example 21 with SubstitutionScheduleDay

use of me.vertretungsplan.objects.SubstitutionScheduleDay in project substitution-schedule-parser by vertretungsplanme.

the class SubstitutionScheduleDiff method compare.

/**
 * Constructs a {@link SubstitutionScheduleDiff} from two {@link SubstitutionSchedule}s
 * @param a Old substitution schedule
 * @param b New substitution schedule
 * @return difference between the two schedules
 */
public static SubstitutionScheduleDiff compare(SubstitutionSchedule a, SubstitutionSchedule b) {
    SubstitutionScheduleDiff diff = new SubstitutionScheduleDiff();
    diff.newAdditionalInfos = new ArrayList<>();
    diff.removedAdditionalInfos = new ArrayList<>();
    diff.newDays = new ArrayList<>();
    diff.editedDays = new ArrayList<>();
    diff.removedDays = new ArrayList<>();
    for (AdditionalInfo newInfo : b.getAdditionalInfos()) {
        if (!a.getAdditionalInfos().contains(newInfo)) {
            diff.newAdditionalInfos.add(newInfo);
        }
    }
    for (AdditionalInfo oldInfo : a.getAdditionalInfos()) {
        if (!b.getAdditionalInfos().contains(oldInfo)) {
            diff.removedAdditionalInfos.add(oldInfo);
        }
    }
    for (SubstitutionScheduleDay newDay : b.getDays()) {
        SubstitutionScheduleDay oldDay = findSameDateDay(newDay, a.getDays());
        if (oldDay != null) {
            SubstitutionScheduleDayDiff dayDiff = SubstitutionScheduleDayDiff.compare(oldDay, newDay);
            if (dayDiff.isNotEmpty())
                diff.editedDays.add(dayDiff);
        } else {
            diff.newDays.add(newDay);
        }
    }
    for (SubstitutionScheduleDay oldDay : a.getDays()) {
        SubstitutionScheduleDay newDay = findSameDateDay(oldDay, b.getDays());
        if (newDay == null) {
            diff.removedDays.add(oldDay);
        }
    }
    return diff;
}
Also used : AdditionalInfo(me.vertretungsplan.objects.AdditionalInfo) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay)

Example 22 with SubstitutionScheduleDay

use of me.vertretungsplan.objects.SubstitutionScheduleDay in project substitution-schedule-parser by vertretungsplanme.

the class CSVParser method parseCSV.

@NotNull
SubstitutionSchedule parseCSV(String response) throws JSONException, IOException {
    SubstitutionSchedule schedule = SubstitutionSchedule.fromData(scheduleData);
    String[] lines = response.split("\n");
    String separator = data.getString(PARAM_SEPARATOR);
    for (int i = data.optInt(PARAM_SKIP_LINES, 0); i < lines.length; i++) {
        String[] columns = lines[i].split(separator);
        Substitution v = new Substitution();
        String dayName = null;
        String stand = "";
        int j = 0;
        for (String column : columns) {
            String type = data.getJSONArray(PARAM_COLUMNS).getString(j);
            switch(type) {
                case "lesson":
                    v.setLesson(column);
                    break;
                case "subject":
                    v.setSubject(column);
                    break;
                case "previousSubject":
                    v.setPreviousSubject(column);
                    break;
                case "type":
                    v.setType(column);
                    v.setColor(colorProvider.getColor(column));
                    break;
                case "type-entfall":
                    if (column.equals("x")) {
                        v.setType("Entfall");
                        v.setColor(colorProvider.getColor("Entfall"));
                    } else {
                        v.setType("Vertretung");
                        v.setColor(colorProvider.getColor("Vertretung"));
                    }
                    break;
                case "room":
                    v.setRoom(column);
                    break;
                case "teacher":
                    v.setTeacher(column);
                    break;
                case "previousTeacher":
                    v.setPreviousTeacher(column);
                    break;
                case "desc":
                    v.setDesc(column);
                    break;
                case "desc-type":
                    v.setDesc(column);
                    String recognizedType = recognizeType(column);
                    v.setType(recognizedType);
                    v.setColor(colorProvider.getColor(recognizedType));
                    break;
                case "previousRoom":
                    v.setPreviousRoom(column);
                    break;
                case "class":
                    v.getClasses().add(getClassName(column, data));
                    break;
                case "day":
                    dayName = column;
                    break;
                case "stand":
                    stand = column;
                    break;
                case "ignore":
                    break;
                default:
                    throw new IllegalArgumentException("Unknown column type: " + column);
            }
            j++;
        }
        if (v.getType() == null) {
            v.setType("Vertretung");
            v.setColor(colorProvider.getColor("Vertretung"));
        }
        if (dayName != null) {
            SubstitutionScheduleDay day = new SubstitutionScheduleDay();
            day.setDateString(dayName);
            day.setDate(ParserUtils.parseDate(dayName));
            day.setLastChangeString(stand);
            day.setLastChange(ParserUtils.parseDateTime(stand));
            day.addSubstitution(v);
            schedule.addDay(day);
        }
    }
    if (scheduleData.getData().has(PARAM_WEBSITE)) {
        schedule.setWebsite(scheduleData.getData().getString(PARAM_WEBSITE));
    }
    schedule.setClasses(getAllClasses());
    schedule.setTeachers(getAllTeachers());
    return schedule;
}
Also used : Substitution(me.vertretungsplan.objects.Substitution) SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with SubstitutionScheduleDay

use of me.vertretungsplan.objects.SubstitutionScheduleDay in project substitution-schedule-parser by vertretungsplanme.

the class DaVinciParser method parseDaVinciTable.

static void parseDaVinciTable(Element table, SubstitutionSchedule v, String klasse, SubstitutionScheduleDay day, ColorProvider colorProvider) {
    boolean skipRow = false;
    List<String> headers = new ArrayList<>();
    for (Element header : table.select("thead tr th")) {
        headers.add(header.text());
    }
    if (headers.size() == 0) {
        skipRow = true;
        for (Element header : table.select(" tr:first-child td")) {
            headers.add(header.text());
        }
    }
    // These three variables can
    Set<String> classes = new HashSet<>();
    String lesson = null;
    LocalDate currentDate = null;
    Pattern previousCurrentPattern = Pattern.compile("\\+([^\\s]+) \\(([^)]+)\\)");
    Pattern previousPattern = Pattern.compile("\\(([^)]+)\\)");
    for (Element row : table.select("tr:not(thead tr)")) {
        if (skipRow) {
            skipRow = false;
            continue;
        }
        Substitution subst = new Substitution();
        LocalDate substDate = null;
        Elements columns = row.select("td");
        for (int i = 0; i < headers.size(); i++) {
            String value = columns.get(i).text().replace("\u00a0", "");
            String header = headers.get(i);
            if (value.isEmpty()) {
                if (header.equals("Klasse"))
                    subst.setClasses(new HashSet<>(classes));
                if (header.equals("Pos") || header.equals("Stunde") || header.equals("Std.") || header.equals("Dstd.")) {
                    subst.setLesson(lesson);
                }
                if (header.equals("Art") || header.equals("Merkmal"))
                    subst.setType("Vertretung");
                if (header.equals("Datum"))
                    substDate = currentDate;
                continue;
            }
            Matcher previousCurrentMatcher = previousCurrentPattern.matcher(value);
            Matcher previousMatcher = previousPattern.matcher(value);
            switch(header) {
                case "Klasse":
                    String classesStr = value;
                    if (previousMatcher.find()) {
                        classesStr = previousMatcher.group(1);
                    }
                    classes = new HashSet<>(Arrays.asList(classesStr.split(", ")));
                    subst.setClasses(classes);
                    break;
                case "Pos":
                case "Stunde":
                case "Std.":
                case "Dstd.":
                    lesson = value;
                    subst.setLesson(lesson);
                    break;
                case "VLehrer Kürzel":
                case "VLehrer":
                case "Vertreter":
                case "Vertretungslehrkraft":
                    if (!value.startsWith("*")) {
                        subst.setTeacher(value);
                    } else {
                        subst.setType(value.substring(1));
                    }
                    break;
                case "Lehrer":
                case "Lehrer Kürzel":
                case "Lehrer Name":
                case "Lehrkraft":
                    if (previousCurrentMatcher.find()) {
                        subst.setTeacher(previousCurrentMatcher.group(1));
                        subst.setPreviousTeacher(previousCurrentMatcher.group(2));
                    } else if (previousMatcher.find()) {
                        subst.setPreviousTeacher(previousMatcher.group(1));
                    } else {
                        subst.setPreviousTeacher(value);
                    }
                    break;
                case "VFach":
                case "V Fach":
                    subst.setSubject(value);
                    break;
                case "Fach":
                case "Original Fach":
                    if (previousCurrentMatcher.find()) {
                        subst.setSubject(previousCurrentMatcher.group(1));
                        subst.setPreviousSubject(previousCurrentMatcher.group(2));
                    } else {
                        subst.setPreviousSubject(value);
                    }
                    break;
                case "VRaum":
                case "V Raum":
                    subst.setRoom(value);
                    break;
                case "Raum":
                case "Original Raum":
                    if (previousCurrentMatcher.find()) {
                        subst.setRoom(previousCurrentMatcher.group(1));
                        subst.setPreviousRoom(previousCurrentMatcher.group(2));
                    } else {
                        subst.setPreviousRoom(value);
                    }
                    break;
                case "Art":
                case "Merkmal":
                    subst.setType(value);
                    break;
                case "Info":
                case "Mitteilung":
                    subst.setDesc(value);
                    break;
                case "Datum":
                    substDate = ParserUtils.parseDate(value);
                    currentDate = substDate;
                    break;
            }
        }
        if (klasse != null) {
            Set<String> fixedClasses = new HashSet<>();
            fixedClasses.add(klasse);
            subst.setClasses(fixedClasses);
        }
        if (subst.getType() == null) {
            String recognizedType = null;
            if (subst.getDesc() != null)
                recognizedType = recognizeType(subst.getDesc());
            subst.setType(recognizedType != null ? recognizedType : "Vertretung");
        }
        subst.setColor(colorProvider.getColor(subst.getType()));
        if (substDate == null && day == null)
            continue;
        if (day == null || substDate != null && !substDate.equals(day.getDate())) {
            day = null;
            for (SubstitutionScheduleDay d : v.getDays()) {
                if (d.getDate().equals(substDate)) {
                    day = d;
                }
            }
            if (day == null) {
                day = new SubstitutionScheduleDay();
                day.setDate(substDate);
                v.addDay(day);
            }
        }
        day.addSubstitution(subst);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements) LocalDate(org.joda.time.LocalDate) Substitution(me.vertretungsplan.objects.Substitution) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay)

Example 24 with SubstitutionScheduleDay

use of me.vertretungsplan.objects.SubstitutionScheduleDay in project substitution-schedule-parser by vertretungsplanme.

the class ESchoolParser method parseESchoolSchedule.

@NotNull
SubstitutionSchedule parseESchoolSchedule(Document doc) throws IOException, JSONException {
    SubstitutionSchedule schedule = SubstitutionSchedule.fromData(scheduleData);
    String infoString = doc.select("#Content table").first().select("td").get(1).ownText();
    Pattern pattern = Pattern.compile("Letzte Aktualisierung:\u00a0(\\d{2}.\\d{2}.\\d{4} - \\d{2}:\\d{2})");
    Matcher matcher = pattern.matcher(infoString);
    if (matcher.find()) {
        LocalDateTime lastChange = DateTimeFormat.forPattern("dd.MM.yyyy - HH:mm").parseLocalDateTime(matcher.group(1));
        schedule.setLastChange(lastChange);
    }
    Elements titles = doc.select("center b");
    Elements tables = doc.select("table#DATA");
    if (!tables.get(0).text().contains("Keine Daten verfügbar")) {
        if (titles.size() != tables.size())
            throw new IOException("Anzahl Überschriften != Anzahl Tabellen");
        for (int i = 0; i < titles.size(); i++) {
            SubstitutionScheduleDay day = new SubstitutionScheduleDay();
            day.setDate(ParserUtils.parseDate(titles.get(i).text()));
            parseTable(tables.get(i), day);
            schedule.addDay(day);
        }
    }
    schedule.setClasses(getAllClasses());
    schedule.setTeachers(getAllTeachers());
    return schedule;
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay) IOException(java.io.IOException) Elements(org.jsoup.select.Elements) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with SubstitutionScheduleDay

use of me.vertretungsplan.objects.SubstitutionScheduleDay in project substitution-schedule-parser by vertretungsplanme.

the class IndiwareMobileParser method parseDay.

static SubstitutionScheduleDay parseDay(Document doc, ColorProvider colorProvider, SubstitutionScheduleData scheduleData) {
    SubstitutionScheduleDay day = new SubstitutionScheduleDay();
    day.setDate(ParserUtils.parseDate(doc.select("Kopf > DatumPlan").text()));
    day.setLastChange(ParserUtils.parseDateTime(doc.select("Kopf > Zeitstempel").text()));
    for (Element klasse : doc.select("Klassen > Kl")) {
        String className = klasse.select("Kurz").first().text();
        HashSet<String> classes = new HashSet<>();
        classes.add(className);
        for (Element lesson : klasse.select("Pl > Std")) {
            if (lesson.select("If:not(:empty), Le[LeAe], Ra[RaAe], Fa[FaAe]").size() == 0) {
                continue;
            }
            Substitution subst = new Substitution();
            subst.setLesson(text(lesson.select("St")));
            if (scheduleData.getType() == SubstitutionSchedule.Type.STUDENT) {
                subst.setTeachers(split(text(lesson.select("Le"))));
                subst.setClasses(classes);
            } else {
                subst.setClasses(split(text(lesson.select("Le"))));
                subst.setTeachers(classes);
            }
            subst.setSubject(text(lesson.select("Fa")));
            subst.setRoom(text(lesson.select("Ra")));
            IndiwareParser.handleDescription(subst, text(lesson.select("If")), scheduleData.getType() == SubstitutionSchedule.Type.TEACHER);
            if (subst.getType() == null)
                subst.setType("Vertretung");
            subst.setColor(colorProvider.getColor(subst.getType()));
            day.addSubstitution(subst);
        }
    }
    for (Element info : doc.select("ZusatzInfo > ZiZeile")) {
        day.getMessages().add(info.text());
    }
    return day;
}
Also used : Substitution(me.vertretungsplan.objects.Substitution) Element(org.jsoup.nodes.Element) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay) HashSet(java.util.HashSet)

Aggregations

SubstitutionScheduleDay (me.vertretungsplan.objects.SubstitutionScheduleDay)34 LocalDate (org.joda.time.LocalDate)15 Substitution (me.vertretungsplan.objects.Substitution)14 SubstitutionSchedule (me.vertretungsplan.objects.SubstitutionSchedule)14 Test (org.junit.Test)14 LocalDateTime (org.joda.time.LocalDateTime)12 Element (org.jsoup.nodes.Element)10 Matcher (java.util.regex.Matcher)6 Document (org.jsoup.nodes.Document)6 Elements (org.jsoup.select.Elements)6 Pattern (java.util.regex.Pattern)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 NotNull (org.jetbrains.annotations.NotNull)3 JSONObject (org.json.JSONObject)3 JSONArray (org.json.JSONArray)2 HashSet (java.util.HashSet)1 AdditionalInfo (me.vertretungsplan.objects.AdditionalInfo)1 SubstitutionScheduleData (me.vertretungsplan.objects.SubstitutionScheduleData)1 TextNode (org.jsoup.nodes.TextNode)1