@Test public void testDoImageImport() throws Exception { ProductService productService = (ProductService) createContext().getBean("productService"); Product product = productService.getProductById(9998L, true); assertNotNull(product); assertNull(product.getAttributeByCode("IMAGE0")); final JobStatusListener listener = mockery.mock(JobStatusListener.class, "listener"); mockery.checking(new Expectations() {{ allowing(listener).notifyMessage(with(any(String.class)), with(any(Object[].class))); allowing(listener).count(with(any(String.class))); }}); ImageImportDomainObjectStrategy service = (ImageImportDomainObjectStrategy) createContext().getBean("productImageImportDomainObjectStrategy"); boolean rez = service.doImageImport( listener, "im-image-file_BENDER-ua_a.jpeg", "BENDER-ua", "0", null); assertTrue(rez); clearCache(); product = productService.getProductById(9998L, true); assertNotNull(product); assertNotNull(product.getAttributeByCode("IMAGE0")); assertEquals("im-image-file_BENDER-ua_a.jpeg", product.getAttributeValueByCode("IMAGE0")); mockery.assertIsSatisfied(); }
@Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { Product product = null; final Long id = productService.findProductIdByCode(code); if (id != null) { product = productService.getProductById(id, true); } if (product == null) { final Product productBySku = productService.getProductBySkuCode(code); if (productBySku != null) { return false; } statusListener.notifyWarning("product with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(product, Product.class); final Product productWithAttrs = product; final String attributeCode = AttributeNamesKeys.Product.PRODUCT_IMAGE_ATTR_NAME_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueProduct imageAttributeValue = (AttrValueProduct) productWithAttrs.getAttributeByCode(attributeCode); if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.PRODUCT); Attribute attribute = null; for (final Attribute imageAttribute : imageAttributes) { if (attributeCode.equals(imageAttribute.getCode())) { attribute = imageAttribute; break; } } if (attribute == null) { statusListener.notifyWarning("attribute with code {} not found.", attributeCode); statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue = productService.getGenericDao().getEntityFactory().getByIface(AttrValueProduct.class); imageAttributeValue.setProduct(productWithAttrs); imageAttributeValue.setAttributeCode(attribute.getCode()); productWithAttrs.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to product {}", fileName, attributeCode, productWithAttrs.getCode()); try { productService.update(productWithAttrs); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for product with code {} could not be added (db error).", e, fileName, product.getCode()); statusListener.count(SKIP_COUNTER); return false; } }
ProductImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { Product product = null; final Long id = productService.findProductIdByCode(code); if (id != null) { product = productService.getProductById(id, true); } if (product == null) { final Product productBySku = productService.getProductBySkuCode(code); if (productBySku != null) { return false; } statusListener.notifyWarning("product with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(product, Product.class); final Product productWithAttrs = product; final String attributeCode = AttributeNamesKeys.Product.PRODUCT_IMAGE_ATTR_NAME_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueProduct imageAttributeValue = (AttrValueProduct) productWithAttrs.getAttributeByCode(attributeCode); if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.PRODUCT); Attribute attribute = null; for (final Attribute imageAttribute : imageAttributes) { if (attributeCode.equals(imageAttribute.getCode())) { attribute = imageAttribute; break; } } if (attribute == null) { statusListener.notifyWarning("attribute with code {} not found.", attributeCode); statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue = productService.getGenericDao().getEntityFactory().getByIface(AttrValueProduct.class); imageAttributeValue.setProduct(productWithAttrs); imageAttributeValue.setAttributeCode(attribute.getCode()); productWithAttrs.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to product {}", fileName, attributeCode, productWithAttrs.getCode()); try { productService.update(productWithAttrs); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for product with code {} could not be added (db error).", e, fileName, product.getCode()); statusListener.count(SKIP_COUNTER); return false; } } }
ProductImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { Product product = null; final Long id = productService.findProductIdByCode(code); if (id != null) { product = productService.getProductById(id, true); } if (product == null) { final Product productBySku = productService.getProductBySkuCode(code); if (productBySku != null) { return false; } statusListener.notifyWarning("product with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(product, Product.class); final Product productWithAttrs = product; final String attributeCode = AttributeNamesKeys.Product.PRODUCT_IMAGE_ATTR_NAME_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueProduct imageAttributeValue = (AttrValueProduct) productWithAttrs.getAttributeByCode(attributeCode); if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.PRODUCT); Attribute attribute = null; for (final Attribute imageAttribute : imageAttributes) { if (attributeCode.equals(imageAttribute.getCode())) { attribute = imageAttribute; break; } } if (attribute == null) { statusListener.notifyWarning("attribute with code {} not found.", attributeCode); statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue = productService.getGenericDao().getEntityFactory().getByIface(AttrValueProduct.class); imageAttributeValue.setProduct(productWithAttrs); imageAttributeValue.setAttributeCode(attribute.getCode()); productWithAttrs.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to product {}", fileName, attributeCode, productWithAttrs.getCode()); try { productService.update(productWithAttrs); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for product with code {} could not be added (db error).", e, fileName, product.getCode()); statusListener.count(SKIP_COUNTER); return false; } } ProductImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                      final ProductService productService,
                                                      final AttributeService attributeService); }
ProductImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { Product product = null; final Long id = productService.findProductIdByCode(code); if (id != null) { product = productService.getProductById(id, true); } if (product == null) { final Product productBySku = productService.getProductBySkuCode(code); if (productBySku != null) { return false; } statusListener.notifyWarning("product with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(product, Product.class); final Product productWithAttrs = product; final String attributeCode = AttributeNamesKeys.Product.PRODUCT_IMAGE_ATTR_NAME_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueProduct imageAttributeValue = (AttrValueProduct) productWithAttrs.getAttributeByCode(attributeCode); if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.PRODUCT); Attribute attribute = null; for (final Attribute imageAttribute : imageAttributes) { if (attributeCode.equals(imageAttribute.getCode())) { attribute = imageAttribute; break; } } if (attribute == null) { statusListener.notifyWarning("attribute with code {} not found.", attributeCode); statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue = productService.getGenericDao().getEntityFactory().getByIface(AttrValueProduct.class); imageAttributeValue.setProduct(productWithAttrs); imageAttributeValue.setAttributeCode(attribute.getCode()); productWithAttrs.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to product {}", fileName, attributeCode, productWithAttrs.getCode()); try { productService.update(productWithAttrs); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for product with code {} could not be added (db error).", e, fileName, product.getCode()); statusListener.count(SKIP_COUNTER); return false; } } ProductImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                      final ProductService productService,
                                                      final AttributeService attributeService); @Override boolean supports(final String uriPattern); @Override boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale); }
ProductImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { Product product = null; final Long id = productService.findProductIdByCode(code); if (id != null) { product = productService.getProductById(id, true); } if (product == null) { final Product productBySku = productService.getProductBySkuCode(code); if (productBySku != null) { return false; } statusListener.notifyWarning("product with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(product, Product.class); final Product productWithAttrs = product; final String attributeCode = AttributeNamesKeys.Product.PRODUCT_IMAGE_ATTR_NAME_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueProduct imageAttributeValue = (AttrValueProduct) productWithAttrs.getAttributeByCode(attributeCode); if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.PRODUCT); Attribute attribute = null; for (final Attribute imageAttribute : imageAttributes) { if (attributeCode.equals(imageAttribute.getCode())) { attribute = imageAttribute; break; } } if (attribute == null) { statusListener.notifyWarning("attribute with code {} not found.", attributeCode); statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue = productService.getGenericDao().getEntityFactory().getByIface(AttrValueProduct.class); imageAttributeValue.setProduct(productWithAttrs); imageAttributeValue.setAttributeCode(attribute.getCode()); productWithAttrs.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to product {}", fileName, attributeCode, productWithAttrs.getCode()); try { productService.update(productWithAttrs); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for product with code {} could not be added (db error).", e, fileName, product.getCode()); statusListener.count(SKIP_COUNTER); return false; } } ProductImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                      final ProductService productService,
                                                      final AttributeService attributeService); @Override boolean supports(final String uriPattern); @Override boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale);  }