1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.catalog.jpa.model;
18
19 import org.apache.commons.lang3.builder.EqualsBuilder;
20 import org.apache.commons.lang3.builder.HashCodeBuilder;
21
22 import javax.persistence.CascadeType;
23 import javax.persistence.CollectionTable;
24 import javax.persistence.Column;
25 import javax.persistence.ElementCollection;
26 import javax.persistence.Entity;
27 import javax.persistence.FetchType;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.OneToMany;
33 import javax.persistence.Table;
34 import javax.persistence.UniqueConstraint;
35 import java.io.Serializable;
36 import java.util.ArrayList;
37 import java.util.HashSet;
38 import java.util.List;
39 import java.util.Set;
40
41 @Entity
42 @Table(name = "CATALOG_EVENT",
43 uniqueConstraints = { @UniqueConstraint(columnNames = { "NAME" })})
44 public class EventModel implements Serializable {
45 private static final long serialVersionUID = 1512172827909901054L;
46 @Id
47 @GeneratedValue(strategy= GenerationType.IDENTITY)
48 @Column(name = "ID", updatable = false, nullable = false)
49 private Long id;
50 @Column(name = "NAME")
51 private String name;
52 @Column(name = "DISPLAY_NAME")
53 private String displayName;
54 @Column(name = "DESCRIPTION")
55 private String description;
56 @Column(name = "CATALOG_ID")
57 private String catalogId;
58 @ElementCollection(fetch = FetchType.EAGER)
59 @CollectionTable(name = "event_aliases", joinColumns = @JoinColumn(name = "event_id"))
60 @Column(name = "alias")
61 private Set<String> aliases;
62 @OneToMany(fetch = FetchType.EAGER, mappedBy = "event", cascade = CascadeType.ALL, orphanRemoval = true)
63 private Set<EventAttributeModel> attributes = new HashSet<>();
64
65 public EventModel() {
66 catalogId = "DEFAULT";
67 }
68
69
70
71
72
73 public Long getId() {
74 return id;
75 }
76
77
78
79
80
81 public void setId(Long id) {
82 this.id = id;
83 }
84
85
86
87
88
89 public String getName() {
90 return name;
91 }
92
93
94
95
96
97 public void setName(String name) {
98 this.name = name;
99 }
100
101
102
103
104
105 public String getDisplayName() {
106 return displayName;
107 }
108
109
110
111
112
113 public void setDisplayName(String name) {
114 this.displayName = name;
115 }
116
117
118
119
120
121 public String getDescription() {
122 return description;
123 }
124
125
126
127
128
129 public void setDescription(String description) {
130 this.description = description;
131 }
132
133
134
135
136
137 public Set<String> getAliases() {
138 return aliases;
139 }
140
141
142
143
144
145 public void setAliases(Set<String> aliases) {
146 this.aliases = aliases;
147 }
148
149
150
151
152
153 public String getCatalogId() {
154 return catalogId;
155 }
156
157
158
159
160
161 public void setCatalogId(String catalogId) {
162 this.catalogId = catalogId;
163 }
164
165
166
167
168
169 public Set<EventAttributeModel> getAttributes() {
170 return attributes;
171 }
172
173 public List<String> getAttributeNames() {
174 List<String> names = new ArrayList<>(attributes.size());
175 for (EventAttributeModel model : attributes) {
176 names.add(model.getAttribute().getName());
177 }
178 return names;
179 }
180
181 public EventAttributeModel getAttribute(String name) {
182 for (EventAttributeModel model : attributes) {
183 if (name.equals(model.getAttribute().getName())) {
184 return model;
185 }
186 }
187 return null;
188 }
189
190 public void addEventAttribute(EventAttributeModel attribute) {
191 this.attributes.add(attribute);
192 }
193
194
195
196
197
198 public void setAttributes(Set<EventAttributeModel> attributes) {
199 this.attributes = attributes;
200 }
201
202 public int hashCode() {
203 return new HashCodeBuilder().append(name).toHashCode();
204 }
205
206 public boolean equals(Object o) {
207 if (this == o) return true;
208 if (o == null) return false;
209 if (!(o instanceof EventModel)) return false;
210
211 EventModel other = (EventModel)o;
212 return new EqualsBuilder().append(name, other.name).isEquals();
213 }
214
215 public String toString() {
216 StringBuilder sb = new StringBuilder();
217 sb.append("{\"id\" : \"").append(id).append("\"");
218 sb.append(", \"name\" : \"").append(name).append("\", \"displayName\" : \"").append(displayName).append("\"");
219 sb.append(", \"description\" : ").append(description).append("\", \"attributes\" : [");
220 boolean first = true;
221 for (EventAttributeModel attribute : attributes) {
222 if (!first) {
223 sb.append(", ");
224 } else {
225 first = false;
226 }
227 sb.append("{\"name\" : \"").append(attribute.getAttribute().getName()).append("\", \"required\" : ").append(attribute.isRequired()).append("}");
228 }
229 sb.append("]}");
230 return sb.toString();
231 }
232 }