View Javadoc
1   package info.ejava.examples.ejb.cdisales.bo;
2   
3   import java.math.BigDecimal;
4   import java.util.Comparator;
5   import java.util.Date;
6   
7   import javax.persistence.*;
8   import javax.validation.constraints.NotNull;
9   
10  @Entity
11  @Table(name="CDISALES_PRODUCT")
12  @NamedQueries({
13      @NamedQuery(name="CDIProduct.getProductsByBuyer", 
14          query="select p from Product p where p.buyer=:buyer"),
15      @NamedQuery(name="CDIProduct.getProductsByBuyerLogin", 
16          query="select p from Product p where p.buyer.login=:login"),
17      @NamedQuery(name="CDIProduct.getProductsBySeller", 
18          query="select p from Product p where p.seller=:seller"),
19      @NamedQuery(name="CDIProduct.getProductsBySellerLogin", 
20          query="select p from Product p where p.seller.login=:login")
21  })
22  public class Product {
23      @Id @GeneratedValue
24      @Column(name="PRODUCT_ID")
25      private int id;
26      
27      @NotNull
28      @Temporal(TemporalType.TIMESTAMP)
29      @Column(name="DATE_CREATED")
30      private Date dateCreated;
31      
32      @NotNull
33      @Enumerated(EnumType.ORDINAL)
34      @Column(name="CATEGORY", nullable=false)
35      private ProductCategory category;
36      
37      @NotNull
38      @Column(name="PRODUCT_NAME", length=50)
39      private String name;
40      
41      @Column(name="YEAR")
42      private int year;
43      
44      @NotNull
45      @Column(name="PRICE", precision=7, scale=2, nullable=false)
46      private BigDecimal price;
47      
48      @NotNull
49      @ManyToOne(fetch=FetchType.LAZY, optional=false)
50      @JoinColumn(name="SELLER_ID", nullable=false)
51      private Member seller;
52      
53      @ManyToOne(fetch=FetchType.EAGER)
54      @JoinColumn(name="BUYER_ID")
55      private Member buyer;
56      
57      @PrePersist
58      protected void prePersist() { if (dateCreated==null) { dateCreated=new Date(); } }
59      
60      public Product() {}
61      public Product(int id) {
62          this.id = id;
63      }
64      
65      public int getId() { return id; }    
66      public Date getDateCreated() { return dateCreated; }
67      
68      public ProductCategory getCategory() { return category;}
69      public void setCategory(ProductCategory category) {
70          this.category = category;
71      }
72      
73      public String getName() { return name; }
74      public void setName(String name) {
75          this.name = name;
76      }
77      
78      public int getYear() { return year; }
79      public void setYear(int year) {
80          this.year = year;
81      }
82      
83      public BigDecimal getPrice() { return price;}
84      public void setPrice(BigDecimal price) {
85          this.price = price;
86      }
87      
88      public Member getSeller() { return seller; }
89      public void setSeller(Member seller) {
90          this.seller = seller;
91      }
92      
93      public Member getBuyer() { return buyer; }
94      public void setBuyer(Member buyer) {
95          this.buyer = buyer;
96      }
97      
98      @Override
99      public String toString() {
100         StringBuilder builder = new StringBuilder();
101         builder.append("Product [category=").append(category)
102                 .append(", name=").append(name)
103                 .append(", year=").append(year)
104                 .append(", price=").append(price)
105                 .append("]");
106         return builder.toString();
107     }
108 
109     public static class ProductASC implements Comparator<Product> {
110         @Override
111         public int compare(Product lhs, Product rhs) {
112             if (lhs==null && rhs==null) { return 0; }
113             if (lhs!=null && (rhs==null || rhs.name==null)) { return 1; }
114             if (rhs!=null && (lhs==null || lhs.name==null)) { return -1; }
115             int result=lhs.name.compareTo(rhs.name);
116             if (result!=0) { return result; }
117             
118             if (lhs.dateCreated==null && rhs.dateCreated==null) { return result; }
119             if (lhs.dateCreated!=null && rhs.dateCreated==null) { return -1; }
120             if (rhs.dateCreated!=null && lhs.dateCreated==null) { return 1; }
121             return lhs.dateCreated.compareTo(lhs.dateCreated);
122         }        
123     }
124 }