Commit 9c338a42 by Tuomas Riihimäki

Add placeslot entity and some utility functions

1 parent 3fd67c49
......@@ -35,6 +35,7 @@ import fi.codecrew.moya.model.GroupMembership;
import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.model.PlaceGroup;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.model.PlaceSlot;
/**
*
......@@ -115,4 +116,6 @@ public interface PlaceBeanLocal {
List<Product> getMapProducts(EventMap map);
List<PlaceSlot> getFreePlaceslots(EventUser user);
}
......@@ -64,6 +64,7 @@ import fi.codecrew.moya.facade.EventUserFacade;
import fi.codecrew.moya.facade.GroupMembershipFacade;
import fi.codecrew.moya.facade.PlaceFacade;
import fi.codecrew.moya.facade.PlaceGroupFacade;
import fi.codecrew.moya.facade.PlaceSlotFacade;
import fi.codecrew.moya.facade.UserFacade;
import fi.codecrew.moya.model.EventMap;
import fi.codecrew.moya.model.EventUser;
......@@ -71,6 +72,7 @@ import fi.codecrew.moya.model.GroupMembership;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.model.PlaceGroup;
import fi.codecrew.moya.model.PlaceSlot;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.model.ProductFlag;
......@@ -127,6 +129,8 @@ public class PlaceBean implements PlaceBeanLocal {
private BarcodeBeanLocal barcodeBean;
@EJB
private EventMapFacade eventMapFacade;
@EJB
private PlaceSlotFacade placeSlotFacade;
@Override
@RolesAllowed(MapPermission.S_MANAGE_MAPS)
......@@ -146,6 +150,8 @@ public class PlaceBean implements PlaceBeanLocal {
@RolesAllowed(SpecialPermission.S_USER)
@Override
/** Use place slots */
@Deprecated
public BigDecimal getTotalReservationPrice(Place newPlace)
{
return addAndCalcPrice(permbean.getCurrentUser(), newPlace);
......@@ -153,12 +159,16 @@ public class PlaceBean implements PlaceBeanLocal {
@RolesAllowed(MapPermission.S_MANAGE_OTHERS)
@Override
/** Use place slots */
@Deprecated
public BigDecimal getTotalReservationPrice(EventUser user, Place newPlace)
{
return addAndCalcPrice(user, newPlace);
}
/** Use place slots */
@Deprecated()
private BigDecimal addAndCalcPrice(EventUser user, Place newPlace) {
Set<Place> places = new HashSet<Place>();
......@@ -241,6 +251,7 @@ public class PlaceBean implements PlaceBeanLocal {
for (Timer t : ts.getTimers()) {
if (t.getInfo().equals(PLACE_RESERVE_TIMEOUTER)) {
foundTimeout = true;
break;
}
}
if (!foundTimeout) {
......@@ -689,6 +700,13 @@ public class PlaceBean implements PlaceBeanLocal {
@Override
public List<EventMap> getMaps() {
return eventMapFacade.getMaps();
@Override
public List<PlaceSlot> getFreePlaceslots(EventUser user) {
user = eventUserFacade.reload(user);
if (!permbean.isCurrentUser(user) && !permbean.hasPermission(MapPermission.MANAGE_OTHERS))
throw new EJBAccessException("User " + permbean.getCurrentUser() + "tried to fetch free places for user " + user);
return placeSlotFacade.findFreePlaces(user);
}
@Override
......
/*
* 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.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.model.Bill;
import fi.codecrew.moya.model.Bill_;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.model.PlaceSlot;
import fi.codecrew.moya.model.PlaceSlot_;
@Stateless
@LocalBean
public class PlaceSlotFacade extends IntegerPkGenericFacade<Place> {
private static final Logger logger = LoggerFactory.getLogger(PlaceSlotFacade.class);
@EJB
EventBeanLocal eventBean;
public PlaceSlotFacade() {
super(Place.class);
}
/**
* Returns placeslots this user can use.
*
* @param user
* @return
*/
public List<PlaceSlot> findFreePlaceSlots(EventUser user) {
CriteriaBuilder cb = getEm().getCriteriaBuilder();
CriteriaQuery<PlaceSlot> q = cb.createQuery(PlaceSlot.class);
Root<PlaceSlot> root = q.from(PlaceSlot.class);
Path<Bill> bill = root.get(PlaceSlot_.bill);
q.where(cb.(bill.get(Bill_.accountEvent))
}
// private List<PlaceSlot> getSlotsForUser(EventUser u) {
//
// CriteriaBuilder cb = getEm().getCriteriaBuilder();
// CriteriaQuery<PlaceSlot> cq = cb.createQuery(PlaceSlot.class);
//
// }
}
......@@ -227,12 +227,12 @@ public class Bill extends GenericEntity {
public Bill(LanEvent event, EventUser user, long expireTimeHours) {
this(event, user, Calendar.getInstance());
this.expires.setTimeInMillis((System.currentTimeMillis() + (expireTimeHours * 60 * 60 * 1000)));
this.expires.setTimeInMillis((System.currentTimeMillis() + (expireTimeHours*60*60 * 1000 )));
}
public Bill(LanEvent event, long expireTimeHours) {
this(event, Calendar.getInstance());
this.expires.setTimeInMillis((System.currentTimeMillis() + (expireTimeHours * 60 * 60 * 1000)));
this.expires.setTimeInMillis((System.currentTimeMillis() + (expireTimeHours*60*60 * 1000 )));
}
public Bill() {
......@@ -391,7 +391,7 @@ public class Bill extends GenericEntity {
}
public void setPaidDate(Date paidDate) {
if (paidDate != null)
if(paidDate != null)
expires = null;
this.paidDate = paidDate;
......@@ -462,14 +462,14 @@ public class Bill extends GenericEntity {
}
public boolean isExpired() {
if (expires == null)
if(isPaid() || expires == null)
return false;
return Calendar.getInstance().after(expires);
}
public void markExpired() {
if (isExpired() || isPaid())
if(isExpired() || isPaid())
return;
expires = Calendar.getInstance();
......@@ -484,7 +484,7 @@ public class Bill extends GenericEntity {
continue;
total = total.add(l.getQuantity());
}
}
return total;
}
......
package fi.codecrew.moya.model;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "place_slots")
public class PlaceSlot extends GenericEntity {
private static final long serialVersionUID = -7163361140751806941L;
@Temporal(TemporalType.TIMESTAMP)
private Date created;
@Temporal(TemporalType.TIMESTAMP)
private Date used;
@JoinColumn(nullable = false)
@ManyToOne
private Bill bill;
@JoinColumn(nullable = false)
@ManyToOne
private Product product;
@JoinColumn(nullable = true, unique = true)
@OneToOne
private Place place;
@Lob
private String description;
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUsed() {
return used;
}
public void setUsed(Date used) {
this.used = used;
}
public Bill getBill() {
return bill;
}
public void setBill(Bill bill) {
this.bill = bill;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Place getPlace() {
return place;
}
public void setPlace(Place place) {
this.place = place;
}
}
......@@ -19,6 +19,7 @@
package fi.codecrew.moya.web.cdiview.map;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
......@@ -40,6 +41,8 @@ import fi.codecrew.moya.model.EventMap;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.model.PlaceGroup;
import fi.codecrew.moya.model.PlaceSlot;
import fi.codecrew.moya.model.ProductFlag;
import fi.codecrew.moya.model.User;
import fi.codecrew.moya.utilities.SearchQuery;
import fi.codecrew.moya.utilities.SearchQuery.QuerySortOrder;
......@@ -117,6 +120,11 @@ public class PlaceView extends GenericCDIView {
} else if (place.isBuyable() && !place.isTaken()) {
boolean canReserve = false;
// If place product is marked "PREPAID_CREDIT", places are bought from
// slots. If flag does not exist place slot is created from a bill.
if (place.getProduct().getProductFlags().contains(ProductFlag.PREPAID_CREDIT)) {
BigDecimal balance = user.getAccountBalance();
BigDecimal price = null;
......@@ -125,18 +133,24 @@ public class PlaceView extends GenericCDIView {
} else {
price = placebean.getTotalReservationPrice(user, place);
}
canReserve = (price.compareTo(balance) <= 0);
logger.debug("Balance {}, price {}", balance, price);
if (price.compareTo(balance) <= 0) {
logger.debug("Place was free. Marking for user.");
if (!placebean.reservePlace(place, user)) {
this.addFaceMessage("mapView.errorWhenReservingPlace");
}
} else {
if (balance.compareTo(BigDecimal.ZERO) > 0) {
addFaceMessage("mapView.notEnoughCreditsToReserve");
}
if (!canReserve)
logger.debug("Did not have enought credits to reserve place! required {} , got {}", price, balance);
} else {
List<PlaceSlot> placeslots = placebean.getFreePlaceslots(user);
canReserve = placeslots.size() > 0;
}
if (canReserve) {
logger.debug("Place was free. Marking for user.");
if (!placebean.reservePlace(place, user)) {
this.addFaceMessage("mapView.errorWhenReservingPlace");
}
}
}
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!