1    /*
2     * Copyright 2009 :torweg free software group
3     *
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.component.statistics.model.aggregation;
19   
20   import javax.persistence.Basic;
21   import javax.persistence.ManyToOne;
22   import javax.persistence.MappedSuperclass;
23   import javax.xml.bind.annotation.XmlAccessOrder;
24   import javax.xml.bind.annotation.XmlAccessType;
25   import javax.xml.bind.annotation.XmlAccessorOrder;
26   import javax.xml.bind.annotation.XmlAccessorType;
27   import javax.xml.bind.annotation.XmlElement;
28   import javax.xml.bind.annotation.XmlRootElement;
29   import javax.xml.bind.annotation.XmlTransient;
30   
31   import org.hibernate.LazyInitializationException;
32   import org.slf4j.Logger;
33   import org.slf4j.LoggerFactory;
34   import org.torweg.pulse.component.statistics.model.RegexVersion;
35   import org.torweg.pulse.util.entity.AbstractBasicEntity;
36   
37   /**
38    * An abstract base class to derive a statistical data aggregation entity
39    * aggregating {@code RegexVersion}s from. This would basically be the actual
40    * counter, counting the {@code RegexVersion}s of the {@code
41    * AbstractAggregation} the {@code AbstractRegexVersionedCounter} belongs to.
42    * 
43    * @param <T>
44    *            the type of the {@code AbstractAggregation} the {@code
45    *            AbstractRegexVersionedCounter} belongs to
46    * 
47    * @author Daniel Dietz
48    * @version $Revision: 1579 $
49    * 
50    */
51   @XmlRootElement(name = "abstract-regex-versioned-counter")
52   @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
53   @XmlAccessorType(XmlAccessType.FIELD)
54   @MappedSuperclass
55   public abstract class AbstractRegexVersionedCounter<T extends AbstractAggregation>
56           extends AbstractBasicEntity implements ICount {
57   
58       /**
59        * The serialVersionUID.
60        */
61       private static final long serialVersionUID = 172778008120627703L;
62   
63       /**
64        * The logger.
65        */
66       private static final Logger LOGGER = LoggerFactory
67               .getLogger(AbstractRegexVersionedCounter.class);
68   
69       /**
70        * The "parent-"{@code AbstractAggregation} the {@code
71        * AbstractRegexVersionedCounter} belongs to.
72        */
73       @ManyToOne(optional = false)
74       @XmlTransient
75       // getAggreationIdJAXB JAXB-annotated
76       private T abstractAggregation;
77   
78       /**
79        * The underlying {@code Version}.
80        */
81       @ManyToOne(optional = false)
82       @XmlTransient
83       // getter JAXB-annotated
84       private RegexVersion version;
85   
86       /**
87        * The actual counter.
88        */
89       @Basic
90       @XmlElement(name = "counter")
91       private int counter;
92   
93       /**
94        * Returns the "parent-"{@code AbstractAggregation} the {@code
95        * AbstractRegexVersionedCounter} belongs to.
96        * 
97        * @return the <tt>abstractAggregation</tt>
98        */
99       @XmlTransient
100      public final T getAggreation() {
101          return this.abstractAggregation;
102      }
103  
104      /**
105       * For JAXB only.
106       * 
107       * @return this.getAggreation().getId()
108       */
109      @XmlElement(name = "aggregation-id")
110      @SuppressWarnings("unused")
111      @Deprecated
112      private Long getAggreationIdJAXB() {
113          try {
114              return ((AbstractBasicEntity) getAggreation()).getId();
115          } catch (LazyInitializationException e) {
116              LOGGER.debug("igonred: " + e.getLocalizedMessage(), e);
117              return null;
118          }
119      }
120  
121      /**
122       * Sets the given {@code AbstractAggregation} for the {@code
123       * AbstractRegexVersionedCounter}.
124       * 
125       * @param aggregation
126       *            the {@code AbstractAggregation}
127       * 
128       * @throws NullPointerException
129       *             if
130       *             <ul>
131       *             <li>given aggregation is {@code null}</li>
132       *             </ul>
133       */
134      protected final void setAggreation(final T aggregation) {
135          if (aggregation == null) {
136              throw new NullPointerException(
137                      "Given AbstractAggregation must not be null");
138          }
139          this.abstractAggregation = aggregation;
140      }
141  
142      /**
143       * Returns the value of the underlying counter.
144       * 
145       * @return the count.
146       */
147      public final int getCount() {
148          return this.counter;
149      }
150  
151      /**
152       * Sets the internal counter to the given value.
153       * 
154       * @param count
155       *            the value to set
156       * 
157       * @throws IllegalArgumentException
158       *             if
159       *             <ul>
160       *             <li>count &lt; 0</li>
161       *             </ul>
162       */
163      protected final void setCounter(final int count) {
164          if (count < 0) {
165              throw new IllegalArgumentException("Given count must not be < 0.");
166          }
167          this.counter = count;
168      }
169  
170      /**
171       * Returns the underlying {@code Version}.
172       * 
173       * @return the <tt>version</tt>
174       */
175      @XmlTransient
176      public final RegexVersion getVersion() {
177          return this.version;
178      }
179  
180      /**
181       * For JAXB only.
182       * 
183       * @return this.getVersion()
184       */
185      @XmlElement(name = "version")
186      @SuppressWarnings("unused")
187      @Deprecated
188      private RegexVersion getVersionJAXB() { // NOPMD
189          try {
190              return getVersion();
191          } catch (LazyInitializationException e) {
192              LOGGER.debug("ignored: {}", e.getLocalizedMessage());
193              return null;
194          }
195      }
196  
197      /**
198       * Sets the given {@code Version}.
199       * 
200       * @param v
201       *            the {@code Version}
202       * 
203       * @throws NullPointerException
204       *             if
205       *             <ul>
206       *             <li>given version is {@code null}</li>
207       *             </ul>
208       */
209      protected final void setVersion(final RegexVersion v) {
210          if (v == null) {
211              throw new NullPointerException("Given Version must not be null");
212          }
213          this.version = v;
214      }
215  
216      /**
217       * Adds 1 to the internal counter.
218       */
219      public final void increase() {
220          this.counter++;
221      }
222  
223      /**
224       * Checks the given string against the underlying version.
225       * 
226       * @param s
227       *            the string to check
228       * 
229       * @return {@code true} if the given match matches against the underlying
230       *         version, {@code false} otherwise
231       */
232      public final boolean isMatch(final String s) {
233          return s.matches(getVersion().getRegex());
234      }
235  
236      /**
237       * Returns a string representation of the {@code
238       * AbstractRegexVersionedCounter}.
239       * 
240       * @return a string representation
241       */
242      @Override
243      public String toString() {
244          return "{" + super.toString() + "@[" + getId() + "], count: "
245                  + getCount() + ", aggr: "
246                  + getAggreation().getClass().getCanonicalName() + ", version: "
247                  + getVersion() + "}";
248      }
249  
250  }
251