forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FixedDueDateSchedulesAPI.java
207 lines (185 loc) · 8.06 KB
/
FixedDueDateSchedulesAPI.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
package org.folio.rest.impl;
import io.vertx.core.*;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import org.folio.rest.annotations.Validate;
import org.folio.rest.jaxrs.model.Errors;
import org.folio.rest.jaxrs.model.FixedDueDateSchedule;
import org.folio.rest.jaxrs.model.FixedDueDateSchedules;
import org.folio.rest.jaxrs.model.Schedule;
import org.folio.rest.jaxrs.resource.FixedDueDateScheduleStorage;
import org.folio.rest.persist.MyPgUtil;
import org.folio.rest.persist.PgUtil;
import org.folio.rest.persist.PostgresClient;
import org.folio.rest.tools.PomReader;
import org.folio.rest.tools.utils.TenantTool;
import org.folio.rest.tools.utils.ValidationHelper;
import org.folio.support.UUIDValidation;
import javax.ws.rs.core.Response;
import java.util.Date;
import java.util.List;
import java.util.Map;
import static org.folio.rest.impl.Headers.TENANT_HEADER;
public class FixedDueDateSchedulesAPI implements FixedDueDateScheduleStorage {
private static final Logger log = LoggerFactory.getLogger(FixedDueDateSchedulesAPI.class);
private static final String FIXED_SCHEDULE_TABLE = "fixed_due_date_schedule";
private static final String INVALID_DATE_MSG = "Unable to save fixed loan date. Date range not valid";
private static final Class<FixedDueDateSchedule> DUE_DATE_SCHEDULE_CLASS = FixedDueDateSchedule.class;
@Override
@Validate
public void deleteFixedDueDateScheduleStorageFixedDueDateSchedules(
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(
String.format("DELETE FROM %s_%s.%s", tenantId,
PomReader.INSTANCE.getModuleName(), FIXED_SCHEDULE_TABLE), reply -> {
if(reply.succeeded()){
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(
FixedDueDateScheduleStorage
.DeleteFixedDueDateScheduleStorageFixedDueDateSchedulesResponse
.noContent().build()));
}
else{
log.error(reply.cause());
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(
FixedDueDateScheduleStorage.DeleteFixedDueDateScheduleStorageFixedDueDateSchedulesResponse
.respond500WithTextPlain(reply.cause().getMessage())));
}
});
} catch (Exception e) {
log.error(e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(
FixedDueDateScheduleStorage.DeleteFixedDueDateScheduleStorageFixedDueDateSchedulesResponse
.respond500WithTextPlain(e.getMessage())));
}
});
}
@Override
public void getFixedDueDateScheduleStorageFixedDueDateSchedules(
int offset,
int limit,
String query,
String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.get(FIXED_SCHEDULE_TABLE, DUE_DATE_SCHEDULE_CLASS, FixedDueDateSchedules.class,
query, offset, limit, okapiHeaders, vertxContext,
FixedDueDateScheduleStorage.GetFixedDueDateScheduleStorageFixedDueDateSchedulesResponse.class,
asyncResultHandler);
}
@Override
@Validate
public void postFixedDueDateScheduleStorageFixedDueDateSchedules(
String lang,
FixedDueDateSchedule entity,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
Errors errors = isDateRangeValid(entity.getSchedules());
if (errors != null){
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(
FixedDueDateScheduleStorage.PostFixedDueDateScheduleStorageFixedDueDateSchedulesResponse
.respond422WithApplicationJson(errors)));
return;
}
PgUtil.post(FIXED_SCHEDULE_TABLE, entity, okapiHeaders, vertxContext,
FixedDueDateScheduleStorage.PostFixedDueDateScheduleStorageFixedDueDateSchedulesResponse.class,
asyncResultHandler);
}
@Override
@Validate
public void getFixedDueDateScheduleStorageFixedDueDateSchedulesByFixedDueDateScheduleId(
String fixedDueDateScheduleId,
String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
// TODO: do we really need this special check?
// A 404 "Not found" from PgUtil.getById without "invalid UUID format" is good enough, isn't it?
if (! UUIDValidation.isValidUUID(fixedDueDateScheduleId)) {
asyncResultHandler.handle(Future.succeededFuture(
GetFixedDueDateScheduleStorageFixedDueDateSchedulesByFixedDueDateScheduleIdResponse
.respond404WithTextPlain("Not found, invalid UUID format")));
return;
}
PgUtil.getById(FIXED_SCHEDULE_TABLE, DUE_DATE_SCHEDULE_CLASS, fixedDueDateScheduleId, okapiHeaders, vertxContext,
GetFixedDueDateScheduleStorageFixedDueDateSchedulesByFixedDueDateScheduleIdResponse.class,
asyncResultHandler);
}
@Override
@Validate
public void deleteFixedDueDateScheduleStorageFixedDueDateSchedulesByFixedDueDateScheduleId(
String fixedDueDateScheduleId,
String lang,
Map<String,
String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext
) {
PgUtil.deleteById(FIXED_SCHEDULE_TABLE, fixedDueDateScheduleId, okapiHeaders, vertxContext,
DeleteFixedDueDateScheduleStorageFixedDueDateSchedulesByFixedDueDateScheduleIdResponse.class,
asyncResultHandler);
}
@Override
@Validate
public void putFixedDueDateScheduleStorageFixedDueDateSchedulesByFixedDueDateScheduleId(
String fixedDueDateScheduleId,
String lang,
FixedDueDateSchedule entity,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext
) {
Errors errors = isDateRangeValid(entity.getSchedules());
if (errors != null){
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(
FixedDueDateScheduleStorage.PostFixedDueDateScheduleStorageFixedDueDateSchedulesResponse
.respond422WithApplicationJson(errors)));
return;
}
MyPgUtil.putUpsert204(FIXED_SCHEDULE_TABLE, entity, fixedDueDateScheduleId, okapiHeaders, vertxContext,
PutFixedDueDateScheduleStorageFixedDueDateSchedulesByFixedDueDateScheduleIdResponse.class, asyncResultHandler);
}
private Errors isDateRangeValid(List<Schedule> schedules) {
Errors errors = null;
try {
//String from, String to, String due
int size = schedules.size();
for (int i = 0; i < size; i++) {
Date dueDate = schedules.get(i).getDue();
Date fromDate = schedules.get(i).getFrom();
Date toDate = schedules.get(i).getTo();
if(fromDate.compareTo(dueDate) > 0){
errors = ValidationHelper.createValidationErrorMessage("from", fromDate.toString(),
INVALID_DATE_MSG + " from date after due date");
}
else if(fromDate.compareTo(toDate) > 0){
errors = ValidationHelper.createValidationErrorMessage("from", fromDate.toString(),
INVALID_DATE_MSG + " from date after to date");
}
else if(toDate.compareTo(dueDate) > 0){
errors = ValidationHelper.createValidationErrorMessage("to", toDate.toString(),
INVALID_DATE_MSG + " to date after due date");
}
if(errors != null){
log.info(
INVALID_DATE_MSG + schedules.get(i).getDue()
+ " , from date: " + schedules.get(i).getFrom()
+ " , to date: " + schedules.get(i).getTo());
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return errors;
}
}