Search in sources :

Example 6 with Substitution

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

the class SubstitutionScheduleDayDiff method findSimilarSubstitution.

private static Substitution findSimilarSubstitution(Substitution subst, Set<Substitution> substs, Set<Substitution> handledSubsts) {
    int maxScore = 0;
    Substitution maxScoreSubstitution = null;
    for (Substitution currentSubst : substs) {
        if (currentSubst.getClasses().equals(subst.getClasses()) && !handledSubsts.contains(currentSubst)) {
            int score = calculateSimilarityScore(currentSubst, subst);
            if (score > maxScore) {
                maxScore = score;
                maxScoreSubstitution = currentSubst;
            }
        }
    }
    if (maxScore > 0) {
        return maxScoreSubstitution;
    } else {
        return null;
    }
}
Also used : Substitution(me.vertretungsplan.objects.Substitution)

Example 7 with Substitution

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

the class UntisCommonParser method parseSubstitutionScheduleTable.

/**
 * Parses an Untis substitution schedule table
 *
 * @param table        the <code>table</code> Element from the HTML document
 * @param data         {@link SubstitutionScheduleData#getData()}
 * @param day          the {@link SubstitutionScheduleDay} where the substitutions will be stored
 * @param defaultClass the class that should be set if there is no class column in the table
 */
