AccountEventBean.java
4.77 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
package fi.insomnia.bortal.beans;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.insomnia.bortal.enums.Permission;
import fi.insomnia.bortal.enums.RolePermission;
import fi.insomnia.bortal.facade.AccountEventFacade;
import fi.insomnia.bortal.model.AccountEvent;
import fi.insomnia.bortal.model.EventPk;
import fi.insomnia.bortal.model.LanEvent;
import fi.insomnia.bortal.model.Place;
import fi.insomnia.bortal.model.Product;
import fi.insomnia.bortal.model.Role;
import fi.insomnia.bortal.model.User;
/**
* Session Bean implementation class AccountEventBean
*/
@Stateless
public class AccountEventBean implements AccountEventBeanLocal {
@EJB
private AccountEventFacade accountfacade;
@EJB
private UserBeanLocal userbean;
@EJB
private SecurityBeanLocal sessionbean;
@EJB
private EventBeanLocal eventBean;
@EJB
private ProductBeanLocal prodbean;
@EJB
private PlaceBeanLocal placebean;
private static final Logger logger = LoggerFactory.getLogger(AccountEventBean.class);
public AccountEventBean() {
super();
}
@Override
public AccountEvent merge(AccountEvent account) {
userbean.fatalPermission(Permission.ACCOUNT_MANAGEMENT, RolePermission.WRITE, "Error mergin account event", account);
return accountfacade.merge(account);
}
@Override
public void delete(AccountEvent account) {
userbean.fatalPermission(Permission.ACCOUNT_MANAGEMENT, RolePermission.WRITE, "Error deleting account event: ", account);
AccountEvent acco = accountfacade.find(account.getId());
sessionbean.logMessage(SecurityLogType.accountEvent, userbean.getCurrentUser(), "Deleting AccountEvent '", acco.getProduct().getName(), "' count: '", acco.getQuantity().toString(), "' unitprice: '", acco.getUnitPrice().toString(), "' accouser: '", acco.getUser().getLogin(), "'");
acco.getProduct().getAccountEvents().remove(acco);
if (acco.getBill() != null) {
acco.getBill().setAccountEvent(null);
}
acco.getUser().getAccountEvents().remove(acco);
accountfacade.remove(acco);
}
@Override
public AccountEvent find(EventPk id) {
return accountfacade.find(id);
}
@Override
public List<Role> getRolesFromAccountEvents(User u) {
return accountfacade.findProvidedRoles(eventBean.getCurrentEvent(), u);
}
@Override
public List<AccountEvent> shopCash(User shoppingUser, Map<Product, BigDecimal> shopMap, boolean buyInstant) {
logger.debug("Shoping cash. buyinstant {}", buyInstant);
userbean.fatalPermission(Permission.SHOP, RolePermission.EXECUTE, "User tried to create accountEvents via shop without SHOP:EXECUTE");
ArrayList<AccountEvent> ret = new ArrayList<AccountEvent>();
LanEvent ev = eventBean.getCurrentEvent();
User seller = userbean.getCurrentUser();
BigDecimal tot = BigDecimal.ZERO;
for (Entry<Product, BigDecimal> prodentry : shopMap.entrySet()) {
AccountEvent ac = new AccountEvent(ev, shoppingUser, prodentry.getKey(), prodentry.getKey().getPrice(), prodentry.getValue(), Calendar.getInstance());
if (buyInstant && prodentry.getKey().getPrice().compareTo(BigDecimal.ZERO) > 0) {
tot = tot.add(prodentry.getValue().multiply(prodentry.getKey().getPrice()));
}
if (prodentry.getKey().isPrepaidInstant() && prodentry.getKey().getPlaces() != null && prodentry.getKey().getPlaces().size() > 0) {
logger.debug("Prepaidplace");
placebean.lockPlaceProduct(shoppingUser, prodentry.getKey(), BigDecimal.ONE);
// for (Place p : prodentry.getKey().getPlaces()) {
// if (!p.isTaken()) {
// logger.debug("Locking user {} to place {}", shoppingUser,p);
// placebean.lockPlaces(shoppingUser, p);
// break;
// }
// }
}
ac.setSeller(seller);
shoppingUser.getAccountEvents().add(ac);
}
logger.debug("ShopCash price {}", tot);
if (buyInstant && tot.compareTo(BigDecimal.ZERO) > 0) {
logger.debug("Creating buy instant product!");
Product creditProd = prodbean.findCreditProduct();
AccountEvent ac = new AccountEvent(ev, shoppingUser, creditProd, creditProd.getPrice(), tot, Calendar.getInstance());
shoppingUser.getAccountEvents().add(ac);
}
userbean.mergeChanges(shoppingUser);
return ret;
}
}