View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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       * Return the identifier for this event.
71       * @return the identifier for this event.
72       */
73      public Long getId() {
74          return id;
75      }
76  
77      /**
78       * Set the identifier for this event.
79       * @param id the identifier for this event.
80       */
81      public void setId(Long id) {
82          this.id = id;
83      }
84  
85      /**
86       * Returns the name for this event.
87       * @return the name for this event.
88       */
89      public String getName() {
90          return name;
91      }
92  
93      /**
94       * Set the name for this event.
95       * @param name the name for this event.
96       */
97      public void setName(String name) {
98          this.name = name;
99      }
100 
101     /**
102      * Returns the name to display for this event.
103      * @return the display name for this event.
104      */
105     public String getDisplayName() {
106         return displayName;
107     }
108 
109     /**
110      * Set the display name for this event.
111      * @param name the name to display for this event.
112      */
113     public void setDisplayName(String name) {
114         this.displayName = name;
115     }
116 
117     /**
118      * Return the description of the event.
119      * @return the event description.
120      */
121     public String getDescription() {
122         return description;
123     }
124 
125     /**
126      * Set the event description.
127      * @param description The description of the event.
128      */
129     public void setDescription(String description) {
130         this.description = description;
131     }
132 
133     /**
134      * Returns the List of alias Strings.
135      * @return the List of alias Strings.
136      */
137     public Set<String> getAliases() {
138         return aliases;
139     }
140 
141     /**
142      * Sets the List of alias Strings.
143      * @param aliases the List of alias Strings.
144      */
145     public void setAliases(Set<String> aliases) {
146         this.aliases = aliases;
147     }
148 
149     /**
150      * Get the Catalog Id this Event is associated with.
151      * @return the catalog id or null.
152      */
153     public String getCatalogId() {
154         return catalogId;
155     }
156 
157     /**
158      * Set the catalog id this Event is associated with.
159      * @param catalogId The catalog id or null.
160      */
161     public void setCatalogId(String catalogId) {
162         this.catalogId = catalogId;
163     }
164 
165     /**
166      * Returns the List of AttributeDto objects.
167      * @return the List of Attributes.
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      * Sets the List of Atribute objects.
196      * @param attributes The List of Attributes.
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 }