forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatronNoticePoliciesAPI.java
344 lines (296 loc) · 13.9 KB
/
PatronNoticePoliciesAPI.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
package org.folio.rest.impl;
import static io.vertx.core.Future.failedFuture;
import static io.vertx.core.Future.succeededFuture;
import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import static org.folio.rest.impl.CirculationRulesAPI.CIRCULATION_RULES_TABLE;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import org.folio.cql2pgjson.CQL2PgJSON;
import org.folio.rest.RestVerticle;
import org.folio.rest.jaxrs.model.CirculationRules;
import org.folio.rest.jaxrs.model.Error;
import org.folio.rest.jaxrs.model.Errors;
import org.folio.rest.jaxrs.model.LoanNotice;
import org.folio.rest.jaxrs.model.PatronNoticePolicies;
import org.folio.rest.jaxrs.model.PatronNoticePolicy;
import org.folio.rest.jaxrs.model.SendOptions;
import org.folio.rest.jaxrs.resource.FixedDueDateScheduleStorage;
import org.folio.rest.jaxrs.resource.PatronNoticePolicyStorage;
import org.folio.rest.persist.PgUtil;
import org.folio.rest.persist.PostgresClient;
import org.folio.rest.persist.Criteria.Criterion;
import org.folio.rest.persist.Criteria.Limit;
import org.folio.rest.persist.Criteria.Offset;
import org.folio.rest.persist.cql.CQLWrapper;
import org.folio.rest.persist.interfaces.Results;
import org.folio.rest.tools.utils.ValidationHelper;
import org.folio.support.exception.NoticePolicyInUseException;
public class PatronNoticePoliciesAPI implements PatronNoticePolicyStorage {
private static final Logger logger = LoggerFactory.getLogger(PatronNoticePoliciesAPI.class);
public static final String PATRON_NOTICE_POLICY_TABLE = "patron_notice_policy";
public static final String STATUS_CODE_DUPLICATE_NAME = "duplicate.name";
public static final String NOT_FOUND = "Not found";
private static final String INTERNAL_SERVER_ERROR = "Internal server error";
private static final String IN_USE_POLICY_ERROR_MESSAGE = "Cannot delete in use notice policy";
private static final String VALIDATION_ERROR_MESSAGE = "This option is not valid for selected";
@Override
public void getPatronNoticePolicyStoragePatronNoticePolicies(
int offset,
int limit,
String query,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
vertxContext.runOnContext(v -> {
try {
String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT);
PostgresClient pgClient = PostgresClient.getInstance(vertxContext.owner(), tenantId);
String[] fieldList = {"*"};
CQL2PgJSON cql2pgJson = new CQL2PgJSON(PATRON_NOTICE_POLICY_TABLE + ".jsonb");
CQLWrapper cql = new CQLWrapper(cql2pgJson, query)
.setLimit(new Limit(limit))
.setOffset(new Offset(offset));
pgClient.get(PATRON_NOTICE_POLICY_TABLE, PatronNoticePolicy.class, fieldList, cql, true, get -> {
if (get.failed()) {
logger.error(get.cause());
asyncResultHandler.handle(succeededFuture(
GetPatronNoticePolicyStoragePatronNoticePoliciesResponse.respond500WithTextPlain(get.cause())));
return;
}
PatronNoticePolicies patronNoticePolicies = new PatronNoticePolicies()
.withPatronNoticePolicies(get.result().getResults())
.withTotalRecords(get.result().getResultInfo().getTotalRecords());
asyncResultHandler.handle(succeededFuture(
GetPatronNoticePolicyStoragePatronNoticePoliciesResponse
.respond200WithApplicationJson(patronNoticePolicies)));
});
} catch (Exception e) {
logger.error(e);
asyncResultHandler.handle(succeededFuture(
GetPatronNoticePolicyStoragePatronNoticePoliciesResponse.respond500WithTextPlain(e)));
}
});
}
@Override
public void postPatronNoticePolicyStoragePatronNoticePolicies(
PatronNoticePolicy entity,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
vertxContext.runOnContext(v -> {
try {
String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT);
PostgresClient pgClient = PostgresClient.getInstance(vertxContext.owner(), tenantId);
if (entity.getId() == null) {
entity.setId(UUID.randomUUID().toString());
}
pgClient.save(PATRON_NOTICE_POLICY_TABLE, entity.getId(), entity, save -> {
if (save.failed()) {
logger.error(save.cause());
if (ValidationHelper.isDuplicate(save.cause().getMessage())) {
asyncResultHandler.handle(succeededFuture(
PostPatronNoticePolicyStoragePatronNoticePoliciesResponse
.respond422WithApplicationJson(createNotUniqueNameErrors(entity.getName()))));
}
asyncResultHandler.handle(succeededFuture(
PostPatronNoticePolicyStoragePatronNoticePoliciesResponse.respond500WithTextPlain(save.cause())));
return;
}
asyncResultHandler.handle(succeededFuture(
PostPatronNoticePolicyStoragePatronNoticePoliciesResponse.respond201WithApplicationJson(entity)));
});
} catch (Exception e) {
logger.error(e);
asyncResultHandler.handle(succeededFuture(
PostPatronNoticePolicyStoragePatronNoticePoliciesResponse.respond500WithTextPlain(e)));
}
});
}
@Override
public void getPatronNoticePolicyStoragePatronNoticePoliciesByPatronNoticePolicyId(
String patronNoticePolicyId,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.getById(PATRON_NOTICE_POLICY_TABLE, PatronNoticePolicy.class, patronNoticePolicyId, okapiHeaders,
vertxContext, GetPatronNoticePolicyStoragePatronNoticePoliciesByPatronNoticePolicyIdResponse.class,
asyncResultHandler);
}
@Override
public void deletePatronNoticePolicyStoragePatronNoticePoliciesByPatronNoticePolicyId(
String patronNoticePolicyId,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PostgresClient pgClient = PgUtil.postgresClient(vertxContext, okapiHeaders);
findCirculationRules(pgClient)
.map(CirculationRules::getRulesAsText)
.map(text -> text.contains(patronNoticePolicyId))
.compose(contains -> contains ? failedFuture(new NoticePolicyInUseException(IN_USE_POLICY_ERROR_MESSAGE)) : succeededFuture())
.compose(v -> deleteNoticePolicyById(pgClient, patronNoticePolicyId))
.map(v -> DeletePatronNoticePolicyStoragePatronNoticePoliciesByPatronNoticePolicyIdResponse.respond204())
.map(Response.class::cast)
.otherwise(this::mapExceptionToResponse)
.onComplete(asyncResultHandler);
}
@Override
public void putPatronNoticePolicyStoragePatronNoticePoliciesByPatronNoticePolicyId(
String patronNoticePolicyId,
PatronNoticePolicy entity,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
Errors errors = validateEntity(entity);
if (!errors.getErrors().isEmpty()) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(
FixedDueDateScheduleStorage.PostFixedDueDateScheduleStorageFixedDueDateSchedulesResponse
.respond422WithApplicationJson(errors)));
return;
}
vertxContext.runOnContext(v -> {
try {
String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT);
PostgresClient pgClient = PostgresClient.getInstance(vertxContext.owner(), tenantId);
pgClient.update(PATRON_NOTICE_POLICY_TABLE, entity, patronNoticePolicyId, update -> {
if (update.failed()) {
logger.error(update.cause());
if (ValidationHelper.isDuplicate(update.cause().getMessage())) {
asyncResultHandler.handle(succeededFuture(
PutPatronNoticePolicyStoragePatronNoticePoliciesByPatronNoticePolicyIdResponse
.respond422WithApplicationJson(createNotUniqueNameErrors(entity.getName()))));
return;
}
asyncResultHandler.handle(succeededFuture(
PutPatronNoticePolicyStoragePatronNoticePoliciesByPatronNoticePolicyIdResponse
.respond500WithTextPlain(update.cause())));
return;
}
if (update.result().rowCount() == 0) {
asyncResultHandler.handle(succeededFuture(
PutPatronNoticePolicyStoragePatronNoticePoliciesByPatronNoticePolicyIdResponse
.respond404WithTextPlain(NOT_FOUND)));
return;
}
asyncResultHandler.handle(succeededFuture(
PutPatronNoticePolicyStoragePatronNoticePoliciesByPatronNoticePolicyIdResponse.respond204()));
});
} catch (Exception e) {
logger.error(e);
asyncResultHandler.handle(succeededFuture(
PutPatronNoticePolicyStoragePatronNoticePoliciesByPatronNoticePolicyIdResponse.respond500WithTextPlain(e)));
}
});
}
private Errors createNotUniqueNameErrors(String name) {
Error error = new Error();
error.setMessage(format("'%s' name in not unique", name));
error.setCode(STATUS_CODE_DUPLICATE_NAME);
return new Errors().withErrors(singletonList(error));
}
private Future<CirculationRules> findCirculationRules(PostgresClient pgClient) {
Promise<Results<CirculationRules>> promise = Promise.promise();
pgClient.get(CIRCULATION_RULES_TABLE, CirculationRules.class, new Criterion(), false, promise.future());
return promise.future().map(Results::getResults)
.compose(list -> list.size() == 1 ? succeededFuture(list) :
failedFuture(new IllegalStateException("Number of records in circulation_rules table is " + list.size())))
.map(list -> list.get(0));
}
private Future<Void> deleteNoticePolicyById(PostgresClient pgClient, String id) {
final Promise<RowSet<Row>> promise = Promise.promise();
pgClient.delete(PATRON_NOTICE_POLICY_TABLE, id, promise);
return promise.future().map(RowSet::rowCount)
.compose(updated -> updated > 0 ? succeededFuture() : failedFuture(new NotFoundException(NOT_FOUND)));
}
private Response mapExceptionToResponse(Throwable t) {
if (t.getClass() == NotFoundException.class) {
return Response.status(404)
.header(CONTENT_TYPE, TEXT_PLAIN)
.entity(t.getMessage())
.build();
}
if (t.getClass() == NoticePolicyInUseException.class) {
return Response.status(400)
.header(CONTENT_TYPE, TEXT_PLAIN)
.entity(t.getMessage())
.build();
}
return Response.status(500)
.header(CONTENT_TYPE, TEXT_PLAIN)
.entity(INTERNAL_SERVER_ERROR)
.build();
}
private Errors validateEntity(PatronNoticePolicy entity) {
List<Error> loanNoticesErrors = entity.getLoanNotices().stream()
.map(this::validateLoanNotice)
.map(Errors::getErrors)
.flatMap(Collection::stream)
.collect(Collectors.toList());
return new Errors().withErrors(loanNoticesErrors);
}
private Errors validateLoanNotice(LoanNotice loanNotice) {
List<Errors> errors = new ArrayList<>();
SendOptions sendOptions = loanNotice.getSendOptions();
if (sendOptions.getSendWhen() != SendOptions.SendWhen.DUE_DATE) {
errors.add(validateSendOptionsForNotDueDate(sendOptions));
} else {
errors.add(validateFrequencyForDueDate(loanNotice));
}
errors.add(validateOneTimeFrequency(loanNotice));
List<Error> mergedErrors = errors.stream()
.map(Errors::getErrors)
.flatMap(Collection::stream)
.collect(Collectors.toList());
return new Errors().withErrors(mergedErrors);
}
private Errors validateFrequencyForDueDate(LoanNotice loanNotice) {
SendOptions sendOptions = loanNotice.getSendOptions();
if (sendOptions.getSendHow() != SendOptions.SendHow.BEFORE
&& sendOptions.getSendHow() != SendOptions.SendHow.AFTER && loanNotice.getFrequency() != null) {
return ValidationHelper.createValidationErrorMessage("frequency",
loanNotice.getFrequency().toString(), VALIDATION_ERROR_MESSAGE + " Send");
}
return new Errors().withErrors(Collections.emptyList());
}
private Errors validateOneTimeFrequency(LoanNotice loanNotice) {
if (loanNotice.getFrequency() != null && loanNotice.getFrequency() == LoanNotice.Frequency.ONE_TIME
&& loanNotice.getSendOptions().getSendEvery() != null) {
return ValidationHelper.createValidationErrorMessage("sendEvery",
loanNotice.getSendOptions().getSendEvery().toString(), VALIDATION_ERROR_MESSAGE + " Frequency");
}
return new Errors().withErrors(Collections.emptyList());
}
private Errors validateSendOptionsForNotDueDate(SendOptions sendOptions) {
if (sendOptions.getSendHow() != null) {
return ValidationHelper.createValidationErrorMessage("sendHow",
sendOptions.getSendHow().toString(), VALIDATION_ERROR_MESSAGE + " Triggering event");
}
if (sendOptions.getSendBy() != null) {
return ValidationHelper.createValidationErrorMessage("sendBy",
sendOptions.getSendBy().toString(), VALIDATION_ERROR_MESSAGE + " Triggering event");
}
if (sendOptions.getSendEvery() != null) {
return ValidationHelper.createValidationErrorMessage("sendEvery",
sendOptions.getSendEvery().toString(), VALIDATION_ERROR_MESSAGE + " Triggering event");
}
return new Errors().withErrors(Collections.emptyList());
}
}