%--
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
--%>
<%@page import="openbook.server.OpenBookService"%>
<%@page import="openbook.domain.Book"%>
<%@page import="openbook.domain.Customer"%>
<%@page import="openbook.domain.ShoppingCart"%>
<%@page import="openbook.domain.PurchaseOrder"%>
<%@page import="openbook.domain.LineItem"%>
<%@page import="openbook.util.JSPUtility"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@include file="header.jsp"%>
Optimistic semantics and Orphan Delete
This page displays all the orders placed by the current users.
This page also allows to
deliver an order. Delivering an order essentially amounts to
decrementing the inventory for each line item,
changing the status
which, as an interesting side-effect, nullifies the Line Items.
- Optimistic Semantics: Delivery is one of the operations that may fail due to
optimistic transaction model used by OpenBooks and which is also the default transaction model
proposed in JPA. The optimistic transaction model promoted that an Order can always
be placed, even if the inventory is inadequate. Only while fulfilling the order in a separate
transaction, the insufficient inventory may fail to deliver an order.
- Orphan Delete: JPA 2.0 had added support for composite relation via new orphan delete
functionality. To demonstrate its effect, on delivery an Order nullifies its Line Items. As a
result, the Line Items gets deleted from the database as they are no more referred. That is why,
for pending orders, you can see their line items -- but once an order is delivered its line items
are no more available.
<%
OpenBookService service = (OpenBookService)session.getAttribute(KEY_SERVICE);
if (service == null) {
%>
<%
}
if (ACTION_DELIVER.equals(request.getParameter(KEY_ACTION))) {
String oid = request.getParameter(KEY_OID);
PurchaseOrder order = (PurchaseOrder)session.getAttribute(oid);
service.deliver(order);
}
Customer customer = (Customer)session.getAttribute(KEY_USER);
List
pendingOrders = service.getOrders(PurchaseOrder.Status.DELIVERED, customer);
List deliveredOrders = service.getOrders(PurchaseOrder.Status.PENDING, customer);
List orders = new ArrayList(pendingOrders);
orders.addAll(deliveredOrders);
if (orders.isEmpty()) {
%>
<%= customer.getName() %>, you have not placed any order yet.
<%
return;
}
%>
<%= customer.getName() %>, you have placed <%= orders.size() %> (
<%= pendingOrders.size() == 0 ? "none" : "" + pendingOrders.size()%> pending,
<%= deliveredOrders.size() == 0 ? " none" : " " + deliveredOrders.size()%> delivered) orders
ID |
Total |
Placed On |
Status |
Delivered On |
Deliver |
<%
int i = 0;
for (PurchaseOrder order : orders) {
session.setAttribute(""+order.getId(), order);
%>
<%= order.getId() %> |
<%= order.getTotal() %> |
<%= JSPUtility.format(order.getPlacedOn()) %> |
<%= order.getStatus() %> |
<%
if (order.isDelivered()) {
%>
<%= JSPUtility.format(order.getDeliveredOn()) %> |
|
<%
} else {
%>
|
|
<%
}
%>
<%
}
%>
<%
if (ACTION_DETAILS.equals(request.getParameter(KEY_ACTION))) {
String oid = request.getParameter(KEY_OID);
PurchaseOrder order = (PurchaseOrder)session.getAttribute(oid);
List items = order.getItems();
if (order.isDelivered()) {
if (items != null && items.isEmpty()) {
%>
Order <%= order.getId() %> has been delivered. Line items of delivered orders are automatically
deleted due to orphan delete nature of Master-Details relationship.
<% } else {
%>
Order <%= order.getId() %> has been delivered but still contains line items.
This is an implementation error because delivered orders must have at least one line item by design.
<%
}
} else {
%>
Total of <%= items.size() %> Book<%= items.size() == 0 ? "" : "s" %>
in Order <%= order.getId() %>
Title |
Price |
Quantity |
Cost |
<%
int j = 0;
for (LineItem item : items) {
%>
<%= item.getBook().getTitle() %> |
<%= JSPUtility.format(item.getBook().getPrice()) %> |
<%= item.getQuantity() %> |
<%= JSPUtility.format(item.getBook().getPrice() * item.getQuantity()) %> |
<%
}
%>
Total | | |
<%= JSPUtility.format(order.getTotal()) %> |
<%
}
}
%>
<%@include file="footer.jsp"%>