forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScheduledNoticesAPI.java
161 lines (125 loc) · 7 KB
/
ScheduledNoticesAPI.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
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 io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.sql.UpdateResult;
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.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 = LoggerFactory.getLogger(ScheduledNoticeStorage.class);
private static final String SCHEDULED_NOTICE_TABLE = "scheduled_notice";
private static final String INTERNAL_SERVER_ERROR = "Internal Server Error";
@Override
public void getScheduledNoticeStorageScheduledNotices(int offset,
int limit,
String query,
String lang,
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);
}
@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 -> executeSql(pgClient, sql))
.map(v -> DeleteScheduledNoticeStorageScheduledNoticesResponse.respond204())
.map(Response.class::cast)
.otherwise(this::mapExceptionToResponse)
.setHandler(asyncResultHandler);
}
@Override
public void postScheduledNoticeStorageScheduledNotices(String lang,
ScheduledNotice entity,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.post(SCHEDULED_NOTICE_TABLE, entity, okapiHeaders, vertxContext,
PostScheduledNoticeStorageScheduledNoticesResponse.class, asyncResultHandler);
}
@Override
public void getScheduledNoticeStorageScheduledNoticesByScheduledNoticeId(String scheduledNoticeId,
String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.getById(SCHEDULED_NOTICE_TABLE, ScheduledNotice.class, scheduledNoticeId, okapiHeaders, vertxContext,
GetScheduledNoticeStorageScheduledNoticesByScheduledNoticeIdResponse.class, asyncResultHandler);
}
@Override
public void deleteScheduledNoticeStorageScheduledNoticesByScheduledNoticeId(String scheduledNoticeId,
String lang, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.deleteById(SCHEDULED_NOTICE_TABLE, scheduledNoticeId, okapiHeaders, vertxContext,
DeleteScheduledNoticeStorageScheduledNoticesByScheduledNoticeIdResponse.class, asyncResultHandler);
}
@Override
public void putScheduledNoticeStorageScheduledNoticesByScheduledNoticeId(String scheduledNoticeId,
String lang,
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<Void> executeSql(PostgresClient pgClient, String sql) {
Future<UpdateResult> future = Future.future();
pgClient.execute(sql, future.completer());
return future.map(ur -> null);
}
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();
}
}