1   package com.trendmicro.grid.acl.ds.jpa.entities;
2   
3   import com.trendmicro.grid.acl.Limits;
4   import com.trendmicro.grid.acl.ds.datatypes.SharedVendor;
5   import com.trendmicro.grid.acl.ds.jpa.util.NameValidator;
6   import com.trendmicro.grid.acl.l0.datatypes.Vendor;
7   import com.trendmicro.grid.acl.metadata.Metadata;
8   import org.hibernate.annotations.Index;
9   import org.hibernate.annotations.NaturalId;
10  import org.hibernate.annotations.Type;
11  
12  import javax.persistence.*;
13  import java.sql.Timestamp;
14  import java.util.Date;
15  
16  import static com.trendmicro.grid.acl.Limits.MAX_DISPLAY_NAME_LENGTH;
17  import static com.trendmicro.grid.acl.Limits.MAX_NAME_LENGTH;
18  import static com.trendmicro.grid.acl.ds.jpa.QueryHints.*;
19  import static com.trendmicro.grid.acl.ds.jpa.QueryHints.CACHE_STORE_MODE;
20  import static com.trendmicro.grid.acl.ds.jpa.QueryHints.V_USE;
21  
22  /**
23   * Binds Vendor to the table "VENDORS".
24   *
25   * @author juergen_kellerer, 2010-06-10
26   * @version 1.0
27   */
28  @NamedQueries({
29  		@NamedQuery(name = "Vendor.SelectByName", query = "" +
30  				"SELECT v FROM VENDORS v WHERE v.name = :name",
31  				hints = {@QueryHint(name = CACHEABLE, value = V_YES),
32  						@QueryHint(name = CACHE_RETRIEVE_MODE, value = V_USE), @QueryHint(name = CACHE_STORE_MODE, value = V_USE)}),
33  
34  		@NamedQuery(name = "Vendor.SelectPrimaryKeyByName", query = "" +
35  				"SELECT v.id FROM VENDORS v WHERE v.name = :name",
36  				hints = {@QueryHint(name = CACHEABLE, value = V_YES),
37  						@QueryHint(name = CACHE_RETRIEVE_MODE, value = V_USE), @QueryHint(name = CACHE_STORE_MODE, value = V_USE)}),
38  
39  		@NamedQuery(name = "Vendor.SelectNames", query = "" +
40  				"SELECT v.name FROM VENDORS v")
41  })
42  @Cacheable
43  @Entity(name = "VENDORS")
44  public class JpaVendor extends SharedVendor {
45  
46  	private static final long serialVersionUID = 1500391932224951960L;
47  
48  	private int primaryKey;
49  
50  	public JpaVendor() {
51  	}
52  
53  	public JpaVendor(String name, String displayName, Date firstSeen, Metadata metadata) {
54  		super(name, displayName, firstSeen, metadata == null ? null : metadata.clone());
55  		NameValidator.getInstance().validateVendorName(name);
56  	}
57  
58  	@Id
59  	@GeneratedValue
60  	@Column(name = "VENDOR_ID")
61  	public int getPrimaryKey() {
62  		return primaryKey;
63  	}
64  
65  	public void setPrimaryKey(int primaryKey) {
66  		this.primaryKey = primaryKey;
67  	}
68  
69  	@Column(name = "CREATED", nullable = false)
70  	@Temporal(TemporalType.TIMESTAMP)
71  	public Date getFirstSeen() {
72  		if (firstSeen == null)
73  			firstSeen = new Date();
74  		return firstSeen;
75  	}
76  
77  	public void setFirstSeen(Date firstSeen) {
78  		// Note: Converting Timestamp to Date to avoid that "equals()" fails to compare 2 instances.
79  		this.firstSeen = firstSeen instanceof Timestamp ? new Date(firstSeen.getTime()) : firstSeen;
80  	}
81  
82  	@NaturalId
83  	@Type(type = "nstring")
84  	@Index(name = "IX_VENDORS__NAME_INDEX")
85  	@Column(name = "NAME", length = MAX_NAME_LENGTH, unique = true, nullable = false, updatable = false)
86  	public String getName() {
87  		return name;
88  	}
89  
90  	public void setName(String name) {
91  		this.name = name;
92  	}
93  
94  	@Type(type = "nstring")
95  	@Column(name = "DISPLAY_NAME", length = MAX_DISPLAY_NAME_LENGTH, nullable = false)
96  	public String getDisplayName() {
97  		return displayName;
98  	}
99  
100 	public void setDisplayName(String displayName) {
101 		this.displayName = displayName;
102 	}
103 
104 	@Override
105 	@Type(type = "metadata")
106 	@Basic(fetch = FetchType.EAGER)
107 	@Column(name = "META_DATA", length = Limits.MAX_SERIALIZED_METADATA_LENGTH)
108 	public Metadata getMetadata() {
109 		return super.getMetadata();
110 	}
111 
112 	@Override
113 	public void setMetadata(Metadata metadata) { //NOSONAR - Override is required as hook for JPA.
114 		super.setMetadata(metadata == null ? null : metadata.clone());
115 	}
116 
117 	/**
118 	 * {@inheritDoc}
119 	 */
120 	@Override
121 	public String toString() {
122 		return "JpaVendor{" +
123 				"primaryKey='" + primaryKey + '\'' +
124 				", name='" + name + '\'' +
125 				", displayName='" + displayName + '\'' +
126 				", firstSeen=" + firstSeen +
127 				", metadata=" + metadata +
128 				'}';
129 	}
130 }