AccountEventBean.java 4.15 KB
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.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.insomnia.bortal.enums.apps.ShopPermission;
import fi.insomnia.bortal.enums.apps.UserPermission;
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.Product;
import fi.insomnia.bortal.model.Role;
import fi.insomnia.bortal.model.User;

/**
 * Session Bean implementation class AccountEventBean
 */
@Stateless
@DeclareRoles({ UserPermission.S_VIEW_ACCOUNTEVENTS })
public class AccountEventBean implements AccountEventBeanLocal {

	@EJB
	private AccountEventFacade accountfacade;

	@EJB
	private UserBeanLocal userbean;
	@EJB
	private LoggingBeanLocal loggingbean;
	@EJB
	private EventBeanLocal eventBean;
	@EJB
	private ProductBeanLocal prodbean;
	@EJB
	private PlaceBean placebean;
	@EJB
	private PermissionBeanLocal permbean;

	private static final Logger logger = LoggerFactory.getLogger(AccountEventBean.class);

	public AccountEventBean() {
		super();
	}

	@Override
	@RolesAllowed(UserPermission.S_MODIFY_ACCOUNTEVENTS)
	public AccountEvent merge(AccountEvent account) {
		return accountfacade.merge(account);
	}

	@Override
	@RolesAllowed(UserPermission.S_MODIFY_ACCOUNTEVENTS)
	public void delete(AccountEvent account) {

		AccountEvent acco = accountfacade.find(account.getId());
		loggingbean.logMessage(SecurityLogType.accountEvent, permbean.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);
	}

	/**
	 * Create accountevents for the products in the parameter shopMap
	 */
	@Override
	@RolesAllowed(ShopPermission.S_SHOP_PRODUCTS)
	public List<AccountEvent> shopCash(User shoppingUser, Map<Product, BigDecimal> shopMap, boolean buyInstant) {
		logger.debug("Shoping cash. buyinstant {}", buyInstant);
		User seller = permbean.getCurrentUser();
		shoppingUser = userbean.findById(shoppingUser.getId());

		ArrayList<AccountEvent> ret = new ArrayList<AccountEvent>();
		LanEvent ev = eventBean.getCurrentEvent();

		BigDecimal tot = BigDecimal.ZERO;

		for (Entry<Product, BigDecimal> prodentry : shopMap.entrySet()) {

			// Create account event for the product.
			AccountEvent ac = new AccountEvent(ev, shoppingUser, prodentry.getKey(), prodentry.getKey().getPrice(), prodentry.getValue(), Calendar.getInstance());
			ac.setSeller(seller);
			shoppingUser.getAccountEvents().add(ac);

			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().isEmpty()) {
				logger.debug("Prepaidplace");

				placebean.lockPlaceProduct(shoppingUser, prodentry.getKey(), BigDecimal.ONE);

			}

		}

		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;
	}
}