@Test public void testDoImageImport() throws Exception { ProductService productService = (ProductService) createContext().getBean("productService"); Product product = productService.getProductById(10000L, true); assertNotNull(product); for (ProductSku productSku : product.getSku()) { if (productSku.getCode().equals("SOBOT-BEER")) { assertNull(productSku.getAttributeByCode("IMAGE0")); assertNull(productSku.getAttributeByCode("IMAGE1")); } } 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("productSkuImageImportDomainObjectStrategy"); boolean rez = service.doImageImport( listener, "im-image1-file_SOBOT-BEER_a.jpeg", "SOBOT-BEER", "0", null); rez &= service.doImageImport( listener, "im-image2-file_SOBOT-BEER_b.jpeg", "SOBOT-BEER", "1", null); rez &= service.doImageImport( listener, "im-image-file_SOBOT-BEER_c.jpeg", "SOBOT-BEER", "2", null); assertTrue(rez); clearCache(); product = productService.getProductById(10000L); assertNotNull(product); for (ProductSku productSku : product.getSku()) { if (productSku.getCode().equals("SOBOT-BEER")) { assertNotNull(productSku.getAttributeByCode("IMAGE0")); assertNotNull(productSku.getAttributeByCode("IMAGE1")); assertNotNull(productSku.getAttributeByCode("IMAGE2")); assertEquals("im-image1-file_SOBOT-BEER_a.jpeg", productSku.getAttributeValueByCode("IMAGE0")); assertEquals("im-image2-file_SOBOT-BEER_b.jpeg", productSku.getAttributeValueByCode("IMAGE1")); assertEquals("im-image-file_SOBOT-BEER_c.jpeg", productSku.getAttributeValueByCode("IMAGE2")); } } mockery.assertIsSatisfied(); }
@Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final ProductSku productSku = productSkuService.findProductSkuBySkuCode(code); if (productSku == null) { statusListener.notifyWarning("product sku with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(productSku, ProductSku.class); final String attributeCode = AttributeNamesKeys.Product.PRODUCT_IMAGE_ATTR_NAME_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueProductSku imageAttributeValue = (AttrValueProductSku) productSku.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 = productSkuService.getGenericDao().getEntityFactory().getByIface(AttrValueProductSku.class); imageAttributeValue.setProductSku(productSku); imageAttributeValue.setAttributeCode(attribute.getCode()); productSku.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 sku {}", fileName, attributeCode, productSku.getCode()); try { productSkuService.update(productSku); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for product sku with code {} could not be added (db error).", e, fileName, productSku.getCode()); statusListener.count(SKIP_COUNTER); return false; } }
ProductSkuImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final ProductSku productSku = productSkuService.findProductSkuBySkuCode(code); if (productSku == null) { statusListener.notifyWarning("product sku with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(productSku, ProductSku.class); final String attributeCode = AttributeNamesKeys.Product.PRODUCT_IMAGE_ATTR_NAME_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueProductSku imageAttributeValue = (AttrValueProductSku) productSku.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 = productSkuService.getGenericDao().getEntityFactory().getByIface(AttrValueProductSku.class); imageAttributeValue.setProductSku(productSku); imageAttributeValue.setAttributeCode(attribute.getCode()); productSku.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 sku {}", fileName, attributeCode, productSku.getCode()); try { productSkuService.update(productSku); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for product sku with code {} could not be added (db error).", e, fileName, productSku.getCode()); statusListener.count(SKIP_COUNTER); return false; } } }
ProductSkuImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final ProductSku productSku = productSkuService.findProductSkuBySkuCode(code); if (productSku == null) { statusListener.notifyWarning("product sku with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(productSku, ProductSku.class); final String attributeCode = AttributeNamesKeys.Product.PRODUCT_IMAGE_ATTR_NAME_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueProductSku imageAttributeValue = (AttrValueProductSku) productSku.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 = productSkuService.getGenericDao().getEntityFactory().getByIface(AttrValueProductSku.class); imageAttributeValue.setProductSku(productSku); imageAttributeValue.setAttributeCode(attribute.getCode()); productSku.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 sku {}", fileName, attributeCode, productSku.getCode()); try { productSkuService.update(productSku); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for product sku with code {} could not be added (db error).", e, fileName, productSku.getCode()); statusListener.count(SKIP_COUNTER); return false; } } ProductSkuImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                         final ProductSkuService productSkuService,
                                                         final AttributeService attributeService); }
ProductSkuImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final ProductSku productSku = productSkuService.findProductSkuBySkuCode(code); if (productSku == null) { statusListener.notifyWarning("product sku with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(productSku, ProductSku.class); final String attributeCode = AttributeNamesKeys.Product.PRODUCT_IMAGE_ATTR_NAME_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueProductSku imageAttributeValue = (AttrValueProductSku) productSku.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 = productSkuService.getGenericDao().getEntityFactory().getByIface(AttrValueProductSku.class); imageAttributeValue.setProductSku(productSku); imageAttributeValue.setAttributeCode(attribute.getCode()); productSku.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 sku {}", fileName, attributeCode, productSku.getCode()); try { productSkuService.update(productSku); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for product sku with code {} could not be added (db error).", e, fileName, productSku.getCode()); statusListener.count(SKIP_COUNTER); return false; } } ProductSkuImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                         final ProductSkuService productSkuService,
                                                         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); }
ProductSkuImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final ProductSku productSku = productSkuService.findProductSkuBySkuCode(code); if (productSku == null) { statusListener.notifyWarning("product sku with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(productSku, ProductSku.class); final String attributeCode = AttributeNamesKeys.Product.PRODUCT_IMAGE_ATTR_NAME_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueProductSku imageAttributeValue = (AttrValueProductSku) productSku.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 = productSkuService.getGenericDao().getEntityFactory().getByIface(AttrValueProductSku.class); imageAttributeValue.setProductSku(productSku); imageAttributeValue.setAttributeCode(attribute.getCode()); productSku.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 sku {}", fileName, attributeCode, productSku.getCode()); try { productSkuService.update(productSku); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for product sku with code {} could not be added (db error).", e, fileName, productSku.getCode()); statusListener.count(SKIP_COUNTER); return false; } } ProductSkuImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                         final ProductSkuService productSkuService,
                                                         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);  }