-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathLoanRepresentation.java
More file actions
203 lines (162 loc) · 8.19 KB
/
Copy pathLoanRepresentation.java
File metadata and controls
203 lines (162 loc) · 8.19 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
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
package org.folio.circulation.domain;
import static java.util.Objects.isNull;
import static org.apache.commons.lang3.StringUtils.firstNonBlank;
import static org.folio.circulation.domain.representations.LoanProperties.BORROWER;
import static org.folio.circulation.domain.representations.LoanProperties.LOAN_POLICY;
import static org.folio.circulation.domain.representations.LoanProperties.LOST_ITEM_POLICY;
import static org.folio.circulation.domain.representations.LoanProperties.OVERDUE_FINE_POLICY;
import static org.folio.circulation.domain.representations.LoanProperties.PATRON_GROUP_ID_AT_CHECKOUT;
import static org.folio.circulation.domain.representations.LoanProperties.REMINDERS;
import static org.folio.circulation.support.json.JsonPropertyWriter.write;
import java.lang.invoke.MethodHandles;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.circulation.domain.policy.Policy;
import org.folio.circulation.domain.representations.ItemSummaryRepresentation;
import org.folio.circulation.domain.representations.LoanProperties;
import org.folio.circulation.resources.context.RenewalContext;
import io.vertx.core.json.JsonObject;
public class LoanRepresentation {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());
public JsonObject extendedLoan(Loan loan) {
if (loan == null) {
log.warn("extendedLoan:: loan is null");
return null;
}
JsonObject extendedRepresentation = extendedLoan(loan.asJson(), loan.getItem());
if(loan.isDueDateChangedByNearExpireUser()) {
log.debug("extendedLoan:: due date changed by near expire user");
extendedRepresentation.put("dueDateChangedByNearExpireUser", loan.isDueDateChangedByNearExpireUser());
}
if(loan.isDueDateChangedByHold()) {
log.debug("extendedLoan:: due date changed by hold");
extendedRepresentation.put("dueDateChangedByHold",loan.isDueDateChangedByHold());
}
if(loan.getCheckinServicePoint() != null) {
log.debug("extendedLoan:: checkinServicePoint is not null");
addAdditionalServicePointProperties(extendedRepresentation, loan.getCheckinServicePoint(), "checkinServicePoint");
}
if(loan.getCheckoutServicePoint() != null) {
log.debug("extendedLoan:: checkoutServicePoint is not null");
addAdditionalServicePointProperties(extendedRepresentation, loan.getCheckoutServicePoint(), "checkoutServicePoint");
}
if (loan.getUser() != null) {
log.debug("extendedLoan:: user is not null");
additionalBorrowerProperties(extendedRepresentation, loan.getUser());
} else {
//When there is no user, it means that the loan has been anonymized
log.debug("extendedLoan:: there is no user, removing borrower");
extendedRepresentation.remove(BORROWER);
}
if (loan.getOverdueFinePolicy().isReminderFeesPolicy()
&& loan.getLastReminderFeeBilledNumber() != null) {
extendedRepresentation.getJsonObject(REMINDERS)
.put("renewalBlocked",
!loan.getOverdueFinePolicy()
.getRemindersPolicy().getAllowRenewalOfItemsWithReminderFees());
}
addPolicy(extendedRepresentation, loan.getLoanPolicy(), LOAN_POLICY);
addPolicy(extendedRepresentation, loan.getOverdueFinePolicy(), OVERDUE_FINE_POLICY);
addPolicy(extendedRepresentation, loan.getLostItemPolicy(), LOST_ITEM_POLICY);
additionalAccountProperties(extendedRepresentation, loan);
extendedRepresentation.remove(PATRON_GROUP_ID_AT_CHECKOUT);
return extendedRepresentation;
}
private void addPolicy(JsonObject extendedRepresentation, Policy policy,
String policyName) {
if (policy != null) {
additionalPolicyProperties(extendedRepresentation, policy, policyName);
} else {
log.info("Unable to add {} properties to loan {}, {} is null",
policyName, extendedRepresentation.getString("id"), policyName);
extendedRepresentation.remove(policyName);
}
}
public JsonObject extendedLoan(RenewalContext renewalContext) {
return extendedLoan(renewalContext.getLoan());
}
private JsonObject extendedLoan(JsonObject loan, Item item) {
//No need to pass on the itemStatus property, as only used to populate the history
//and could be confused with aggregation of current status
loan.remove("itemStatus");
if (item != null && item.isFound()) {
loan.put("item", new ItemSummaryRepresentation()
.createItemSummary(item));
}
loan.remove(LoanProperties.AGED_TO_LOST_DELAYED_BILLING);
return loan;
}
private void additionalAccountProperties(JsonObject loanRepresentation, Loan loan) {
log.debug("additionalAccountProperties:: parameters loanRepresentation: {}, loan: {}",
() -> loanRepresentation, () -> loan);
if (loan.getAccounts() == null) {
log.info("additionalAccountProperties:: accounts is null");
return;
}
JsonObject feesAndFinesSummary = loanRepresentation.containsKey(LoanProperties.FEESANDFINES)
? loanRepresentation.getJsonObject(LoanProperties.FEESANDFINES)
: new JsonObject();
write(feesAndFinesSummary, "amountRemainingToPay", loan.getRemainingFeeFineAmount()
.toDouble());
write(loanRepresentation, LoanProperties.FEESANDFINES, feesAndFinesSummary);
}
private void additionalPolicyProperties(JsonObject representation,
Policy policy, String policyName) {
JsonObject summary = representation.containsKey(policyName)
? representation.getJsonObject(policyName)
: new JsonObject();
summary.put("name", policy.getName());
representation.put(policyName, summary);
}
private void addAdditionalServicePointProperties(
JsonObject loanRepresentation,
ServicePoint servicePoint,
String fieldName) {
if (servicePoint == null) {
log.info("Unable to add servicepoint properties to loan {},"
+ " servicepoint is null", loanRepresentation.getString("id"));
return;
}
JsonObject spSummary = loanRepresentation.containsKey(fieldName)
? loanRepresentation.getJsonObject(fieldName)
: new JsonObject();
spSummary.put("name", servicePoint.getName());
spSummary.put("code", servicePoint.getCode());
spSummary.put("discoveryDisplayName", servicePoint.getDiscoveryDisplayName());
spSummary.put("description", servicePoint.getDescription());
spSummary.put("shelvingLagTime", servicePoint.getShelvingLagTime());
spSummary.put("pickupLocation", servicePoint.isPickupLocation());
loanRepresentation.put(fieldName, spSummary);
}
private void additionalBorrowerProperties(JsonObject loanRepresentation, User borrower) {
if (borrower == null) {
log.info("Unable to add borrower properties to loan {},"
+ " borrower is null", loanRepresentation.getString("id"));
return;
}
JsonObject borrowerSummary = loanRepresentation.containsKey(BORROWER)
? loanRepresentation.getJsonObject(BORROWER)
: new JsonObject();
borrowerSummary.put("firstName", borrower.getFirstName());
borrowerSummary.put("lastName", borrower.getLastName());
borrowerSummary.put("middleName", borrower.getMiddleName());
borrowerSummary.put("barcode", borrower.getBarcode());
borrowerSummary.put("preferredFirstName", firstNonBlank(borrower.getPreferredFirstName(), borrower.getFirstName()));
borrowerSummary.put("patronGroup",borrower.getPatronGroupId());
loanRepresentation.put(BORROWER, borrowerSummary);
additionalPatronGroupProperties(loanRepresentation, borrower.getPatronGroup());
}
private void additionalPatronGroupProperties(JsonObject loanRepresentation,
PatronGroup patronGroupAtCheckout) {
if (isNull(patronGroupAtCheckout)) {
log.info("additionalPatronGroupProperties:: patronGroupAtCheckout is null");
return;
}
JsonObject patronGroupAtCheckoutSummary = loanRepresentation.containsKey(LoanProperties.PATRON_GROUP_AT_CHECKOUT)
? loanRepresentation.getJsonObject(LoanProperties.PATRON_GROUP_AT_CHECKOUT)
: new JsonObject();
write(patronGroupAtCheckoutSummary, "id", patronGroupAtCheckout.getId());
write(patronGroupAtCheckoutSummary, "name", patronGroupAtCheckout.getGroup());
loanRepresentation.put(LoanProperties.PATRON_GROUP_AT_CHECKOUT, patronGroupAtCheckoutSummary);
}
}