forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestPoliciesAPI.java
147 lines (126 loc) · 6.72 KB
/
RequestPoliciesAPI.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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.cql2pgjson.CQL2PgJSON;
import org.folio.rest.annotations.Validate;
import org.folio.rest.jaxrs.model.RequestPolicies;
import org.folio.rest.jaxrs.model.RequestPolicy;
import org.folio.rest.jaxrs.resource.RequestPolicyStorage;
import org.folio.rest.persist.MyPgUtil;
import org.folio.rest.persist.PgUtil;
import org.folio.rest.persist.PostgresClient;
import org.folio.rest.persist.cql.CQLWrapper;
import org.folio.rest.tools.utils.OutStream;
import org.folio.rest.tools.utils.TenantTool;
import javax.ws.rs.core.Response;
import java.lang.invoke.MethodHandles;
import java.util.Map;
import java.util.UUID;
import static org.folio.rest.impl.Headers.TENANT_HEADER;
public class RequestPoliciesAPI implements RequestPolicyStorage {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final String REQUEST_POLICY_TABLE = "request_policy";
private static final Class<RequestPolicy> REQUEST_POLICY_CLASS = RequestPolicy.class;
@Validate
@Override
public void getRequestPolicyStorageRequestPolicies(int offset, int limit, String query, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.get(REQUEST_POLICY_TABLE, REQUEST_POLICY_CLASS, RequestPolicies.class,
query, offset, limit, okapiHeaders, vertxContext,
GetRequestPolicyStorageRequestPoliciesResponse.class, asyncResultHandler);
}
@Override
public void postRequestPolicyStorageRequestPolicies(String lang, RequestPolicy entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
// TODO: replace by PgUtil.post once RMB >= 25.0.2 gets released with this fix:
// https://issues.folio.org/browse/RMB-401 "PgUtil.post: Object or POJO type for entity parameter of respond201WithApplicationJson"
// https://github.com/folio-org/raml-module-builder/pull/444
String tenantId = okapiHeaders.get(TENANT_HEADER);
try {
PostgresClient postgresClient =
PostgresClient.getInstance(
vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
vertxContext.runOnContext(v -> {
try {
if(entity.getId() == null) {
entity.setId(UUID.randomUUID().toString());
}
postgresClient.save(REQUEST_POLICY_TABLE, entity.getId(), entity,
reply -> {
try {
if(reply.succeeded()) {
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(
io.vertx.core.Future.succeededFuture(
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse
.respond201WithApplicationJson(entity,
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse.headersFor201().withLocation(reply.result()))));
}
else {
asyncResultHandler.handle(
io.vertx.core.Future.succeededFuture(
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse
.respond500WithTextPlain(reply.cause().toString())));
}
} catch (Exception e) {
log.error(e);
asyncResultHandler.handle(
io.vertx.core.Future.succeededFuture(
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse
.respond500WithTextPlain(e.getMessage())));
}
});
} catch (Exception e) {
log.error(e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse
.respond500WithTextPlain(e.getMessage())));
}
});
} catch (Exception e) {
log.error(e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse
.respond500WithTextPlain(e.getMessage())));
}
}
@Override
public void deleteRequestPolicyStorageRequestPolicies(String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
String tenantId = okapiHeaders.get(TENANT_HEADER);
vertxContext.runOnContext(v -> {
try {
PostgresClient postgresClient = PostgresClient.getInstance(
vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
CQL2PgJSON cql2pgJson = new CQL2PgJSON("request_policy.jsonb");
CQLWrapper cql = new CQLWrapper(cql2pgJson, null);
postgresClient.delete(REQUEST_POLICY_TABLE, cql,
reply -> asyncResultHandler.handle(Future.succeededFuture(
DeleteRequestPolicyStorageRequestPoliciesResponse.respond204())));
}
catch(Exception e) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(
RequestPolicyStorage.DeleteRequestPolicyStorageRequestPoliciesResponse
.respond500WithTextPlain(e.getMessage())));
}
});
}
@Override
public void getRequestPolicyStorageRequestPoliciesByRequestPolicyId(String requestPolicyId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.getById(REQUEST_POLICY_TABLE, REQUEST_POLICY_CLASS, requestPolicyId, okapiHeaders, vertxContext,
GetRequestPolicyStorageRequestPoliciesByRequestPolicyIdResponse.class, asyncResultHandler);
}
@Override
public void putRequestPolicyStorageRequestPoliciesByRequestPolicyId(String requestPolicyId, String lang, RequestPolicy entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
// TODO: on insert return 201, not 204
MyPgUtil.putUpsert204(REQUEST_POLICY_TABLE, entity, requestPolicyId, okapiHeaders, vertxContext,
PutRequestPolicyStorageRequestPoliciesByRequestPolicyIdResponse.class, asyncResultHandler);
}
@Override
public void deleteRequestPolicyStorageRequestPoliciesByRequestPolicyId(String requestPolicyId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.deleteById(REQUEST_POLICY_TABLE, requestPolicyId, okapiHeaders, vertxContext,
DeleteRequestPolicyStorageRequestPoliciesByRequestPolicyIdResponse.class, asyncResultHandler);
}
}