Автоматизация сбора данных. Результаты тестирования Внедрение системы
Скачать 5.77 Mb.
|
5.1 Особенности реализации системыСогласно проекту ПП, реализуем необходимые классы и структуры. Приведем набор реализованных классов в виде таблицы, см. табл.3
Табл.3 Приведем код некоторых классов и методов: public class AcmpParser extends HtmlParser { private static final int INDEX_DATE = 1; private static final int INDEX_TASK_NUMBER = 3; private static final int INDEX_LANG = 4; private static final int INDEX_STATUS = 5; private static HttpGetAcmp httpGetAcmp = new HttpGetAcmp(); /** * Конструктор, формирует лист с результатами {@link ResultItem} * * @param queryData */ public AcmpParser(QueryData queryData) { super(queryData, httpGetAcmp); addResultAll(); createResultItemList(); } private void createResultItemList() { if (table.size() != 0) { for (Element elem : table) { ResultItem resultItem = new ResultItem() .setTaskNum(getTaskNum(elem, INDEX_TASK_NUMBER)) .setStatus(getStatus(elem, INDEX_STATUS)) .setLang(getLanguage(elem, INDEX_LANG)) .setDate(getDate(elem)) .setSiteName(ACMP); addResultItemAll(resultItem); } } else { ResultItem resultItem = new ResultItem(); resultItem.setStatus("none"); addResultItemAll(resultItem); } } private Date getDate(Element elem) { try { return DateFormat.getInstance().parse(getTbByIndex(elem, INDEX_DATE).text()); } catch (ParseException e) { e.printStackTrace(); } return new Date(); } private void addResultAll() { Elements elements1 = document.getElementsByClass("gray"); Elements elements2 = document.getElementsByClass("white"); int page = 1; while (!elements1.isEmpty() || !elements2.isEmpty()) { String urlNextPage = urlSite + "&page=" + page; List tempList.addAll(elements1.subList(0, elements1.size())); tempList.addAll(elements2.subList(0, elements2.size())); for (Element elem : tempList) { if (isRequiredLang(elem, INDEX_LANG) || ControllerMain.language.isEmpty()) { table.add(elem); } } document = Jsoup.parse(ic.getHTML(urlNextPage)); elements1 = document.getElementsByClass("gray"); elements2 = document.getElementsByClass("white"); page++; } } } Обработка нажатия кнопки "Получить отчет" public void getReport(ActionEvent actionEvent) { language = field_lang.getText(); setCheckedDate(); errors = DataChecker.validData(); if (errors.isEmpty()) { UserList userList = new UserList(new ArrayList<>(userObservableList)); TaskList taskList = new TaskList(new ArrayList<>(taskObservableList)); QueryDataListBuilder queryDataListBuilder = new QueryDataListBuilder(userList, taskList); ResultMapper resultMapper = new ResultMapper(queryDataListBuilder); ReportBuilder reportBuilder = new ReportBuilder(resultMapper, taskList); String resultFile = reportBuilder.reportBuild(); if (Main.getFileResult() == null) { Main.showAndSave(resultFile); } else { Main.saveHtmlDocument(resultFile); } } else { controllerErrorMS.createErrorsMS(); } } Класс построения отчета public class ReportBuilder { private static final String TEMPLATE = "row"; private static final String POINTS = "points"; private static final String LEVEL = "level"; private static final String NAME = "name"; private static final String SOLVE_CLASS = "solve"; private static final String OLD_SOLVE_CLASS = "oldsolve"; private static final String NOT_SOLVE_CLASS = "notsolve"; private static final String PLUS = "+"; private static final String MINUS = "-"; private static final String POINTS_HEAD = "pointsHead"; private Element rowTemplate; private Element headerTemplate; private Element tdTemplate; private Document table; private String lastIdRow = "header"; private String lastIdColumn = "fio"; private HashMap private StoreBuilder sb; private ResultMapper resultMapper; ReportBuilder(ResultMapper resultMapper, TaskList taskList) { this.resultMapper = resultMapper; resultMap = resultMapper.getResultMap(); sb = new StoreBuilder(resultMapper); String html = Template.template; table = Jsoup.parse(html); if (sort.equals(SORT_BY_SOLVE)) { table.getElementById(POINTS_HEAD).remove(); } setThTemplate(); setTdTemplate(); setTaskColumns(taskList); setRowTemplate(taskList); createReport(); } private void createReport() { ArrayList String status, style; for (String name : names) { Set Element row = createNewResultRow(name); row.getElementById(NAME).html(name); row.getElementById(LEVEL).html(getPlace(name)); if (sort.equals(SORT_BY_POINTS)) { row.getElementById(POINTS).html(getPoints(name)); } for (String task : taskNums) { List status = isSolveOrOldSolve(name, task) ? PLUS : MINUS; if (status.equals(PLUS)) { style = (sb.isSolve(name, task)) ? SOLVE_CLASS : OLD_SOLVE_CLASS; } else { style = NOT_SOLVE_CLASS; } row.getElementById(task).addClass(style); row.getElementById(task).html(resultMapper.getFio_task_tryCount_map().get(name).get(task) + status); } table.getElementById(lastIdRow).after(row.toString()); lastIdRow = name; } } private String getPoints(String name) { return (sb.getFio_solvePoints_map().get(name) != null) ? String.valueOf(sb.getFio_solvePoints_map().get(name)) : "0"; } private String getPlace(String name) { return String.valueOf(sb.getFio_place().get(name)); } private boolean isSolveOrOldSolve(String name, String task) { return sb.isSolve(name, task) || sb.isOldSolve(name, task); } private void setTaskColumns(TaskList taskList) { String[] taskNum = taskList.getAllTasks(); Element newTh; for (String aTaskNum : taskNum) { newTh = createNewTh(aTaskNum); table.getElementById(lastIdColumn).after(newTh.toString()); lastIdColumn = aTaskNum; } } String reportBuild() { return table.toString(); } private Element createNewTemplateTd(String id) { Element newTd = tdTemplate.clone(); newTd.attr("id", id); return newTd; } private void setRowTemplate(TaskList taskList) { rowTemplate = table.getElementById(TEMPLATE); table.getElementById(TEMPLATE).remove(); if (sort.equals(SORT_BY_SOLVE)) { rowTemplate.getElementById(POINTS).remove(); } String lastTdID = "name"; String[] taskNums = taskList.getAllTasks(); for (String task : taskNums) { Element newTd = createNewTemplateTd(task); rowTemplate.getElementById(lastTdID).after(newTd.toString()); lastTdID = task; } } private void setThTemplate() { headerTemplate = table.getElementById("taskNum"); table.getElementById("taskNum").remove(); } private void setTdTemplate() { tdTemplate = table.getElementById("td"); table.getElementById("td").remove(); } private Element createNewTh(String taskNum) { Element newTh = headerTemplate.clone(); newTh.attr("id", taskNum); newTh.html(taskNum); return newTh; } private Element createNewResultRow(String id) { Element newRow = rowTemplate.clone(); newRow.attr("id", id); return newRow; } } |