private void parseSubstitutionScheduleTable(Element table, JSONObject data, SubstitutionScheduleDay day, String defaultClass, List<String> allClasses) throws JSONException, CredentialInvalidException, IOException {
    Elements headerRows = table.select("tr:has(th)");
    List<String> columnTitles = new ArrayList<>();
    if (headerRows.size() > 0) {
        Elements headers = headerRows.get(0).select("th");
        for (int i = 0; i < headers.size(); i++) {
            StringBuilder builder = new StringBuilder();
            boolean first = true;
            for (Element headerRow : headerRows) {
                final String text = headerRow.select("th").get(i).text().replace("\u00a0", " ").trim();
                if (first) {
                    if (!text.equals(""))
                        first = false;
                } else {
                    builder.append(" ");
                }
                builder.append(text);
            }
            columnTitles.add(builder.toString());
        }
    }
    debuggingDataHandler.columnTitles(columnTitles);
    final JSONArray columnsJson = data.optJSONArray(PARAM_COLUMNS);
    List<String> columns = new ArrayList<>();
    if (columnTitles.size() == 0) {
        for (int i = 0; i < columnsJson.length(); i++) columns.add(columnsJson.getString(i));
    } else {
        for (String title : columnTitles) {
            String type = getDetector().getColumnType(title, columnTitles);
            if (type != null) {
                columns.add(type);
            } else {
                if (columnsJson != null && columnsJson.length() == columnTitles.size()) {
                    columns.clear();
                    for (int i = 0; i < columnsJson.length(); i++) columns.add(columnsJson.getString(i));
                } else {
                    throw new IOException("unknown column title: " + title);
                }
                break;
            }
        }
    }
    if (data.optBoolean(PARAM_CLASS_IN_EXTRA_LINE) || data.optBoolean("class_in_extra_line")) {
        // backwards compatibility
        for (Element element : table.select("td.inline_header")) {
            String className = getClassName(element.text(), data);
            if (isValidClass(className, data)) {
                parseWithExtraLine(data, day, columns, element, className, null);
            }
        }
    } else if (data.optBoolean(PARAM_TEACHER_IN_EXTRA_LINE)) {
        for (Element element : table.select("td.inline_header")) {
            String teacherName = getClassName(element.text(), data);
            parseWithExtraLine(data, day, columns, element, null, teacherName);
        }
    } else {
        boolean hasType = false;
        for (String column : columns) {
            if (column.equals("type")) {
                hasType = true;
            }
        }
        int skipLines = 0;
        for (Element zeile : table.select("tr.list.odd:not(:has(td.inline_header)), " + "tr.list.even:not(:has(td.inline_header)), " + "tr:has(td[align=center]):gt(0)")) {
            if (skipLines > 0) {
                skipLines--;
                continue;
            }
            Substitution v = new Substitution();
            String klassen = defaultClass != null ? defaultClass : "";
            String course = null;
            int i = 0;
            for (Element spalte : zeile.select("td")) {
                String text = spalte.text();
                String type = columns.get(i);
                if (isEmpty(text) && !type.equals("type-entfall") && !type.equals("teacher")) {
                    i++;
                    continue;
                }
                int skipLinesForThisColumn = 0;
                Element nextLine = zeile.nextElementSibling();
                boolean continueSkippingLines = true;
                while (continueSkippingLines) {
                    if (nextLine != null && nextLine.children().size() == zeile.children().size()) {
                        Element columnInNextLine = nextLine.child(spalte.elementSiblingIndex());
                        if (columnInNextLine.text().replaceAll("\u00A0", "").trim().equals(nextLine.text().replaceAll("\u00A0", "").trim())) {
                            // Continued in the next line
                            text += " " + columnInNextLine.text();
                            skipLinesForThisColumn++;
                            nextLine = nextLine.nextElementSibling();
                        } else {
                            continueSkippingLines = false;
                        }
                    } else {
                        continueSkippingLines = false;
                    }
                }
                if (skipLinesForThisColumn > skipLines)
                    skipLines = skipLinesForThisColumn;
                switch(type) {
                    case "lesson":
                        v.setLesson(text);
                        break;
                    case "subject":
                        handleSubject(v, spalte);
                        if (course != null) {
                            v.setSubject((v.getSubject() != null ? v.getSubject() + " " : "") + course);
                            course = null;
                        }
                        break;
                    case "course":
                        if (v.getSubject() != null) {
                            v.setSubject(v.getSubject() + " " + text);
                        } else {
                            course = text;
                        }
                        break;
                    case "previousSubject":
                        v.setPreviousSubject(text);
                        break;
                    case "type":
                        v.setType(text);
                        v.setColor(colorProvider.getColor(text));
                        break;
                    case "type-entfall":
                        if (text.equals("x")) {
                            v.setType("Entfall");
                            v.setColor(colorProvider.getColor("Entfall"));
                        } else if (!hasType && v.getType() == null) {
                            v.setType("Vertretung");
                            v.setColor(colorProvider.getColor("Vertretung"));
                        }
                        break;
                    case "room":
                        handleRoom(v, spalte);
                        break;
                    case "previousRoom":
                        v.setPreviousRoom(text);
                        break;
                    case "desc":
                        v.setDesc(text);
                        break;
                    case "desc-type":
                        v.setDesc(text);
                        String recognizedType = recognizeType(text);
                        v.setType(recognizedType);
                        v.setColor(colorProvider.getColor(recognizedType));
                        break;
                    case "teacher":
                        if (text.equals("+")) {
                            v.setType("Eigenverantw. Arbeiten");
                            v.setColor(colorProvider.getColor(v.getType()));
                        } else if (!isEmpty(text)) {
                            handleTeacher(v, spalte, data);
                        }
                        break;
                    case "previousTeacher":
                        v.setPreviousTeachers(splitTeachers(text, data));
                        break;
                    case "substitutionFrom":
                        v.setSubstitutionFrom(text);
                        break;
                    case "teacherTo":
                        v.setTeacherTo(text);
                        break;
                    case "class":
                        klassen = getClassName(text, data);
                        break;
                    case "ignore":
                        break;
                    case // used by UntisSubstitutionParser
                    "date":
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown column type: " + type);
                }
                i++;
            }
            if (course != null) {
                v.setSubject(course);
            }
            if (v.getLesson() == null || v.getLesson().equals("")) {
                continue;
            }
            autoDetectType(data, zeile, v);
            handleClasses(data, v, klassen, allClasses);
            if (data.optBoolean(PARAM_MERGE_WITH_DIFFERENT_TYPE, false)) {
                boolean found = false;
                for (Substitution subst : day.getSubstitutions()) {
                    if (subst.equalsExcludingType(v)) {
                        found = true;
                        if (v.getType().equals("Vertretung")) {
                            subst.setType("Vertretung");
                            subst.setColor(colorProvider.getColor("Vertretung"));
                        }
                        break;
                    }
                }
                if (!found) {
                    day.addSubstitution(v);
                }
            } else {
                day.addSubstitution(v);
            }
        }
    }
}
Also used : Substitution(me.vertretungsplan.objects.Substitution) JSONArray(org.json.JSONArray) IOException(java.io.IOException) Elements(org.jsoup.select.Elements)

Example 8 with Substitution

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

the class UntisInfoParser method parseTimetableCell.

