ContentImageImportDomainObjectStrategyImpl extends AbstractImageImportDomainObjectStrategyImpl implements ImageImportDomainObjectStrategy { @Override public boolean doImageImport(final JobStatusListener statusListener, final String fileName, final String code, final String suffix, final String locale) { final Content content = contentService.findContentIdBySeoUriOrGuid(code); if (content == null) { statusListener.notifyWarning("content with code {} not found.", code); statusListener.count(SKIP_COUNTER); return false; } validateAccessBeforeUpdate(content, Content.class); final String attributeCode = AttributeNamesKeys.Category.CATEGORY_IMAGE_PREFIX + suffix + (StringUtils.isNotEmpty(locale) ? "_" + locale : ""); AttrValueContent imageAttributeValue = (AttrValueContent) content.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 = contentService.getGenericDao().getEntityFactory().getByIface(AttrValueContent.class); imageAttributeValue.setContent(content); imageAttributeValue.setAttributeCode(attribute.getCode()); content.getAttributes().add(imageAttributeValue); } else if (isInsertOnly()) { statusListener.count(SKIP_COUNTER); return false; } imageAttributeValue.setVal(fileName); imageAttributeValue.setIndexedVal(fileName); statusListener.notifyMessage("file {} attached as {} to content {}", fileName, attributeCode, content.getName()); try { contentService.update(content); statusListener.count(MERGE_COUNTER); return true; } catch (DataIntegrityViolationException e) { statusListener.notifyError("image {} for content with code {} could not be added (db error).", e, fileName, content.getGuid()); statusListener.count(SKIP_COUNTER); return false; } } ContentImageImportDomainObjectStrategyImpl(final FederationFacade federationFacade,
                                                      final ContentService contentService,
                                                      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);  }
@Test public void testDoImageImport() throws Exception { ContentService contentService = (ContentService) createContext().getBean("contentServiceCMS3"); Content content = contentService.getById(10106L); assertNotNull(content); assertNull(content.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("contentImageImportDomainObjectStrategy"); boolean rez = service.doImageImport( listener, "im-image-file_SHOIP2-001_a.jpeg", "SHOIP2-001", "0", null); assertTrue(rez); clearCache(); content = contentService.getById(10106L); assertNotNull(content); assertNotNull(content.getAttributeByCode(AttributeNamesKeys.Category.CATEGORY_IMAGE)); assertEquals("im-image-file_SHOIP2-001_a.jpeg", content.getAttributeValueByCode(AttributeNamesKeys.Category.CATEGORY_IMAGE)); mockery.assertIsSatisfied(); }