forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckOutLockAPI.java
221 lines (192 loc) · 9.02 KB
/
CheckOutLockAPI.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
package org.folio.rest.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Handler;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.rest.RestVerticle;
import org.folio.rest.jaxrs.model.CheckoutLock;
import org.folio.rest.jaxrs.model.CheckoutLockRequest;
import org.folio.rest.jaxrs.model.CheckoutLocks;
import org.folio.rest.jaxrs.resource.CheckOutLockStorage;
import org.folio.rest.persist.PostgresClient;
import org.folio.util.UuidUtil;
import javax.ws.rs.core.Response;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import static io.vertx.core.Future.succeededFuture;
import static org.folio.rest.persist.PgUtil.postgresClient;
import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard;
import static org.folio.support.DbUtil.rowSetToStream;
public class CheckOutLockAPI implements CheckOutLockStorage {
private static final String CHECK_OUT_LOCK_TABLE = "check_out_lock";
private static final Logger log = LogManager.getLogger();
@Override
public void getCheckOutLockStorageByLockId(String lockId, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
log.debug("getCheckOutLockStorageByLockId:: getting lock with lockId {} ", lockId);
String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT);
PostgresClient postgresClient = postgresClient(vertxContext, okapiHeaders);
if (!UuidUtil.isUuid(lockId)) {
asyncResultHandler.handle(succeededFuture(
GetCheckOutLockStorageByLockIdResponse.respond400WithTextPlain("Invalid lock id")));
return;
}
postgresClient.execute(getLockByIdSql(lockId, tenantId), handler -> {
if (handler.succeeded()) {
asyncResultHandler.handle(succeededFuture(
GetCheckOutLockStorageByLockIdResponse.respond200WithApplicationJson(
this.mapToCheckOutLock(handler.result()))));
} else {
asyncResultHandler.handle(succeededFuture(
GetCheckOutLockStorageByLockIdResponse.respond404()));
}
});
}
@Override
public void getCheckOutLockStorage(String userId, int offset, int limit,
Map<String, String> okapiHeaders,Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
log.debug("getCheckOutLockStorage:: getting locks");
String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT);
PostgresClient postgresClient = postgresClient(vertxContext, okapiHeaders);
postgresClient.execute(getLocksSqlWithQueryParams(tenantId, userId, offset, limit),
handler -> {
if (handler.succeeded()) {
asyncResultHandler.handle(succeededFuture(
GetCheckOutLockStorageResponse.respond200WithApplicationJson(
this.mapToCheckOutLocks(handler.result()))));
} else {
asyncResultHandler.handle(succeededFuture(
GetCheckOutLockStorageResponse.respond422WithTextPlain("Invalid Parameters")));
}
});
}
@Override
public void postCheckOutLockStorage(CheckoutLockRequest entity, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
log.debug("postCheckOutLockStorage:: entity {} {} ", entity.getUserId(), entity.getTtlMs());
PostgresClient postgresClient = postgresClient(vertxContext, okapiHeaders);
String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT);
postgresClient.execute(deleteOutdatedLockSql(entity.getUserId(), tenantId, entity.getTtlMs()),
deleteLockHandler -> {
try {
if (deleteLockHandler.succeeded()) {
log.info("postCheckOutLockStorage:: creating lock for the userId {} ",
entity.getUserId());
postgresClient.execute(insertSql(entity.getUserId(), tenantId),
createLockHandler -> {
try {
if (createLockHandler.succeeded()) {
log.info("postCheckOutLockStorage:: New lock is created for the userId {} ",
entity.getUserId());
asyncResultHandler.handle(succeededFuture(
PostCheckOutLockStorageResponse.respond201WithApplicationJson(
this.mapToCheckOutLock(createLockHandler.result()))));
} else {
log.info("postCheckOutLockStorage:: Unable to create a lock ",
createLockHandler.cause());
respondWith503Error(asyncResultHandler);
}
} catch (Exception ex) {
log.warn("postCheckOutLockStorage:: Exception caught while creating lock ", ex);
asyncResultHandler.handle(succeededFuture(
PostCheckOutLockStorageResponse.respond500WithTextPlain(ex.getMessage())));
}
});
} else {
log.warn("postCheckOutLockStorage:: Deleting outdated lock is failed ",
deleteLockHandler.cause());
respondWith503Error(asyncResultHandler);
}
} catch (Exception ex) {
log.warn("postCheckOutLockStorage:: Exception caught while deleting lock ", ex);
asyncResultHandler.handle(succeededFuture(
PostCheckOutLockStorageResponse.respond500WithTextPlain(ex.getMessage())));
}
});
}
private void respondWith503Error(Handler<AsyncResult<Response>> asyncResultHandler) {
asyncResultHandler.handle(succeededFuture(
PostCheckOutLockStorageResponse.respond503WithTextPlain("Unable to acquire lock")));
}
@Override
public void deleteCheckOutLockStorageByLockId(String lockId, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
log.debug("deleteCheckOutLockStorageByLockId:: deleting lock with lockId {} ", lockId);
String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT);
PostgresClient postgresClient = postgresClient(vertxContext, okapiHeaders);
if (!UuidUtil.isUuid(lockId)) {
asyncResultHandler.handle(succeededFuture(
PostCheckOutLockStorageResponse.respond400WithTextPlain("Invalid lock id")));
return;
}
postgresClient.execute(deleteLockByIdSql(lockId, tenantId), handler -> {
if (handler.succeeded()) {
asyncResultHandler.handle(
succeededFuture(DeleteCheckOutLockStorageByLockIdResponse.respond204()));
} else {
asyncResultHandler.handle(
succeededFuture(DeleteCheckOutLockStorageByLockIdResponse.respond500WithTextPlain(
handler.cause())));
}
});
}
private String getLocksSqlWithQueryParams(String tenantId, String userId, int offset,
int limit) {
return String.format("select * from %s.%s where user_id = '%s' OFFSET '%s' LIMIT '%s'",
convertToPsqlStandard(tenantId), CHECK_OUT_LOCK_TABLE, userId, offset, limit);
}
private String getLockByIdSql(String lockId, String tenantId) {
return String.format("select * from %s.%s where id = '%s'", convertToPsqlStandard(tenantId),
CHECK_OUT_LOCK_TABLE, lockId);
}
private String deleteLockByIdSql(String lockId, String tenantId) {
return String.format("delete from %s.%s where id = '%s'", convertToPsqlStandard(tenantId),
CHECK_OUT_LOCK_TABLE, lockId);
}
private String insertSql(String userId, String tenantId) {
return String.format("Insert into %s.%s (id, user_id) values ('%s','%s') " +
"returning id,user_id,creation_date",
convertToPsqlStandard(tenantId), CHECK_OUT_LOCK_TABLE, UUID.randomUUID(), userId);
}
private String deleteOutdatedLockSql(String userId, String tenantId, int ttlMs) {
return String.format("delete from %s.%s where user_id = '%s' and " +
"creation_date + interval '%s milliseconds' < current_timestamp",
convertToPsqlStandard(tenantId), CHECK_OUT_LOCK_TABLE, userId, ttlMs);
}
private CheckoutLock mapToCheckOutLock(RowSet<Row> rowSet) {
log.debug("mapToCheckOutLock:: rowSet {} ", rowSet.size());
if (rowSet.size() == 0) {
return null;
}
return rowSetToStream(rowSet).map(row -> new CheckoutLock()
.withId(row.getUUID("id").toString())
.withUserId(row.getUUID("user_id").toString())
.withCreationDate(Date.from(
row.getLocalDateTime("creation_date").atZone(ZoneId.systemDefault()).toInstant())))
.toList().get(0);
}
private CheckoutLocks mapToCheckOutLocks(RowSet<Row> rowSet) {
log.debug("mapToCheckOutLocks:: rowSet {} ", rowSet.size());
if (rowSet.size() == 0) {
return null;
}
List<CheckoutLock> checkoutLocks = rowSetToStream(rowSet)
.map(row -> new CheckoutLock()
.withId(row.getUUID("id").toString())
.withUserId(row.getUUID("user_id").toString())
.withCreationDate(Date.from(
row.getLocalDateTime("creation_date").atZone(ZoneId.systemDefault()).toInstant())))
.toList();
CheckoutLocks response = new CheckoutLocks();
response.setCheckoutLocks(checkoutLocks);
response.setTotalRecords(checkoutLocks.size());
return response;
}
}