forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoanPoliciesAPI.java
221 lines (196 loc) · 8.37 KB
/
LoanPoliciesAPI.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.Future;
import io.vertx.core.Handler;
import org.folio.rest.annotations.Validate;
import org.folio.rest.jaxrs.model.Errors;
import org.folio.rest.jaxrs.model.LoanPolicies;
import org.folio.rest.jaxrs.model.LoanPolicy;
import org.folio.rest.jaxrs.resource.LoanPolicyStorage;
import org.folio.rest.persist.MyPgUtil;
import org.folio.rest.persist.PgUtil;
import org.folio.rest.persist.PostgresClient;
import org.folio.rest.tools.utils.TenantTool;
import org.folio.rest.tools.utils.ValidationHelper;
import javax.ws.rs.core.Response;
import java.util.Map;
import java.util.function.Consumer;
import static org.folio.rest.impl.Headers.TENANT_HEADER;
public class LoanPoliciesAPI implements LoanPolicyStorage {
private static final String LOAN_POLICY_TABLE = "loan_policy";
private static final Class<LoanPolicy> LOAN_POLICY_CLASS = LoanPolicy.class;
@Override
@Validate
public void deleteLoanPolicyStorageLoanPolicies(
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("TRUNCATE TABLE %s_%s.%s",
tenantId, "mod_circulation_storage", LOAN_POLICY_TABLE),
reply -> asyncResultHandler.handle(Future.succeededFuture(
DeleteLoanPolicyStorageLoanPoliciesResponse.respond204())));
}
catch(Exception e) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(
LoanPolicyStorage.DeleteLoanPolicyStorageLoanPoliciesResponse
.respond500WithTextPlain(e.getMessage())));
}
});
}
@Override
@Validate
public void getLoanPolicyStorageLoanPolicies(
int offset,
int limit,
String query,
String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.get(LOAN_POLICY_TABLE, LOAN_POLICY_CLASS, LoanPolicies.class, query, offset, limit,
okapiHeaders, vertxContext,
GetLoanPolicyStorageLoanPoliciesResponse.class, asyncResultHandler);
}
@Override
@Validate
public void postLoanPolicyStorageLoanPolicies(
String lang, LoanPolicy entity,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
validate(entity, errors -> respond422(errors, asyncResultHandler), () ->
PgUtil.post(LOAN_POLICY_TABLE, entity, okapiHeaders, vertxContext,
PostLoanPolicyStorageLoanPoliciesResponse.class, asyncResultHandler));
}
@Override
@Validate
public void getLoanPolicyStorageLoanPoliciesByLoanPolicyId(
String loanPolicyId,
String lang,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.getById(LOAN_POLICY_TABLE, LOAN_POLICY_CLASS, loanPolicyId, okapiHeaders, vertxContext,
GetLoanPolicyStorageLoanPoliciesByLoanPolicyIdResponse.class, asyncResultHandler);
}
@Override
@Validate
public void deleteLoanPolicyStorageLoanPoliciesByLoanPolicyId(
String loanPolicyId,
String lang, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.deleteById(LOAN_POLICY_TABLE, loanPolicyId, okapiHeaders, vertxContext,
DeleteLoanPolicyStorageLoanPoliciesByLoanPolicyIdResponse.class, asyncResultHandler);
}
@Override
@Validate
public void putLoanPolicyStorageLoanPoliciesByLoanPolicyId(
String loanPolicyId,
String lang,
LoanPolicy entity,
Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
validate(entity, errors -> respond422(errors, asyncResultHandler), () ->
MyPgUtil.putUpsert204(LOAN_POLICY_TABLE, entity, loanPolicyId, okapiHeaders, vertxContext,
PutLoanPolicyStorageLoanPoliciesByLoanPolicyIdResponse.class, asyncResultHandler));
}
private void respond422(Errors errors, Handler<AsyncResult<Response>> asyncResultHandler) {
Response response = Response.status(422).header("Content-Type", "application/json").entity(errors).build();
asyncResultHandler.handle(Future.succeededFuture(response));
}
/**
* @return true if bool is TRUE, false if bool is FALSE or null
*/
private boolean isTrue(Boolean bool) {
return Boolean.TRUE.equals(bool);
}
void validate(LoanPolicy loanPolicy, Consumer<Errors> runIfInvalid, Runnable runIfValid) {
final boolean isFixed =
loanPolicy.getLoansPolicy() != null
&& loanPolicy.getLoansPolicy().getProfileId() != null
&& loanPolicy.getLoansPolicy().getProfileId().equalsIgnoreCase("Fixed");
// alternate fixed due date
if ((isTrue(loanPolicy.getRenewable())
&& loanPolicy.getRenewalsPolicy() != null
&& isTrue(loanPolicy.getRenewalsPolicy().getDifferentPeriod())
&& isFixed
&& loanPolicy.getRenewalsPolicy().getAlternateFixedDueDateScheduleId() == null
)
||
( loanPolicy.getRenewalsPolicy() != null
&&
( ! isTrue(loanPolicy.getRenewable()) ||
! isTrue(loanPolicy.getRenewalsPolicy().getDifferentPeriod())
)
&& loanPolicy.getRenewalsPolicy().getAlternateFixedDueDateScheduleId() != null
)) {
String message =
"Alternate fixed due date cannot be " + loanPolicy.getRenewalsPolicy().getAlternateFixedDueDateScheduleId() +
" if renewable is " + loanPolicy.getRenewable() +
", different period is " + loanPolicy.getRenewalsPolicy().getDifferentPeriod() +
" and profile is " + loanPolicy.getLoansPolicy().getProfileId();
runIfInvalid.accept(ValidationHelper.createValidationErrorMessage(
"alternateFixedDueDateScheduleId",
"" + loanPolicy.getRenewalsPolicy().getAlternateFixedDueDateScheduleId(),
message));
return;
}
// fixed profile id
if ( (isTrue(loanPolicy.getLoanable())
&& isFixed
&& loanPolicy.getLoansPolicy().getFixedDueDateScheduleId() == null
)
||
// TODO: consider adjusting the message
(! isTrue(loanPolicy.getLoanable())
&& loanPolicy.getLoansPolicy() != null
// TODO: consider removing this last condition
&& loanPolicy.getLoansPolicy().getFixedDueDateScheduleId() == null
)
) {
String message = "Fixed due date cannot be null if loanable is " +
loanPolicy.getLoanable() + " and profile is of type fixed";
runIfInvalid.accept(ValidationHelper.createValidationErrorMessage(
"fixedDueDateScheduleId",
"" + loanPolicy.getLoansPolicy().getFixedDueDateScheduleId(),
message));
return;
}
// alternate renewal loan period
if (isFixed
&& isTrue(loanPolicy.getRenewable())
&& loanPolicy.getRequestManagement() != null
&& loanPolicy.getRequestManagement().getHolds() != null
&& isTrue(loanPolicy.getRequestManagement().getHolds().getRenewItemsWithRequest())
&& loanPolicy.getRequestManagement().getHolds().getAlternateRenewalLoanPeriod() != null) {
String message = "Alternate Renewal Loan Period for Holds is not allowed for policies with Fixed profile";
runIfInvalid.accept(ValidationHelper.createValidationErrorMessage(
"alternateRenewalLoanPeriod",
loanPolicy.getRequestManagement().getHolds().getAlternateRenewalLoanPeriod().toString(),
message));
return;
}
// renewabls policy period
if (isFixed
&& isTrue(loanPolicy.getRenewable())
&& loanPolicy.getRenewalsPolicy() != null
&& isTrue(loanPolicy.getRenewalsPolicy().getDifferentPeriod())
&& loanPolicy.getRenewalsPolicy().getPeriod() != null) {
String message = "Period in RenewalsPolicy is not allowed for policies with Fixed profile";
runIfInvalid.accept(ValidationHelper.createValidationErrorMessage(
"period",
loanPolicy.getRenewalsPolicy().getPeriod().toString(),
message));
return;
}
runIfValid.run();
}
}