ProductPBean.java
3.75 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
package fi.codecrew.moya.beans;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.facade.AccountEventFacade;
import fi.codecrew.moya.facade.ProductFacade;
import fi.codecrew.moya.model.AccountEvent;
import fi.codecrew.moya.model.Discount;
import fi.codecrew.moya.model.DiscountInstance;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.FoodWave;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.model.ProductFlag;
/**
* Session Bean implementation class ProductPBean
*/
@Stateless
@LocalBean
public class ProductPBean {
@EJB
private PermissionBean permbean;
@EJB
private DiscountBean discountBean;
@EJB
private AccountEventFacade accounteventfacade;
@EJB
private PlaceBean placebean;
@EJB
private ProductFacade productFacade;
private static final Logger logger = LoggerFactory
.getLogger(ProductPBean.class);
/**
* Default constructor.
*/
public ProductPBean() {
// TODO Auto-generated constructor stub
}
public AccountEvent createAccountEvent(Product product,
BigDecimal quantity, EventUser user, Calendar date) {
return this.createAccountEvent(product, quantity, user, date, null);
}
/**
* Creates new AccountEvent from provided product to provided user <br>
* <strong>Notice</strong>, that this function expects the user parameter to
* be attached entity!
*
* @param product
* Product to create the account event from
* @param quantity
* How many products have been bought
* @param user
* user to whom the product should be created. MUST BE ATTACHED
* ENTITY!
* @param date
* AccountEvent creation time
* @return The created AccountEvent entity
*/
public AccountEvent createAccountEvent(Product product,
BigDecimal quantity, EventUser user, Calendar date,
FoodWave foodwave) {
if (!accounteventfacade.isAttached(product)) {
product = productFacade.reload(product);
}
if (!product.getEvent().equals(user.getEvent())) {
throw new EJBException("Trying to create accountevent for different event in user and product");
}
BigDecimal unitPrice = product.getPrice().negate();
List<Discount> discounts = discountBean.getActiveDiscountsByProduct(product, quantity, date, user);
for (Discount d : discounts) {
unitPrice = unitPrice.multiply(d.getPercentage());
}
AccountEvent ret = new AccountEvent(user, product, unitPrice, quantity, Calendar.getInstance());
// ret.setDelivered(Calendar.getInstance());
ret.setSeller(permbean.getCurrentUser());
if (foodwave != null) {
ret.setFoodWave(foodwave);
if (foodwave.getAccountEvents() == null) {
foodwave.setAccountEvents(new ArrayList<AccountEvent>());
}
foodwave.getAccountEvents().add(ret);
}
if (product.getProductFlags().contains(ProductFlag.RESERVE_PLACE_WHEN_BOUGHT)
|| product.getProductFlags().contains(ProductFlag.CREATE_NEW_PLACE_WHEN_BOUGHT)) {
placebean.lockPlaceProduct(user, product, quantity);
}
List<DiscountInstance> accEventdiscounts = ret.getDiscountInstances();
for (Discount d : discounts) {
// discountsArray.add(discInst);
// discountinstancefacade.create(discInst);
accEventdiscounts.add(new DiscountInstance(ret, d));
}
if (product.getAccountEvents() == null) {
product.setAccountEvents(new ArrayList<AccountEvent>());
}
product.getAccountEvents().add(ret);
user.addAccountevent(ret);
accounteventfacade.create(ret);
logger.debug("create ac {} for user {}", ret, user.getUser());
// flush changes to db.
// userFacade.flush();
return ret;
}
}