private void saveClasses(List<ClassScheduleDataHolder> dataHolderList) { for (ClassScheduleDataHolder dataHolder : dataHolderList) { UnisantaApplication.Log_i("Verificando horário de: " + dataHolder.subjectName); Subject subject = subjectRepository.findByOriginalIdAndSchoolYear( Long.parseLong(dataHolder.id), actualSchoolYear); if (subject != null) { subject.setNickName(dataHolder.subjectName.replace("- DP", "").trim()); subjectRepository.saveSubject(subject); Time start = new Time(dataHolder.startTime); Time end = new Time(dataHolder.endTime); int weekDay = getWeekDay(dataHolder.weekDay); ClassSchedule classSchedule = classesRepository.findBySubjectAndDateTime(subject, weekDay, start, end); // Não existe, preciso criar e salvar if (classSchedule == null) { classSchedule = ClassScheduleFactory.createClassSchedule( weekDay, dataHolder.classroom, subject, start, end, actualSchoolYear); subject.setDP(dataHolder.isDP); subjectRepository.saveSubject(subject); classesRepository.saveClassSchedule(classSchedule); } } else { UnisantaApplication.Log_e("Não encontrei a matéria de: " + dataHolder.subjectName); } } }
public void sync() throws SyncFailedException { try { String html = classesPage.getHtml(); classesPage.checkDataAvailable(html); int pos = SyncDataHelper.getIndexAndGoToEnd("Letivo:", html); int endPos = SyncDataHelper.getIndexOf("<", html, pos); actualSchoolYear = new SchoolYear(html.substring(pos, endPos).trim()); if (actualSchoolYear.isBefore(SchoolYear.getCurrent())) { throw new UnavailableDataException( "Você não tem aulas no semestre corrente", classesPage.getUrl(), classesPage.getPageName()); } pos = SyncDataHelper.getIndexAndGoToEnd("table id=\"horarios\"", html); endPos = SyncDataHelper.getIndexOf("</table>", html); html = html.substring(pos, endPos); List<ClassScheduleDataHolder> dataHolderList = new ArrayList<>(); endPos = 0; do { ClassScheduleDataHolder dataHolder = new ClassScheduleDataHolder(); // Id pos = SyncDataHelper.getIndexAndGoToEnd("data-id=\"", html, endPos); endPos = SyncDataHelper.getIndexOf("\"", html, pos); dataHolder.id = html.substring(pos, endPos); UnisantaApplication.Log_i("ID: " + dataHolder.id); // Dia da semana pos = SyncDataHelper.getIndexAndGoToEnd("strong>", html, endPos); endPos = SyncDataHelper.getIndexOf("<", html, pos); dataHolder.weekDay = html.substring(pos, endPos); UnisantaApplication.Log_i( "Dia da semana encontrado: " + dataHolder.weekDay + " | na posição " + pos + " até " + endPos); // Horário pos = SyncDataHelper.getIndexAndGoToEnd("<td class=\"colunaDuracaHorario", html, endPos); pos = SyncDataHelper.getIndexOf(">", html, pos); endPos = SyncDataHelper.getIndexOf("<", html, pos); String horario = html.substring(pos, endPos); int aux = horario.indexOf("-"); dataHolder.startTime = horario.substring(1, aux).trim(); dataHolder.endTime = horario.substring(aux + 1, horario.length()).trim(); UnisantaApplication.Log_i( "A aula começa às: " + dataHolder.startTime + " e acaba às " + dataHolder.endTime); // Matéria pos = SyncDataHelper.getIndexAndGoToEnd("alinhamentoEsquerda", html, endPos); pos = SyncDataHelper.getIndexOf(">", html, pos) + 3; endPos = SyncDataHelper.getIndexOf("<", html, pos); dataHolder.subjectName = SyncDataHelper.fixSpecialCharacters(html.substring(pos, endPos).trim()); dataHolder.isDP = dataHolder.subjectName.contains("- DP"); UnisantaApplication.Log_i("Matéria encontrada: " + dataHolder.subjectName); // Professor pos = SyncDataHelper.getIndexAndGoToEnd("alinhamentoEsquerda", html, endPos); pos = SyncDataHelper.getIndexOf(">", html, pos) + 1; endPos = SyncDataHelper.getIndexOf("<", html, pos); dataHolder.teacherName = SyncDataHelper.fixSpecialCharacters(html.substring(pos, endPos)); UnisantaApplication.Log_i("Professor encontrado: " + dataHolder.teacherName); // Sala pos = SyncDataHelper.getIndexAndGoToEnd("colunaSalaHorario", html, endPos); pos = SyncDataHelper.getIndexOf(">", html, pos) + 1; endPos = SyncDataHelper.getIndexOf("<", html, pos); dataHolder.classroom = html.substring(pos, endPos); UnisantaApplication.Log_i("Sala de aula encontrada: " + dataHolder.classroom); dataHolderList.add(dataHolder); } while (html.indexOf("<tr", endPos) > -1); saveClasses(dataHolderList); } catch (StringNotFoundException e) { throw new MissingTagException(classesPage.getUrl(), classesPage.getPageName(), e.getString()); } catch (UnavailableDataException e) { throw e; } catch (Exception e) { throw new SyncFailedException(e, classesPage.getUrl(), classesPage.getPageName()); } }