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;
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;
......@@ -40,6 +41,9 @@ public class ProductPBean {
@EJB
private PlaceBean placebean;
@EJB
private ProductFacade productFacade;
private static final Logger logger = LoggerFactory
.getLogger(ProductPBean.class);
......@@ -75,9 +79,12 @@ public class ProductPBean {
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");
throw new EJBException("Trying to create accountevent for different event in user and product");
}
BigDecimal unitPrice = product.getPrice().negate();
......@@ -110,6 +117,10 @@ public class ProductPBean {
// 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);
......
......@@ -289,4 +289,9 @@ public abstract class GenericFacade<C extends ModelInterface> {
public void evict(C entity) {
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!