forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoansAPI.java
358 lines (301 loc) · 12.1 KB
/
LoansAPI.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package org.folio.rest.impl;
import static io.vertx.core.Future.succeededFuture;
import static org.folio.HttpStatus.HTTP_BAD_REQUEST;
import static org.folio.rest.impl.Headers.TENANT_HEADER;
import static org.folio.support.ModuleConstants.LOAN_CLASS;
import static org.folio.support.ModuleConstants.LOAN_HISTORY_TABLE;
import static org.folio.support.ModuleConstants.LOAN_TABLE;
import static org.folio.support.ModuleConstants.MODULE_NAME;
import static org.folio.support.ModuleConstants.OPEN_LOAN_STATUS;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.function.Function;
import javax.validation.constraints.NotNull;
import javax.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.folio.rest.annotations.Validate;
import org.folio.rest.impl.util.OkapiResponseUtil;
import org.folio.rest.jaxrs.model.Error;
import org.folio.rest.jaxrs.model.Errors;
import org.folio.rest.jaxrs.model.Loan;
import org.folio.rest.jaxrs.model.Loans;
import org.folio.rest.jaxrs.model.LoansHistoryItem;
import org.folio.rest.jaxrs.model.LoansHistoryItems;
import org.folio.rest.jaxrs.model.Status;
import org.folio.rest.jaxrs.resource.LoanStorage;
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 org.folio.rest.tools.utils.ValidationHelper;
import org.folio.support.ResultHandlerFactory;
import org.folio.support.ServerErrorResponder;
import org.folio.support.UUIDValidation;
import org.folio.support.VertxContextRunner;
import org.joda.time.DateTime;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Handler;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
public class LoansAPI implements LoanStorage {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@Override
public void deleteLoanStorageLoans(
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.loan",
tenantId, MODULE_NAME),
reply -> asyncResultHandler.handle(succeededFuture(
DeleteLoanStorageLoansResponse.respond204())));
} catch (Exception e) {
asyncResultHandler.handle(succeededFuture(
LoanStorage.DeleteLoanStorageLoansResponse
.respond500WithTextPlain(e.getMessage())));
}
});
}
@Validate
@Override
public void getLoanStorageLoans(
int offset,
int limit,
String query,
String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.get(LOAN_TABLE, LOAN_CLASS, Loans.class, query, offset, limit, okapiHeaders, vertxContext,
GetLoanStorageLoansResponse.class, asyncResultHandler);
}
@Override
public void postLoanStorageLoans(
String lang,
Loan loan,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
if (loan.getStatus() == null) {
loan.setStatus(new Status().withName(OPEN_LOAN_STATUS));
}
if (isOpenAndHasNoUserId(loan)) {
respondWithError(asyncResultHandler,
PostLoanStorageLoansResponse::respond422WithApplicationJson,
"Open loan must have a user ID");
return;
}
//TODO: Convert this to use validation responses (422 and error of errors)
ImmutablePair<Boolean, String> validationResult = validateLoan(loan);
if (!validationResult.getLeft()) {
asyncResultHandler.handle(
succeededFuture(
LoanStorage.PostLoanStorageLoansResponse
.respond400WithTextPlain(
validationResult.getRight())));
return;
}
PgUtil.post(LOAN_TABLE, loan, okapiHeaders, vertxContext, PostLoanStorageLoansResponse.class, reply -> {
if (isMultipleOpenLoanError(reply)) {
asyncResultHandler.handle(
succeededFuture(LoanStorage.PostLoanStorageLoansResponse
.respond422WithApplicationJson(moreThanOneOpenLoanError(loan))));
return;
}
asyncResultHandler.handle(reply);
});
}
@Override
public void postLoanStorageLoansAnonymizeByUserId(
String userId,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> responseHandler,
Context vertxContext) {
final ServerErrorResponder serverErrorResponder =
new ServerErrorResponder(PostLoanStorageLoansAnonymizeByUserIdResponse
::respond500WithTextPlain, responseHandler, log);
final VertxContextRunner runner = new VertxContextRunner(
vertxContext, serverErrorResponder::withError);
runner.runOnContext(() -> {
if (!UUIDValidation.isValidUUID(userId)) {
final Errors errors = ValidationHelper.createValidationErrorMessage(
"userId", userId, "Invalid user ID, should be a UUID");
responseHandler.handle(succeededFuture(
PostLoanStorageLoansAnonymizeByUserIdResponse
.respond422WithApplicationJson(errors)));
return;
}
final String tenantId = TenantTool.tenantId(okapiHeaders);
final PostgresClient postgresClient = PostgresClient.getInstance(
vertxContext.owner(), tenantId);
final String combinedAnonymizationSql = createAnonymizationSQL(userId,
tenantId);
log.info(String.format("Anonymization SQL: %s", combinedAnonymizationSql));
postgresClient.execute(combinedAnonymizationSql, ResultHandlerFactory.when(
s -> responseHandler.handle(succeededFuture(
PostLoanStorageLoansAnonymizeByUserIdResponse.respond204())),
serverErrorResponder::withError));
});
}
@Validate
@Override
public void getLoanStorageLoansByLoanId(
String loanId,
String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.getById(LOAN_TABLE, LOAN_CLASS, loanId, okapiHeaders, vertxContext,
GetLoanStorageLoansByLoanIdResponse.class, asyncResultHandler);
}
@Override
public void deleteLoanStorageLoansByLoanId(
String loanId,
String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.deleteById(LOAN_TABLE, loanId, okapiHeaders, vertxContext,
DeleteLoanStorageLoansByLoanIdResponse.class, asyncResultHandler);
}
@Override
public void putLoanStorageLoansByLoanId(
String loanId,
String lang,
Loan loan,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
if (loan.getStatus() == null) {
loan.setStatus(new Status().withName(OPEN_LOAN_STATUS));
}
ImmutablePair<Boolean, String> validationResult = validateLoan(loan);
if (!validationResult.getLeft()) {
asyncResultHandler.handle(
succeededFuture(
LoanStorage.PutLoanStorageLoansByLoanIdResponse
.respond400WithTextPlain(
validationResult.getRight())));
return;
}
if (isOpenAndHasNoUserId(loan)) {
respondWithError(asyncResultHandler,
PutLoanStorageLoansByLoanIdResponse::respond422WithApplicationJson,
"Open loan must have a user ID");
return;
}
MyPgUtil.putUpsert204(LOAN_TABLE, loan, loanId, okapiHeaders, vertxContext,
PutLoanStorageLoansByLoanIdResponse.class, reply -> {
if (isMultipleOpenLoanErrorOnUpsert(reply)) {
asyncResultHandler.handle(
succeededFuture(
LoanStorage.PutLoanStorageLoansByLoanIdResponse
.respond422WithApplicationJson(
moreThanOneOpenLoanError(loan))));
return;
}
asyncResultHandler.handle(reply);
});
}
private ImmutablePair<Boolean, String> validateLoan(Loan loan) {
Boolean valid = true;
StringJoiner messages = new StringJoiner("\n");
//ISO8601 is less strict than RFC3339 so will not catch some issues
try {
DateTime.parse(loan.getLoanDate());
} catch (Exception e) {
valid = false;
messages.add("loan date must be a date time (in RFC3339 format)");
}
if (loan.getReturnDate() != null) {
//ISO8601 is less strict than RFC3339 so will not catch some issues
try {
DateTime.parse(loan.getReturnDate());
} catch (Exception e) {
valid = false;
messages.add("return date must be a date time (in RFC3339 format)");
}
}
return new ImmutablePair<>(valid, messages.toString());
}
@Validate
@Override
public void getLoanStorageLoanHistory(int offset, int limit, String query, String lang,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
String cql = query;
if (StringUtils.isBlank(cql)) {
cql = "cql.allRecords=1";
}
if (!cql.toLowerCase().contains(" sortby ")) {
cql += " sortBy createdDate/sort.descending";
}
PgUtil.get(LOAN_HISTORY_TABLE, LoansHistoryItem.class, LoansHistoryItems.class, cql, offset, limit, okapiHeaders,
vertxContext, GetLoanStorageLoanHistoryResponse.class, asyncResultHandler);
}
private Errors moreThanOneOpenLoanError(Loan entity) {
return ValidationHelper.createValidationErrorMessage(
"itemId", entity.getItemId(),
"Cannot have more than one open loan for the same item");
}
// Remove/Replace this function when MyPgUtil.putUpsert204() is removed/replaced.
private boolean isMultipleOpenLoanErrorOnUpsert(AsyncResult<Response> reply) {
return reply.succeeded()
&& reply.result().getStatus() == HTTP_BAD_REQUEST.toInt()
&& reply.result().hasEntity()
&& reply.result().getEntity().toString().contains("loan_itemid_idx_unique");
}
private boolean isMultipleOpenLoanError(AsyncResult<Response> reply) {
return OkapiResponseUtil.containsErrorMessage(
reply, "value already exists in table loan: ");
}
private boolean isOpenAndHasNoUserId(Loan loan) {
return Objects.equals(loan.getStatus().getName(), OPEN_LOAN_STATUS)
&& loan.getUserId() == null;
}
private void respondWithError(
Handler<AsyncResult<Response>> asyncResultHandler,
Function<Errors, Response> responseCreator,
String message) {
final ArrayList<Error> errorsList = new ArrayList<>();
errorsList.add(new Error().withMessage(message));
final Errors errors = new Errors()
.withErrors(errorsList);
asyncResultHandler.handle(succeededFuture(
responseCreator.apply(errors)));
}
private String createAnonymizationSQL(
@NotNull String userId,
String tenantId) {
final String anonymizeLoansSql = String.format(
"UPDATE %s_%s.loan"
+ " SET jsonb = jsonb - 'userId'"
+ " WHERE jsonb->>'userId' = '" + userId + "'"
+ " AND jsonb->'status'->>'name' = 'Closed'",
tenantId, MODULE_NAME);
//Only anonymize the history for loans that are currently closed
//meaning that we need to refer to loans in this query
final String anonymizeLoansActionHistorySql = String.format(
"UPDATE %s_%s.%s"
+ " SET jsonb = jsonb #- '{loan,userId}'"
+ " WHERE jsonb->'loan'->>'id' IN"
+ " (SELECT l.jsonb->>'id'"
+ " FROM %s_%s.loan l"
+ " WHERE l.jsonb->>'userId' = '" + userId + "'"
+ " AND l.jsonb->'status'->>'name' = 'Closed')",
tenantId, MODULE_NAME, LOAN_HISTORY_TABLE,
tenantId, MODULE_NAME);
//Loan action history needs to go first, as needs to be for specific loans
return anonymizeLoansActionHistorySql + "; " + anonymizeLoansSql;
}
}