Skip to content

Commit

Permalink
Migrate to latest Play version - addresses #92
Browse files Browse the repository at this point in the history
- removes the AuthProvider from the repositories
- fixes unit tests
  • Loading branch information
resamsel committed Nov 9, 2020
1 parent 170c876 commit e37a3e5
Show file tree
Hide file tree
Showing 84 changed files with 1,668 additions and 2,140 deletions.
5 changes: 1 addition & 4 deletions app/auth/AccessTokenAuthenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import play.inject.Injector;
import services.AccessTokenService;

import javax.inject.Inject;
import javax.inject.Singleton;

public class AccessTokenAuthenticator implements Authenticator<TokenCredentials> {

private final Injector injector;
Expand All @@ -26,7 +23,7 @@ public AccessTokenAuthenticator(Injector injector, String clientName) {
public void validate(TokenCredentials credentials, WebContext context) {
init();

AccessToken accessToken = accessTokenService.byKey(credentials.getToken(), null) /* FIXME */;
AccessToken accessToken = accessTokenService.byKey(credentials.getToken(), null);

if (accessToken == null) {
throw new CredentialsException("Could not validate access token");
Expand Down
3 changes: 2 additions & 1 deletion app/criterias/AbstractContextCriteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public UUID getLoggedInUserId() {
return loggedInUserId;
}

public void setLoggedInUserId(UUID loggedInUserId) {
@Override
public void setLoggedInUserId(@CheckForNull UUID loggedInUserId) {
this.loggedInUserId = loggedInUserId;
}

Expand Down
4 changes: 3 additions & 1 deletion app/criterias/AbstractSearchCriteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import play.mvc.Http.Request;
import utils.NumberUtils;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.UUID;

Expand Down Expand Up @@ -68,7 +69,8 @@ public UUID getLoggedInUserId() {
return loggedInUserId;
}

public void setLoggedInUserId(UUID loggedInUserId) {
@Override
public void setLoggedInUserId(@CheckForNull UUID loggedInUserId) {
this.loggedInUserId = loggedInUserId;
}

Expand Down
2 changes: 2 additions & 0 deletions app/criterias/ContextCriteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public interface ContextCriteria extends FetchCriteria {

@CheckForNull
UUID getLoggedInUserId();

void setLoggedInUserId(@CheckForNull UUID loggedInUserId);
}
46 changes: 0 additions & 46 deletions app/forms/ProjectForm.java

This file was deleted.

50 changes: 0 additions & 50 deletions app/forms/ProjectUserForm.java

This file was deleted.

23 changes: 12 additions & 11 deletions app/importers/AbstractImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.mvc.Http;
import services.KeyService;
import services.MessageService;

Expand Down Expand Up @@ -49,40 +50,40 @@ public AbstractImporter(KeyService keyService, MessageService messageService) {
}

@Override
public void apply(File file, Locale locale) throws Exception {
public void apply(File file, Locale locale, Http.Request request) throws Exception {
LOGGER.debug("Importing from file {}", file.getName());

Properties properties = retrieveProperties(new FileInputStream(file), locale);

load(locale, properties.stringPropertyNames());
load(locale, properties.stringPropertyNames(), request);

saveKeys(locale, properties);
saveMessages(locale, properties);
saveKeys(locale, properties, request);
saveMessages(locale, properties, request);

LOGGER.debug("Imported from file {}", file.getName());
}

abstract Properties retrieveProperties(InputStream stream, Locale locale) throws Exception;

protected void load(Locale locale, Collection<String> keyNames) {
protected void load(Locale locale, Collection<String> keyNames, Http.Request request) {
keys = keyService.findBy(
new KeyCriteria()
KeyCriteria.from(request)
.withLimit(Integer.MAX_VALUE)
.withProjectId(locale.project.id)
.withNames(keyNames))
.getList()
.stream()
.collect(toMap(k -> k.name, a -> a));
messages = messageService.findBy(
new MessageCriteria()
MessageCriteria.from(request)
.withLimit(Integer.MAX_VALUE)
.withLocaleId(locale.id))
.getList()
.stream()
.collect(toMap(m -> m.key.name, a -> a));
}

void saveKeys(Locale locale, Properties properties) {
void saveKeys(Locale locale, Properties properties, Http.Request request) {
List<Key> newKeys = new ArrayList<>();
for (String keyName : properties.stringPropertyNames()) {
String value = (String) properties.get(keyName);
Expand All @@ -97,12 +98,12 @@ void saveKeys(Locale locale, Properties properties) {
}

// Update keys cache
for (Key key : keyService.save(newKeys)) {
for (Key key : keyService.save(newKeys, request)) {
keys.put(key.name, key);
}
}

void saveMessages(Locale locale, Properties properties) {
void saveMessages(Locale locale, Properties properties, Http.Request request) {
List<Message> newMessages = new ArrayList<>();
for (String keyName : properties.stringPropertyNames()) {
String value = (String) properties.get(keyName);
Expand Down Expand Up @@ -132,6 +133,6 @@ void saveMessages(Locale locale, Properties properties) {
}
}

messageService.save(newMessages);
messageService.save(newMessages, request);
}
}
3 changes: 2 additions & 1 deletion app/importers/Importer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package importers;

import models.Locale;
import play.mvc.Http;

import java.io.File;

public interface Importer {
void apply(File file, Locale locale) throws Exception;
void apply(File file, Locale locale, Http.Request request) throws Exception;
}
14 changes: 6 additions & 8 deletions app/importers/PropertiesImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import models.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.mvc.Http;
import services.KeyService;
import services.MessageService;

Expand All @@ -18,16 +19,13 @@
public abstract class PropertiesImporter extends AbstractImporter implements Importer {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesImporter.class);

/**
*
*/
protected PropertiesImporter(KeyService keyService, MessageService messageService) {
super(keyService, messageService);
}

/**
* {@inheritDoc}
*
*
* @throws IOException
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
Expand All @@ -44,15 +42,15 @@ Properties retrieveProperties(InputStream inputStream, Locale locale) throws IOE
}

@Override
public void apply(File file, Locale locale) throws Exception {
public void apply(File file, Locale locale, Http.Request request) throws Exception {
LOGGER.debug("Importing from file {}", file.getName());

Properties properties = retrieveProperties(new FileInputStream(file), null);

load(locale, properties.stringPropertyNames());
load(locale, properties.stringPropertyNames(), request);

saveKeys(locale, properties);
saveMessages(locale, properties);
saveKeys(locale, properties, request);
saveMessages(locale, properties, request);

LOGGER.debug("Imported from file {}", file.getName());
}
Expand Down
1 change: 0 additions & 1 deletion app/mappers/AccessTokenMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import dto.AccessToken;
import models.User;
import play.mvc.Http;

public class AccessTokenMapper {
public static models.AccessToken toModel(dto.AccessToken in) {
Expand Down
4 changes: 2 additions & 2 deletions app/mappers/NotificationMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public Notification toDto(SimpleActivity in, LogEntry activity, Http.Request req
Notification out = new Notification();

out.id = in.getId();
out.user = UserMapper.toDto(userService.byId(Notification.extractUuid(in.getActor())));
out.user = UserMapper.toDto(userService.byId(Notification.extractUuid(in.getActor()), request));
out.verb = in.getVerb();
out.time = in.getTime() != null ? new Date(in.getTime().getTime()) : null;
if (activity == null)
activity = logEntryService.byId(Notification.extractUuid(in.getForeignId()));
activity = logEntryService.byId(Notification.extractUuid(in.getForeignId()), request);
if (activity != null) {
out.activityId = activity.id;
out.contentType = activity.getSimpleContentType();
Expand Down
2 changes: 0 additions & 2 deletions app/models/ProjectUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.joda.time.DateTime;
import utils.CacheUtils;
import validators.NameUnique;
import validators.ProjectUserModifyAllowed;
import validators.ProjectUserOwnerExists;
import validators.ProjectUserUniqueChecker;

Expand All @@ -29,7 +28,6 @@
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"project_id", "user_id"})})
@NameUnique(checker = ProjectUserUniqueChecker.class, field = "user", message = "error.projectuserunique")
@ProjectUserOwnerExists
@ProjectUserModifyAllowed
public class ProjectUser implements Model<ProjectUser, Long> {

private static final int ROLE_LENGTH = 16;
Expand Down
26 changes: 24 additions & 2 deletions app/repositories/ModelRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,36 @@ public interface ModelRepository<T extends Model<T, ID>, ID, CRITERIA extends Ab

T byId(GetCriteria<ID> criteria);

/**
* Create a new model in the database. Eventually, the {@link ModelRepository#save(Model)} method will be invoked.
*
* @param model the model including the ID
* @return the created model
* @throws javax.validation.ValidationException when either the ID is not unique, or the validation of the entity
* fails
*/
T create(T model);

/**
* Update existing model in the database. If the model doesn't exist, a validation exception will be thrown.
* Eventually, the {@link ModelRepository#save(Model)} method will be invoked.
*
* @param model the model including the ID
* @return the updated model
* @throws javax.validation.ValidationException when either the ID is missing, the entity doesn't exist in the
* database, or the validation of the entity fails
*/
T update(T model);

/**
* Persist model to database.
* Save or update existing model in the database. Decides on the existence of the ID whether or not the entity needs
* to be saved or updated.
*
* @param model the model
* @return the saved model
* @throws javax.validation.ValidationException when the validation of the entity fails
*/
T save(T t);
T save(T model);

/**
* Persist model collection to database.
Expand Down
2 changes: 1 addition & 1 deletion app/repositories/ProjectRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ FETCH_KEYS, singletonList(FETCH_KEYS),
FETCH_LOCALES, singletonList(FETCH_LOCALES)
);

Project byOwnerAndName(String username, String name, String... fetches);
Project byOwnerAndName(String username, String name, UUID loggedInUserId, String... fetches);

Map<UUID, Double> progress(List<UUID> projectIds);
}
Loading

0 comments on commit e37a3e5

Please sign in to comment.