Place.java 9.7 KB
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fi.insomnia.bortal.model;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;



/**
 * 
 */
@Entity
@Table(name = "places")
@NamedQueries({
    @NamedQuery(name = "Place.findAll", query = "SELECT p FROM Place p"),
    @NamedQuery(name = "Place.findByDescription", query = "SELECT p FROM Place p WHERE p.description = :description"),
    @NamedQuery(name = "Place.findByName", query = "SELECT p FROM Place p WHERE p.name = :name"),
    @NamedQuery(name = "Place.findByMapX", query = "SELECT p FROM Place p WHERE p.mapX = :mapX"),
    @NamedQuery(name = "Place.findByMapY", query = "SELECT p FROM Place p WHERE p.mapY = :mapY"),
    @NamedQuery(name = "Place.findByDetails", query = "SELECT p FROM Place p WHERE p.details = :details"),
    @NamedQuery(name = "Place.findByCode", query = "SELECT p FROM Place p WHERE p.code = :code")})
public class Place implements EventChildInterface, Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -5314851260772328611L;
    @EmbeddedId
    private EventPk id;
    @Lob
    @Column(name = "place_description")
    private String description;
    @Column(name = "place_name")
    private String name;
    @Column(name = "map_x")
    private int mapX = 0;
    @Column(name = "map_y")
    private int mapY = 0;
    @Column(name = "width")
    private int  width = 0;
    @Column(name = "height")
    private int height = 0;
    @Column(name = "release_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar releaseTime = Calendar.getInstance();
    @Column(name = "place_details")
    @Lob
    private String details = "";
    @Column(name = "place_code")
    private String code = "";
    @OneToOne(mappedBy = "placeReservation")
    private GroupMembership placeReserver;
    /**
     * Which group has bought the place
     */
    @JoinColumns({
        @JoinColumn(name = "group_id", referencedColumnName = "id", updatable = true, insertable = true),
        @JoinColumn(name = "event_id", referencedColumnName = "event_id", nullable = false, updatable = false, insertable = false)})
    @ManyToOne
    private PlaceGroup group;
    @JoinColumns({
        @JoinColumn(name = "map_id", referencedColumnName = "id", nullable = false, updatable = true, insertable = true),
        @JoinColumn(name = "event_id", referencedColumnName = "event_id", nullable = false, updatable = false, insertable = false)})
    @ManyToOne(optional = false)
    private EventMap map;
    /**
     * Which ticket type is this place sold as
     */
    @JoinColumns({
        @JoinColumn(name = "products_id", referencedColumnName = "id"),
        @JoinColumn(name = "event_id", referencedColumnName = "event_id", nullable = false, updatable = false, insertable = false)})
    @ManyToOne()
    private Product product;
    /**
     * Who is the current currentUser (mapped with code printed on the place) of
     * the place. Used in Vectorama currentUser tracking.
     */
    @JoinColumn(name = "current_user_id", referencedColumnName = "user_id")
    @ManyToOne
    private User currentUser;
    @Version
    @Column(nullable = false)
    private int jpaVersionField = 0;

    public Place() {
    }

    public Place(EventMap eventMap) {
        this.id = new EventPk();
        this.id.setEventId(eventMap.getId().getEventId());

    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String placeDescription) {
        this.description = placeDescription;
    }

    public String getName() {
        return name;
    }

    public void setName(String placeName) {
        this.name = placeName;
    }

    public Integer getMapX() {
        return mapX;
    }

    public void setMapX(Integer mapX) {
        this.mapX = mapX;
    }

    public Integer getMapY() {
        return mapY;
    }

    public void setMapY(Integer mapY) {
        this.mapY = mapY;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String placeDetails) {
        this.details = placeDetails;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String placeCode) {
        this.code = placeCode;
    }

    public PlaceGroup getGroup() {
        return group;
    }

    public void setGroup(PlaceGroup group) {
        if (group.getId().getEventId() != getId().getEventId()) {
            throw new RuntimeException("Can not set Group from different Event to Place!");
        }
        this.group = group;
    }

    public EventMap getMap() {
        return map;
    }

    public void setMap(EventMap map) {
        if (map.getId().getEventId() != getId().getEventId()) {
            throw new RuntimeException("Can not set Map from different Event to Place!");
        }
        this.map = map;
        // this.mapId = map.getId().getId();
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product productsId) {
        this.product = productsId;
    }

    public User getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(User usersId) {
        this.currentUser = usersId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getId() != null ? getId().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are
        // not set
        if (!(object instanceof Place)) {
            return false;
        }
        Place other = (Place) object;
        if ((this.getId() == null && other.getId() != null)
                || (this.getId() != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "fi.insomnia.bortal.model.Place[id=" + getId() + "]";
    }

    /**
     * @return the id
     */
    @Override
    public EventPk getId() {
        return id;
    }

    /**
     * @param id
     *            the id to set
     */
    @Override
    public void setId(EventPk id) {
        this.id = id;
    }

    /**
     * @return the jpaVersionField
     */
    @Override
    public int getJpaVersionField() {
        return jpaVersionField;
    }

    /**
     * @param jpaVersionField
     *            the jpaVersionField to set
     */
    @Override
    public void setJpaVersionField(int jpaVersionField) {
        this.jpaVersionField = jpaVersionField;
    }

    public void setPlaceReserver(GroupMembership placeReserver) {
        this.placeReserver = placeReserver;
    }

    public GroupMembership getPlaceReserver() {
        return placeReserver;
    }

    /**
     * @return the height
     */
    public Integer getHeight() {
        return height;
    }

    /**
     * @param height
     *            the height to set
     */
    public void setHeight(Integer height) {
        this.height = height;
    }

    /**
     * @return the width
     */
    public Integer getWidth() {
        return width;
    }

    /**
     * @param width
     *            the width to set
     */
    public void setWidth(Integer width) {
        this.width = width;
    }

    public boolean isCoordinateInPlace(int x, int y) {
        if (x > mapX
                && x < (mapX + width)
                && y > mapY
                && y < (mapY + height)) {
            return true;
        }

        return false;
    }
    private static final Color NORMAL_COLOR = Color.BLUE;
    private static final Color RESERVED_COLOR = Color.RED;
    private static final Color SELECTED_COLOR = Color.GREEN;
    private static final Color OWNED_COLOR = Color.GREEN;

    public void drawPlace(BufferedImage targetImage) {
        Graphics2D g = targetImage.createGraphics();

        if (isReserved()) {
            g.setColor(RESERVED_COLOR);
            g.fill(new Rectangle(mapX, mapY, width, height));
        } else {
            g.setColor(NORMAL_COLOR);
            g.draw(new Rectangle(mapX, mapY, width, height));
        }
    }

    public void drawSelectedPlace(BufferedImage targetImage) {
        Graphics2D g = targetImage.createGraphics();
        g.setColor(SELECTED_COLOR);

        g.fill(new Rectangle(mapX, mapY, width, height));
    }

    public void drawOwnedPlace(BufferedImage targetImage) {
        Graphics2D g = targetImage.createGraphics();
        g.setColor(OWNED_COLOR);

        g.fill(new Rectangle(mapX, mapY, width, height));
    }

    public boolean isReserved() {



        if (releaseTime != null) {
            if (System.currentTimeMillis() > releaseTime.getTimeInMillis()) {
                setReserved(false);
            } else {
                return true;
            }
        }
  
        
        if(group != null) {
            return true;
        }

        return false;
    }

    public static final long RESERVE_TIME = 1000*60*20; // 20min.
    public void setReserved(boolean reserved) {
        if(reserved) {
            releaseTime = Calendar.getInstance();
            releaseTime.setTimeInMillis(System.currentTimeMillis()+RESERVE_TIME);
        } else {
            releaseTime = null;
        }
    }
}