Commit f99c7b26 by Tuomas Riihimäki

Printed card filtering and handling fixes.

 Set all but biggest  cards as disabled
 Get from filter only enabled cards.
1 parent ffaa168e
...@@ -122,16 +122,24 @@ public class CardTemplateBean implements CardTemplateBeanLocal { ...@@ -122,16 +122,24 @@ public class CardTemplateBean implements CardTemplateBeanLocal {
List<PrintedCard> myCards = printedcardfacade.getCards(user); List<PrintedCard> myCards = printedcardfacade.getCards(user);
PrintedCard biggestCard = null; PrintedCard biggestCard = null;
PrintedCard smallestCard = null; // PrintedCard smallestCard = null;
for (PrintedCard card : myCards) { for (PrintedCard card : myCards) {
logger.info("Checking card {}", card); // logger.info("Checking card {}", card);
if (card.getEnabled()) { if (card.getEnabled()) {
if (biggestCard == null || biggestCard.getTemplate().getPower() < card.getTemplate().getPower()) { if (biggestCard == null || biggestCard.getTemplate().getPower() < card.getTemplate().getPower()) {
// The biggest card should be the only one enabled.
if (biggestCard != null) {
biggestCard.setEnabled(false);
}
biggestCard = card; biggestCard = card;
biggestCard.setEnabled(true);
} }
if (smallestCard == null || smallestCard.getTemplate().getPower() > card.getTemplate().getPower()) { // if (smallestCard == null ||
smallestCard = card; // smallestCard.getTemplate().getPower() >
} // card.getTemplate().getPower()) {
// smallestCard = card;
// }
} }
} }
......
...@@ -13,7 +13,6 @@ import javax.persistence.criteria.CriteriaBuilder; ...@@ -13,7 +13,6 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;
import fi.codecrew.moya.beans.EventBeanLocal; import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.enums.CardState; import fi.codecrew.moya.enums.CardState;
...@@ -37,7 +36,6 @@ public class PrintedCardFacade extends IntegerPkGenericFacade<PrintedCard> { ...@@ -37,7 +36,6 @@ public class PrintedCardFacade extends IntegerPkGenericFacade<PrintedCard> {
@EJB @EJB
private EventBeanLocal eventbean; private EventBeanLocal eventbean;
public PrintedCard findByCode(String code) { public PrintedCard findByCode(String code) {
CriteriaBuilder cb = getEm().getCriteriaBuilder(); CriteriaBuilder cb = getEm().getCriteriaBuilder();
...@@ -48,8 +46,8 @@ public class PrintedCardFacade extends IntegerPkGenericFacade<PrintedCard> { ...@@ -48,8 +46,8 @@ public class PrintedCardFacade extends IntegerPkGenericFacade<PrintedCard> {
cb.equal(root.get(CardCode_.event), eventbean.getCurrentEvent())); cb.equal(root.get(CardCode_.event), eventbean.getCurrentEvent()));
CardCode cardcode = getSingleNullableResult(getEm().createQuery(cq)); CardCode cardcode = getSingleNullableResult(getEm().createQuery(cq));
return (cardcode == null)?null : cardcode.getPrintedCard(); return (cardcode == null) ? null : cardcode.getPrintedCard();
} }
public List<PrintedCard> findAllEnabled(LanEvent currentEvent) { public List<PrintedCard> findAllEnabled(LanEvent currentEvent) {
...@@ -71,19 +69,20 @@ public class PrintedCardFacade extends IntegerPkGenericFacade<PrintedCard> { ...@@ -71,19 +69,20 @@ public class PrintedCardFacade extends IntegerPkGenericFacade<PrintedCard> {
cq.where(cb.equal(root.get(PrintedCard_.user), user), cq.where(cb.equal(root.get(PrintedCard_.user), user),
cb.equal(root.get(PrintedCard_.event), eventbean.getCurrentEvent())); cb.equal(root.get(PrintedCard_.event), eventbean.getCurrentEvent()));
cq.orderBy(cb.asc(root.get(PrintedCard_.template).get(CardTemplate_.power)));
return getEm().createQuery(cq).getResultList(); return getEm().createQuery(cq).getResultList();
} }
public PrintedCard findLatestByCodeFromAny(String code) { public PrintedCard findLatestByCodeFromAny(String code) {
CriteriaBuilder cb = getEm().getCriteriaBuilder(); CriteriaBuilder cb = getEm().getCriteriaBuilder();
CriteriaQuery<PrintedCard> cq = cb.createQuery(PrintedCard.class); CriteriaQuery<PrintedCard> cq = cb.createQuery(PrintedCard.class);
Root<CardCode> root = cq.from(CardCode.class); Root<CardCode> root = cq.from(CardCode.class);
cq.select(root.get(CardCode_.printedCard)); cq.select(root.get(CardCode_.printedCard));
cq.where(cb.equal(root.get(CardCode_.code), code)); cq.where(cb.equal(root.get(CardCode_.code), code));
cq.orderBy(cb.desc(root.get(CardCode_.printedCard).get(PrintedCard_.printTime))); cq.orderBy(cb.desc(root.get(CardCode_.printedCard).get(PrintedCard_.printTime)));
TypedQuery<PrintedCard> q = getEm().createQuery(cq); TypedQuery<PrintedCard> q = getEm().createQuery(cq);
q.setMaxResults(1); q.setMaxResults(1);
...@@ -96,7 +95,7 @@ public class PrintedCardFacade extends IntegerPkGenericFacade<PrintedCard> { ...@@ -96,7 +95,7 @@ public class PrintedCardFacade extends IntegerPkGenericFacade<PrintedCard> {
CriteriaBuilder cb = getEm().getCriteriaBuilder(); CriteriaBuilder cb = getEm().getCriteriaBuilder();
CriteriaQuery<PrintedCard> cq = cb.createQuery(PrintedCard.class); CriteriaQuery<PrintedCard> cq = cb.createQuery(PrintedCard.class);
Root<PrintedCard> root = cq.from(PrintedCard.class); Root<PrintedCard> root = cq.from(PrintedCard.class);
List<Predicate> preds = new ArrayList<Predicate>(); List<Predicate> preds = new ArrayList<Predicate>();
preds.add(cb.equal(root.get(PrintedCard_.event), eventbean.getCurrentEvent())); preds.add(cb.equal(root.get(PrintedCard_.event), eventbean.getCurrentEvent()));
...@@ -104,19 +103,19 @@ public class PrintedCardFacade extends IntegerPkGenericFacade<PrintedCard> { ...@@ -104,19 +103,19 @@ public class PrintedCardFacade extends IntegerPkGenericFacade<PrintedCard> {
if (state.length > 0) { if (state.length > 0) {
preds.add(root.get(PrintedCard_.cardState).in(Arrays.asList(state))); preds.add(root.get(PrintedCard_.cardState).in(Arrays.asList(state)));
} }
preds.add(cb.equal(root.get(PrintedCard_.enabled), true)); preds.add(cb.equal(root.get(PrintedCard_.enabled), true));
cq.where(preds.toArray(new Predicate[preds.size()])); cq.where(preds.toArray(new Predicate[preds.size()]));
cq.orderBy(cb.asc(root.get(PrintedCard_.user)), cb.asc(root.get(PrintedCard_.template).get(CardTemplate_.power))); cq.orderBy(cb.asc(root.get(PrintedCard_.user)), cb.asc(root.get(PrintedCard_.template).get(CardTemplate_.power)));
List<PrintedCard> pcl = getEm().createQuery(cq).getResultList(); List<PrintedCard> pcl = getEm().createQuery(cq).getResultList();
HashMap<Integer, PrintedCard> pch = new HashMap<>(); HashMap<Integer, PrintedCard> pch = new HashMap<>();
for(PrintedCard pc : pcl) { for (PrintedCard pc : pcl) {
pch.put(pc.getUser().getId(), pc); pch.put(pc.getUser().getId(), pc);
} }
return new ArrayList<PrintedCard>(pch.values()); return new ArrayList<PrintedCard>(pch.values());
} }
......
...@@ -38,11 +38,10 @@ public class EventUserCardStateFilter implements FacadeCallback<EventUser> { ...@@ -38,11 +38,10 @@ public class EventUserCardStateFilter implements FacadeCallback<EventUser> {
logger.debug("Requiring states {}", states); logger.debug("Requiring states {}", states);
Path<Integer> rootId = root.get(EventUser_.id); Path<Integer> rootId = root.get(EventUser_.id);
Subquery<Integer> subq = cq.subquery(Integer.class); Subquery<Integer> subq = cq.subquery(Integer.class);
Root<PrintedCard> subroot = subq.from(PrintedCard.class); Root<PrintedCard> subroot = subq.from(PrintedCard.class);
subq.select(subroot.join(PrintedCard_.user).get(EventUser_.id)); subq.select(subroot.join(PrintedCard_.user).get(EventUser_.id));
subq.where(subroot.get(PrintedCard_.cardState).in(states)); subq.where(subroot.get(PrintedCard_.cardState).in(states), cb.isTrue(subroot.get(PrintedCard_.enabled)));
predicates.add(rootId.in(subq)); predicates.add(rootId.in(subq));
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!