Skip to content

Commit

Permalink
Migrate to latest Play version - addresses #92
Browse files Browse the repository at this point in the history
Removes last set of FIXMEs.
  • Loading branch information
resamsel committed Sep 26, 2020
1 parent 177ec2a commit c5e69bb
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/actors/KeyWordCountActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion app/actors/LocaleWordCountActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
12 changes: 6 additions & 6 deletions app/actors/MessageWordCountActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChangeMessageWordCount> wordCounts = (Collection<ChangeMessageWordCount>) 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));
})
Expand Down
2 changes: 1 addition & 1 deletion app/actors/ProjectWordCountActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
18 changes: 12 additions & 6 deletions app/actors/WordCountProtocol.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package actors;

import play.mvc.Http;

import java.util.UUID;

/**
Expand All @@ -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;
}
}

Expand All @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions app/repositories/MessageRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@ public interface MessageRepository extends ModelRepository<Message, UUID, Messag
List<Message> byKey(Key key);

List<Message> byKeys(Collection<UUID> ids);

List<Message> latest(Project project, int limit);
}
26 changes: 0 additions & 26 deletions app/repositories/impl/MessageRepositoryImpl.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
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;
import io.ebean.ExpressionList;
import io.ebean.PagedList;
import io.ebean.Query;
import mappers.MessageMapper;
import models.ActionType;
import models.Key;
import models.Message;
import models.Project;
Expand All @@ -20,7 +16,6 @@
import org.slf4j.LoggerFactory;
import repositories.MessageRepository;
import repositories.Persistence;
import utils.MessageUtils;
import utils.QueryUtils;

import javax.inject.Inject;
Expand All @@ -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
Expand Down Expand Up @@ -147,22 +139,4 @@ public List<Message> byKey(Key key) {
public List<Message> byKeys(Collection<UUID> ids) {
return fetch().where().in("key.id", ids).findList();
}

@Override
public List<Message> 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);
}
}
24 changes: 18 additions & 6 deletions app/services/impl/MessageServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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<Message> t, Http.Request request) {
super.preSave(t, request);
Expand All @@ -187,7 +198,8 @@ protected void preSave(Collection<Message> 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));

Expand All @@ -197,7 +209,7 @@ protected void preSave(Collection<Message> 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));
Expand Down Expand Up @@ -227,14 +239,14 @@ protected void postSave(Collection<Message> t, Http.Request request) {
Map<UUID, WordCountProtocol.ChangeMessageWordCount> 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);
Expand Down Expand Up @@ -280,7 +292,7 @@ protected void postDelete(Collection<Message> 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);
Expand Down

0 comments on commit c5e69bb

Please sign in to comment.