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.SharedPackageFamily;
5   import com.trendmicro.grid.acl.ds.jpa.util.NameValidator;
6   import com.trendmicro.grid.acl.metadata.Metadata;
7   import org.hibernate.annotations.Index;
8   import org.hibernate.annotations.NaturalId;
9   import org.hibernate.annotations.Type;
10  
11  import javax.persistence.*;
12  
13  import static com.trendmicro.grid.acl.Limits.MAX_DISPLAY_NAME_LENGTH;
14  import static com.trendmicro.grid.acl.Limits.MAX_NAME_LENGTH;
15  import static com.trendmicro.grid.acl.ds.jpa.QueryHints.*;
16  import static com.trendmicro.grid.acl.ds.jpa.QueryHints.CACHE_STORE_MODE;
17  import static com.trendmicro.grid.acl.ds.jpa.QueryHints.V_USE;
18  
19  /**
20   * Binds PackageFamily to the table "PACKAGE_FAMILY".
21   *
22   * @author juergen_kellerer, 2010-06-10
23   * @version 1.0
24   */
25  @NamedQueries({
26  		@NamedQuery(name = "PackageFamily.SelectByName", query = "" +
27  				"SELECT f FROM PACKAGE_FAMILY f WHERE f.basename = :basename",
28  				hints = {@QueryHint(name = CACHEABLE, value = V_YES),
29  						@QueryHint(name = CACHE_RETRIEVE_MODE, value = V_USE), @QueryHint(name = CACHE_STORE_MODE, value = V_USE)}),
30  
31  		@NamedQuery(name = "PackageFamily.SelectPrimaryKeyByName", query = "" +
32  				"SELECT f.id FROM PACKAGE_FAMILY f WHERE f.basename = :basename",
33  				hints = {@QueryHint(name = CACHEABLE, value = V_YES),
34  						@QueryHint(name = CACHE_RETRIEVE_MODE, value = V_USE), @QueryHint(name = CACHE_STORE_MODE, value = V_USE)}),
35  
36  		@NamedQuery(name = "PackageFamily.SelectNames", query = "" +
37  				"SELECT f.basename FROM PACKAGE_FAMILY f"),
38  
39  		@NamedQuery(name = "PackageFamily.SelectNamesByVendor", query = "" +
40  				"SELECT f.basename FROM PACKAGE_FAMILY f " +
41  				"WHERE f.vendor.name = :vendorName")
42  })
43  @Cacheable
44  @Entity(name = "PACKAGE_FAMILY")
45  public class JpaPackageFamily extends SharedPackageFamily {
46  
47  	private static final long serialVersionUID = -6897458179126577078L;
48  
49  	private int primaryKey;
50  
51  	public JpaPackageFamily() {
52  	}
53  
54  	public JpaPackageFamily(JpaVendor vendor, String basename, String displayName, Metadata metadata) {
55  		super(vendor, basename, displayName, metadata == null ? null : metadata.clone());
56  		NameValidator.getInstance().validatePackageFamilyName(vendor.getName(), basename);
57  	}
58  
59  	@Id
60  	@GeneratedValue
61  	@Column(name = "PACKAGE_FAMILY_ID")
62  	public int getPrimaryKey() {
63  		return primaryKey;
64  	}
65  
66  	public void setPrimaryKey(int primaryKey) {
67  		this.primaryKey = primaryKey;
68  	}
69  
70  	@JoinColumn(name = "VENDOR_ID")
71  	@ManyToOne(optional = false, cascade = CascadeType.REMOVE)
72  	public JpaVendor getVendor() {
73  		return (JpaVendor) vendor;
74  	}
75  
76  	public void setVendor(JpaVendor vendor) {
77  		this.vendor = vendor;
78  	}
79  
80  	@NaturalId
81  	@Type(type = "nstring")
82  	@Index(name = "IX_PACKAGE_FAMILY__BASE_NAME_INDEX")
83  	@Column(name = "BASE_NAME", length = MAX_NAME_LENGTH, unique = true, nullable = false, updatable = false)
84  	public String getBasename() {
85  		return basename;
86  	}
87  
88  	public void setBasename(String basename) {
89  		this.basename = basename;
90  	}
91  
92  	@Type(type = "nstring")
93  	@Column(name = "DISPLAY_NAME", length = MAX_DISPLAY_NAME_LENGTH, nullable = false)
94  	public String getDisplayName() {
95  		return displayName;
96  	}
97  
98  	public void setDisplayName(String displayName) {
99  		this.displayName = displayName;
100 	}
101 
102 	@Override
103 	@Type(type = "metadata")
104 	@Basic(fetch = FetchType.EAGER)
105 	@Column(name = "META_DATA", length = Limits.MAX_SERIALIZED_METADATA_LENGTH)
106 	public Metadata getMetadata() {
107 		return super.getMetadata();
108 	}
109 
110 	@Override
111 	public void setMetadata(Metadata metadata) { //NOSONAR - Override is required as hook for JPA.
112 		super.setMetadata(metadata == null ? null : metadata.clone());
113 	}
114 
115 	/**
116 	 * {@inheritDoc}
117 	 */
118 	@Override
119 	public String toString() {
120 		return "JpaPackageFamily{" +
121 				"primaryKey=" + primaryKey +
122 				", basename='" + basename + '\'' +
123 				", displayName='" + displayName + '\'' +
124 				", metadata=" + metadata +
125 				", vendor=" + vendor +
126 				'}';
127 	}
128 }