forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCirculationRulesAPI.java
More file actions
90 lines (76 loc) · 3.34 KB
/
Copy pathCirculationRulesAPI.java
File metadata and controls
90 lines (76 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.folio.rest.impl;
import static io.vertx.core.Future.succeededFuture;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Response;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.rest.annotations.Validate;
import org.folio.rest.jaxrs.model.CirculationRules;
import org.folio.rest.jaxrs.resource.CirculationRulesStorage;
import org.folio.rest.persist.Criteria.Criterion;
import org.folio.rest.persist.PostgresClient;
import org.folio.rest.tools.utils.TenantTool;
import org.folio.service.CirculationRulesService;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Handler;
public class CirculationRulesAPI implements CirculationRulesStorage {
private static final Logger log = LogManager.getLogger();
static final String CIRCULATION_RULES_TABLE = "circulation_rules";
private void internalErrorGet(Handler<AsyncResult<Response>> asyncResultHandler, Throwable e) {
log.error(e);
asyncResultHandler.handle(succeededFuture(
CirculationRulesStorage.GetCirculationRulesStorageResponse.
respond500WithTextPlain(e.getMessage())));
}
@Override
@Validate
public void getCirculationRulesStorage(Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
try {
vertxContext.runOnContext(v -> {
try {
Criterion filter = new Criterion();
PostgresClient postgresClient = PostgresClient.getInstance(
vertxContext.owner(), TenantTool.tenantId(okapiHeaders));
postgresClient.get(CIRCULATION_RULES_TABLE, CirculationRules.class, filter, true,
reply -> {
try {
if (reply.failed()) {
internalErrorGet(asyncResultHandler, reply.cause());
return;
}
List<CirculationRules> circulationRulesList = reply.result().getResults();
if (circulationRulesList.size() != 1) {
internalErrorGet(asyncResultHandler, new IllegalStateException("circulationRulesList.size() = "
+ circulationRulesList.size()));
return;
}
CirculationRules circulationRules = circulationRulesList.get(0);
asyncResultHandler.handle(succeededFuture(
CirculationRulesStorage.GetCirculationRulesStorageResponse.respond200WithApplicationJson(circulationRules)));
} catch (Exception e) {
internalErrorGet(asyncResultHandler, e);
}
});
} catch (Exception e) {
internalErrorGet(asyncResultHandler, e);
}
});
} catch (Exception e) {
internalErrorGet(asyncResultHandler, e);
}
}
@Override
@Validate
public void putCirculationRulesStorage(CirculationRules entity, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
new CirculationRulesService(vertxContext, okapiHeaders)
.update(entity)
.onSuccess(v -> asyncResultHandler.handle(succeededFuture(
PutCirculationRulesStorageResponse.respond204())))
.onFailure(t -> asyncResultHandler.handle(succeededFuture(
PutCirculationRulesStorageResponse.respond500WithTextPlain(t.getMessage()))));
}
}