View Javadoc
1   package ejava.examples.ejbwar.inventory.bo;
2   
3   import javax.json.bind.annotation.JsonbProperty;
4   import javax.json.bind.annotation.JsonbPropertyOrder;
5   import javax.json.bind.annotation.JsonbTransient;
6   import javax.persistence.Column;
7   import javax.persistence.Entity;
8   import javax.persistence.GeneratedValue;
9   import javax.persistence.GenerationType;
10  import javax.persistence.Id;
11  import javax.persistence.NamedQueries;
12  import javax.persistence.NamedQuery;
13  import javax.persistence.Table;
14  import javax.xml.bind.annotation.XmlAccessType;
15  import javax.xml.bind.annotation.XmlAccessorType;
16  import javax.xml.bind.annotation.XmlAttribute;
17  import javax.xml.bind.annotation.XmlElement;
18  import javax.xml.bind.annotation.XmlRootElement;
19  import javax.xml.bind.annotation.XmlTransient;
20  import javax.xml.bind.annotation.XmlType;
21  
22  import com.fasterxml.jackson.annotation.JsonIgnore;
23  import com.fasterxml.jackson.annotation.JsonProperty;
24  
25  /**
26   * This class represents a specific product. It has been mapped to both
27   * the DB, XML, and JSON -- which is not that common to do and causes 
28   * some conflict.
29   * 
30   * JAX-RS originally only had to support JAXB (i.e., @Xml annotations) and
31   * each vendor added optional extensions to support alternate formats like
32   * JSON. One common approach was to apply the Xml-centric JAXB bindings to 
33   * JSON marshaling. That is what was done by the Jackson JSON marshaler.
34   * Jackson also had annotations (@Json) that could override JAXB (@Xml) 
35   * annotations. That is how things sat for < jee8.
36   * 
37   * With jee8 and jax-rs 2.1, JSON-B was created and support mandated. This 
38   * example was written before full jee8 compliance. Thus it has been annotated 
39   * with JSOB-B annotations (@Jsonb) and Jackson (@Json) annotations so that 
40   * it will work with both jee7 and jee8 deployments. The client will always
41   * be using JSON-B.
42   * 
43   * The use of JAXB annotations for Json does confuse the marshaling decision 
44   * logic. So we have to make sure we have JSON-B annotations on the class,
45   * and passed to the marshaling methods. Of course -- if your marshaled class
46   * is a true DTO, then there is much less need for overrides.
47   */
48  @XmlRootElement(name="product", namespace=InventoryRepresentation.NAMESPACE)
49  @XmlType(name="product", namespace=InventoryRepresentation.NAMESPACE, propOrder={
50  		"name",
51  		"quantity",
52  		"price"
53  })
54  @XmlAccessorType(XmlAccessType.PROPERTY)
55  @JsonbPropertyOrder({"id","name", "quantity", "price"})
56  
57  @Entity
58  @Table(name="JAXRSINV_PRODUCT")
59  @NamedQueries({
60  	@NamedQuery(name=Product.FIND_BY_NAME, 
61  			query="select p from Product p where p.name like :criteria")
62  })
63  public class Product extends InventoryRepresentation {
64  	private static final long serialVersionUID = -4058695470696405277L;
65  	public static final String FIND_BY_NAME = "Inventory.findProductByName";
66  
67  	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
68  	@Column(name="ID")
69  	private int id;
70  	
71  	@Column(name="NAME", nullable=false)
72  	private String name;
73  	
74  	@Column(name="QTY", nullable=true)
75  	private Integer quantity;
76  	
77  	@Column(name="PRICE", nullable=true)
78  	private Double price;
79  
80      @Column(name="PROTECTED_VALUE", length=36, nullable=false, updatable=false)
81      private String protectedValue;
82  
83      public Product() {}
84  	public Product(String name, Integer quantity, Double price) {
85  		this.name=name;
86  		this.quantity=quantity;
87  		this.price=price;
88  	}
89  	public Product(String name) {
90  		this(name, null, null);
91  	}
92  	
93  	@XmlAttribute(required=true)
94  	public int getId() { return id;}
95  	public void setId(int id) {
96  		this.id = id;
97  	}
98  
99  	@XmlAttribute(required=true, name="xmlName") //used both during marshal and unmarshal
100 	@JsonbProperty("jsonName") //for json-b -- used during toJson()
101 	@JsonProperty("jsonName")  //for jackson json -- needed when server not >=jee8 mode
102 	public String getName() { return name; }
103     @JsonbProperty("jsonName") //used during fromJson()
104 	public void setName(String name) {
105 		this.name = name;
106 	}
107 
108 	@XmlElement(required=false)
109 	public Integer getQuantity() { return quantity; }
110 	public void setQuantity(Integer quantity) {
111 		this.quantity = quantity;
112 	}
113 
114 	@XmlElement(required=false)
115 	public Double getPrice() { return price; }
116 	public void setPrice(Double price) {
117 		this.price = price;
118 	}
119 	
120 	@XmlTransient
121     @JsonbTransient
122 	public String getProtectedValue() { return protectedValue; }
123     @JsonbTransient
124     @JsonIgnore //for jackson json -- when < jee8 or overriding JAXB annotation 
125 	public void setProtectedValue(String protectedValue) {
126         this.protectedValue = protectedValue;
127     }
128     public Product withProtectedValue(String string) {
129         setProtectedValue(string);
130         return this;
131     }
132     
133     @Override
134     public String toString() {
135         StringBuilder builder = new StringBuilder();
136         builder.append("Product [id=").append(id)
137                 .append(", name=").append(name)
138                 .append(", quantity=").append(quantity)
139                 .append(", price=").append(price)
140                 .append(", protectedValue=")
141                 .append(protectedValue).append("]");
142         return builder.toString();
143     }
144     
145     
146 }