forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestPoliciesAPI.java
More file actions
199 lines (173 loc) · 8.29 KB
/
Copy pathRequestPoliciesAPI.java
File metadata and controls
199 lines (173 loc) · 8.29 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package org.folio.rest.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Handler;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.folio.cql2pgjson.CQL2PgJSON;
import org.folio.rest.annotations.Validate;
import org.folio.rest.jaxrs.model.Errors;
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 org.folio.service.policy.RequestPolicyValidationService;
import org.folio.support.exception.ValidationException;
import javax.ws.rs.core.Response;
import java.util.Map;
import java.util.UUID;
import static io.vertx.core.Future.succeededFuture;
import static org.folio.rest.impl.Headers.TENANT_HEADER;
import static org.folio.support.ModuleConstants.REQUEST_POLICY_CLASS;
import static org.folio.support.ModuleConstants.REQUEST_POLICY_TABLE;
public class RequestPoliciesAPI implements RequestPolicyStorage {
private static final Logger log = LogManager.getLogger();
@Validate
@Override
public void getRequestPolicyStorageRequestPolicies(String totalRecords, int offset, int limit,
String query, 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);
}
@Validate
@Override
public void postRequestPolicyStorageRequestPolicies(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());
}
new RequestPolicyValidationService(vertxContext.owner(), okapiHeaders)
.validate(entity)
.onFailure(throwable -> {
if (throwable instanceof ValidationException e) {
asyncResultHandler.handle(succeededFuture(RequestPolicyStorage
.PostRequestPolicyStorageRequestPoliciesResponse
.respond422WithApplicationJson(new Errors().withErrors(e.getErrors()))));
} else {
asyncResultHandler.handle(succeededFuture(RequestPolicyStorage
.PostRequestPolicyStorageRequestPoliciesResponse
.respond500WithTextPlain(throwable.getMessage())));
}
})
.onSuccess(ignored ->
postgresClient.save(REQUEST_POLICY_TABLE, entity.getId(), entity,
reply -> {
try {
if (reply.succeeded()) {
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(
succeededFuture(
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse
.respond201WithApplicationJson(entity,
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse.headersFor201()
.withLocation(reply.result()))));
} else {
asyncResultHandler.handle(
succeededFuture(
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse
.respond500WithTextPlain(reply.cause().toString())));
}
} catch (Exception e) {
log.error(e);
asyncResultHandler.handle(
succeededFuture(
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse
.respond500WithTextPlain(e.getMessage())));
}
})
);
} catch (Exception e) {
log.error(e);
asyncResultHandler.handle(succeededFuture(
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse
.respond500WithTextPlain(e.getMessage())));
}
});
} catch (Exception e) {
log.error(e);
asyncResultHandler.handle(succeededFuture(
RequestPolicyStorage.PostRequestPolicyStorageRequestPoliciesResponse
.respond500WithTextPlain(e.getMessage())));
}
}
@Validate
@Override
public void deleteRequestPolicyStorageRequestPolicies(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(succeededFuture(
DeleteRequestPolicyStorageRequestPoliciesResponse.respond204())));
}
catch(Exception e) {
asyncResultHandler.handle(succeededFuture(
RequestPolicyStorage.DeleteRequestPolicyStorageRequestPoliciesResponse
.respond500WithTextPlain(e.getMessage())));
}
});
}
@Validate
@Override
public void getRequestPolicyStorageRequestPoliciesByRequestPolicyId(String requestPolicyId,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.getById(REQUEST_POLICY_TABLE, REQUEST_POLICY_CLASS, requestPolicyId, okapiHeaders, vertxContext,
GetRequestPolicyStorageRequestPoliciesByRequestPolicyIdResponse.class, asyncResultHandler);
}
@Validate
@Override
public void putRequestPolicyStorageRequestPoliciesByRequestPolicyId(String requestPolicyId,
RequestPolicy entity, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
new RequestPolicyValidationService(vertxContext.owner(), okapiHeaders)
.validate(entity)
.onFailure(throwable -> {
if (throwable instanceof ValidationException e) {
asyncResultHandler.handle(succeededFuture(RequestPolicyStorage
.PutRequestPolicyStorageRequestPoliciesByRequestPolicyIdResponse
.respond422WithApplicationJson(new Errors().withErrors(e.getErrors()))));
} else {
asyncResultHandler.handle(succeededFuture(RequestPolicyStorage
.PutRequestPolicyStorageRequestPoliciesByRequestPolicyIdResponse
.respond500WithTextPlain(throwable.getMessage())));
}
})
.onSuccess(ignored ->
// TODO: on insert return 201, not 204
MyPgUtil.putUpsert204(REQUEST_POLICY_TABLE, entity, requestPolicyId, okapiHeaders, vertxContext,
PutRequestPolicyStorageRequestPoliciesByRequestPolicyIdResponse.class, asyncResultHandler));
}
@Validate
@Override
public void deleteRequestPolicyStorageRequestPoliciesByRequestPolicyId(String requestPolicyId,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.deleteById(REQUEST_POLICY_TABLE, requestPolicyId, okapiHeaders, vertxContext,
DeleteRequestPolicyStorageRequestPoliciesByRequestPolicyIdResponse.class, asyncResultHandler);
}
}