forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CirculationRulesAPI.java
113 lines (100 loc) · 4.33 KB
/
CirculationRulesAPI.java
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package org.folio.rest.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
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.Criteria.UpdateSection;
import org.folio.rest.persist.interfaces.Results;
import org.folio.rest.persist.PostgresClient;
import org.folio.rest.tools.utils.TenantTool;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;
public class CirculationRulesAPI implements CirculationRulesStorage {
private static final Logger log = LoggerFactory.getLogger(CirculationRulesStorage.class);
static final String CIRCULATION_RULES_TABLE = "circulation_rules";
private void internalErrorGet(Handler<AsyncResult<Response>> asyncResultHandler, Throwable e) {
log.error(e);
asyncResultHandler.handle(Future.succeededFuture(
CirculationRulesStorage.GetCirculationRulesStorageResponse.
respond500WithTextPlain(e.getMessage())));
}
@Override
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(Future.succeededFuture(
CirculationRulesStorage.GetCirculationRulesStorageResponse.respond200WithApplicationJson(circulationRules)));
} catch (Exception e) {
internalErrorGet(asyncResultHandler, e);
}
});
} catch (Exception e) {
internalErrorGet(asyncResultHandler, e);
}
});
} catch (Exception e) {
internalErrorGet(asyncResultHandler, e);
}
}
private void internalErrorPut(Handler<AsyncResult<Response>> asyncResultHandler, Throwable e) {
log.error(e);
asyncResultHandler.handle(Future.succeededFuture(
CirculationRulesStorage.PutCirculationRulesStorageResponse.
respond500WithTextPlain(e.getMessage())));
}
@Override
public void putCirculationRulesStorage(CirculationRules entity, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
try {
vertxContext.runOnContext(v -> {
try {
PostgresClient postgresClient = PostgresClient.getInstance(
vertxContext.owner(), TenantTool.tenantId(okapiHeaders));
UpdateSection updateSection = new UpdateSection().addField("rulesAsText");
updateSection.setValue(entity.getRulesAsText());
postgresClient.update(CIRCULATION_RULES_TABLE, updateSection, (Criterion)null, true, update -> {
try {
if (update.failed()) {
internalErrorPut(asyncResultHandler, update.cause());
return;
}
asyncResultHandler.handle(Future.succeededFuture(
PutCirculationRulesStorageResponse.respond204()));
} catch (Exception e) {
internalErrorPut(asyncResultHandler, e);
}
});
} catch (Exception e) {
internalErrorPut(asyncResultHandler, e);
}
});
} catch (Exception e) {
internalErrorPut(asyncResultHandler, e);
}
}
}