Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Work around scope/thread problems in TranslationStateCacheImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Oct 17, 2013
1 parent 2484c34 commit 3e5b225
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
Expand Up @@ -35,22 +35,19 @@
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;
import org.zanata.cache.CacheWrapper;
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;
Expand All @@ -65,6 +62,7 @@
* href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Name("translationStateCacheImpl")
// TODO split into APPLICATION and STATELESS beans
@Scope(ScopeType.APPLICATION)
public class TranslationStateCacheImpl implements TranslationStateCache {
private static final String BASE = TranslationStateCacheImpl.class
Expand All @@ -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<TranslatedDocumentKey, DocumentStatus> docStatusCache;
private CacheWrapper<Long, Map<ValidationId, Boolean>> targetValidationCache;
Expand All @@ -100,14 +86,10 @@ public TranslationStateCacheImpl() {

public TranslationStateCacheImpl(
CacheLoader<TranslatedDocumentKey, DocumentStatus> docStatsLoader,
CacheLoader<Long, Map<ValidationId, Boolean>> targetValidationLoader,
TextFlowTargetDAO textFlowTargetDAO,
ValidationService validationServiceImpl) {
CacheLoader<Long, Map<ValidationId, Boolean>> targetValidationLoader) {
// constructor for testing
this.docStatusLoader = docStatsLoader;
this.targetValidationLoader = targetValidationLoader;
this.textFlowTargetDAO = textFlowTargetDAO;
this.validationServiceImpl = validationServiceImpl;
}

@Create
Expand Down Expand Up @@ -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<TranslatedDocumentKey, DocumentStatus> {
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<Long, Map<ValidationId, Boolean>> {
@Override
public Map<ValidationId, Boolean> load(Long key) throws Exception {
Expand All @@ -206,6 +203,7 @@ public Map<ValidationId, Boolean> load(Long key) throws Exception {

private Boolean loadTargetValidation(Long textFlowTargetId,
ValidationId validationId) {
TextFlowTargetDAO textFlowTargetDAO = getTextFlowTargetDAO();
HTextFlowTarget tft =
textFlowTargetDAO.findById(textFlowTargetId, false);

Expand All @@ -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 = "";

Expand Down
Expand Up @@ -53,16 +53,18 @@ public class TranslationStateCacheImplTest {
private TextFlowTargetDAO textFlowTargetDAO;
@Mock
private CacheLoader<Long, Map<ValidationId, Boolean>> 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();
Expand Down

0 comments on commit 3e5b225

Please sign in to comment.