forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduledNoticesAPI.java
More file actions
147 lines (115 loc) · 5.39 KB
/
Copy pathScheduledNoticesAPI.java
File metadata and controls
147 lines (115 loc) · 5.39 KB
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
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 javax.ws.rs.core.HttpHeaders.CONTENT_TYPE;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard;
import java.util.Map;
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 org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.commons.lang3.StringUtils;
import org.folio.cql2pgjson.CQL2PgJSON;
import org.folio.cql2pgjson.exception.FieldException;
import org.folio.cql2pgjson.exception.QueryValidationException;
import org.folio.rest.RestVerticle;
import org.folio.rest.annotations.Validate;
import org.folio.rest.jaxrs.model.ScheduledNotice;
import org.folio.rest.jaxrs.model.ScheduledNotices;
import org.folio.rest.jaxrs.resource.ScheduledNoticeStorage;
import org.folio.rest.persist.PgUtil;
import org.folio.rest.persist.PostgresClient;
public class ScheduledNoticesAPI implements ScheduledNoticeStorage {
private static final Logger logger = LogManager.getLogger();
private static final String SCHEDULED_NOTICE_TABLE = "scheduled_notice";
private static final String INTERNAL_SERVER_ERROR = "Internal Server Error";
@Validate
@Override
public void getScheduledNoticeStorageScheduledNotices(String totalRecords, int offset, int limit,
String query, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.get(SCHEDULED_NOTICE_TABLE, ScheduledNotice.class, ScheduledNotices.class, query, offset, limit,
okapiHeaders, vertxContext, GetScheduledNoticeStorageScheduledNoticesResponse.class, asyncResultHandler);
}
@Validate
@Override
public void deleteScheduledNoticeStorageScheduledNotices(String query,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PostgresClient pgClient = PgUtil.postgresClient(vertxContext, okapiHeaders);
cqlToSqlDeleteQuery(query, okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT))
.compose(sql -> pgClient.execute(sql))
.map(v -> DeleteScheduledNoticeStorageScheduledNoticesResponse.respond204())
.map(Response.class::cast)
.otherwise(this::mapExceptionToResponse)
.onComplete(asyncResultHandler);
}
@Validate
@Override
public void postScheduledNoticeStorageScheduledNotices(ScheduledNotice entity,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.post(SCHEDULED_NOTICE_TABLE, entity, okapiHeaders, vertxContext,
PostScheduledNoticeStorageScheduledNoticesResponse.class, asyncResultHandler);
}
@Validate
@Override
public void getScheduledNoticeStorageScheduledNoticesByScheduledNoticeId(String scheduledNoticeId,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.getById(SCHEDULED_NOTICE_TABLE, ScheduledNotice.class, scheduledNoticeId, okapiHeaders, vertxContext,
GetScheduledNoticeStorageScheduledNoticesByScheduledNoticeIdResponse.class, asyncResultHandler);
}
@Validate
@Override
public void deleteScheduledNoticeStorageScheduledNoticesByScheduledNoticeId(
String scheduledNoticeId, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.deleteById(SCHEDULED_NOTICE_TABLE, scheduledNoticeId, okapiHeaders, vertxContext,
DeleteScheduledNoticeStorageScheduledNoticesByScheduledNoticeIdResponse.class, asyncResultHandler);
}
@Validate
@Override
public void putScheduledNoticeStorageScheduledNoticesByScheduledNoticeId(String scheduledNoticeId,
ScheduledNotice entity, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.put(SCHEDULED_NOTICE_TABLE, entity, scheduledNoticeId, okapiHeaders, vertxContext,
PutScheduledNoticeStorageScheduledNoticesByScheduledNoticeIdResponse.class, asyncResultHandler);
}
private Future<String> cqlToSqlDeleteQuery(String cql, String tenant) {
String sql = format("DELETE FROM %s.%s", convertToPsqlStandard(tenant), SCHEDULED_NOTICE_TABLE);
if (StringUtils.isEmpty(cql)) {
return succeededFuture(sql);
} else {
return cqlToSqlQuery(cql)
.map(where -> sql + " WHERE " + where);
}
}
private Future<String> cqlToSqlQuery(String cql) {
try {
return succeededFuture(new CQL2PgJSON("jsonb").cql2pgJson(cql));
} catch (FieldException | QueryValidationException e) {
return failedFuture(e);
}
}
private Response mapExceptionToResponse(Throwable t) {
logger.error(t.getMessage(), t);
if (t.getClass() == QueryValidationException.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();
}
}