diff --git a/app/actors/KeyWordCountActor.java b/app/actors/KeyWordCountActor.java index 1c51750c..7a4e8667 100644 --- a/app/actors/KeyWordCountActor.java +++ b/app/actors/KeyWordCountActor.java @@ -26,7 +26,7 @@ public KeyWordCountActor(KeyService keyService) { public Receive createReceive() { return receiveBuilder() .match(ChangeWordCount.class, - wordCount -> keyService.increaseWordCountBy(wordCount.id, wordCount.wordCountDiff, null /* FIXME */)) + wordCount -> keyService.increaseWordCountBy(wordCount.id, wordCount.wordCountDiff, wordCount.request)) .build(); } } diff --git a/app/actors/LocaleWordCountActor.java b/app/actors/LocaleWordCountActor.java index 069972f4..bda8aa69 100644 --- a/app/actors/LocaleWordCountActor.java +++ b/app/actors/LocaleWordCountActor.java @@ -26,7 +26,7 @@ public LocaleWordCountActor(LocaleService localeService) { public Receive createReceive() { return receiveBuilder() .match(ChangeWordCount.class, - wordCount -> localeService.increaseWordCountBy(wordCount.id, wordCount.wordCountDiff, null /* FIXME */)) + wordCount -> localeService.increaseWordCountBy(wordCount.id, wordCount.wordCountDiff, wordCount.request)) .build(); } } diff --git a/app/actors/MessageWordCountActor.java b/app/actors/MessageWordCountActor.java index 09bfc629..ace52f16 100644 --- a/app/actors/MessageWordCountActor.java +++ b/app/actors/MessageWordCountActor.java @@ -42,29 +42,29 @@ public Receive createReceive() { .match(ChangeMessageWordCount.class, wordCount -> { if (wordCount.projectId != null) projectWordCountActor.tell( - new ChangeWordCount(wordCount.projectId, wordCount.wordCount, wordCount.wordCountDiff), + ChangeWordCount.from(wordCount, wordCount.projectId), self()); if (wordCount.localeId != null) localeWordCountActor.tell( - new ChangeWordCount(wordCount.localeId, wordCount.wordCount, wordCount.wordCountDiff), + ChangeWordCount.from(wordCount, wordCount.localeId), self()); if (wordCount.keyId != null) keyWordCountActor.tell( - new ChangeWordCount(wordCount.keyId, wordCount.wordCount, wordCount.wordCountDiff), + ChangeWordCount.from(wordCount, wordCount.keyId), self()); }) .match(Collection.class, t -> { Collection wordCounts = (Collection) t; wordCounts.stream() - .map(wc -> new ChangeWordCount(wc.projectId, wc.wordCount, wc.wordCountDiff)) + .map(wc -> ChangeWordCount.from(wc, wc.projectId)) .collect(groupingBy(wc -> wc.id, reducing(ChangeWordCount::merge))) .forEach((projectId, wc) -> projectWordCountActor.tell(wc.get(), null)); wordCounts.stream() - .map(wc -> new ChangeWordCount(wc.localeId, wc.wordCount, wc.wordCountDiff)) + .map(wc -> ChangeWordCount.from(wc, wc.localeId)) .collect(groupingBy(wc -> wc.id, reducing(ChangeWordCount::merge))) .forEach((localeId, wc) -> localeWordCountActor.tell(wc.get(), null)); - wordCounts.stream().map(wc -> new ChangeWordCount(wc.keyId, wc.wordCount, wc.wordCountDiff)) + wordCounts.stream().map(wc -> ChangeWordCount.from(wc, wc.keyId)) .collect(groupingBy(wc -> wc.id, reducing(ChangeWordCount::merge))) .forEach((keyId, wc) -> keyWordCountActor.tell(wc.get(), null)); }) diff --git a/app/actors/ProjectWordCountActor.java b/app/actors/ProjectWordCountActor.java index 00ef4768..589c5cf6 100644 --- a/app/actors/ProjectWordCountActor.java +++ b/app/actors/ProjectWordCountActor.java @@ -26,7 +26,7 @@ public ProjectWordCountActor(ProjectService projectService) { public Receive createReceive() { return receiveBuilder() .match(ChangeWordCount.class, - wordCount -> projectService.increaseWordCountBy(wordCount.id, wordCount.wordCountDiff, null /* FIXME */)) + wordCount -> projectService.increaseWordCountBy(wordCount.id, wordCount.wordCountDiff, wordCount.request)) .build(); } } diff --git a/app/actors/WordCountProtocol.java b/app/actors/WordCountProtocol.java index f2bbdc6c..2cbd8223 100644 --- a/app/actors/WordCountProtocol.java +++ b/app/actors/WordCountProtocol.java @@ -1,5 +1,7 @@ package actors; +import play.mvc.Http; + import java.util.UUID; /** @@ -16,15 +18,17 @@ public static class ChangeMessageWordCount { public final UUID keyId; public final int wordCount; public final int wordCountDiff; + public final Http.Request request; public ChangeMessageWordCount(UUID messageId, UUID projectId, UUID localeId, UUID keyId, - int wordCount, int wordCountDiff) { + int wordCount, int wordCountDiff, Http.Request request) { this.messageId = messageId; this.projectId = projectId; this.localeId = localeId; this.keyId = keyId; this.wordCount = wordCount; this.wordCountDiff = wordCountDiff; + this.request = request; } } @@ -33,17 +37,19 @@ public static class ChangeWordCount { public final UUID id; public int wordCount; public int wordCountDiff; + public final Http.Request request; - public ChangeWordCount(UUID id, int wordCount, int wordCountDiff) { + public ChangeWordCount(UUID id, int wordCount, int wordCountDiff, Http.Request request) { this.id = id; this.wordCount = wordCount; this.wordCountDiff = wordCountDiff; + this.request = request; + } + + public static ChangeWordCount from(ChangeMessageWordCount wordCount, UUID id) { + return new ChangeWordCount(id, wordCount.wordCount, wordCount.wordCountDiff, wordCount.request); } - /** - * @param b - * @return - */ public ChangeWordCount merge(ChangeWordCount b) { wordCount += b.wordCount; wordCountDiff += b.wordCountDiff; diff --git a/app/repositories/MessageRepository.java b/app/repositories/MessageRepository.java index 1b6cc70e..91762330 100644 --- a/app/repositories/MessageRepository.java +++ b/app/repositories/MessageRepository.java @@ -33,6 +33,4 @@ public interface MessageRepository extends ModelRepository byKey(Key key); List byKeys(Collection ids); - - List latest(Project project, int limit); } diff --git a/app/repositories/impl/MessageRepositoryImpl.java b/app/repositories/impl/MessageRepositoryImpl.java index 3040fda8..41956ecf 100644 --- a/app/repositories/impl/MessageRepositoryImpl.java +++ b/app/repositories/impl/MessageRepositoryImpl.java @@ -1,9 +1,6 @@ package repositories.impl; -import actors.ActivityProtocol.Activities; -import actors.ActivityProtocol.Activity; import actors.MessageWordCountActorRef; -import actors.WordCountProtocol.ChangeMessageWordCount; import criterias.ContextCriteria; import criterias.MessageCriteria; import criterias.PagedListFactory; @@ -11,7 +8,6 @@ import io.ebean.PagedList; import io.ebean.Query; import mappers.MessageMapper; -import models.ActionType; import models.Key; import models.Message; import models.Project; @@ -20,7 +16,6 @@ import org.slf4j.LoggerFactory; import repositories.MessageRepository; import repositories.Persistence; -import utils.MessageUtils; import utils.QueryUtils; import javax.inject.Inject; @@ -31,11 +26,8 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.UUID; -import static java.util.stream.Collectors.toList; -import static java.util.stream.Collectors.toMap; import static utils.Stopwatch.log; @Singleton @@ -147,22 +139,4 @@ public List byKey(Key key) { public List byKeys(Collection ids) { return fetch().where().in("key.id", ids).findList(); } - - @Override - public List latest(Project project, int limit) { - return fetch().where().eq("key.project", project).order("whenUpdated desc").setMaxRows(limit) - .findList(); - } - - /** - * {@inheritDoc} - */ - @Override - public void preSave(Message t, boolean update) { - int wordCount = t.wordCount != null ? t.wordCount : 0; - t.wordCount = MessageUtils.wordCount(t.value); - - messageWordCountActor.tell(new ChangeMessageWordCount(t.id, t.locale.project.id, t.locale.id, - t.key.id, t.wordCount, t.wordCount - wordCount), null); - } } diff --git a/app/services/impl/MessageServiceImpl.java b/app/services/impl/MessageServiceImpl.java index 07dc5877..acdce4d5 100644 --- a/app/services/impl/MessageServiceImpl.java +++ b/app/services/impl/MessageServiceImpl.java @@ -158,7 +158,7 @@ protected void postDelete(Message t, Http.Request request) { if (t.wordCount != null) { messageWordCountActor.tell(new WordCountProtocol.ChangeMessageWordCount(t.id, t.locale.project.id, t.locale.id, - t.key.id, 0, -t.wordCount), null); + t.key.id, 0, -t.wordCount, request), null); } } @@ -173,6 +173,17 @@ protected void preUpdate(Message t, Http.Request request) { } } + @Override + protected void preSave(Message t, Http.Request request) { + super.preSave(t, request); + + int wordCount = t.wordCount != null ? t.wordCount : 0; + t.wordCount = MessageUtils.wordCount(t.value); + + messageWordCountActor.tell(new WordCountProtocol.ChangeMessageWordCount(t.id, t.locale.project.id, t.locale.id, + t.key.id, t.wordCount, t.wordCount - wordCount, request), null); + } + @Override protected void preSave(Collection t, Http.Request request) { super.preSave(t, request); @@ -187,7 +198,8 @@ protected void preSave(Collection t, Http.Request request) { m.locale.id, m.key.id, wc, - wc - (m.wordCount != null ? m.wordCount : 0)); + wc - (m.wordCount != null ? m.wordCount : 0), + request); }) .collect(toMap(wc -> wc.messageId, wc -> wc, (a, b) -> a)); @@ -197,7 +209,7 @@ protected void preSave(Collection t, Http.Request request) { t.stream() .filter(m -> m.id != null) .forEach(m -> m.wordCount = wordCount.getOrDefault(m.id, - new WordCountProtocol.ChangeMessageWordCount(null, null, null, null, 0, 0)).wordCount); + new WordCountProtocol.ChangeMessageWordCount(null, null, null, null, 0, 0, request)).wordCount); t.stream() .filter(m -> m.id == null) .forEach(m -> m.wordCount = MessageUtils.wordCount(m)); @@ -227,14 +239,14 @@ protected void postSave(Collection t, Http.Request request) { Map wordCount = noWordCountMessages.stream().map(m -> { int wc = MessageUtils.wordCount(m); return new WordCountProtocol.ChangeMessageWordCount(m.id, m.locale.project.id, m.locale.id, m.key.id, wc, - wc - (m.wordCount != null ? m.wordCount : 0)); + wc - (m.wordCount != null ? m.wordCount : 0), request); }).collect(toMap(wc -> wc.messageId, wc -> wc)); messageWordCountActor.tell(wordCount.values(), null); // Update model noWordCountMessages.stream().filter(m -> m.id != null).forEach(m -> m.wordCount = wordCount - .getOrDefault(m.id, new WordCountProtocol.ChangeMessageWordCount(null, null, null, null, 0, 0)).wordCount); + .getOrDefault(m.id, new WordCountProtocol.ChangeMessageWordCount(null, null, null, null, 0, 0, request)).wordCount); try { save(noWordCountMessages, request); @@ -280,7 +292,7 @@ protected void postDelete(Collection t, Http.Request request) { .filter(m -> m.wordCount != null) .map( m -> new WordCountProtocol.ChangeMessageWordCount(m.id, m.locale.project.id, m.locale.id, m.key.id, 0, - -m.wordCount)) + -m.wordCount, request)) .collect(toMap(wc -> wc.messageId, wc -> wc, (a, b) -> a)); messageWordCountActor.tell(wordCount.values(), null);