BillLineFacade.java 3.5 KB
/*
 * Copyright Codecrew Ry
 * 
 * All rights reserved.
 * 
 * This license applies to any software containing a notice placed by the 
 * copyright holder. Such software is herein referred to as the Software. 
 * This license covers modification, distribution and use of the Software. 
 * 
 * Any distribution and use in source and binary forms, with or without 
 * modification is not permitted without explicit written permission from the 
 * copyright owner. 
 * 
 * A non-exclusive royalty-free right is granted to the copyright owner of the 
 * Software to use, modify and distribute all modifications to the Software in 
 * future versions of the Software. 
 * 
 */
package fi.codecrew.moya.facade;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

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

import fi.codecrew.moya.model.BillLine_;
import fi.codecrew.moya.model.Bill_;
import fi.codecrew.moya.bortal.views.BillSummary;
import fi.codecrew.moya.model.BillLine;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.Product;

@Stateless
@LocalBean
public class BillLineFacade extends IntegerPkGenericFacade<BillLine> {
	
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(BillLineFacade.class);

	public BillLineFacade() {

		super(BillLine.class);
	}

	// @Override
	// public void create(BillLine entity) {
	// super.create(entity);
	// billfacade.evict(entity.getBill());
	// }

	public Collection<BillSummary> getLineSummary(LanEvent event) {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<BillLine> cq = cb.createQuery(BillLine.class);
		Root<BillLine> root = cq.from(BillLine.class);
		cq.where(cb.equal(root.get(BillLine_.bill).get(Bill_.event), event));

		List<BillLine> lines = getEm().createQuery(cq).getResultList();
		Map<String, BillSummary> retmap = new HashMap<String, BillSummary>();

		for (BillLine bl : lines) {
			if (!retmap.containsKey(bl.getName())) {
				retmap.put(bl.getName(), new BillSummary(bl.getName()));
			}
			retmap.get(bl.getName()).addLine(bl);
		}
		return retmap.values();
	}

	public BillSummary getLineSummary(Product list, LanEvent event) {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<BillLine> cq = cb.createQuery(BillLine.class);
		Root<BillLine> root = cq.from(BillLine.class);
		cq.where(cb.equal(root.get(BillLine_.bill).get(Bill_.event), event),
				cb.equal(root.get(BillLine_.lineProduct), list));

		List<BillLine> lines = getEm().createQuery(cq).getResultList();
		BillSummary ret = new BillSummary(list.getName());

		for (BillLine bl : lines) {
			ret.addLine(bl);
		}
		return ret;
	}

	public BillSummary getLineSummary(Product list, LanEvent event, EventUser user) {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<BillLine> cq = cb.createQuery(BillLine.class);
		Root<BillLine> root = cq.from(BillLine.class);
		cq.where(cb.equal(root.get(BillLine_.bill).get(Bill_.event), event),
				cb.equal(root.get(BillLine_.lineProduct), list),
				cb.equal(root.get(BillLine_.bill).get(Bill_.user), user)
				);

		List<BillLine> lines = getEm().createQuery(cq).getResultList();
		BillSummary ret = new BillSummary(list.getName());

		for (BillLine bl : lines) {
			ret.addLine(bl);
		}
		return ret;
	}

}