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.SharedFileInformation;
5   import com.trendmicro.grid.acl.ds.jpa.types.TagListFieldBridge;
6   import com.trendmicro.grid.acl.ds.jpa.types.TwoWayByteArrayFieldBridge;
7   import com.trendmicro.grid.acl.l0.datatypes.FileInformation;
8   import org.hibernate.annotations.Index;
9   import org.hibernate.annotations.Type;
10  import org.hibernate.search.annotations.*;
11  import org.hibernate.search.bridge.builtin.impl.BuiltinArrayBridge;
12  
13  import javax.persistence.*;
14  import java.util.Arrays;
15  import java.util.Date;
16  import java.util.List;
17  
18  /**
19   * Binds FileInformation to the table "FILE_CONTENTS".
20   *
21   * @author juergen_kellerer, 2010-06-07
22   * @version 1.0
23   */
24  @Cacheable
25  @Embeddable
26  public class JpaFileInformation extends SharedFileInformation {
27  
28  	private static final long serialVersionUID = -2793881122155314188L;
29  
30  	public JpaFileInformation() {
31  	}
32  
33  	public JpaFileInformation(FileInformation source) {
34  		super(source.getFirstSeen() == null ? null : new Date(source.getFirstSeen().getTime()),
35  				source.getLastRetrieved() == null ? null : new Date(source.getLastRetrieved().getTime()),
36  				source.getLastProcessed() == null ? null : new Date(source.getLastProcessed().getTime()),
37  				null, 0, 0);
38  		List<String> tagList = source.getTags();
39  		tags = tagList.toArray(new String[tagList.size()]);
40  	}
41  
42  	public JpaFileInformation(boolean unknown) {
43  		this.unknown = unknown;
44  	}
45  
46  	@Override
47  	@Column(name = "UNKNOWN", nullable = false)
48  	public boolean isUnknown() { //NOSONAR: Override is needed for JPA
49  		return super.isUnknown();
50  	}
51  
52  	@Override
53  	public void setUnknown(boolean unknown) { //NOSONAR: Override is needed for JPA
54  		super.setUnknown(unknown);
55  	}
56  
57  	@Override
58  	@Temporal(TemporalType.TIMESTAMP)
59  	@Column(name = "CREATED", nullable = false, updatable = false)
60  	// DB index configuration
61  	@Index(name = "IX_FILE_CONTENTS__CREATED_INDEX")
62  	// Fulltext index configuration
63  	@Field(analyze = Analyze.NO, norms = Norms.NO, termVector = TermVector.NO)
64  	@DateBridge(resolution = Resolution.DAY)
65  	public Date getFirstSeen() {
66  		if (firstSeen == null) firstSeen = new Date();
67  		return firstSeen;
68  	}
69  
70  	public void setFirstSeen(Date firstSeen) {
71  		// Note: Converting Timestamp to Date to avoid that "equals()" fails to compare 2 instances.
72  		//       (Copy on set increases security on mutable type.)
73  		this.firstSeen = firstSeen == null ? null : new Date(firstSeen.getTime());
74  	}
75  
76  	@Override
77  	@Temporal(TemporalType.TIMESTAMP)
78  	@Column(name = "LAST_RETRIEVED")
79  	// Fulltext index configuration
80  	@Field(analyze = Analyze.NO, norms = Norms.NO, termVector = TermVector.NO)
81  	@DateBridge(resolution = Resolution.DAY)
82  	public Date getLastRetrieved() {
83  		return lastRetrieved;
84  	}
85  
86  	public void setLastRetrieved(Date lastRetrieved) {
87  		// Note: Converting Timestamp to Date to avoid that "equals()" fails to compare 2 instances.
88  		//       (Copy on set increases security on mutable type.)
89  		this.lastRetrieved = lastRetrieved == null ? null : new Date(lastRetrieved.getTime());
90  	}
91  
92  	@Override
93  	@Temporal(TemporalType.TIMESTAMP)
94  	@Column(name = "LAST_PROCESSED")
95  	// DB index configuration
96  	@Index(name = "IX_FILE_CONTENTS__LAST_PROCESSED_INDEX")
97  	// Fulltext index configuration
98  	@Field(analyze = Analyze.NO, norms = Norms.NO, termVector = TermVector.NO)
99  	@DateBridge(resolution = Resolution.HOUR)
100 	public Date getLastProcessed() {
101 		return lastProcessed;
102 	}
103 
104 	public void setLastProcessed(Date lastProcessed) {
105 		// Note: Converting Timestamp to Date to avoid that "equals()" fails to compare 2 instances.
106 		//       (Copy on set increases security on mutable type.)
107 		this.lastProcessed = lastProcessed == null ? null : new Date(lastProcessed.getTime());
108 	}
109 
110 	@Type(type = "taglist")
111 	@Column(name = "TAGS", length = Limits.MAX_TAG_STRING_LENGTH, nullable = true)
112 	// Fulltext index configuration
113 	@Field(analyze = Analyze.NO, norms = Norms.NO, termVector = TermVector.NO)
114 	@FieldBridge(impl = TagListFieldBridge.class)
115 	public String[] getTagsAsArray() {
116 		return tags;
117 	}
118 
119 	public void setTagsAsArray(String[] tags) {
120 		this.tags = tags;
121 		tagsAreSorted = true; // taglist type guarantees that tags are sorted.
122 	}
123 
124 	@Override
125 	@Column(name = "INCOMING_PACKAGES_COUNT", updatable = false, insertable = false)
126 	public Integer getSourcePackageCount() {
127 		return sourcePackageCount;
128 	}
129 
130 	public void setSourcePackageCount(Integer sourcePackageCount) {
131 		this.sourcePackageCount = sourcePackageCount;
132 	}
133 
134 	@Override
135 	@Column(name = "INCOMING_SOURCES_COUNT", updatable = false, insertable = false)
136 	public Integer getSourceSiteCount() {
137 		return sourceSiteCount;
138 	}
139 
140 	public void setSourceSiteCount(Integer sourceSiteCount) {
141 		this.sourceSiteCount = sourceSiteCount;
142 	}
143 
144 	/**
145 	 * {@inheritDoc}
146 	 */
147 	@Override
148 	public String toString() {
149 		return "JpaFileInformation{" +
150 				"firstSeen=" + firstSeen +
151 				", lastUpdated=" + lastRetrieved +
152 				", unknown=" + unknown +
153 				", tags=" + (tags == null ? null : Arrays.asList(tags)) +
154 				", sourcePackageCount=" + sourcePackageCount +
155 				", sourceSiteCount=" + sourceSiteCount +
156 				'}';
157 	}
158 }