@Test public void testDoImageImport() throws Exception { CategoryService categoryService = (CategoryService) createContext().getBean("categoryService"); Category category = categoryService.getById(101L); assertNotNull(category); assertNull(category.getAttributeByCode(AttributeNamesKeys.Category.CATEGORY_IMAGE)); 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("categoryImageImportDomainObjectStrategy"); boolean rez = service.doImageImport( listener, "im-image-file_101_a.jpeg", "101", "0", null); assertTrue(rez); clearCache(); category = categoryService.getById(101L); assertNotNull(category); assertNotNull(category.getAttributeByCode(AttributeNamesKeys.Category.CATEGORY_IMAGE)); assertEquals("im-image-file_101_a.jpeg", category.getAttributeValueByCode(AttributeNamesKeys.Category.CATEGORY_IMAGE)); mockery.assertIsSatisfied(); }
@Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final Category category = categoryService.findCategoryIdBySeoUriOrGuid(code); if (category == null) { statusListener.notifyWarning("category with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(category, Category.class); final String attributeCode = AttributeNamesKeys.Category.CATEGORY_IMAGE_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueCategory imageAttributeValue = (AttrValueCategory) category.getAttributeByCode(attributeCode); if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.CATEGORY); 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 = categoryService.getGenericDao().getEntityFactory().getByIface(AttrValueCategory.class); imageAttributeValue.setCategory(category); imageAttributeValue.setAttributeCode(attribute.getCode()); category.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to category {}", fileName, attributeCode, category.getName()); try { categoryService.update(category); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for category with code {} could not be added (db error).", e, fileName, category.getGuid()); statusListener.count(SKIP_COUNTER); return false; } }
CategoryImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final Category category = categoryService.findCategoryIdBySeoUriOrGuid(code); if (category == null) { statusListener.notifyWarning("category with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(category, Category.class); final String attributeCode = AttributeNamesKeys.Category.CATEGORY_IMAGE_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueCategory imageAttributeValue = (AttrValueCategory) category.getAttributeByCode(attributeCode); if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.CATEGORY); 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 = categoryService.getGenericDao().getEntityFactory().getByIface(AttrValueCategory.class); imageAttributeValue.setCategory(category); imageAttributeValue.setAttributeCode(attribute.getCode()); category.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to category {}", fileName, attributeCode, category.getName()); try { categoryService.update(category); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for category with code {} could not be added (db error).", e, fileName, category.getGuid()); statusListener.count(SKIP_COUNTER); return false; } } }
CategoryImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final Category category = categoryService.findCategoryIdBySeoUriOrGuid(code); if (category == null) { statusListener.notifyWarning("category with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(category, Category.class); final String attributeCode = AttributeNamesKeys.Category.CATEGORY_IMAGE_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueCategory imageAttributeValue = (AttrValueCategory) category.getAttributeByCode(attributeCode); if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.CATEGORY); 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 = categoryService.getGenericDao().getEntityFactory().getByIface(AttrValueCategory.class); imageAttributeValue.setCategory(category); imageAttributeValue.setAttributeCode(attribute.getCode()); category.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to category {}", fileName, attributeCode, category.getName()); try { categoryService.update(category); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for category with code {} could not be added (db error).", e, fileName, category.getGuid()); statusListener.count(SKIP_COUNTER); return false; } } CategoryImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                       final CategoryService categoryService,
                                                       final AttributeService attributeService); }
CategoryImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final Category category = categoryService.findCategoryIdBySeoUriOrGuid(code); if (category == null) { statusListener.notifyWarning("category with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(category, Category.class); final String attributeCode = AttributeNamesKeys.Category.CATEGORY_IMAGE_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueCategory imageAttributeValue = (AttrValueCategory) category.getAttributeByCode(attributeCode); if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.CATEGORY); 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 = categoryService.getGenericDao().getEntityFactory().getByIface(AttrValueCategory.class); imageAttributeValue.setCategory(category); imageAttributeValue.setAttributeCode(attribute.getCode()); category.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to category {}", fileName, attributeCode, category.getName()); try { categoryService.update(category); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for category with code {} could not be added (db error).", e, fileName, category.getGuid()); statusListener.count(SKIP_COUNTER); return false; } } CategoryImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                       final CategoryService categoryService,
                                                       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); }
CategoryImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final Category category = categoryService.findCategoryIdBySeoUriOrGuid(code); if (category == null) { statusListener.notifyWarning("category with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(category, Category.class); final String attributeCode = AttributeNamesKeys.Category.CATEGORY_IMAGE_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueCategory imageAttributeValue = (AttrValueCategory) category.getAttributeByCode(attributeCode); if (imageAttributeValue == null) { final List<Attribute> imageAttributes = attributeService.getAvailableImageAttributesByGroupCode(AttributeGroupNames.CATEGORY); 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 = categoryService.getGenericDao().getEntityFactory().getByIface(AttrValueCategory.class); imageAttributeValue.setCategory(category); imageAttributeValue.setAttributeCode(attribute.getCode()); category.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to category {}", fileName, attributeCode, category.getName()); try { categoryService.update(category); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for category with code {} could not be added (db error).", e, fileName, category.getGuid()); statusListener.count(SKIP_COUNTER); return false; } } CategoryImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                       final CategoryService categoryService,
                                                       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);  }