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  import org.apache.logging.log4j.catalog.api.DataType;
22  import org.apache.logging.log4j.catalog.jpa.converter.BooleanToStringConverter;
23  import org.apache.logging.log4j.catalog.jpa.converter.DataTypeConverter;
24  
25  import javax.persistence.CascadeType;
26  import javax.persistence.CollectionTable;
27  import javax.persistence.Column;
28  import javax.persistence.Convert;
29  import javax.persistence.ElementCollection;
30  import javax.persistence.Entity;
31  import javax.persistence.FetchType;
32  import javax.persistence.GeneratedValue;
33  import javax.persistence.GenerationType;
34  import javax.persistence.Id;
35  import javax.persistence.JoinColumn;
36  import javax.persistence.OneToMany;
37  import javax.persistence.Table;
38  import javax.persistence.UniqueConstraint;
39  import java.io.Serializable;
40  import java.util.HashSet;
41  import java.util.Set;
42  
43  @Entity
44  @Table(name = "EVENT_ATTRIBUTE",
45          uniqueConstraints = { @UniqueConstraint(columnNames = { "NAME" })})
46  public class AttributeModel implements Serializable {
47      private static final long serialVersionUID = -756109102178482698L;
48  
49      @Id
50      @GeneratedValue(strategy= GenerationType.IDENTITY)
51      @Column(name = "ID", updatable = false, nullable = false)
52      private Long id;
53      @Column(name = "NAME")
54      private String name;
55      @Column(name = "DISPLAY_NAME")
56      private String displayName;
57      @Column(name = "DESCRIPTION")
58      private String description;
59      @Column(name = "CATALOG_ID")
60      private String catalogId;
61      @Column(name = "DATATYPE")
62      @Convert(converter=DataTypeConverter.class)
63      private DataType dataType;
64      @Column(name = "INDEXED")
65      @Convert(converter=BooleanToStringConverter.class)
66      private boolean indexed;
67      @Column(name = "SORTABLE")
68      @Convert(converter=BooleanToStringConverter.class)
69      private boolean sortable;
70      @Column(name = "REQUIRED")
71      @Convert(converter=BooleanToStringConverter.class)
72      private boolean required;
73      @Column(name = "REQUEST_CONTEXT")
74      @Convert(converter=BooleanToStringConverter.class)
75      private boolean requestContext;
76      @ElementCollection(fetch = FetchType.EAGER)
77      @CollectionTable(name = "attribute_examples", joinColumns = @JoinColumn(name = "attribute_id"))
78      @Column(name = "example")
79      private Set<String> examples = new HashSet<>();
80      @ElementCollection(fetch = FetchType.EAGER)
81      @CollectionTable(name = "attribute_aliases", joinColumns = @JoinColumn(name = "attribute_id"))
82      @Column(name = "alias")
83      private Set<String> aliases = new HashSet<>();
84      @OneToMany(fetch = FetchType.EAGER, mappedBy = "attribute", cascade = CascadeType.ALL)
85      private Set<ConstraintModel> constraints;
86  
87      public AttributeModel() {
88          catalogId = "DEFAULT";
89      }
90  
91  
92      /**
93       * Returns the id of the AttributeDto.
94       * @return
95       */
96      public Long getId() {
97          return id;
98      }
99  
100     /**
101      * Set the id of the AttributeDto.
102      * @param id
103      */
104     public void setId(Long id) {
105         this.id = id;
106     }
107 
108     /**
109      * Returns the name of the AttributeDto.
110      */
111     public String getName() {
112         return name;
113     }
114 
115     /**
116      * Set the name of the AttributeDto.
117      * @param name the name of the attribute.
118      */
119     public void setName(String name) {
120         this.name = name;
121     }
122 
123     /**
124      * Returns the name used when displaying the attribute.
125      * @return the display name of the attribute.
126      */
127     public String getDisplayName() {
128         return displayName;
129     }
130 
131     /**
132      * Set the name to be displayed for this attribute.
133      * @param name the display name for the attribute.
134      */
135     public void setDisplayName(String name) {
136         this.displayName = name;
137     }
138 
139     /**
140      * Returns the description of the attribute.
141      * @return the description of the attribute.
142      */
143     public String getDescription() {
144         return description;
145     }
146 
147     /**
148      * Set the description of the attribute.
149      * @param description the description of the attribute.
150      */
151     public void setDescription(String description) {
152         this.description = description;
153     }
154 
155     /**
156      * Returns the data type of this attribute.
157      * @return the data type of the attribute.
158      */
159     public DataType getDataType() {
160         return dataType;
161     }
162 
163     /**
164      * Set the data type of the attribute.
165      * @param dataType the data type of the attribute.
166      */
167     public void setDataType(DataType dataType) {
168         this.dataType = dataType;
169     }
170 
171     /**
172      * Identifies whether this attribute is an index.
173      * @return true if this attribute is an index, false otherwise.
174      */
175     public boolean isIndexed() {
176         return indexed;
177     }
178 
179     /**
180      * Set whether this attribute is an index.
181      * @param indexed true if this attribute is an index, false otherwise.
182      */
183     public void setIndexed(boolean indexed) {
184         this.indexed = indexed;
185     }
186 
187     /**
188      * Returns whether a sort may be performed on this attribute.
189      * @return true if a sort can be performed on this attribute, false otherwise.
190      */
191     public boolean isSortable() {
192         return sortable;
193     }
194 
195     /**
196      * Set whether a sort may be performed on this attribute.
197      * @param sortable true if a sort may be performed on this attribute, false otherwise.
198      */
199     public void setSortable(boolean sortable) {
200         this.sortable = sortable;
201     }
202 
203     /**
204      * Returns whether this attribute is required.
205      * @return true if this attribute is required, false otherwise.
206      */
207     public boolean isRequired() {
208         return required;
209     }
210 
211     /**
212      * Set whether this attribute is required.
213      * @param required true if this attribute is required, false otherwise.
214      */
215     public void setRequired(boolean required) {
216         this.required = required;
217     }
218 
219     /**
220      * Returns whether this attribute is part of the RequestContext.
221      * @return true if this attribute is part of the RequestContext, false otherwise.
222      */
223     public boolean isRequestContext() {
224         return requestContext;
225     }
226 
227     /**
228      * Set whether this attribute is part of the RequestContext.
229      * @param isRequestContext true if this attribute is part of the RequestContext, false otherwise.
230      */
231     public void setRequestContext(boolean isRequestContext) {
232         this.requestContext = isRequestContext;
233     }
234 
235     /**
236      * Returns the List of example Strings.
237      * @return the List of example Strings.
238      */
239     public Set<String> getExamples() {
240         return examples;
241     }
242 
243     /**
244      * Sets the List of example Strings.
245      * @param examples the List of example Strings.
246      */
247     public void setExamples(Set<String> examples) {
248         this.examples = examples;
249     }
250 
251     /**
252      * Returns the List of alias Strings.
253      * @return the List of alias Strings.
254      */
255     public Set<String> getAliases() {
256         return aliases;
257     }
258 
259     /**
260      * Get the Catalog Id this attribute is associated with.
261      * @return the catalog id or null.
262      */
263     public String getCatalogId() {
264         return catalogId;
265     }
266 
267     /**
268      * Set the catalog id this attribute is associated with.
269      * @param catalogId The catalog id or null.
270      */
271     public void setCatalogId(String catalogId) {
272         this.catalogId = catalogId;
273     }
274 
275     /**
276      * Sets List of alias Strings.
277      * @param aliases The List of alias Strings.
278      */
279     public void setAliases(Set<String> aliases) {
280         this.aliases = aliases;
281     }
282 
283     public Set<ConstraintModel> getConstraints() {
284         return constraints;
285     }
286 
287     public void setConstraints(Set<ConstraintModel> constraints) {
288         if (constraints == null) {
289             if (this.constraints == null) {
290                 this.constraints = new HashSet<>();
291             }
292         } else {
293             for (ConstraintModel constraint : constraints) {
294                 if (constraint.getAttribute() != this) {
295                     constraint.setAttribute(this);
296                 }
297             }
298             this.constraints = constraints;
299         }
300     }
301 
302     /*
303     public Set<EventAttributeModel> getAttributes() {
304         return attributes;
305     }
306 
307     public void setAttributes(Set<EventAttributeModel> attributes) {
308         this.attributes = attributes;
309     } */
310 
311     public int hashCode() {
312         return new HashCodeBuilder().append(name).append(catalogId).toHashCode();
313     }
314 
315     public boolean equals(Object o) {
316         if (this == o) return true;
317         if (o == null) return false;
318         if (!(o instanceof AttributeModel)) return false;
319 
320         AttributeModel other = (AttributeModel)o;
321         return new EqualsBuilder().append(name, other.name).append(catalogId, other.catalogId).isEquals();
322     }
323 }