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.SharedSource;
5   import com.trendmicro.grid.acl.metadata.Metadata;
6   import org.hibernate.annotations.Type;
7   
8   import javax.persistence.*;
9   import java.net.URI;
10  
11  import static com.trendmicro.grid.acl.Limits.SOURCE_MAX_INTERNAL_URI_LENGTH;
12  import static com.trendmicro.grid.acl.Limits.SOURCE_MAX_REMOTE_URI_LENGTH;
13  import static com.trendmicro.grid.acl.ds.jpa.QueryHints.*;
14  import static com.trendmicro.grid.acl.ds.jpa.QueryHints.CACHE_STORE_MODE;
15  import static com.trendmicro.grid.acl.ds.jpa.QueryHints.V_USE;
16  
17  /**
18   * Binds Source to the table "SOURCE".
19   *
20   * @author juergen_kellerer, 2010-06-01
21   * @version 1.0
22   */
23  @NamedQueries({
24  		@NamedQuery(name = "Source.SelectSourcesOfDomainInRange", query = "" +
25  				"SELECT s.sourceInformation.publicGUID FROM SOURCE s " +
26  				"WHERE 	s.sourceDomain.name = :name AND " +
27  				"		s.sourceInformation.lastModified BETWEEN :fromDate AND :toDate"),
28  		@NamedQuery(name = "Source.SelectSourcesOfDomain", query = "" +
29  				"SELECT s.sourceInformation.publicGUID FROM SOURCE s " +
30  				"WHERE 	s.sourceDomain.name = :name"),
31  
32  		@NamedQuery(name = "Source.SelectSourceInformationById", query = "" +
33  				"SELECT s.sourceInformation FROM SOURCE s " +
34  				"WHERE 	s.sourceInformation.publicGUID = :publicGUID",
35  				hints = {@QueryHint(name = CACHEABLE, value = V_YES),
36  						@QueryHint(name = CACHE_RETRIEVE_MODE, value = V_USE), @QueryHint(name = CACHE_STORE_MODE, value = V_USE)}),
37  
38  		@NamedQuery(name = "Source.SelectSourceById", query = "" +
39  				"SELECT s FROM SOURCE s " +
40  				"WHERE 	s.sourceInformation.publicGUID = :publicGUID",
41  				hints = {@QueryHint(name = CACHEABLE, value = V_YES),
42  						@QueryHint(name = CACHE_RETRIEVE_MODE, value = V_USE), @QueryHint(name = CACHE_STORE_MODE, value = V_USE)}),
43  
44  		@NamedQuery(name = "Source.SelectSourcePrimaryKeyById", query = "" +
45  				"SELECT s.id FROM SOURCE s " +
46  				"WHERE 	s.sourceInformation.publicGUID = :publicGUID",
47  				hints = {@QueryHint(name = CACHEABLE, value = V_YES),
48  						@QueryHint(name = CACHE_RETRIEVE_MODE, value = V_USE), @QueryHint(name = CACHE_STORE_MODE, value = V_USE)}),
49  
50  		@NamedQuery(name = "Source.SelectSourceExistsById", query = "" +
51  				"SELECT COUNT(*) FROM SOURCE s " +
52  				"WHERE 	s.sourceInformation.publicGUID = :publicGUID"),
53  
54  		@NamedQuery(name = "Source.UpdateInternalURIById", query = "" +
55  				"UPDATE SOURCE s SET s.internalURIString = :internalURI " +
56  				"WHERE 	s.sourceInformation.publicGUID = :publicGUID AND s.remoteURIString IS NOT NULL")
57  })
58  @Cacheable
59  @Entity(name = "SOURCE")
60  public class JpaSource extends SharedSource {
61  
62  	private static final long serialVersionUID = 891571981945509368L;
63  
64  	int primaryKey;
65  
66  	public JpaSource() {
67  	}
68  
69  	public JpaSource(URI remoteURI, URI internalURI, JpaSourceInformation sourceInformation,
70  					 JpaSourceDomain sourceDomain, Metadata metadata) {
71  		super(remoteURI, internalURI, sourceInformation, sourceDomain,
72  				metadata == null ? null : metadata.clone());
73  	}
74  
75  	@Id
76  	@GeneratedValue
77  	@Column(name = "SOURCE_ID")
78  	public int getPrimaryKey() {
79  		return primaryKey;
80  	}
81  
82  	public void setPrimaryKey(int primaryKey) {
83  		this.primaryKey = primaryKey;
84  	}
85  
86  	@Column(name = "PUBLIC_URI", length = SOURCE_MAX_REMOTE_URI_LENGTH, nullable = true)
87  	public String getRemoteURIString() {
88  		return remoteURI == null ? null : remoteURI.toString();
89  	}
90  
91  	public void setRemoteURIString(String remoteURI) {
92  		this.remoteURI = remoteURI == null ? null : URI.create(remoteURI);
93  	}
94  
95  	@Column(name = "INTERNAL_URI", length = SOURCE_MAX_INTERNAL_URI_LENGTH, nullable = true)
96  	public String getInternalURIString() {
97  		return internalURI == null ? null : internalURI.toString();
98  	}
99  
100 	public void setInternalURIString(String internalURI) {
101 		this.internalURI = internalURI == null ? null : URI.create(internalURI);
102 	}
103 
104 	@Embedded
105 	public JpaSourceInformation getSourceInformation() {
106 		return (JpaSourceInformation) sourceInformation;
107 	}
108 
109 	public void setSourceInformation(JpaSourceInformation sourceInformation) {
110 		this.sourceInformation = sourceInformation;
111 	}
112 
113 	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
114 	@JoinColumn(name = "SOURCE_DOMAIN_ID", nullable = false, updatable = false)
115 	public JpaSourceDomain getSourceDomain() {
116 		return (JpaSourceDomain) sourceDomain;
117 	}
118 
119 	public void setSourceDomain(JpaSourceDomain sourceDomain) {
120 		this.sourceDomain = sourceDomain;
121 	}
122 
123 	@Override
124 	@Type(type = "metadata")
125 	@Basic(fetch = FetchType.EAGER)
126 	@Column(name = "META_DATA", length = Limits.MAX_SERIALIZED_METADATA_LENGTH)
127 	public Metadata getMetadata() {
128 		return super.getMetadata();
129 	}
130 
131 	@Override
132 	public void setMetadata(Metadata metadata) { //NOSONAR - Override is required as hook for JPA.
133 		super.setMetadata(metadata == null ? null : metadata.clone());
134 	}
135 
136 	/**
137 	 * {@inheritDoc}
138 	 */
139 	@Override
140 	public String toString() {
141 		return "JpaSource{" +
142 				"primaryKey=" + primaryKey +
143 				", remoteURL=" + remoteURI +
144 				", internalURL=" + internalURI +
145 				", sourceDomain=" + sourceDomain +
146 				", metadata=" + metadata +
147 				'}';
148 	}
149 }