1
18 package org.torweg.pulse.component.statistics.model.aggregation;
19
20 import java.util.HashSet;
21 import java.util.Set;
22
23 import javax.persistence.Basic;
24 import javax.persistence.CascadeType;
25 import javax.persistence.ManyToOne;
26 import javax.persistence.MappedSuperclass;
27 import javax.persistence.OneToMany;
28 import javax.xml.bind.annotation.XmlAccessOrder;
29 import javax.xml.bind.annotation.XmlAccessType;
30 import javax.xml.bind.annotation.XmlAccessorOrder;
31 import javax.xml.bind.annotation.XmlAccessorType;
32 import javax.xml.bind.annotation.XmlElement;
33 import javax.xml.bind.annotation.XmlElementWrapper;
34 import javax.xml.bind.annotation.XmlRootElement;
35 import javax.xml.bind.annotation.XmlTransient;
36
37 import org.hibernate.LazyInitializationException;
38 import org.torweg.pulse.util.entity.AbstractBasicEntity;
39
40
55 @XmlRootElement(name = "abstract-regex-versioned-counter-aggregation")
56 @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
57 @XmlAccessorType(XmlAccessType.FIELD)
58 @MappedSuperclass
59 public abstract class AbstractRegexVersionedCounterAggregation<T, U extends AbstractRegexVersionedCounter<?>>
60 extends AbstractAggregation implements ICount {
61
62
65 private static final long serialVersionUID = 3041219383267733481L;
66
67
72 @ManyToOne(optional = false, cascade = { CascadeType.REFRESH,
73 CascadeType.MERGE, CascadeType.PERSIST })
74 @XmlTransient
75 private T counted;
77
78
82 @OneToMany(cascade = CascadeType.ALL)
83 @XmlTransient
84 private final Set<U> counters = new HashSet<U>();
86
87
91 @Basic
92 @XmlElement(name = "counter")
93 private int counter;
94
95
102 @XmlTransient
103 public final T getCounted() {
104 return this.counted;
105 }
106
107
112 @XmlElement(name = "counted-id")
113 @SuppressWarnings("unused")
114 @Deprecated
115 private Long getCountedIdJAXB() {
116 try {
117 return ((AbstractBasicEntity) getCounted()).getId();
118 } catch (LazyInitializationException e) {
119 LOGGER.debug("igonred: " + e.getLocalizedMessage(), e);
120 return null;
121 }
122 }
123
124
134 protected final void setCounted(final T count) {
135 if (count == null) {
136 throw new NullPointerException("Given count must not be null.");
137 }
138 this.counted = count;
139 }
140
141
146 @XmlTransient
147 public final Set<U> getVersionCounters() {
148 return this.counters;
149 }
150
151
156 @XmlElementWrapper(name = "version-counters")
157 @XmlElement(name = "version-counter")
158 @SuppressWarnings("unused")
159 @Deprecated
160 private Set<U> getVersionCountersJAXB() { try {
162 return getVersionCounters();
163 } catch (LazyInitializationException e) {
164 LOGGER.debug("ignored: {}", e.getLocalizedMessage());
165 return null;
166 }
167 }
168
169
174 public final int getCount() {
175 return this.counter;
176 }
177
178
190 protected final void setCount(final int value) {
191 if (value < 0) {
192 throw new IllegalArgumentException("Given value must not be < 0.");
193 }
194 this.counter = value;
195 }
196
197
200 protected final void increase() {
201 this.counter++;
202 }
203
204
209 public final int getUnknownVersionTotal() {
210 int i = getCount();
211 for (U versionCounter : getVersionCounters()) {
212 i -= versionCounter.getCount();
213 }
214 return i;
215 }
216
217
222 @Override
223 public String toString() {
224 return "{" + super.toString() + ", counter: " + getCount()
225 + ", counted: " + getCounted().getClass().getCanonicalName()
226 + "}";
227 }
228
229 }
230