forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaffSlipsAPI.java
152 lines (113 loc) · 5.67 KB
/
StaffSlipsAPI.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
package org.folio.rest.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Handler;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.folio.rest.jaxrs.model.StaffSlip;
import org.folio.rest.jaxrs.model.StaffSlips;
import org.folio.rest.jaxrs.resource.LoanStorage;
import org.folio.rest.jaxrs.resource.StaffSlipsStorage;
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 java.util.StringJoiner;
import static io.vertx.core.Future.succeededFuture;
import static org.folio.rest.impl.Headers.TENANT_HEADER;
public class StaffSlipsAPI implements StaffSlipsStorage {
private static final String STAFF_SLIP_TABLE = "staff_slips";
private static final Class<StaffSlip> STAFF_SLIP_CLASS = StaffSlip.class;
@Override
public void deleteStaffSlipsStorageStaffSlips(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(
// TODO: Need to add 204 response to staff slips RAML interface definition!
String.format("TRUNCATE TABLE %s_%s.%s", tenantId, "mod_circulation_storage", STAFF_SLIP_TABLE),
reply -> asyncResultHandler.handle(succeededFuture(
DeleteStaffSlipsStorageStaffSlipsResponse.noContent().build())));
} catch (Exception e) {
asyncResultHandler
.handle(succeededFuture(
StaffSlipsStorage.DeleteStaffSlipsStorageStaffSlipsResponse
.respond500WithTextPlain(e.getMessage())));
}
});
}
@Override
public void getStaffSlipsStorageStaffSlips(int offset, int limit, String query, String lang,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.get(STAFF_SLIP_TABLE, STAFF_SLIP_CLASS, StaffSlips.class, query, offset, limit, okapiHeaders, vertxContext,
GetStaffSlipsStorageStaffSlipsResponse.class, asyncResultHandler);
}
@Override
public void postStaffSlipsStorageStaffSlips(String lang, StaffSlip entity, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
ImmutablePair<Boolean, String> validationResult = validateStaffSlip(entity);
if (!validationResult.getLeft()) {
asyncResultHandler.handle(succeededFuture(StaffSlipsStorage.PostStaffSlipsStorageStaffSlipsResponse
.respond400WithTextPlain(validationResult.getRight())));
return;
}
createStaffSlip(entity, okapiHeaders, asyncResultHandler, vertxContext);
}
@Override
public void getStaffSlipsStorageStaffSlipsByStaffSlipId(String staffSlipId, String lang,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.getById(STAFF_SLIP_TABLE, STAFF_SLIP_CLASS, staffSlipId, okapiHeaders, vertxContext,
GetStaffSlipsStorageStaffSlipsByStaffSlipIdResponse.class, asyncResultHandler);
}
@Override
public void deleteStaffSlipsStorageStaffSlipsByStaffSlipId(String staffSlipId, String lang,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.deleteById(STAFF_SLIP_TABLE, staffSlipId, okapiHeaders, vertxContext,
DeleteStaffSlipsStorageStaffSlipsByStaffSlipIdResponse.class, asyncResultHandler);
}
@Override
public void putStaffSlipsStorageStaffSlipsByStaffSlipId(String staffSlipId, String lang, StaffSlip entity,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
ImmutablePair<Boolean, String> validationResult = validateStaffSlip(entity);
if (!validationResult.getLeft()) {
asyncResultHandler.handle(succeededFuture(
LoanStorage.PostLoanStorageLoansResponse.respond400WithTextPlain(validationResult.getRight())));
return;
}
PgUtil.put(STAFF_SLIP_TABLE, entity, staffSlipId, okapiHeaders, vertxContext,
PutStaffSlipsStorageStaffSlipsByStaffSlipIdResponse.class, reply -> {
if (reply.failed() || reply.result().getStatus() != 404) {
asyncResultHandler.handle(reply);
return;
}
entity.setId(staffSlipId);
// FIXME: This returns the non-declared 201 status code by using
// PostStaffSlipsStorageStaffSlipsResponse
createStaffSlip(entity, okapiHeaders, asyncResultHandler, vertxContext);
});
}
private void createStaffSlip(StaffSlip entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
if (entity.getActive() == null) {
entity.setActive(true);
}
PgUtil.post(STAFF_SLIP_TABLE, entity, okapiHeaders, vertxContext,
PostStaffSlipsStorageStaffSlipsResponse.class, asyncResultHandler);
}
private ImmutablePair<Boolean, String> validateStaffSlip(StaffSlip staffSlip) {
Boolean valid = true;
StringJoiner messages = new StringJoiner("\n");
if (staffSlip.getName() == null || staffSlip.getName().equals("")) {
valid = false;
messages.add("Name is a required property");
}
if (staffSlip.getTemplate() == null || staffSlip.getTemplate().equals("")) {
valid = false;
messages.add("Template is a required property");
}
return new ImmutablePair<>(valid, messages.toString());
}
}