Skip to content

Commit

Permalink
Remove duplicated code (#172)
Browse files Browse the repository at this point in the history
* Remove duplicated code

* Remove duplicated codes
  • Loading branch information
gulzhaukhar-tukayeva-epam authored Feb 5, 2024
1 parent ff90703 commit 0754e1a
Showing 1 changed file with 35 additions and 65 deletions.
100 changes: 35 additions & 65 deletions src/main/java/org/folio/rest/impl/BLUsersAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,42 +462,7 @@ else if(mode[0].equals("username")){
}
}

cf = completedLookup.get(SERVICEPOINTS_INCLUDE);
CompletableFuture<Response> ecf = completedLookup.get(
EXPANDED_SERVICEPOINTS_INCLUDE);
if(ecf != null && cf != null && cf.get().getBody() != null) {
JsonArray array = cf.get().getBody().getJsonArray("servicePointsUsers");
if(!array.isEmpty()) {
JsonObject spuJson = array.getJsonObject(0);
ServicePointsUser spu = (ServicePointsUser)Response.convertToPojo(spuJson,
ServicePointsUser.class);
Response resp = null;
List<ServicePoint> spList = new ArrayList<>();
try {
resp = ecf.get();
} catch(Exception e) {
logger.error(String.format(
"Unable to get expanded service point Response: %s",
e.getLocalizedMessage()));
}
if(resp != null) {
JsonObject spCollectionJson = resp.getBody();
if(spCollectionJson != null) {
JsonArray spArray = spCollectionJson.getJsonArray("servicepoints");
if(spArray != null) {
for(Object ob: spArray) {
JsonObject json = (JsonObject)ob;
ServicePoint sp = (ServicePoint)Response.convertToPojo(json,
ServicePoint.class);
spList.add(sp);
}
spu.setServicePoints(spList);
}
}
}
cu.setServicePointsUser(spu);
}
}
fillCompositeUserWithServicePoint (completedLookup, cu);

if(mode[0].equals("id")){
asyncResultHandler.handle(Future.succeededFuture(
Expand Down Expand Up @@ -1040,35 +1005,7 @@ okapiHeaders, null, handlePreviousResponse(false, false, false,

//TODO: Refactor so less copy/paste

cf = completedLookup.get(SERVICEPOINTS_INCLUDE);
CompletableFuture<Response> ecf = completedLookup.get(
EXPANDED_SERVICEPOINTS_INCLUDE);
if(ecf != null && cf != null && cf.get().getBody() != null) {
JsonArray array = cf.get().getBody().getJsonArray("servicePointsUsers");
if(!array.isEmpty()) {
JsonObject spuJson = array.getJsonObject(0);
ServicePointsUser spu = (ServicePointsUser)Response.convertToPojo(spuJson,
ServicePointsUser.class);
List<ServicePoint> spList = new ArrayList<>();
Response resp = ecf.get();
if(resp != null) {
JsonObject spCollectionJson = resp.getBody();
if(spCollectionJson != null) {
JsonArray spArray = spCollectionJson.getJsonArray("servicepoints");
if(spArray != null) {
for(Object ob: spArray) {
JsonObject json = (JsonObject)ob;
ServicePoint sp = (ServicePoint)Response.convertToPojo(json,
ServicePoint.class);
spList.add(sp);
}
spu.setServicePoints(spList);
}
}
}
cu.setServicePointsUser(spu);
}
}
fillCompositeUserWithServicePoint(completedLookup, cu);

if(!aRequestHasFailed[0]){
var r = respond.apply(loginResponse, cu);
Expand All @@ -1086,6 +1023,39 @@ okapiHeaders, null, handlePreviousResponse(false, false, false,
});
}

private static void fillCompositeUserWithServicePoint(Map<String, CompletableFuture<Response>> completedLookup, CompositeUser cu) throws Exception {
CompletableFuture<Response> cf;
cf = completedLookup.get(SERVICEPOINTS_INCLUDE);
CompletableFuture<Response> ecf = completedLookup.get(
EXPANDED_SERVICEPOINTS_INCLUDE);
if(ecf != null && cf != null && cf.get().getBody() != null) {
JsonArray array = cf.get().getBody().getJsonArray("servicePointsUsers");
if(!array.isEmpty()) {
JsonObject spuJson = array.getJsonObject(0);
ServicePointsUser spu = (ServicePointsUser)Response.convertToPojo(spuJson,
ServicePointsUser.class);
List<ServicePoint> spList = new ArrayList<>();
Response resp = ecf.get();
if(resp != null) {
JsonObject spCollectionJson = resp.getBody();
if(spCollectionJson != null) {
JsonArray spArray = spCollectionJson.getJsonArray("servicepoints");
if(spArray != null) {
for(Object ob: spArray) {
JsonObject json = (JsonObject)ob;
ServicePoint sp = (ServicePoint)Response.convertToPojo(json,
ServicePoint.class);
spList.add(sp);
}
spu.setServicePoints(spList);
}
}
}
cu.setServicePointsUser(spu);
}
}
}

@SuppressWarnings("java:S1874")
private javax.ws.rs.core.Response loginResponseLegacy(Response loginResponse, CompositeUser cu) {
String token = String.valueOf(loginResponse.getHeaders().get(OKAPI_TOKEN_HEADER));
Expand Down

0 comments on commit 0754e1a

Please sign in to comment.