remove magic numbers, dry code
This commit is contained in:
@@ -30,6 +30,18 @@ import static java.util.Objects.nonNull;
|
||||
public class DownloadHeadlessService {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(DownloadHeadlessService.class);
|
||||
private static final int COL_TYPE = 1;
|
||||
private static final int COL_NOTICE = 3;
|
||||
private static final int COL_BID_START = 6;
|
||||
private static final int COL_BID_END = 7;
|
||||
private static final int COL_TRADES_DATE = 9;
|
||||
private static final int COL_LOT_NUMBER = 16;
|
||||
private static final int COL_STATUS = 17;
|
||||
private static final int COL_INIT_MONEY = 24;
|
||||
private static final int COL_STEP = 25;
|
||||
private static final int COL_START_PRICE = 26;
|
||||
private static final int COL_FINAL_PRICE = 27;
|
||||
private static final int COL_FULL_INFO = 29;
|
||||
|
||||
@SneakyThrows
|
||||
public Map<String, Lot> downloadExcel(int retryCount, String link) {
|
||||
@@ -41,7 +53,8 @@ public class DownloadHeadlessService {
|
||||
connection.setRequestProperty("Accept-Language", "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7");
|
||||
connection.setConnectTimeout(5000);
|
||||
connection.setReadTimeout(10000);
|
||||
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
Map<String, Lot> data = new HashMap<>();
|
||||
Workbook wb = new XSSFWorkbook(connection.getInputStream());
|
||||
Sheet sheetWithData = wb.getSheetAt(0);
|
||||
@@ -62,12 +75,12 @@ public class DownloadHeadlessService {
|
||||
}
|
||||
return data;
|
||||
} else {
|
||||
logger.error("Error in line 99 ");
|
||||
logger.error("Wrong response code {} during call link {} ", responseCode, link);
|
||||
Thread.sleep(1000);
|
||||
return downloadExcel(retryCount + 1, link);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Error in line 103 " + e);
|
||||
logger.error("Error during processing link {} ", link, e);
|
||||
if (retryCount > 10) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
@@ -78,19 +91,19 @@ public class DownloadHeadlessService {
|
||||
|
||||
private Lot extractDataFromRow(Row row, String cadastral) {
|
||||
Lot lot = new Lot();
|
||||
lot.setBidStart(row.getCell(6).getStringCellValue());
|
||||
lot.setBidEnd(row.getCell(7).getStringCellValue());
|
||||
lot.setNotice(row.getCell(COL_NOTICE).getStringCellValue());
|
||||
lot.setBidStart(row.getCell(COL_BID_START).getStringCellValue());
|
||||
lot.setBidEnd(row.getCell(COL_BID_END).getStringCellValue());
|
||||
lot.setTradesDate(row.getCell(COL_TRADES_DATE).getStringCellValue());
|
||||
lot.setCadastral(cadastral);
|
||||
lot.setLotNumber((int) row.getCell(16).getNumericCellValue());
|
||||
lot.setFinalPrice(row.getCell(27).getStringCellValue());
|
||||
lot.setNotice(row.getCell(3).getStringCellValue());
|
||||
lot.setInitMoney(row.getCell(24).getStringCellValue());
|
||||
lot.setStep(row.getCell(25).getStringCellValue());
|
||||
lot.setStartPrice(row.getCell(26).getStringCellValue());
|
||||
lot.setStatus(row.getCell(17).getStringCellValue());
|
||||
lot.setTradesDate(row.getCell(9).getStringCellValue());
|
||||
lot.setFullInfo(row.getCell(29).getStringCellValue());
|
||||
lot.setType(row.getCell(1).getStringCellValue());
|
||||
lot.setLotNumber((int) row.getCell(COL_LOT_NUMBER).getNumericCellValue());
|
||||
lot.setStatus(row.getCell(COL_STATUS).getStringCellValue());
|
||||
lot.setInitMoney(row.getCell(COL_INIT_MONEY).getStringCellValue());
|
||||
lot.setStep(row.getCell(COL_STEP).getStringCellValue());
|
||||
lot.setStartPrice(row.getCell(COL_START_PRICE).getStringCellValue());
|
||||
lot.setFinalPrice(row.getCell(COL_FINAL_PRICE).getStringCellValue());
|
||||
lot.setFullInfo(row.getCell(COL_FULL_INFO).getStringCellValue());
|
||||
lot.setType(row.getCell(COL_TYPE).getStringCellValue());
|
||||
return lot;
|
||||
}
|
||||
|
||||
@@ -126,31 +139,6 @@ public class DownloadHeadlessService {
|
||||
return cadastrals;
|
||||
}
|
||||
|
||||
public void createNewDocument(TorgiEventRepository eventRepository, LocalDate now) {
|
||||
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
|
||||
XSSFSheet sheet = workbook.createSheet("sheet");
|
||||
setRowHead(sheet);
|
||||
logger.error("before write body");
|
||||
int i = 0;
|
||||
int currentRow = 1;
|
||||
while (true) {
|
||||
List<Lot> partition = eventRepository.findAllByOrderByCreatedAtDesc(PageRequest.of(i, 1000)).stream().map(Lot::new).toList();
|
||||
if (partition.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
currentRow = setRowBodyNew(sheet, currentRow, partition);
|
||||
i++;
|
||||
}
|
||||
//setRowWith(sheet);
|
||||
logger.error("before write to file");
|
||||
try (FileOutputStream fileOut = new FileOutputStream(getFileToSave(now))) {
|
||||
workbook.write(fileOut);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
logger.error("in createNewDocument " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void createNewDocument(TorgiEventRepository eventRepository, List<String> cadastrals, LocalDate now) {
|
||||
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
|
||||
XSSFSheet sheet = workbook.createSheet("sheet");
|
||||
@@ -160,8 +148,6 @@ public class DownloadHeadlessService {
|
||||
int currentRow = 1;
|
||||
setRowBodyNew(sheet, currentRow, data);
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
workbook.write(out);
|
||||
logger.error("before write to file");
|
||||
try (FileOutputStream fileOut = new FileOutputStream(getFileToSave(now))) {
|
||||
workbook.write(fileOut);
|
||||
|
||||
Reference in New Issue
Block a user