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

Commit

Permalink
refactor(ZNTA-1017): translation updates event to handle batch
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Apr 14, 2016
1 parent 23ef404 commit 5db2be2
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 145 deletions.
1 change: 1 addition & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* [ZNTA-905](https://zanata.atlassian.net/browse/ZNTA-905) - Remove 0% matching translation memory entry
* [ZNTA-928](https://zanata.atlassian.net/browse/ZNTA-928) - Readonly project doesn't have "lock" icon in UI
* [ZNTA-793](https://zanata.atlassian.net/browse/ZNTA-793) - Remove unused method in TranslationMemoryAction
* [ZNTA-1017](https://zanata.atlassian.net/browse/ZNTA-1017) - Refactor translation update event

<h5>Infrastructure Changes</h5>
* Recommended platform: JBoss EAP 6 (6.4.6.GA or later).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@

package org.zanata.events;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nullable;

import lombok.Value;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;

Expand All @@ -32,16 +38,58 @@
* href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
@Value
@AllArgsConstructor
public final class TextFlowTargetStateEvent {
// this may be null in the case of document uploads
private final @Nullable
Long actorId;
private final Long projectIterationId;
private final Long documentId;
private final Long textFlowId;
private final LocaleId localeId;
private final Long textFlowTargetId;
private final ContentState newState;
private final ContentState previousState;
@Getter
private final DocumentLocaleKey key;

@Getter
private final List<TextFlowTargetState> states;

public TextFlowTargetStateEvent(DocumentLocaleKey key, TextFlowTargetState state) {
this.key = key;
this.states = Arrays.asList(state);
}

// public void add(final @Nullable Long actorId,
// final Long projectIterationId, final Long documentId,
// final Long textFlowId, final LocaleId localeId,
// final Long textFlowTargetId, final ContentState newState,
// final ContentState previousState) {
//
// DocumentLocaleKey key = new DocumentLocaleKey(actorId,
// projectIterationId, documentId, localeId);
//
// TextFlowTargetState state =
// new TextFlowTargetState(textFlowId, textFlowTargetId, newState,
// previousState);
//
// List<TextFlowTargetState> states = targetStateList.get(key);
// if(states == null) {
// states = Lists.newArrayList();
// }
// states.add(state);
// targetStateList.put(key, states);
// }

@Getter
@EqualsAndHashCode
@AllArgsConstructor
public static final class TextFlowTargetState implements Serializable {
private final Long textFlowId;
private final Long textFlowTargetId;
private final ContentState newState;
private final ContentState previousState;
}

@AllArgsConstructor
@EqualsAndHashCode
@Getter
public static final class DocumentLocaleKey implements Serializable {
// this may be null in the case of document uploads
private final @Nullable Long actorId;
private final Long projectIterationId;
private final Long documentId;
private final LocaleId localeId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;

import javax.enterprise.context.RequestScoped;
Expand Down Expand Up @@ -184,24 +185,34 @@ public Object getEntity(EntityType entityType, long entityId) {
public void logTextFlowStateUpdate(@Observes(during = TransactionPhase.AFTER_SUCCESS) TextFlowTargetStateEvent event_) {
// workaround for https://issues.jboss.org/browse/WELD-2019
final TextFlowTargetStateEvent event = event_;
Long actorId = event.getActorId();

Long actorId = event.getKey().getActorId();

if (actorId != null) {
Lock lock = activityLockManager.getLock(actorId);
lock.lock();
try {
transactionUtil.run(() -> {
HTextFlowTarget target =
textFlowTargetDAO.findById(event.getTextFlowTargetId(),
false);
HDocument document = documentDAO.getById(event.getDocumentId());
ActivityType activityType =
event.getNewState().isReviewed() ? ActivityType.REVIEWED_TRANSLATION
: ActivityType.UPDATE_TRANSLATION;

logActivityAlreadyLocked(actorId,
document.getProjectIteration(), target, activityType,
target.getTextFlow().getWordCount().intValue());
});
HDocument document =
documentDAO.getById(event.getKey().getDocumentId());

for (TextFlowTargetStateEvent.TextFlowTargetState state : event
.getStates()) {
transactionUtil.run(() -> {
HTextFlowTarget target =
textFlowTargetDAO.findById(
state.getTextFlowTargetId(), false);
ActivityType activityType =
state.getNewState().isReviewed()
? ActivityType.REVIEWED_TRANSLATION
: ActivityType.UPDATE_TRANSLATION;

logActivityAlreadyLocked(actorId,
document.getProjectIteration(), target,
activityType,
target.getTextFlow().getWordCount()
.intValue());
});
}
} catch (Exception e) {
Throwables.propagate(e);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
Expand Down Expand Up @@ -403,12 +404,22 @@ private void signalCopiedTranslation(HTextFlowTarget target,
// TODO how was this not causing duplicate events? Is this bypassing TranslationServiceImpl?
// FIXME other observers may not be notified
HDocument document = target.getTextFlow().getDocument();
TextFlowTargetStateEvent updateEvent =
new TextFlowTargetStateEvent(null, document
.getProjectIteration().getId(), document.getId(),
target.getTextFlow().getId(), target.getLocaleId(),
target.getId(), target.getState(), previousState);
versionStateCacheImpl.textFlowStateUpdated(updateEvent);

TextFlowTargetStateEvent.DocumentLocaleKey key =
new TextFlowTargetStateEvent.DocumentLocaleKey(null,
document.getProjectIteration().getId(), document.getId(),
target.getLocaleId());

TextFlowTargetStateEvent.TextFlowTargetState state =
new TextFlowTargetStateEvent.TextFlowTargetState(
target.getTextFlow().getId(),
target.getId(), target.getState(),
previousState);

TextFlowTargetStateEvent event =
new TextFlowTargetStateEvent(key, state);

versionStateCacheImpl.textFlowStateUpdated(event);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import javax.annotation.Nonnull;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import javax.enterprise.context.RequestScoped;
Expand Down Expand Up @@ -56,6 +57,8 @@
import com.google.common.base.Stopwatch;
import com.google.common.collect.Maps;

import static org.zanata.events.TextFlowTargetStateEvent.DocumentLocaleKey;
import static org.zanata.events.TextFlowTargetStateEvent.TextFlowTargetState;
import static org.zanata.transaction.TransactionUtil.runInTransaction;

/**
Expand Down Expand Up @@ -89,7 +92,7 @@ public class MergeTranslationsServiceImpl implements MergeTranslationsService {
private LocaleService localeServiceImpl;

@Inject
private Event<TextFlowTargetStateEvent> textFlowTargetStateEventEvent;
private Event<TextFlowTargetStateEvent> textFlowTargetStateEvent;

private final static int TRANSLATION_BATCH_SIZE = 10;

Expand Down Expand Up @@ -200,6 +203,9 @@ private Integer mergeTranslations(
sourceVersionId, targetVersionId, batchStart,
batchLength);

Map<DocumentLocaleKey, List<TextFlowTargetState>> eventMap =
Maps.newHashMap();

for (HTextFlow[] results : matches) {
HTextFlow sourceTf = results[0];
HTextFlow targetTf = results[1];
Expand Down Expand Up @@ -237,28 +243,36 @@ private Integer mergeTranslations(
textFlowDAO.makePersistent(targetTf);
textFlowDAO.flush();

// TODO: Fire single event with batch of updated textFlowTarget
if (!localeContentStateMap.isEmpty()) {
for (Map.Entry<Long, ContentState> entry : localeContentStateMap
.entrySet()) {
HTextFlowTarget updatedTarget =
targetTf.getTargets().get(entry.getKey());

TextFlowTargetStateEvent event =
new TextFlowTargetStateEvent(null,
targetVersionId,
targetTf.getDocument().getId(),
targetTf.getId(),
updatedTarget.getLocale().getLocaleId(),
updatedTarget.getId(),
updatedTarget.getState(),
entry.getValue());

textFlowTargetStateEventEvent.fire(event);
DocumentLocaleKey key = new DocumentLocaleKey(null,
targetVersionId, targetTf.getDocument().getId(),
updatedTarget.getLocale().getLocaleId());

List<TextFlowTargetState> events = eventMap.get(key);
if(events == null) {
events = Lists.newArrayList();
}
events.add(new TextFlowTargetState(targetTf.getId(),
updatedTarget.getId(), updatedTarget.getState(),
entry.getValue()));
}
}
}
}
if (!eventMap.isEmpty()) {
for (Map.Entry<DocumentLocaleKey, List<TextFlowTargetState>> entry : eventMap
.entrySet()) {
TextFlowTargetStateEvent tftUpdatedEvent =
new TextFlowTargetStateEvent(entry.getKey(),
entry.getValue());
textFlowTargetStateEvent.fire(tftUpdatedEvent);
}
}
stopwatch.stop();
log.info("Complete merge translations of {} in {}", matches.size()
* supportedLocales.size(), stopwatch);
Expand Down
Loading

0 comments on commit 5db2be2

Please sign in to comment.