diff --git a/zanata-war/src/main/java/org/zanata/service/impl/TranslationStateCacheImpl.java b/zanata-war/src/main/java/org/zanata/service/impl/TranslationStateCacheImpl.java index 89ef153701..9f8fab44cc 100644 --- a/zanata-war/src/main/java/org/zanata/service/impl/TranslationStateCacheImpl.java +++ b/zanata-war/src/main/java/org/zanata/service/impl/TranslationStateCacheImpl.java @@ -35,7 +35,6 @@ import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.Create; import org.jboss.seam.annotations.Destroy; -import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Observer; import org.jboss.seam.annotations.Scope; @@ -43,14 +42,12 @@ import org.zanata.cache.EhcacheWrapper; import org.zanata.common.LocaleId; import org.zanata.dao.DocumentDAO; -import org.zanata.dao.TextFlowDAO; import org.zanata.dao.TextFlowTargetDAO; import org.zanata.events.TextFlowTargetStateEvent; import org.zanata.model.HDocument; import org.zanata.model.HTextFlowTarget; import org.zanata.service.TranslationStateCache; import org.zanata.service.ValidationFactoryProvider; -import org.zanata.service.ValidationService; import org.zanata.webtrans.shared.model.DocumentId; import org.zanata.webtrans.shared.model.DocumentStatus; import org.zanata.webtrans.shared.model.ValidationAction; @@ -65,6 +62,7 @@ * href="mailto:camunoz@redhat.com">camunoz@redhat.com */ @Name("translationStateCacheImpl") +// TODO split into APPLICATION and STATELESS beans @Scope(ScopeType.APPLICATION) public class TranslationStateCacheImpl implements TranslationStateCache { private static final String BASE = TranslationStateCacheImpl.class @@ -74,18 +72,6 @@ public class TranslationStateCacheImpl implements TranslationStateCache { private static final String TFT_VALIDATION_CACHE_NAME = BASE + ".targetValidationCache"; - @In - private TextFlowDAO textFlowDAO; - - @In - private DocumentDAO documentDAO; - - @In - private TextFlowTargetDAO textFlowTargetDAO; - - @In - private ValidationService validationServiceImpl; - private CacheManager cacheManager; private CacheWrapper docStatusCache; private CacheWrapper> targetValidationCache; @@ -100,14 +86,10 @@ public TranslationStateCacheImpl() { public TranslationStateCacheImpl( CacheLoader docStatsLoader, - CacheLoader> targetValidationLoader, - TextFlowTargetDAO textFlowTargetDAO, - ValidationService validationServiceImpl) { + CacheLoader> targetValidationLoader) { // constructor for testing this.docStatusLoader = docStatsLoader; this.targetValidationLoader = targetValidationLoader; - this.textFlowTargetDAO = textFlowTargetDAO; - this.validationServiceImpl = validationServiceImpl; } @Create @@ -173,30 +155,45 @@ private void updateDocStatusCache(Long documentId, LocaleId localeId, DocumentStatus documentStatus = docStatusCache.get(new TranslatedDocumentKey(documentId, localeId)); + TextFlowTargetDAO textFlowTargetDAO = getTextFlowTargetDAO(); HTextFlowTarget target = textFlowTargetDAO.findById(updatedTargetId, false); - updateDocumentStatus(documentStatus, documentId, localeId, target); + updateDocumentStatus(getDocumentDAO(), documentStatus, documentId, localeId, target); + } + + DocumentDAO getDocumentDAO() { + return (DocumentDAO) Component.getInstance(DocumentDAO.class); + } + + TextFlowTargetDAO getTextFlowTargetDAO() { + TextFlowTargetDAO textFlowTargetDAO = (TextFlowTargetDAO) Component.getInstance(TextFlowTargetDAO.class); + return textFlowTargetDAO; } private void invalidateTargetValidationCache(Long textFlowTargetId) { targetValidationCache.remove(textFlowTargetId); } - private final class HTextFlowTargetIdLoader extends + private static class HTextFlowTargetIdLoader extends CacheLoader { + DocumentDAO getDocumentDAO() { + return (DocumentDAO) Component.getInstance(DocumentDAO.class); + } + @Override public DocumentStatus load(TranslatedDocumentKey key) throws Exception { + DocumentDAO documentDAO = getDocumentDAO(); HTextFlowTarget target = documentDAO.getLastTranslatedTarget(key.getDocumentId(), key.getLocaleId()); DocumentStatus documentStatus = new DocumentStatus(); - return updateDocumentStatus(documentStatus, key.getDocumentId(), + return updateDocumentStatus(documentDAO, documentStatus, key.getDocumentId(), key.getLocaleId(), target); } } - private final class HTextFlowTargetValidationLoader extends + private static class HTextFlowTargetValidationLoader extends CacheLoader> { @Override public Map load(Long key) throws Exception { @@ -206,6 +203,7 @@ public Map load(Long key) throws Exception { private Boolean loadTargetValidation(Long textFlowTargetId, ValidationId validationId) { + TextFlowTargetDAO textFlowTargetDAO = getTextFlowTargetDAO(); HTextFlowTarget tft = textFlowTargetDAO.findById(textFlowTargetId, false); @@ -221,8 +219,9 @@ private Boolean loadTargetValidation(Long textFlowTargetId, return null; } - private DocumentStatus updateDocumentStatus(DocumentStatus documentStatus, - Long documentId, LocaleId localeId, HTextFlowTarget target) { + private static DocumentStatus updateDocumentStatus(DocumentDAO documentDAO, + DocumentStatus documentStatus, Long documentId, LocaleId localeId, + HTextFlowTarget target) { Date lastTranslatedDate = null; String lastTranslatedBy = ""; diff --git a/zanata-war/src/test/java/org/zanata/service/impl/TranslationStateCacheImplTest.java b/zanata-war/src/test/java/org/zanata/service/impl/TranslationStateCacheImplTest.java index 45bfd4aff4..9e6bea1466 100644 --- a/zanata-war/src/test/java/org/zanata/service/impl/TranslationStateCacheImplTest.java +++ b/zanata-war/src/test/java/org/zanata/service/impl/TranslationStateCacheImplTest.java @@ -53,16 +53,18 @@ public class TranslationStateCacheImplTest { private TextFlowTargetDAO textFlowTargetDAO; @Mock private CacheLoader> targetValidationLoader; - @Mock - private ValidationService validationServiceImpl; @BeforeMethod public void beforeMethod() { MockitoAnnotations.initMocks(this); tsCache = new TranslationStateCacheImpl(docStatsLoader, - targetValidationLoader, textFlowTargetDAO, - validationServiceImpl); + targetValidationLoader) { + @Override + TextFlowTargetDAO getTextFlowTargetDAO() { + return textFlowTargetDAO; + } + }; tsCache.create(); tsCache.destroy();