📄 orderdaobean.java
字号:
/*
* Copyright 2006 Borys Burnayev
*
* Licensed 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.
*/
package com.rdacorp.petstore.business.dao.impl;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Date;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.StaleObjectStateException;
import com.rdacorp.petstore.business.dao.AddressException;
import com.rdacorp.petstore.business.dao.OrderDao;
import com.rdacorp.petstore.business.dao.OrderException;
import com.rdacorp.petstore.domain.CreditCardCompany;
import com.rdacorp.petstore.domain.Item;
import com.rdacorp.petstore.domain.Order;
import com.rdacorp.petstore.domain.OrderItem;
/**
* @author Borys Burnayev
*/
@Stateless(name = "OrderDao")
public class OrderDaoBean implements OrderDao {
@PersistenceContext(unitName = "manager1")
protected EntityManager em;
public Order placeOrder(Order order) throws AddressException, OrderException, ConcurrentModificationException {
Date currentDateTime = new Date();
order.setOrderTs(currentDateTime);
order.getPayment().setPaymentTs(currentDateTime);
for (OrderItem orderItem : order.getOrderItems()) {
Item item = orderItem.getItem();
int currentQuantity = item.getAvailableQuantity();
int newQuantity = currentQuantity - Integer.parseInt(new Long(orderItem.getQuantity()).toString());
item.setAvailableQuantity(newQuantity > 0 ? newQuantity : 0);
try {
em.merge(item);
}
catch (StaleObjectStateException e) {
throw new ConcurrentModificationException();
}
}
em.persist(order);
return order;
}
public Collection<CreditCardCompany> getCreditCardCompanies() {
return em.createQuery("from CreditCardCompany").getResultList();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -