forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestsAPI.java
153 lines (130 loc) · 5.2 KB
/
RequestsAPI.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
148
149
150
151
152
153
package org.folio.rest.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Handler;
import org.folio.rest.impl.util.OkapiResponseUtil;
import org.folio.rest.impl.util.RequestsApiUtil;
import org.folio.rest.jaxrs.model.Errors;
import org.folio.rest.jaxrs.model.Request;
import org.folio.rest.jaxrs.model.Requests;
import org.folio.rest.jaxrs.resource.RequestStorage;
import org.folio.rest.persist.MyPgUtil;
import org.folio.rest.persist.PgUtil;
import org.folio.rest.persist.PostgresClient;
import org.folio.rest.tools.utils.TenantTool;
import javax.ws.rs.core.Response;
import java.util.Map;
import static io.vertx.core.Future.succeededFuture;
import static org.folio.HttpStatus.HTTP_BAD_REQUEST;
import static org.folio.rest.impl.Headers.TENANT_HEADER;
public class RequestsAPI implements RequestStorage {
public static final String REQUEST_TABLE = "request";
@Override
public void deleteRequestStorageRequests(
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));
postgresClient.execute(String.format("TRUNCATE TABLE %s_%s.%s",
tenantId, "mod_circulation_storage", REQUEST_TABLE),
reply -> asyncResultHandler.handle(succeededFuture(
DeleteRequestStorageRequestsResponse.respond204())));
}
catch(Exception e) {
asyncResultHandler.handle(succeededFuture(
DeleteRequestStorageRequestsResponse
.respond500WithTextPlain(e.getMessage())));
}
});
}
@Override
public void getRequestStorageRequests(
int offset,
int limit,
String query,
String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.get(REQUEST_TABLE, Request.class, Requests.class, query, offset, limit, okapiHeaders, vertxContext,
GetRequestStorageRequestsResponse.class, asyncResultHandler);
}
@Override
public void postRequestStorageRequests(
String lang,
Request entity,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.post(REQUEST_TABLE, entity, okapiHeaders, vertxContext,
PostRequestStorageRequestsResponse.class, reply -> {
if (isSamePositionInQueueError(reply)) {
asyncResultHandler.handle(succeededFuture(
PostRequestStorageRequestsResponse
.respond422WithApplicationJson(samePositionInQueueError(entity))));
return;
}
asyncResultHandler.handle(reply);
});
}
@Override
public void getRequestStorageRequestsByRequestId(
String requestId,
String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.getById(REQUEST_TABLE, Request.class, requestId, okapiHeaders, vertxContext,
GetRequestStorageRequestsByRequestIdResponse.class, asyncResultHandler);
}
@Override
public void deleteRequestStorageRequestsByRequestId(
String requestId,
String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.deleteById(REQUEST_TABLE, requestId, okapiHeaders, vertxContext,
DeleteRequestStorageRequestsByRequestIdResponse.class, asyncResultHandler);
}
@Override
public void putRequestStorageRequestsByRequestId(
String requestId,
String lang, Request entity,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
// TODO: On insert don't return 204, we must return 201!
MyPgUtil.putUpsert204(REQUEST_TABLE, entity, requestId, okapiHeaders, vertxContext,
PutRequestStorageRequestsByRequestIdResponse.class, reply -> {
if (isSamePositionInQueueErrorOnUpsert(reply)) {
asyncResultHandler.handle(succeededFuture(
PutRequestStorageRequestsByRequestIdResponse
.respond422WithApplicationJson(samePositionInQueueError(entity))));
return;
}
asyncResultHandler.handle(reply);
});
}
private Errors samePositionInQueueError(Request request) {
return RequestsApiUtil
.samePositionInQueueError(request.getItemId(), request.getPosition());
}
// Remove/Replace this function when MyPgUtil.putUpsert204() is removed/replaced.
private boolean isSamePositionInQueueErrorOnUpsert(AsyncResult<Response> reply) {
return reply.succeeded()
&& reply.result().getStatus() == HTTP_BAD_REQUEST.toInt()
&& reply.result().hasEntity()
&& RequestsApiUtil
.hasSamePositionConstraintViolated(reply.result().getEntity().toString());
}
private boolean isSamePositionInQueueError(AsyncResult<Response> reply) {
String message = OkapiResponseUtil.getErrorMessage(reply);
return RequestsApiUtil.hasSamePositionConstraintViolated(message);
}
}