@Test public void testDoImageImport() throws Exception { BrandService brandService = (BrandService) createContext().getBean("brandService"); Brand brand = brandService.findByNameOrGuid("FutureRobots"); assertNotNull(brand); final Collection<AttrValueBrand> attributesBefore = brand.getAttributes(); if (attributesBefore != null) { for (AttrValueBrand attrValue : attributesBefore) { if (AttributeNamesKeys.Brand.BRAND_IMAGE.equals(attrValue.getAttributeCode())) { fail("Has image - it should not"); break; } } } 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("brandImageImportDomainObjectStrategy"); boolean rez = service.doImageImport( listener, "im-image-file_FutureRobots_a.jpeg", "FutureRobots", "0", null); assertTrue(rez); clearCache(); brand = brandService.findByNameOrGuid("FutureRobots"); assertNotNull(brand); AttrValueBrand saved = null; final Collection<AttrValueBrand> attributesAfter = brand.getAttributes(); if (attributesAfter != null) { for (AttrValueBrand attrValue : attributesAfter) { if (AttributeNamesKeys.Brand.BRAND_IMAGE.equals(attrValue.getAttributeCode())) { saved = attrValue; break; } } } assertNotNull(saved); assertEquals("im-image-file_FutureRobots_a.jpeg", saved.getVal()); mockery.assertIsSatisfied(); }
@Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final Brand brand = brandService.findByNameOrGuid(code); if (brand == null) { statusListener.notifyWarning("brand with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(brand, Brand.class); final String attributeCode = AttributeNamesKeys.Brand.BRAND_IMAGE_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueBrand imageAttributeValue = null; final Collection<AttrValueBrand> attributes = brand.getAttributes(); if (attributes != null) { for (AttrValueBrand attrValue : attributes) { if (attributeCode.equals(attrValue.getAttributeCode())) { imageAttributeValue = attrValue; break; } } } if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.BRAND); 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 = brandService.getGenericDao().getEntityFactory().getByIface(AttrValueBrand.class); imageAttributeValue.setBrand(brand); imageAttributeValue.setAttributeCode(attribute.getCode()); brand.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to brand {}", fileName, attributeCode, brand.getName()); try { brandService.update(brand); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for brand with name {} could not be added (db error).", e, fileName, brand.getName()); statusListener.count(SKIP_COUNTER); return false; } }
BrandImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final Brand brand = brandService.findByNameOrGuid(code); if (brand == null) { statusListener.notifyWarning("brand with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(brand, Brand.class); final String attributeCode = AttributeNamesKeys.Brand.BRAND_IMAGE_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueBrand imageAttributeValue = null; final Collection<AttrValueBrand> attributes = brand.getAttributes(); if (attributes != null) { for (AttrValueBrand attrValue : attributes) { if (attributeCode.equals(attrValue.getAttributeCode())) { imageAttributeValue = attrValue; break; } } } if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.BRAND); 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 = brandService.getGenericDao().getEntityFactory().getByIface(AttrValueBrand.class); imageAttributeValue.setBrand(brand); imageAttributeValue.setAttributeCode(attribute.getCode()); brand.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to brand {}", fileName, attributeCode, brand.getName()); try { brandService.update(brand); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for brand with name {} could not be added (db error).", e, fileName, brand.getName()); statusListener.count(SKIP_COUNTER); return false; } } }
BrandImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final Brand brand = brandService.findByNameOrGuid(code); if (brand == null) { statusListener.notifyWarning("brand with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(brand, Brand.class); final String attributeCode = AttributeNamesKeys.Brand.BRAND_IMAGE_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueBrand imageAttributeValue = null; final Collection<AttrValueBrand> attributes = brand.getAttributes(); if (attributes != null) { for (AttrValueBrand attrValue : attributes) { if (attributeCode.equals(attrValue.getAttributeCode())) { imageAttributeValue = attrValue; break; } } } if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.BRAND); 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 = brandService.getGenericDao().getEntityFactory().getByIface(AttrValueBrand.class); imageAttributeValue.setBrand(brand); imageAttributeValue.setAttributeCode(attribute.getCode()); brand.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to brand {}", fileName, attributeCode, brand.getName()); try { brandService.update(brand); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for brand with name {} could not be added (db error).", e, fileName, brand.getName()); statusListener.count(SKIP_COUNTER); return false; } } BrandImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                    final BrandService brandService,
                                                    final AttributeService attributeService); }
BrandImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final Brand brand = brandService.findByNameOrGuid(code); if (brand == null) { statusListener.notifyWarning("brand with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(brand, Brand.class); final String attributeCode = AttributeNamesKeys.Brand.BRAND_IMAGE_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueBrand imageAttributeValue = null; final Collection<AttrValueBrand> attributes = brand.getAttributes(); if (attributes != null) { for (AttrValueBrand attrValue : attributes) { if (attributeCode.equals(attrValue.getAttributeCode())) { imageAttributeValue = attrValue; break; } } } if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.BRAND); 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 = brandService.getGenericDao().getEntityFactory().getByIface(AttrValueBrand.class); imageAttributeValue.setBrand(brand); imageAttributeValue.setAttributeCode(attribute.getCode()); brand.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to brand {}", fileName, attributeCode, brand.getName()); try { brandService.update(brand); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for brand with name {} could not be added (db error).", e, fileName, brand.getName()); statusListener.count(SKIP_COUNTER); return false; } } BrandImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                    final BrandService brandService,
                                                    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); }
BrandImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final Brand brand = brandService.findByNameOrGuid(code); if (brand == null) { statusListener.notifyWarning("brand with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(brand, Brand.class); final String attributeCode = AttributeNamesKeys.Brand.BRAND_IMAGE_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueBrand imageAttributeValue = null; final Collection<AttrValueBrand> attributes = brand.getAttributes(); if (attributes != null) { for (AttrValueBrand attrValue : attributes) { if (attributeCode.equals(attrValue.getAttributeCode())) { imageAttributeValue = attrValue; break; } } } if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.BRAND); 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 = brandService.getGenericDao().getEntityFactory().getByIface(AttrValueBrand.class); imageAttributeValue.setBrand(brand); imageAttributeValue.setAttributeCode(attribute.getCode()); brand.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to brand {}", fileName, attributeCode, brand.getName()); try { brandService.update(brand); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for brand with name {} could not be added (db error).", e, fileName, brand.getName()); statusListener.count(SKIP_COUNTER); return false; } } BrandImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                    final BrandService brandService,
                                                    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);  }