Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public ReaderResult readProductImages(InputFiles inputFiles, ReaderResultsInterpreter interpreter) throws OosException {
- try {
- inputFiles.setFileFilter(Constants.PRODUCT_IMAGES_EXTENSION);
- return new Reader(inputFiles, OosProperties.INSTANCE.getProperties()) {
- @Override
- public ReaderResult readEntities(DataSource dataSource, ReaderResultsInterpreter interpreter) throws Exception {
- ReaderResult readerResult = new ReaderResult();
- try {
- connection = dataSource.getConnection();
- List<String> fileNames = inputFiles.getFileNameList();
- if (fileNames.isEmpty()) {
- return readerResult;
- }
- for (String filePath : fileNames) {
- ReaderResult.FileResult fileResult = new ReaderResult.FileResult();
- fileResult.setStartTime(System.currentTimeMillis());
- String fileName = FileUtils.getFileName(filePath);
- log.info(String.format("Importing file %s...", fileName));
- ZipFile zipFile = null;
- InputStream xlsStream = null;
- RepositoryHelper repositoryHelper = null;
- try {
- zipFile = new ZipFile(filePath);
- HashMap<String, ZipEntry> zipEntries = new HashMap<>();
- Enumeration<? extends ZipEntry> entries = zipFile.entries();
- while (entries.hasMoreElements()) {
- ZipEntry zipEntry = entries.nextElement();
- zipEntries.put(zipEntry.getName(), zipEntry);
- }
- if (!zipEntries.containsKey("images.xls")) {
- String error = String.format("ZIP file %s contains no images.xls", fileName);
- log.error(error);
- fileResult.addError(new Exception(error));
- }
- xlsStream = zipFile.getInputStream(zipEntries.get("images.xls"));
- repositoryHelper = new RepositoryHelper(repository);
- HSSFWorkbook workbook = new HSSFWorkbook(xlsStream);
- HSSFSheet sheet = workbook.getSheetAt(0);
- int rowIndex = 0;
- Row row = null;
- InputStream imageStream;
- HashMap<Long, Integer> imageIndexes = new HashMap<Long, Integer>();
- while ((row = sheet.getRow(rowIndex++)) != null) {
- imageStream = null;
- try {
- String productCode = StringUtils.trim(row.getCell(0, Row.CREATE_NULL_AS_BLANK).getStringCellValue());
- String imageFileName = StringUtils.trim(row.getCell(1, Row.CREATE_NULL_AS_BLANK).getStringCellValue());
- if (StringUtils.isEmpty(productCode)) {
- throw new LineValidationReaderException(rowIndex, productCode + "|" + imageFileName, "Oos.error.wrong.line.format");
- }
- Product product = getProductsFromCache(productCode, connection);
- if (product == null) {
- throw new NoDataReaderException(rowIndex, Product.class, productCode);
- }
- if (!StringUtils.isEmpty(imageFileName) && !zipEntries.containsKey(imageFileName)) {
- throw new LineValidationReaderException(rowIndex, productCode + "|" + imageFileName, "Oos.error.product.image.not.found");
- }
- boolean firstTime = !imageIndexes.containsKey(product.getProductId());
- repositoryHelper.switchNode(Product.class, String.valueOf(product.getProductId()));
- if (firstTime) {
- for (int i=0; i<4; i++) {
- try {
- if (repositoryHelper.hasProperty("image-" + i)) {
- repositoryHelper.removeProperty("image-" + i);
- }
- if (repositoryHelper.hasProperty("image-" + i + "-name")) {
- repositoryHelper.removeProperty("image-" + i + "-name");
- }
- } catch (Exception e) {
- log.error("Error removing product images", e);
- }
- }
- }
- int imageIndex = firstTime ? 0 : imageIndexes.get(product.getProductId());
- if (!StringUtils.isEmpty(imageFileName)) {
- if (imageIndex < 4) {
- imageStream = zipFile.getInputStream(zipEntries.get(imageFileName));
- repositoryHelper.add("image-" + imageIndex, imageStream);
- repositoryHelper.add("image-" + imageIndex + "-name", imageFileName);
- imageIndex++;
- } else {
- log.warn(String.format("Image %s for product %s skipped due to limit of 4 images per product!", imageFileName, productCode));
- }
- }
- imageIndexes.put(product.getProductId(), imageIndex);
- } catch (ReaderException e) {
- log.error("Error in images.xls line {}: {}", rowIndex, e.toString());
- fileResult.addError(e);
- } catch (Exception e) {
- log.error("Error in images.xls line " + rowIndex, e);
- fileResult.addError(e);
- } finally {
- IOUtils.closeQuietly(imageStream);
- }
- }
- } catch (Exception e) {
- log.error("Error reading images.zip", e);
- fileResult.addError(e);
- } finally {
- IOUtils.closeQuietly(xlsStream);
- IOUtils.closeQuietly(repositoryHelper);
- IOUtils.closeQuietly(zipFile);
- }
- fileResult.setEndTime(System.currentTimeMillis());
- readerResult.addFileResult(fileName, fileResult);
- logSingleFileImport(fileName, fileResult);
- }
- for (String filePath : fileNames) {
- String fileName = FileUtils.getFileName(filePath);
- interpreter.setFilterFileName(fileName);
- interpreter.interprete(readerResult);
- if (isRemoveFiles()) {
- removeFile(filePath);
- }
- }
- } catch (Exception e) {
- log.error(String.format("Exception while reading *%s file!", inputFiles.getFileFilter()),e);
- } finally {
- DBUtilities.close(connection);
- }
- return readerResult;
- }
- @Override
- protected void parse(String line, int lineNumber) throws Exception {}
- }.readEntities(dataSource, interpreter);
- } catch (Exception e) {
- log.error("Can't read product images!", e);
- throw new OosException("Can't read product images!", e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement