-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from SorinPopteanu/add-factories
Add factories
- Loading branch information
Showing
76 changed files
with
1,764 additions
and
1,329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 26 additions & 60 deletions
86
.../org/chainoptim/desktop/core/notification/service/NotificationPersistenceServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,66 @@ | ||
package org.chainoptim.desktop.core.notification.service; | ||
|
||
import org.chainoptim.desktop.core.notification.model.Notification; | ||
import org.chainoptim.desktop.core.notification.model.NotificationUser; | ||
import org.chainoptim.desktop.core.user.util.TokenManager; | ||
import org.chainoptim.desktop.features.factory.dto.FactoriesSearchDTO; | ||
import org.chainoptim.desktop.features.supplier.model.Supplier; | ||
import org.chainoptim.desktop.core.user.service.TokenManager; | ||
import org.chainoptim.desktop.shared.caching.CacheKeyBuilder; | ||
import org.chainoptim.desktop.shared.caching.CachingService; | ||
import org.chainoptim.desktop.shared.httphandling.RequestBuilder; | ||
import org.chainoptim.desktop.shared.httphandling.RequestHandler; | ||
import org.chainoptim.desktop.shared.httphandling.Result; | ||
import org.chainoptim.desktop.shared.search.model.PaginatedResults; | ||
import org.chainoptim.desktop.shared.search.model.SearchParams; | ||
import org.chainoptim.desktop.shared.util.JsonUtil; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.google.inject.Inject; | ||
|
||
import java.net.HttpURLConnection; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class NotificationPersistenceServiceImpl implements NotificationPersistenceService { | ||
|
||
private final CachingService<PaginatedResults<NotificationUser>> cachingService; | ||
private final HttpClient client = HttpClient.newHttpClient(); | ||
private final RequestHandler requestHandler; | ||
private final RequestBuilder requestBuilder; | ||
private final TokenManager tokenManager; | ||
|
||
private static final String HEADER_KEY = "Authorization"; | ||
private static final String HEADER_VALUE_PREFIX = "Bearer "; | ||
private static final int STALE_TIME = 300; | ||
|
||
@Inject | ||
public NotificationPersistenceServiceImpl(CachingService<PaginatedResults<NotificationUser>> cachingService) { | ||
public NotificationPersistenceServiceImpl( | ||
CachingService<PaginatedResults<NotificationUser>> cachingService, | ||
RequestHandler requestHandler, | ||
RequestBuilder requestBuilder, | ||
TokenManager tokenManager) { | ||
this.cachingService = cachingService; | ||
this.requestHandler = requestHandler; | ||
this.requestBuilder = requestBuilder; | ||
this.tokenManager = tokenManager; | ||
} | ||
|
||
public CompletableFuture<Optional<List<NotificationUser>>> getNotificationsByUserId(String userId) { | ||
public CompletableFuture<Result<List<NotificationUser>>> getNotificationsByUserId(String userId) { | ||
String routeAddress = "http://localhost:8080/api/v1/notifications/user/" + userId; | ||
|
||
String jwtToken = TokenManager.getToken(); | ||
if (jwtToken == null) return new CompletableFuture<>(); | ||
String headerValue = HEADER_VALUE_PREFIX + jwtToken; | ||
|
||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(routeAddress)) | ||
.GET() | ||
.headers(HEADER_KEY, headerValue) | ||
.build(); | ||
HttpRequest request = requestBuilder.buildReadRequest(routeAddress, tokenManager.getToken()); | ||
|
||
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) | ||
.thenApply(response -> { | ||
if (response.statusCode() != HttpURLConnection.HTTP_OK) return Optional.<List<NotificationUser>>empty(); | ||
try { | ||
List<NotificationUser> notifications = JsonUtil.getObjectMapper().readValue(response.body(), new TypeReference<List<NotificationUser>>() {}); | ||
return Optional.of(notifications); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return Optional.<List<NotificationUser>>empty(); | ||
} | ||
}); | ||
return requestHandler.sendRequest(request, new TypeReference<List<NotificationUser>>() {}); | ||
} | ||
|
||
public CompletableFuture<Optional<PaginatedResults<NotificationUser>>> getNotificationsByUserIdAdvanced(String userId, SearchParams searchParams) { | ||
public CompletableFuture<Result<PaginatedResults<NotificationUser>>> getNotificationsByUserIdAdvanced(String userId, SearchParams searchParams) { | ||
String rootAddress = "http://localhost:8080/api/v1/"; | ||
String cacheKey = CacheKeyBuilder.buildAdvancedSearchKey("notifications", "user", userId, searchParams); | ||
String routeAddress = rootAddress + cacheKey; | ||
|
||
String jwtToken = TokenManager.getToken(); | ||
if (jwtToken == null) return new CompletableFuture<>(); | ||
String headerValue = HEADER_VALUE_PREFIX + jwtToken; | ||
|
||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(routeAddress)) | ||
.GET() | ||
.headers(HEADER_KEY, headerValue) | ||
.build(); | ||
HttpRequest request = requestBuilder.buildReadRequest(routeAddress, tokenManager.getToken()); | ||
|
||
if (cachingService.isCached(cacheKey) && !cachingService.isStale(cacheKey)) { | ||
return CompletableFuture.completedFuture(Optional.of(cachingService.get(cacheKey))); | ||
return CompletableFuture.completedFuture(new Result<>(cachingService.get(cacheKey), null, HttpURLConnection.HTTP_OK)); | ||
} | ||
|
||
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) | ||
.thenApply(response -> { | ||
if (response.statusCode() != HttpURLConnection.HTTP_OK) return Optional.<PaginatedResults<NotificationUser>>empty(); | ||
try { | ||
PaginatedResults<NotificationUser> notifications = JsonUtil.getObjectMapper().readValue(response.body(), new TypeReference<PaginatedResults<NotificationUser>>() {}); | ||
|
||
cachingService.remove(cacheKey); // Ensure there isn't a stale cache entry | ||
cachingService.add(cacheKey, notifications, STALE_TIME); | ||
|
||
return Optional.of(notifications); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return Optional.<PaginatedResults<NotificationUser>>empty(); | ||
} | ||
}); | ||
return requestHandler.sendRequest(request, new TypeReference<PaginatedResults<NotificationUser>>() {}, notifications -> { | ||
cachingService.remove(cacheKey); // Ensure there isn't a stale cache entry | ||
cachingService.add(cacheKey, notifications, STALE_TIME); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.