forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CancellationReasonsAPI.java
117 lines (106 loc) · 4.89 KB
/
CancellationReasonsAPI.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
package org.folio.rest.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import org.folio.rest.annotations.Validate;
import org.folio.rest.jaxrs.model.CancellationReason;
import org.folio.rest.jaxrs.model.CancellationReasons;
import org.folio.rest.jaxrs.resource.CancellationReasonStorage;
import org.folio.rest.persist.PgUtil;
import org.folio.rest.persist.PostgresClient;
import org.folio.rest.tools.PomReader;
import javax.ws.rs.core.Response;
import java.util.Map;
import static org.folio.rest.impl.Headers.TENANT_HEADER;
/**
*
* @author kurt
*/
public class CancellationReasonsAPI implements CancellationReasonStorage {
private static final Logger logger = LoggerFactory.getLogger(CancellationReasonsAPI.class);
private static final String TABLE_NAME = "cancellation_reason";
private boolean suppressErrorResponse = false;
private String logAndSaveError(Throwable err) {
String message = err.getLocalizedMessage();
logger.error(message, err);
return message;
}
private String getErrorResponse(String response) {
if(suppressErrorResponse) {
return "Internal Server Error: Please contact Admin";
}
return response;
}
@Override
public void deleteCancellationReasonStorageCancellationReasons(String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
try {
String tenantId = okapiHeaders.get(TENANT_HEADER);
String deleteAllQuery = String.format("DELETE FROM %s_%s.%s", tenantId,
PomReader.INSTANCE.getModuleName(), TABLE_NAME);
PostgresClient.getInstance(vertxContext.owner(), tenantId).execute(deleteAllQuery,
mutateReply -> {
if(mutateReply.failed()) {
String message = logAndSaveError(mutateReply.cause());
asyncResultHandler.handle(Future.succeededFuture(
DeleteCancellationReasonStorageCancellationReasonsResponse
.respond500WithTextPlain(getErrorResponse(message))));
} else {
asyncResultHandler.handle(Future.succeededFuture(
DeleteCancellationReasonStorageCancellationReasonsResponse
.noContent().build()));
}
});
} catch(Exception e) {
String message = logAndSaveError(e);
asyncResultHandler.handle(Future.succeededFuture(
DeleteCancellationReasonStorageCancellationReasonsResponse
.respond500WithTextPlain(getErrorResponse(message))));
}
}
@Override
public void getCancellationReasonStorageCancellationReasons(int offset,
int limit, String query, String lang, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.get(TABLE_NAME, CancellationReason.class, CancellationReasons.class,
query, offset, limit, okapiHeaders, vertxContext,
GetCancellationReasonStorageCancellationReasonsResponse.class, asyncResultHandler);
}
@Override
@Validate
public void postCancellationReasonStorageCancellationReasons(String lang,
CancellationReason entity, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.post(TABLE_NAME, entity, okapiHeaders, vertxContext,
PostCancellationReasonStorageCancellationReasonsResponse.class, asyncResultHandler);
}
@Override
public void getCancellationReasonStorageCancellationReasonsByCancellationReasonId(
String cancellationReasonId, String lang, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.getById(TABLE_NAME, CancellationReason.class, cancellationReasonId, okapiHeaders, vertxContext,
GetCancellationReasonStorageCancellationReasonsByCancellationReasonIdResponse.class, asyncResultHandler);
}
@Override
public void deleteCancellationReasonStorageCancellationReasonsByCancellationReasonId(
String cancellationReasonId, String lang, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.deleteById(TABLE_NAME, cancellationReasonId, okapiHeaders, vertxContext,
DeleteCancellationReasonStorageCancellationReasonsByCancellationReasonIdResponse.class, asyncResultHandler);
}
@Override
@Validate
public void putCancellationReasonStorageCancellationReasonsByCancellationReasonId(
String cancellationReasonId, String lang, CancellationReason entity,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.put(TABLE_NAME, entity, cancellationReasonId, okapiHeaders, vertxContext,
PutCancellationReasonStorageCancellationReasonsByCancellationReasonIdResponse.class, asyncResultHandler);
}
}