org.bitbucket.rosseti.http.client.RequestDetailsPage Maven / Gradle / Ivy
package org.bitbucket.rosseti.http.client;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.text.MessageFormat;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import static org.bitbucket.javatek.require.ObjectRequires.requireNonNull;
import static org.bitbucket.javatek.require.StringRequires.requireNonEmpty;
import static org.bitbucket.rosseti.http.client.Constants.DATE_FORMAT;
/**
* Страница с подробностями по заявке
*/
final class RequestDetailsPage implements RequestDetails {
private final Element workplaceForm;
RequestDetailsPage(Document page) {
workplaceForm =
requireNonNull(
page.selectFirst(
"form#workplaceForm"
)
);
}
@Nonnull
@Override
public String type() {
Element row =
requireNonNull(
workplaceForm.selectFirst(
"tr:contains(Тип заявки)"
)
);
Element cell =
requireNonNull(
row.selectFirst(
"td:has(input))"
)
);
Element input =
requireNonNull(
cell.selectFirst(
"input[type='text']"
)
);
return
requireNonEmpty(
input.attr("value")
);
}
@Nonnull
@Override
public String status() {
Element input =
requireNonNull(
workplaceForm.selectFirst(
"input[data-p-label='Статус']"
)
);
return
requireNonEmpty(
input.attr("value")
);
}
@Nonnull
@Override
public LocalDate created() {
return readMandatoryDate(
"Дата поступления заявки"
);
}
@Nonnull
@Override
public LocalDate statusLastModified() {
return readMandatoryDate(
"Дата изменения статуса"
);
}
@Nonnull
@Override
public ConsumingDevice device() {
Element input =
requireNonNull(
workplaceForm.selectFirst(
"input[data-p-label='Кадастровый номер земельного участка (помещения, здания)']"
)
);
//noinspection Convert2Lambda
return new ConsumingDevice() {
@Override
public String cadastralNumber() {
return
requireNonEmpty(
input.attr("value")
);
}
};
}
@Nullable
@Override
public RequestExecution execution() {
Element agreementNumberInput =
workplaceForm.selectFirst(
"input[data-p-label='Номер договора']"
);
if (agreementNumberInput == null) {
return null;
}
return new RequestExecution() {
@Nonnull
@Override
public String agreementNumber() {
return requireNonEmpty(agreementNumberInput.attr("value"));
}
@Nonnull
@Override
public LocalDate agreementDate() {
return readMandatoryDate(
"Дата заключения договора ТП"
);
}
@Nullable
@Override
public LocalDate executionNotificationDate() {
return readOptionalDate(
"Дата уведомления о выполнении ТУ со стороны сетевой организации"
);
}
@Nullable
@Override
public LocalDate signOffPlanDate() {
return readOptionalDate(
"Плановая дата подписи акта ТУ со стороны заявителя"
);
}
@Nullable
@Override
public LocalDate executionDate() {
return readOptionalDate(
"Дата исполнения обязательств по договору ТП"
);
}
@Override
public String toString() {
return
MessageFormat.format(
"agreement {0} at {1} with executionNotificationDate {2}",
agreementNumber(), agreementDate(), executionNotificationDate()
);
}
};
}
@Nonnull
private LocalDate readMandatoryDate(String dataPLabel) {
Element input =
requireNonNull(
workplaceForm.selectFirst(
"input[data-p-label='" + dataPLabel + "']"
)
);
String date = requireNonEmpty(input.attr("value"));
try {
return LocalDate.parse(date, DATE_FORMAT);
}
catch (DateTimeParseException e) {
throw new RuntimeException(
MessageFormat.format("Не удалось прочитать дату {0}: {1}", dataPLabel, date),
e
);
}
}
@Nullable
private LocalDate readOptionalDate(String dataPLabel) {
Element input =
workplaceForm.selectFirst(
"input[data-p-label='" + dataPLabel + "']"
);
if (input != null) {
String date = input.attr("value").trim();
if (!date.isEmpty()) {
try {
return LocalDate.parse(date, DATE_FORMAT);
}
catch (DateTimeParseException e) {
throw new RuntimeException(
MessageFormat.format("Не удалось прочитать дату {0}: {1}", dataPLabel, date),
e
);
}
}
}
return null;
}
@Override
public String toString() {
return
MessageFormat.format(
"created {0} for device {1} with status {2}. updated {3}. execution: {4}",
created(), device().cadastralNumber(), status(), statusLastModified(), execution()
);
}
}