• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / jpetstore-6 / 2369

25 Jul 2026 01:52AM UTC coverage: 83.278% (+11.3%) from 72.006%
2369

Pull #1076

github

hazendaz
Drop tomee and add link to PR as tomee failed horribly on jstl upgrade from EE9 on
Pull Request #1076: Fix CDI bean discovery, JSTL TLD conflict, and CI startup timing on Jakarta EE full-profile containers

47 of 98 branches covered (47.96%)

108 of 198 new or added lines in 6 files covered. (54.55%)

5 existing lines in 1 file now uncovered.

498 of 598 relevant lines covered (83.28%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.3
/src/main/java/org/mybatis/jpetstore/web/controllers/OrderController.java
1
/*
2
 *    Copyright 2010-2026 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.jpetstore.web.controllers;
17

18
import jakarta.servlet.http.HttpSession;
19

20
import java.util.Arrays;
21
import java.util.Collections;
22
import java.util.List;
23

24
import org.mybatis.jpetstore.domain.Cart;
25
import org.mybatis.jpetstore.domain.Order;
26
import org.mybatis.jpetstore.service.OrderService;
27
import org.springframework.beans.factory.annotation.Autowired;
28
import org.springframework.stereotype.Controller;
29
import org.springframework.ui.Model;
30
import org.springframework.web.bind.annotation.GetMapping;
31
import org.springframework.web.bind.annotation.ModelAttribute;
32
import org.springframework.web.bind.annotation.PostMapping;
33
import org.springframework.web.bind.annotation.RequestMapping;
34
import org.springframework.web.bind.annotation.RequestParam;
35

36
@Controller
37
@RequestMapping("/order")
38
public class OrderController {
1✔
39

40
  private static final String NEW_ORDER_VIEW = "order/NewOrderForm";
41
  private static final String SHIPPING_VIEW = "order/ShippingForm";
42
  private static final String CONFIRM_VIEW = "order/ConfirmOrder";
43
  private static final String VIEW_ORDER_VIEW = "order/ViewOrder";
44
  private static final String LIST_ORDERS_VIEW = "order/ListOrders";
45
  private static final String ERROR_VIEW = "common/Error";
46

47
  private static final List<String> CARD_TYPE_LIST = Collections
1✔
48
      .unmodifiableList(Arrays.asList("Visa", "MasterCard", "American Express"));
1✔
49

50
  @Autowired
51
  private OrderService orderService;
52

53
  @GetMapping("/list")
54
  public String listOrders(HttpSession session, Model model) {
55
    AccountController.AccountSession accountSession = (AccountController.AccountSession) session
1✔
56
        .getAttribute("accountBean");
1✔
57
    if (accountSession == null || !accountSession.isAuthenticated()) {
1!
58
      model.addAttribute("message",
1✔
59
          "You must sign on before attempting to check out. Please sign on and try checking out again.");
60
      return ERROR_VIEW;
1✔
61
    }
62
    List<Order> orderList = orderService.getOrdersByUsername(accountSession.getAccount().getUsername());
1✔
63
    model.addAttribute("orderList", orderList);
1✔
64
    return LIST_ORDERS_VIEW;
1✔
65
  }
66

67
  @GetMapping("/new")
68
  public String newOrderForm(HttpSession session, Model model) {
69
    AccountController.AccountSession accountSession = (AccountController.AccountSession) session
1✔
70
        .getAttribute("accountBean");
1✔
71
    Cart cart = (Cart) session.getAttribute("cart");
1✔
72

73
    if (accountSession == null || !accountSession.isAuthenticated()) {
1!
74
      model.addAttribute("message",
1✔
75
          "You must sign on before attempting to check out. Please sign on and try checking out again.");
76
      return "redirect:/account";
1✔
77
    }
NEW
78
    if (cart != null) {
×
NEW
79
      Order order = new Order();
×
NEW
80
      order.initOrder(accountSession.getAccount(), cart);
×
NEW
81
      session.setAttribute("order", order);
×
NEW
82
      model.addAttribute("order", order);
×
NEW
83
      model.addAttribute("creditCardTypes", CARD_TYPE_LIST);
×
NEW
84
      return NEW_ORDER_VIEW;
×
85
    }
NEW
86
    model.addAttribute("message", "An order could not be created because a cart could not be found.");
×
NEW
87
    return ERROR_VIEW;
×
88
  }
89

90
  @PostMapping("/new")
91
  public String newOrder(@ModelAttribute Order order,
92
      @RequestParam(value = "shippingAddressRequired", defaultValue = "false") boolean shippingAddressRequired,
93
      @RequestParam(value = "confirmed", defaultValue = "false") boolean confirmed, HttpSession session, Model model) {
94

95
    Order sessionOrder = (Order) session.getAttribute("order");
1✔
96

97
    // When confirming, use the session order directly - the confirm form only
98
    // submits confirmed=true with no other fields, so @ModelAttribute Order is empty
99
    if (confirmed) {
1✔
100
      if (sessionOrder != null) {
1!
101
        orderService.insertOrder(sessionOrder);
1✔
102
        session.setAttribute("cart", new Cart());
1✔
103
        model.addAttribute("message", "Thank you, your order has been submitted.");
1✔
104
        model.addAttribute("order", sessionOrder);
1✔
105
        return VIEW_ORDER_VIEW;
1✔
106
      } else {
NEW
107
        model.addAttribute("message", "An error occurred processing your order (order was null).");
×
NEW
108
        return ERROR_VIEW;
×
109
      }
110
    }
111

112
    if (sessionOrder != null) {
1!
113
      // Only overwrite fields that were actually submitted (non-null).
114
      // ShippingForm only posts ship fields; NewOrderForm only posts billing/payment fields.
115
      // Unconditional setters would null-out whichever set is absent from the current form.
116
      if (order.getCardType() != null)
1!
NEW
117
        sessionOrder.setCardType(order.getCardType());
×
118
      if (order.getCreditCard() != null)
1!
NEW
119
        sessionOrder.setCreditCard(order.getCreditCard());
×
120
      if (order.getExpiryDate() != null)
1!
NEW
121
        sessionOrder.setExpiryDate(order.getExpiryDate());
×
122
      if (order.getBillToFirstName() != null)
1!
NEW
123
        sessionOrder.setBillToFirstName(order.getBillToFirstName());
×
124
      if (order.getBillToLastName() != null)
1!
NEW
125
        sessionOrder.setBillToLastName(order.getBillToLastName());
×
126
      if (order.getBillAddress1() != null)
1!
NEW
127
        sessionOrder.setBillAddress1(order.getBillAddress1());
×
128
      if (order.getBillAddress2() != null)
1!
NEW
129
        sessionOrder.setBillAddress2(order.getBillAddress2());
×
130
      if (order.getBillCity() != null)
1!
NEW
131
        sessionOrder.setBillCity(order.getBillCity());
×
132
      if (order.getBillState() != null)
1!
NEW
133
        sessionOrder.setBillState(order.getBillState());
×
134
      if (order.getBillZip() != null)
1!
NEW
135
        sessionOrder.setBillZip(order.getBillZip());
×
136
      if (order.getBillCountry() != null)
1!
NEW
137
        sessionOrder.setBillCountry(order.getBillCountry());
×
138
      if (order.getShipToFirstName() != null) {
1!
139
        sessionOrder.setShipToFirstName(order.getShipToFirstName());
1✔
140
        sessionOrder.setShipToLastName(order.getShipToLastName());
1✔
141
        sessionOrder.setShipAddress1(order.getShipAddress1());
1✔
142
        sessionOrder.setShipAddress2(order.getShipAddress2());
1✔
143
        sessionOrder.setShipCity(order.getShipCity());
1✔
144
        sessionOrder.setShipState(order.getShipState());
1✔
145
        sessionOrder.setShipZip(order.getShipZip());
1✔
146
        sessionOrder.setShipCountry(order.getShipCountry());
1✔
147
      }
148
      order = sessionOrder;
1✔
149
    }
150

151
    if (shippingAddressRequired) {
1!
NEW
152
      session.setAttribute("order", order);
×
NEW
153
      model.addAttribute("order", order);
×
NEW
154
      return SHIPPING_VIEW;
×
155
    } else {
156
      session.setAttribute("order", order);
1✔
157
      model.addAttribute("order", order);
1✔
158
      return CONFIRM_VIEW;
1✔
159
    }
160
  }
161

162
  @GetMapping("/view")
163
  public String viewOrder(@RequestParam("orderId") int orderId, HttpSession session, Model model) {
NEW
164
    AccountController.AccountSession accountSession = (AccountController.AccountSession) session
×
NEW
165
        .getAttribute("accountBean");
×
NEW
166
    if (accountSession == null || !accountSession.isAuthenticated()) {
×
NEW
167
      return "redirect:/account";
×
168
    }
NEW
169
    Order order = orderService.getOrder(orderId);
×
NEW
170
    if (accountSession.getAccount().getUsername().equals(order.getUsername())) {
×
NEW
171
      model.addAttribute("order", order);
×
NEW
172
      return VIEW_ORDER_VIEW;
×
173
    } else {
NEW
174
      model.addAttribute("message", "You may only view your own orders.");
×
NEW
175
      return ERROR_VIEW;
×
176
    }
177
  }
178

179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc