Place.java 8.28 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 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.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 {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    private EventPk id;
    @Lob
    @Column(name = "place_description")
    private String description;
    @Column(name = "place_name")
    private String name;
    @Column(name = "map_x")
    private Integer mapX;
    @Column(name = "map_y")
    private Integer mapY;
    @Column(name = "width")
    private Integer width;
    @Column(name = "height")
    private Integer height;
    @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", nullable = false),
            @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 = false, insertable = false),
            @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 = "id")
    @ManyToOne
    private User currentUser;
    @Version
    @Column(nullable = false)
    private int jpaVersionField = 0;

    public Place() {
    }

    public Place(EventPk placesId) {
        this.id = placesId;
    }

    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 groupsId) {
        this.group = groupsId;
    }

    public EventMap getMap() {
        return map;
    }

    public void setMap(EventMap mapsId) {
        this.map = mapsId;
    }

    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 (placeGroup != null) {
            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));
    }
}