1
18 package org.torweg.pulse.component.statistics.model;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import javax.persistence.Basic;
24 import javax.persistence.CascadeType;
25 import javax.persistence.MappedSuperclass;
26 import javax.persistence.OneToMany;
27 import javax.persistence.Table;
28 import javax.persistence.UniqueConstraint;
29 import javax.xml.bind.annotation.XmlAccessOrder;
30 import javax.xml.bind.annotation.XmlAccessType;
31 import javax.xml.bind.annotation.XmlAccessorOrder;
32 import javax.xml.bind.annotation.XmlAccessorType;
33 import javax.xml.bind.annotation.XmlAttribute;
34 import javax.xml.bind.annotation.XmlElement;
35 import javax.xml.bind.annotation.XmlElementWrapper;
36 import javax.xml.bind.annotation.XmlRootElement;
37 import javax.xml.bind.annotation.XmlTransient;
38
39 import org.hibernate.LazyInitializationException;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.torweg.pulse.util.entity.AbstractNamedEntity;
43
44
50 @XmlRootElement(name = "abstract-regex-versioned")
51 @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
52 @XmlAccessorType(XmlAccessType.FIELD)
53 @MappedSuperclass
54 @Table(uniqueConstraints = @UniqueConstraint(columnNames = { "category" }))
55 public abstract class AbstractRegexVersioned extends AbstractNamedEntity {
56
57
60 private static final long serialVersionUID = -3693288011013232056L;
61
62
65 private static final Logger LOGGER = LoggerFactory
66 .getLogger(AbstractRegexVersioned.class);
67
68
72 public static final String UNKNOWN = "unknown";
73
74
77 @OneToMany(cascade = CascadeType.ALL)
78 @XmlTransient
79 private final List<RegexVersion> versions = new ArrayList<RegexVersion>();
81
82
85 @XmlAttribute(name = "category")
86 @Basic(optional = false)
87 private String category;
88
89
92 @XmlAttribute(name = "regex")
93 @Basic(optional = false)
94 private String regex = "";
95
96
101 @XmlTransient
102 public final List<RegexVersion> getVersions() {
103 return this.versions;
104 }
105
106
111 @XmlElementWrapper(name = "versions")
112 @XmlElement(name = "version")
113 @SuppressWarnings("unused")
114 @Deprecated
115 private List<RegexVersion> getVersionsJAXB() {
116 try {
117 return getVersions();
118 } catch (LazyInitializationException e) {
119 LOGGER.debug("ignored: {}", e.getLocalizedMessage());
120 return null;
121 }
122 }
123
124
134 public final boolean addVersion(final RegexVersion version) {
135 return this.versions.add(version);
136 }
137
138
143 public final String getCategory() {
144 return this.category;
145 }
146
147
156 public final void setCategory(final String cat) {
157 if (cat == null || cat.trim().equals("")) {
158 throw new IllegalArgumentException(
159 "The category must not be null or empty string.");
160 }
161 this.category = cat;
162 }
163
164
173 @Override
174 public final void setName(final String n) {
175 if (n == null || n.trim().equals("")) {
176 throw new IllegalArgumentException(
177 "The regular name must not be null or empty string.");
178 }
179 super.setName(n);
180 }
181
182
187 public final String getRegex() {
188 return this.regex;
189 }
190
191
201 public final void setRegex(final String reg) {
202 if (reg == null || reg.trim().equals("")) {
203 throw new IllegalArgumentException(
204 "The regular expression must not be null or empty string.");
205 }
206 this.regex = reg;
207 }
208
209
219 public final RegexVersion getVersion(final String s) {
220 for (RegexVersion version : getVersions()) {
221 if (version.isMatch(s)) {
222 return version;
223 }
224 }
225 return null;
226 }
227
228
238 public final boolean isMatch(final String osString) {
239 if (this.regex.equals("")) {
240 return false;
242 }
243 return osString.matches(this.regex);
244 }
245
246
253 public final boolean isUnknown() {
254 return (getCategory().equals(AbstractRegexVersioned.UNKNOWN) && getRegex()
255 .equals(""));
256 }
257
258
263 @Override
264 public String toString() {
265 return "{" + super.toString() + "@[" + getId() + "], category: "
266 + getCategory() + ", default-display-name: " + getName()
267 + ", noOfVersions: " + getVersions().size() + "}";
268 }
269
270 }
271