1   package com.trendmicro.grid.acl.ds.jpa.entities;
2   
3   import com.trendmicro.grid.acl.ds.jpa.types.TwoWayByteArrayFieldBridge;
4   import com.trendmicro.grid.acl.l0.datatypes.FileIdentifier;
5   import com.trendmicro.grid.acl.metadata.DataType;
6   import com.trendmicro.grid.acl.metadata.Meta;
7   import com.trendmicro.grid.acl.metadata.Metadata;
8   import org.hibernate.annotations.Index;
9   import org.hibernate.annotations.NaturalId;
10  import org.hibernate.search.annotations.*;
11  
12  import javax.persistence.Cacheable;
13  import javax.persistence.Column;
14  import javax.persistence.Embeddable;
15  
16  /**
17   * Defines the identifier of a file content entry.
18   *
19   * @author juergen_kellerer, 2010-06-07
20   * @version 1.0
21   */
22  @Cacheable
23  @Embeddable
24  public class JpaFileIdentifier extends FileIdentifier {
25  
26  	private static final long serialVersionUID = -5321495980867199909L;
27  
28  	private int crc32;
29  
30  	public JpaFileIdentifier() {
31  	}
32  
33  	public JpaFileIdentifier(FileIdentifier source, Metadata metadata) {
34  		super(source.getSHA1Hash(), source.getMD5Hash());
35  
36  		if (metadata != null) {
37  			Meta crc32 = metadata.get("crc32");
38  			if (crc32 != null && crc32.getDataType() == DataType.NUMERIC)
39  				this.crc32 = (int) crc32.getNumericValues()[0];
40  		}
41  	}
42  
43  	public JpaFileIdentifier(byte[] sha1) {
44  		super(sha1);
45  	}
46  
47  	public JpaFileIdentifier(byte[] sha1, byte[] md5) {
48  		super(sha1, md5);
49  	}
50  
51  	@NaturalId
52  	@Column(name = "SHA1", length = 20, nullable = false, unique = true, updatable = false)
53  	// DB index configuration
54  	@Index(name = "IX_FILE_CONTENTS__FILE_CONTENT_SIGNATURE_INDEX", columnNames = {"SHA1", "MD5", "CRC32"})
55  	// Fulltext index configuration
56  	@Field(bridge = @FieldBridge(impl = TwoWayByteArrayFieldBridge.class),
57  			store = Store.YES, index = org.hibernate.search.annotations.Index.NO,
58  			analyze = Analyze.NO, norms = Norms.NO, termVector = TermVector.NO)
59  	public byte[] getSha1() {
60  		return sha1;
61  	}
62  
63  	public void setSha1(byte[] sha1) {
64  		this.sha1 = sha1;
65  	}
66  
67  	@NaturalId
68  	@Column(name = "MD5", length = 16, nullable = false, updatable = false)
69  	// DB index configuration
70  	@Index(name = "IX_FILE_CONTENTS__FILE_CONTENT_MD5_ONLY_INDEX")
71  	// Fulltext index configuration
72  	@Field(bridge = @FieldBridge(impl = TwoWayByteArrayFieldBridge.class),
73  			store = Store.YES, index = org.hibernate.search.annotations.Index.NO,
74  			analyze = Analyze.NO, norms = Norms.NO, termVector = TermVector.NO)
75  	public byte[] getMd5() {
76  		return md5;
77  	}
78  
79  	public void setMd5(byte[] md5) {
80  		this.md5 = md5;
81  	}
82  
83  	@NaturalId
84  	@Column(name = "CRC32", nullable = false, updatable = false)
85  	public int getCrc32() {
86  		return crc32;
87  	}
88  
89  	public void setCrc32(int crc32) {
90  		this.crc32 = crc32;
91  	}
92  }