Commit 1ba81836 by Tuomas Riihimäki

Add accountevents to product entity.

When buying products update the entity manager attachedproducts accounteventlist at the same time.
This allows viewing of product statistics without flushing caches.
1 parent 52698f10
...@@ -14,6 +14,7 @@ import org.slf4j.Logger; ...@@ -14,6 +14,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fi.codecrew.moya.facade.AccountEventFacade; import fi.codecrew.moya.facade.AccountEventFacade;
import fi.codecrew.moya.facade.ProductFacade;
import fi.codecrew.moya.model.AccountEvent; import fi.codecrew.moya.model.AccountEvent;
import fi.codecrew.moya.model.Discount; import fi.codecrew.moya.model.Discount;
import fi.codecrew.moya.model.DiscountInstance; import fi.codecrew.moya.model.DiscountInstance;
...@@ -40,6 +41,9 @@ public class ProductPBean { ...@@ -40,6 +41,9 @@ public class ProductPBean {
@EJB @EJB
private PlaceBean placebean; private PlaceBean placebean;
@EJB
private ProductFacade productFacade;
private static final Logger logger = LoggerFactory private static final Logger logger = LoggerFactory
.getLogger(ProductPBean.class); .getLogger(ProductPBean.class);
...@@ -75,9 +79,12 @@ public class ProductPBean { ...@@ -75,9 +79,12 @@ public class ProductPBean {
BigDecimal quantity, EventUser user, Calendar date, BigDecimal quantity, EventUser user, Calendar date,
FoodWave foodwave) { FoodWave foodwave) {
if (!accounteventfacade.isAttached(product)) {
product = productFacade.reload(product);
}
if (!product.getEvent().equals(user.getEvent())) { if (!product.getEvent().equals(user.getEvent())) {
throw new EJBException( throw new EJBException("Trying to create accountevent for different event in user and product");
"Trying to create accountevent for different event in user and product");
} }
BigDecimal unitPrice = product.getPrice().negate(); BigDecimal unitPrice = product.getPrice().negate();
...@@ -110,6 +117,10 @@ public class ProductPBean { ...@@ -110,6 +117,10 @@ public class ProductPBean {
// discountinstancefacade.create(discInst); // discountinstancefacade.create(discInst);
accEventdiscounts.add(new DiscountInstance(ret, d)); accEventdiscounts.add(new DiscountInstance(ret, d));
} }
if (product.getAccountEvents() == null) {
product.setAccountEvents(new ArrayList<AccountEvent>());
}
product.getAccountEvents().add(ret);
user.addAccountevent(ret); user.addAccountevent(ret);
accounteventfacade.create(ret); accounteventfacade.create(ret);
......
...@@ -40,7 +40,7 @@ public abstract class GenericFacade<C extends ModelInterface> { ...@@ -40,7 +40,7 @@ public abstract class GenericFacade<C extends ModelInterface> {
public C create(C entity) { public C create(C entity) {
getEm().persist(entity); getEm().persist(entity);
return entity; return entity;
} }
...@@ -289,4 +289,9 @@ public abstract class GenericFacade<C extends ModelInterface> { ...@@ -289,4 +289,9 @@ public abstract class GenericFacade<C extends ModelInterface> {
public void evict(C entity) { public void evict(C entity) {
getEm().getEntityManagerFactory().getCache().evict(getEntityClass(), entity.getId()); getEm().getEntityManagerFactory().getCache().evict(getEntityClass(), entity.getId());
} }
public boolean isAttached(ModelInterface entity) {
return getEm().contains(entity);
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!