1 package com.trendmicro.grid.acl.ds.jpa.entities;
2
3 import com.trendmicro.grid.acl.l0.datatypes.PackageDetails;
4 import com.trendmicro.grid.acl.l0.datatypes.PackageInformation;
5 import com.trendmicro.grid.acl.metadata.Metadata;
6 import org.hibernate.annotations.Type;
7
8 import javax.persistence.*;
9 import java.io.Serializable;
10 import java.util.Arrays;
11 import java.util.Date;
12 import java.util.List;
13
14 import static com.trendmicro.grid.acl.Limits.MAX_SERIALIZED_METADATA_LENGTH;
15 import static com.trendmicro.grid.acl.Limits.MAX_TAG_STRING_LENGTH;
16 import static com.trendmicro.grid.acl.ds.jpa.util.JpaUtils.metadataDiffers;
17 import static com.trendmicro.grid.acl.ds.jpa.util.JpaUtils.tagsDiffer;
18
19
20
21
22
23
24
25 @NamedQueries({
26 @NamedQuery(name = "PackageHistory.SelectMaxRevisionForPackage",
27 query = "SELECT MAX(h.historyId.revision) " +
28 "FROM PACKAGE_HISTORY h WHERE historyId.parent = :parent"),
29 @NamedQuery(name = "PackageHistory.SelectPackageHistoryDetailsByPackageDetail",
30 query = "SELECT h " +
31 "FROM PACKAGE_HISTORY h WHERE historyId.parent = :parent")
32 })
33 @Entity(name = "PACKAGE_HISTORY")
34 public class JpaPackageHistory implements Serializable {
35
36 private static final long serialVersionUID = -4911356794856270325L;
37
38
39
40
41
42
43
44
45
46 public static boolean wasChanged(PackageDetails existingDetails, PackageDetails details, boolean ignoreMetadata) {
47 PackageInformation existingInfo = existingDetails.getPackageInformation(),
48 info = details.getPackageInformation();
49 return (!ignoreMetadata && metadataDiffers(existingDetails, details)) ||
50 !existingInfo.getDisplayName().equals(info.getDisplayName()) ||
51 tagsDiffer(existingInfo, info);
52 }
53
54 @EmbeddedId
55 private JpaPackageHistoryId historyId;
56
57 @Temporal(TemporalType.TIMESTAMP)
58 @Column(name = "CREATED", nullable = false)
59 private Date created = new Date();
60
61 @Type(type = "nstring")
62 @Column(name = "DISPLAY_NAME", nullable = false)
63 private String displayName;
64
65 @Type(type = "taglist")
66 @Column(name = "TAGS", length = MAX_TAG_STRING_LENGTH, nullable = true)
67 String[] tags;
68
69 @Type(type = "metadata")
70 @Basic(fetch = FetchType.LAZY)
71 @Column(name = "META_DATA", length = MAX_SERIALIZED_METADATA_LENGTH)
72 Metadata metadata;
73
74 public JpaPackageHistory() {
75 }
76
77
78
79
80
81
82 public JpaPackageHistory(JpaPackageDetails packageDetails) {
83 this.historyId = new JpaPackageHistoryId(packageDetails);
84
85 final JpaPackageInformation info = packageDetails.getPackageInformation();
86 displayName = info.getDisplayName();
87 tags = info.getTagsAsArray();
88 metadata = packageDetails.getMetadata();
89
90
91 if (tags != null)
92 tags = tags.clone();
93 if (metadata != null)
94 metadata = metadata.clone();
95 }
96
97 public JpaPackageHistoryId getHistoryId() {
98 return historyId;
99 }
100
101 public Date getCreated() {
102 return created;
103 }
104
105 public String getDisplayName() {
106 return displayName;
107 }
108
109 public String[] getTags() {
110 return tags;
111 }
112
113 public Metadata getMetadata() {
114 return metadata;
115 }
116
117 @Override
118 public boolean equals(Object o) {
119 if (this == o) return true;
120 if (!(o instanceof JpaPackageHistory)) return false;
121
122 JpaPackageHistory that = (JpaPackageHistory) o;
123 return !(historyId != null ? !historyId.equals(that.historyId) : that.historyId != null);
124 }
125
126 @Override
127 public int hashCode() {
128 return historyId != null ? historyId.hashCode() : 0;
129 }
130
131 @Override
132 public String toString() {
133 return "JpaPackageHistory{" +
134 "historyId=" + historyId +
135 ", created=" + created +
136 ", displayName='" + displayName + '\'' +
137 ", tags=" + (tags == null ? null : Arrays.asList(tags)) +
138 ", metadata=" + metadata +
139 '}';
140 }
141 }