1 package com.trendmicro.grid.acl.ds.jpa.entities;
2
3 import com.trendmicro.grid.acl.l0.datatypes.FileReport;
4
5 import javax.persistence.*;
6 import java.io.Serializable;
7 import java.text.DateFormat;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10
11 import static net.sf.tinyjee.util.Assert.assertNotNull;
12
13
14
15
16
17
18
19 @NamedQueries({
20 @NamedQuery(name = "FileReport.UpdateAccessCount", query = "" +
21 "UPDATE FILE_CONTENT_STATISTICS s SET s.accessCount = s.accessCount + :accessCount " +
22 "WHERE s.id = :reportId")
23 })
24 @Cacheable
25 @Entity(name = "FILE_CONTENT_STATISTICS")
26 public class JpaFileReport extends FileReport implements Serializable {
27
28 private static final long serialVersionUID = 3523494342088796333L;
29
30 private static DateFormat timePeriodTemplate = new SimpleDateFormat("yyyyMM");
31
32 @EmbeddedId
33 private ID reportId;
34 @Column(name = "ACCESS_COUNT")
35 private int accessCount;
36
37 public JpaFileReport() {
38 }
39
40 public JpaFileReport(JpaFileDetails fileDetails, FileReport other) {
41 super(other.getSHA1Hash(), other.getMD5Hash(), other.getReportType(),
42 other.getAmount(), other.getSourceLocale(), other.getSourceIdentifier());
43 reportId = new ID(fileDetails);
44 }
45
46
47
48
49 @PostLoad
50 public void translateValues() {
51 JpaFileIdentifier id = reportId.fileDetails.getIdentifier();
52 sha1 = id.getSha1();
53 md5 = id.getMd5();
54
55 switch (getReportType()) {
56 case EXECUTED:
57 break;
58 default:
59 amount = accessCount;
60 }
61 }
62
63 public int getAccessCount() {
64 if (accessCount == 0) {
65 if (getReportType() == Type.ACCESSED)
66 accessCount = getAmount();
67 }
68 return accessCount;
69 }
70
71 public void setAccessCount(int accessCount) {
72 this.accessCount = accessCount;
73 }
74
75 public ID getReportId() {
76 return reportId;
77 }
78
79 public JpaFileDetails getFileDetails() {
80 return reportId.getFileDetails();
81 }
82
83 public String getTimePeriod() {
84 return reportId.getTimePeriod();
85 }
86
87
88
89
90 @Embeddable
91 public static class ID implements Serializable {
92
93 private static final long serialVersionUID = -2085246379334933843L;
94
95 @ManyToOne(optional = false, fetch = FetchType.LAZY)
96 @JoinColumn(name = "FILE_CONTENT_ID", nullable = false, updatable = false)
97 private JpaFileDetails fileDetails;
98
99 @Column(name = "TIME_PERIOD", length = 8)
100 private String timePeriod = ((DateFormat) timePeriodTemplate.clone()).format(new Date());
101
102 public ID() {
103 }
104
105 public ID(JpaFileDetails fileDetails) {
106 assertNotNull("JpaFileDetails", fileDetails);
107 this.fileDetails = fileDetails;
108 }
109
110 public JpaFileDetails getFileDetails() {
111 return fileDetails;
112 }
113
114 public String getTimePeriod() {
115 return timePeriod;
116 }
117
118 @Override
119 public boolean equals(Object o) {
120 if (this == o) return true;
121 if (!(o instanceof ID)) return false;
122 ID id = (ID) o;
123 return timePeriod.equals(id.timePeriod) && fileDetails.equals(id.fileDetails);
124
125 }
126
127 @Override
128 public int hashCode() {
129 int result = fileDetails.hashCode();
130 result = 31 * result + timePeriod.hashCode();
131 return result;
132 }
133
134 @Override
135 public String toString() {
136 return "ID{" +
137 "fileDetails=" + fileDetails +
138 ", timePeriod='" + timePeriod + '\'' +
139 '}';
140 }
141 }
142
143 @Override
144 public String toString() {
145 return "JpaFileReport{" +
146 "reportId=" + reportId +
147 ", accessCount=" + accessCount +
148 '}';
149 }
150 }