private static List<Substitution> parseTimetableCell(Element cell, String lesson, String klasse, JSONArray cellFormat, ColorProvider colorProvider) throws JSONException {
    List<Substitution> substitutions = new ArrayList<>();
    if (cell.text().trim().equals("")) {
        return substitutions;
    }
    final Elements rows = cell.select("table").first().select("tr");
    int cols = rows.get(0).select("td").size();
    int courseCount = cols / cellFormat.getJSONArray(0).length();
    for (int course = 0; course < courseCount; course++) {
        Substitution s = new Substitution();
        s.setLesson(lesson);
        final HashSet<String> classes = new HashSet<>();
        classes.add(klasse);
        s.setClasses(classes);
        boolean isChange = false;
        for (int row = 0; row < cellFormat.length() && row < rows.size(); row++) {
            JSONArray rowData = cellFormat.getJSONArray(row);
            Element tr = rows.get(row);
            for (int col = 0; col < rowData.length(); col++) {
                if (rowData.getString(col) == null)
                    continue;
                String type = rowData.getString(col);
                try {
                    Element td = tr.select("td").get(col + course * cellFormat.getJSONArray(0).length());
                    if (td.select("font[color=#FF0000]").size() > 0) {
                        isChange = true;
                    }
                    parseTimetableCellContent(s, type, td);
                } catch (IndexOutOfBoundsException e) {
                    if (course == 0)
                        throw e;
                }
            }
        }
        if (s.getSubject() == null && s.getTeacher() == null && s.getRoom() == null) {
            s.setType("Entfall");
        } else {
            s.setType("Vertretung");
        }
        s.setColor(colorProvider.getColor(s.getType()));
        if (isChange) {
            substitutions.add(s);
        }
    }
    return substitutions;
}
Also used : Substitution(me.vertretungsplan.objects.Substitution) Element(org.jsoup.nodes.Element) JSONArray(org.json.JSONArray) Elements(org.jsoup.select.Elements)

Example 9 with Substitution

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

the class SubstitutionTextUtils method getText.

public static String getText(SubstitutionDiff diff) {
    Substitution oldSubst = diff.getOldSubstitution();
    Substitution newSubst = diff.getNewSubstitution();
    String subjectAndTeacher = diff(subjectAndTeacher(oldSubst), subjectAndTeacher(newSubst));
    String room = diff(room(oldSubst), room(newSubst));
    String desc = diff(oldSubst.getDesc(), newSubst.getDesc());
    return formatOutput(subjectAndTeacher, room, desc);
}
Also used : Substitution(me.vertretungsplan.objects.Substitution)

Example 10 with Substitution

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

the class IndiwareDemoTest method verify.

private void verify(SubstitutionScheduleDay schedule) {
    assertEquals(new LocalDate(2016, 8, 22), schedule.getDate());
    assertEquals(new LocalDateTime(2016, 8, 19, 12, 50), schedule.getLastChange());
    assertEquals(2, schedule.getMessages().size());
    assertEquals("<b>Klassen mit Änderung:</b> bla", schedule.getMessages().get(0));
    assertEquals("Erste Zeile.<br>\nZweite Zeile", schedule.getMessages().get(1));
    assertEquals(1, schedule.getSubstitutions().size());
    Substitution subst = schedule.getSubstitutions().iterator().next();
    assertEquals(2, subst.getClasses().size());
    assertEquals("3", subst.getLesson());
    assertEquals("Bio", subst.getSubject());
    assertEquals("Sch", subst.getTeacher());
    assertEquals("1234", subst.getRoom());
    assertEquals("Mat", subst.getPreviousSubject());
    assertEquals("Mül", subst.getPreviousTeacher());
    assertEquals(null, subst.getDesc());
    assertEquals("Vertretung", subst.getType());
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) Substitution(me.vertretungsplan.objects.Substitution) LocalDate(org.joda.time.LocalDate)

Aggregations

Substitution (me.vertretungsplan.objects.Substitution)35 Test (org.junit.Test)15 SubstitutionScheduleDay (me.vertretungsplan.objects.SubstitutionScheduleDay)14 Element (org.jsoup.nodes.Element)11 LocalDate (org.joda.time.LocalDate)9 SubstitutionSchedule (me.vertretungsplan.objects.SubstitutionSchedule)8 LocalDateTime (org.joda.time.LocalDateTime)7 Elements (org.jsoup.select.Elements)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Document (org.jsoup.nodes.Document)4 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 HashSet (java.util.HashSet)1 SubstitutionDiff (me.vertretungsplan.objects.diff.SubstitutionDiff)1 NotNull (org.jetbrains.annotations.NotNull)1