package models;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;


@Entity
@Table(name = "POS_POSITION")
public class PositionModel extends AuditedModel {

  public final static Finder<Long, PositionModel> find = new Finder<>(PositionModel.class);
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  public Long id;

  @Column(unique = true)
  public String name;

  // this field is not stored in the db, see OrderModel.position_name
  @Transient
  public String alias;

  @Transient
  public boolean isDeletable;

  public Double latitude;
  public Double longitude;

  @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  @JoinTable(name = "pos_position_tags", joinColumns = {
      @JoinColumn(name = "POSITION_ID", nullable = false, updatable = false)},
      inverseJoinColumns = {@JoinColumn(name = "TAG_ID",
          nullable = false, updatable = false)})
  @OrderBy(value="name")
  public List<PositionTagModel> tags = new ArrayList<PositionTagModel>() {
  };

  public static PositionModel findByName(String name) {
    return  find.where().ieq("name", name).findUnique();
  }

}
