Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. @Override
  2. public ReaderResult readProductImages(InputFiles inputFiles, ReaderResultsInterpreter interpreter) throws OosException {
  3. try {
  4. inputFiles.setFileFilter(Constants.PRODUCT_IMAGES_EXTENSION);
  5. return new Reader(inputFiles, OosProperties.INSTANCE.getProperties()) {
  6. @Override
  7. public ReaderResult readEntities(DataSource dataSource, ReaderResultsInterpreter interpreter) throws Exception {
  8. ReaderResult readerResult = new ReaderResult();
  9. try {
  10. connection = dataSource.getConnection();
  11. List<String> fileNames = inputFiles.getFileNameList();
  12. if (fileNames.isEmpty()) {
  13. return readerResult;
  14. }
  15. for (String filePath : fileNames) {
  16. ReaderResult.FileResult fileResult = new ReaderResult.FileResult();
  17. fileResult.setStartTime(System.currentTimeMillis());
  18.  
  19. String fileName = FileUtils.getFileName(filePath);
  20. log.info(String.format("Importing file %s...", fileName));
  21.  
  22. ZipFile zipFile = null;
  23. InputStream xlsStream = null;
  24. RepositoryHelper repositoryHelper = null;
  25. try {
  26. zipFile = new ZipFile(filePath);
  27. HashMap<String, ZipEntry> zipEntries = new HashMap<>();
  28. Enumeration<? extends ZipEntry> entries = zipFile.entries();
  29. while (entries.hasMoreElements()) {
  30. ZipEntry zipEntry = entries.nextElement();
  31. zipEntries.put(zipEntry.getName(), zipEntry);
  32. }
  33.  
  34. if (!zipEntries.containsKey("images.xls")) {
  35. String error = String.format("ZIP file %s contains no images.xls", fileName);
  36. log.error(error);
  37. fileResult.addError(new Exception(error));
  38. }
  39.  
  40. xlsStream = zipFile.getInputStream(zipEntries.get("images.xls"));
  41. repositoryHelper = new RepositoryHelper(repository);
  42. HSSFWorkbook workbook = new HSSFWorkbook(xlsStream);
  43. HSSFSheet sheet = workbook.getSheetAt(0);
  44. int rowIndex = 0;
  45. Row row = null;
  46. InputStream imageStream;
  47. HashMap<Long, Integer> imageIndexes = new HashMap<Long, Integer>();
  48. while ((row = sheet.getRow(rowIndex++)) != null) {
  49. imageStream = null;
  50. try {
  51. String productCode = StringUtils.trim(row.getCell(0, Row.CREATE_NULL_AS_BLANK).getStringCellValue());
  52. String imageFileName = StringUtils.trim(row.getCell(1, Row.CREATE_NULL_AS_BLANK).getStringCellValue());
  53.  
  54. if (StringUtils.isEmpty(productCode)) {
  55. throw new LineValidationReaderException(rowIndex, productCode + "|" + imageFileName, "Oos.error.wrong.line.format");
  56. }
  57.  
  58. Product product = getProductsFromCache(productCode, connection);
  59. if (product == null) {
  60. throw new NoDataReaderException(rowIndex, Product.class, productCode);
  61. }
  62.  
  63. if (!StringUtils.isEmpty(imageFileName) && !zipEntries.containsKey(imageFileName)) {
  64. throw new LineValidationReaderException(rowIndex, productCode + "|" + imageFileName, "Oos.error.product.image.not.found");
  65. }
  66. boolean firstTime = !imageIndexes.containsKey(product.getProductId());
  67. repositoryHelper.switchNode(Product.class, String.valueOf(product.getProductId()));
  68. if (firstTime) {
  69. for (int i=0; i<4; i++) {
  70. try {
  71. if (repositoryHelper.hasProperty("image-" + i)) {
  72. repositoryHelper.removeProperty("image-" + i);
  73. }
  74. if (repositoryHelper.hasProperty("image-" + i + "-name")) {
  75. repositoryHelper.removeProperty("image-" + i + "-name");
  76. }
  77. } catch (Exception e) {
  78. log.error("Error removing product images", e);
  79. }
  80. }
  81. }
  82. int imageIndex = firstTime ? 0 : imageIndexes.get(product.getProductId());
  83. if (!StringUtils.isEmpty(imageFileName)) {
  84. if (imageIndex < 4) {
  85. imageStream = zipFile.getInputStream(zipEntries.get(imageFileName));
  86. repositoryHelper.add("image-" + imageIndex, imageStream);
  87. repositoryHelper.add("image-" + imageIndex + "-name", imageFileName);
  88. imageIndex++;
  89. } else {
  90. log.warn(String.format("Image %s for product %s skipped due to limit of 4 images per product!", imageFileName, productCode));
  91. }
  92. }
  93. imageIndexes.put(product.getProductId(), imageIndex);
  94. } catch (ReaderException e) {
  95. log.error("Error in images.xls line {}: {}", rowIndex, e.toString());
  96. fileResult.addError(e);
  97. } catch (Exception e) {
  98. log.error("Error in images.xls line " + rowIndex, e);
  99. fileResult.addError(e);
  100. } finally {
  101. IOUtils.closeQuietly(imageStream);
  102. }
  103. }
  104. } catch (Exception e) {
  105. log.error("Error reading images.zip", e);
  106. fileResult.addError(e);
  107. } finally {
  108. IOUtils.closeQuietly(xlsStream);
  109. IOUtils.closeQuietly(repositoryHelper);
  110. IOUtils.closeQuietly(zipFile);
  111. }
  112.  
  113. fileResult.setEndTime(System.currentTimeMillis());
  114. readerResult.addFileResult(fileName, fileResult);
  115. logSingleFileImport(fileName, fileResult);
  116. }
  117. for (String filePath : fileNames) {
  118. String fileName = FileUtils.getFileName(filePath);
  119. interpreter.setFilterFileName(fileName);
  120. interpreter.interprete(readerResult);
  121. if (isRemoveFiles()) {
  122. removeFile(filePath);
  123. }
  124. }
  125. } catch (Exception e) {
  126. log.error(String.format("Exception while reading *%s file!", inputFiles.getFileFilter()),e);
  127. } finally {
  128. DBUtilities.close(connection);
  129. }
  130. return readerResult;
  131. }
  132.  
  133. @Override
  134. protected void parse(String line, int lineNumber) throws Exception {}
  135.  
  136. }.readEntities(dataSource, interpreter);
  137. } catch (Exception e) {
  138. log.error("Can't read product images!", e);
  139. throw new OosException("Can't read product images!", e);
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement