olexander_dan

Untitled

Mar 10th, 2020
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. package ua.alexd.excelUtils.imports;
  2.  
  3. import org.apache.poi.ss.usermodel.Cell;
  4. import org.apache.poi.ss.usermodel.DataFormatter;
  5. import org.apache.poi.ss.usermodel.Row;
  6. import org.apache.poi.ss.usermodel.WorkbookFactory;
  7. import org.jetbrains.annotations.NotNull;
  8. import org.springframework.stereotype.Service;
  9. import ua.alexd.domain.Basket;
  10. import ua.alexd.domain.Buying;
  11. import ua.alexd.domain.Laptop;
  12. import ua.alexd.repos.BasketRepo;
  13. import ua.alexd.repos.LaptopRepo;
  14.  
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19.  
  20. import static ua.alexd.excelUtils.imports.TableValidator.isValidTableStructure;
  21.  
  22. @Service
  23. public class BuyingExcelImporter extends Importer {
  24.     private BasketRepo basketRepo;
  25.     private LaptopRepo laptopRepo;
  26.  
  27.     public BuyingExcelImporter(BasketRepo basketRepo, LaptopRepo laptopRepo) {
  28.         this.basketRepo = basketRepo;
  29.         this.laptopRepo = laptopRepo;
  30.     }
  31.  
  32.     @NotNull
  33.     @Override
  34.     public List<Buying> importFile(String uploadedFilePath)
  35.             throws IOException, IllegalArgumentException {
  36.         var workbook = WorkbookFactory.create(new File(uploadedFilePath));
  37.         var buyingSheet = workbook.getSheetAt(0);
  38.  
  39.         var buyingTableFields = new String[]{"Id", "Загальна ціна", "Id кошика", "Час покупки",
  40.                 "Id ноутбуку", "Модель ноутбуку"};
  41.         if (isValidTableStructure(buyingSheet, buyingTableFields)) {
  42.             var dataFormatter = new DataFormatter();
  43.             var newBuyings = new ArrayList<Buying>();
  44.  
  45.             int totalPrice = 0;
  46.             var totalPriceColNum = 1;
  47.             Basket basket = null;
  48.             var basketIdColNum = 2;
  49.             Laptop laptop = null;
  50.             var laptopIdColNum = 4;
  51.  
  52.             for (Row row : buyingSheet) {
  53.                 if (row.getRowNum() != 0)
  54.                     for (Cell cell : row) {
  55.                         var cellValue = dataFormatter.formatCellValue(cell);
  56.                         if (cell.getColumnIndex() == totalPriceColNum)
  57.                             try {
  58.                                 totalPrice = Integer.parseInt(cellValue);
  59.                             } catch (NumberFormatException ignored) {
  60.                             }
  61.                         else if (cell.getColumnIndex() == basketIdColNum) {
  62.                             try {
  63.                                 var basketId = Integer.parseInt(cellValue);
  64.                                 if (basketRepo.findById(basketId).isPresent())
  65.                                     basket = basketRepo.findById(basketId).get();
  66.                             } catch (NumberFormatException ignored) {
  67.                             }
  68.                         } else if (cell.getColumnIndex() == laptopIdColNum) {
  69.                             try {
  70.                                 var laptopId = Integer.parseInt(cellValue);
  71.                                 if (laptopRepo.findById(laptopId).isPresent())
  72.                                     laptop = laptopRepo.findById(laptopId).get();
  73.                             } catch (NumberFormatException ignored) {
  74.                             }
  75.                         }
  76.                     }
  77.                 if (totalPrice >= 5000 && laptop != null && basket != null) {
  78.                     var newBuying = new Buying(totalPrice, laptop, basket);
  79.                     newBuyings.add(newBuying);
  80.  
  81.                     nullExtractedValues(basket, laptop);
  82.                 }
  83.             }
  84.             workbook.close();
  85.             return newBuyings;
  86.         } else
  87.             throw new IllegalArgumentException();
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment