forked from SAP-samples/cloud-cap-samples-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminServiceHandler.java
257 lines (217 loc) · 9.7 KB
/
AdminServiceHandler.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package my.bookshop.handlers;
import static cds.gen.adminservice.AdminService_.ORDERS;
import static cds.gen.adminservice.AdminService_.ORDER_ITEMS;
import static cds.gen.my.bookshop.Bookshop_.BOOKS;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.sap.cds.Result;
import com.sap.cds.ql.Select;
import com.sap.cds.ql.Update;
import com.sap.cds.ql.cqn.CqnAnalyzer;
import com.sap.cds.reflect.CdsModel;
import com.sap.cds.services.ErrorStatuses;
import com.sap.cds.services.ServiceException;
import com.sap.cds.services.cds.CdsService;
import com.sap.cds.services.draft.DraftCancelEventContext;
import com.sap.cds.services.draft.DraftPatchEventContext;
import com.sap.cds.services.draft.DraftService;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.Before;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.messages.Messages;
import com.sap.cds.services.persistence.PersistenceService;
import cds.gen.adminservice.AddToOrderContext;
import cds.gen.adminservice.AdminService_;
import cds.gen.adminservice.Books;
import cds.gen.adminservice.Books_;
import cds.gen.adminservice.OrderItems;
import cds.gen.adminservice.OrderItems_;
import cds.gen.adminservice.Orders;
import cds.gen.my.bookshop.Bookshop_;
import my.bookshop.MessageKeys;
/**
* Custom business logic for the "Admin Service" (see admin-service.cds)
*
* Handles creating and editing orders.
*/
@Component
@ServiceName(AdminService_.CDS_NAME)
class AdminServiceHandler implements EventHandler {
private final DraftService adminService;
private final PersistenceService db;
private final Messages messages;
private final CqnAnalyzer analyzer;
AdminServiceHandler(@Qualifier(AdminService_.CDS_NAME) DraftService adminService, PersistenceService db, Messages messages, CdsModel model) {
this.adminService = adminService;
this.db = db;
this.messages = messages;
// model is a tenant-dependant model proxy
this.analyzer = CqnAnalyzer.create(model);
}
/**
* Validate correctness of an order before finishing the order proces:
* 1. Check Order amount for each Item and return a message if amount is empty or <= 0
* 2. Check Order amount for each Item is available, return message if the stock is too low
*
* @param orders
*/
@Before(event = { CdsService.EVENT_CREATE, CdsService.EVENT_UPSERT, CdsService.EVENT_UPDATE })
public void beforeCreateOrder(Stream<Orders> orders) {
orders.forEach(order -> {
// reset total
order.setTotal(BigDecimal.valueOf(0));
order.getItems().forEach(orderItem -> {
// validation of the Order creation request
Integer amount = orderItem.getAmount();
if (amount == null || amount <= 0) {
// exceptions with localized messages from property files
// exceptions abort the request and set an error http status code
throw new ServiceException(ErrorStatuses.BAD_REQUEST, MessageKeys.AMOUNT_REQUIRE_MINIMUM)
.messageTarget("in", ORDERS, o -> o.Items(i -> i.ID().eq(orderItem.getId()).and(i.IsActiveEntity().eq(false))).amount());
}
String bookId = orderItem.getBookId();
if (bookId == null) {
// Tip: using static text without localization is still possible in exceptions and messages
throw new ServiceException(ErrorStatuses.BAD_REQUEST, "You have to specify the book to order");
}
// calculate the actual amount difference
// FIXME this should handle book changes, currently only amount changes are handled
int diffAmount = amount - db.run(Select.from(Bookshop_.ORDER_ITEMS).columns(i -> i.amount()).byId(orderItem.getId()))
.first(OrderItems.class).map(i -> i.getAmount()).orElse(0);
// check if enough books are available
Result result = db.run(Select.from(BOOKS).columns(b -> b.ID(), b -> b.stock(), b -> b.price()).byId(bookId));
Books book = result.first(Books.class).orElseThrow(notFound(MessageKeys.BOOK_MISSING));
if (book.getStock() < diffAmount) {
// Tip: you can have localized messages and use parameters in your messages
throw new ServiceException(ErrorStatuses.BAD_REQUEST, MessageKeys.BOOK_REQUIRE_STOCK, book.getStock());
}
// update the book with the new stock
book.setStock(book.getStock() - diffAmount);
db.run(Update.entity(BOOKS).data(book));
// update the net amount
BigDecimal updatedNetAmount = book.getPrice().multiply(BigDecimal.valueOf(amount));
orderItem.setNetAmount(updatedNetAmount);
// update the total
order.setTotal(order.getTotal().add(updatedNetAmount));
});
});
}
/**
* Calculate the total order value preview when editing an order item
*
* @param context
* @param orderItem
*/
@Before(event = DraftService.EVENT_DRAFT_PATCH)
public void patchOrderItems(DraftPatchEventContext context, OrderItems orderItem) {
// check if amount or book was updated
Integer amount = orderItem.getAmount();
String bookId = orderItem.getBookId();
String orderItemId = (String) analyzer.analyze(context.getCqn()).targetKeys().get(OrderItems.ID);
BigDecimal netAmount = calculateNetAmountInDraft(orderItemId, amount, bookId);
if (netAmount != null) {
orderItem.setNetAmount(netAmount);
}
}
/**
* Calculate the total order value preview when deleting an order item from the order
*
* @param context
*/
@Before(event = DraftService.EVENT_DRAFT_CANCEL, entity = OrderItems_.CDS_NAME)
public void cancelOrderItems(DraftCancelEventContext context) {
String orderItemId = (String) analyzer.analyze(context.getCqn()).targetKeys().get(OrderItems.ID);
calculateNetAmountInDraft(orderItemId, 0, null);
}
private BigDecimal calculateNetAmountInDraft(String orderItemId, Integer newAmount, String newBookId) {
Integer amount = newAmount;
String bookId = newBookId;
if (amount == null && bookId == null) {
return null; // nothing changed
}
// get the order item that was updated (to get access to the book price, amount and order total)
Result result = adminService.run(Select.from(ORDER_ITEMS)
.columns(o -> o.amount(), o -> o.netAmount(),
o -> o.book().expand(b -> b.ID(), b -> b.price()),
o -> o.parent().expand(p -> p.ID(), p -> p.total()))
.where(o -> o.ID().eq(orderItemId).and(o.IsActiveEntity().eq(false))));
OrderItems itemToPatch = result.first(OrderItems.class).orElseThrow(notFound(MessageKeys.ORDERITEM_MISSING));
BigDecimal bookPrice = null;
// fallback to existing values
if(amount == null) {
amount = itemToPatch.getAmount();
}
if(bookId == null && itemToPatch.getBook() != null) {
bookId = itemToPatch.getBook().getId();
bookPrice = itemToPatch.getBook().getPrice();
}
if(amount == null || bookId == null) {
return null; // not enough data available
}
// only warn about invalid values as we are in draft mode
if(amount <= 0) {
// Tip: add additional messages with localized messages from property files
// these messages are transported in sap-messages and do not abort the request
messages.warn(MessageKeys.AMOUNT_REQUIRE_MINIMUM);
}
// get the price of the updated book ID
if(bookPrice == null) {
result = db.run(Select.from(BOOKS).byId(bookId).columns(b -> b.price()));
Books book = result.first(Books.class).orElseThrow(notFound(MessageKeys.BOOK_MISSING));
bookPrice = book.getPrice();
}
// update the net amount of the order item
BigDecimal updatedNetAmount = bookPrice.multiply(BigDecimal.valueOf(amount));
// update the order's total
BigDecimal previousNetAmount = defaultZero(itemToPatch.getNetAmount());
BigDecimal currentTotal = defaultZero(itemToPatch.getParent().getTotal());
BigDecimal newTotal = currentTotal.subtract(previousNetAmount).add(updatedNetAmount);
adminService.patchDraft(Update.entity(ORDERS)
.where(o -> o.ID().eq(itemToPatch.getParent().getId()).and(o.IsActiveEntity().eq(false)))
.data(Orders.TOTAL, newTotal));
return updatedNetAmount;
}
/**
* Adds a book to an order
* @param context
*/
@On(entity = Books_.CDS_NAME)
public void addBookToOrder(AddToOrderContext context) {
String orderId = context.getOrderId();
List<Orders> orders = adminService.run(Select.from(ORDERS).columns(o -> o._all(), o -> o.Items().expand()).where(o -> o.ID().eq(orderId))).listOf(Orders.class);
Orders order = orders.stream().filter(p -> p.getIsActiveEntity()).findFirst().orElse(null);
// check that the order with given ID exists and is not in draft-mode
if((orders.size() > 0 && order == null) || orders.size() > 1) {
throw new ServiceException(ErrorStatuses.CONFLICT, MessageKeys.ORDER_INDRAFT);
} else if (orders.size() <= 0) {
throw new ServiceException(ErrorStatuses.NOT_FOUND, MessageKeys.ORDER_MISSING);
}
if(order.getItems() == null) {
order.setItems(new ArrayList<>());
}
// get ID of the book on which the action was called (bound action)
String bookId = (String) analyzer.analyze(context.getCqn()).targetKeys().get(Books.ID);
// create order item
OrderItems newItem = OrderItems.create();
newItem.setId(UUID.randomUUID().toString());
newItem.setBookId(bookId);
newItem.setAmount(context.getAmount());
order.getItems().add(newItem);
Orders updatedOrder = adminService.run(Update.entity(ORDERS).data(order)).single(Orders.class);
messages.success(MessageKeys.BOOK_ADDED_ORDER);
context.setResult(updatedOrder);
}
private Supplier<ServiceException> notFound(String message) {
return () -> new ServiceException(ErrorStatuses.NOT_FOUND, message);
}
private BigDecimal defaultZero(BigDecimal decimal) {
return decimal == null ? BigDecimal.valueOf(0) : decimal;
}
}