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.CascadeType;
22   import javax.persistence.ManyToOne;
23   import javax.persistence.MappedSuperclass;
24   import javax.xml.bind.annotation.XmlAccessOrder;
25   import javax.xml.bind.annotation.XmlAccessType;
26   import javax.xml.bind.annotation.XmlAccessorOrder;
27   import javax.xml.bind.annotation.XmlAccessorType;
28   import javax.xml.bind.annotation.XmlElement;
29   import javax.xml.bind.annotation.XmlRootElement;
30   import javax.xml.bind.annotation.XmlTransient;
31   
32   import org.hibernate.LazyInitializationException;
33   
34   /**
35    * An abstract base class to derive aggregations based on an a tree-like
36    * structure using nodes derived from {@code AbstractTreeElement}.
37    * 
38    * @param <T>
39    *            the implementation of {@code AbstractTreeElement} as used by the
40    *            {@code AbstractTreeAggregation}
41    * 
42    * @author Daniel Dietz
43    * @version $Revision: 1579 $
44    */
45   @XmlRootElement(name = "abstract-tree-aggregation")
46   @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
47   @XmlAccessorType(XmlAccessType.FIELD)
48   @MappedSuperclass
49   public abstract class AbstractTreeAggregation<T extends AbstractTreeElement<T>>
50           extends AbstractAggregation {
51   
52       /**
53        * The serialVersionUID.
54        */
55       private static final long serialVersionUID = -3694255080001289064L;
56       /**
57        * The {@code AbstractTreeElement} this {@code AbstractTreeAggregation} is
58        * for.
59        */
60       @ManyToOne(optional = false, cascade = { CascadeType.MERGE,
61               CascadeType.PERSIST, CascadeType.REFRESH })
62       @XmlTransient
63       private T path;
64   
65       /**
66        * The number of (direct) hits for the path.
67        */
68       @Basic
69       @XmlElement(name = "hits")
70       private int hits = 0;
71   
72       /**
73        * Returns the {@code AbstractTreeElement}.
74        * 
75        * @return the path
76        */
77       @XmlTransient
78       public final T getPath() {
79           return this.path;
80       }
81   
82       /**
83        * For JAXB only.
84        * 
85        * @return this.getPath()
86        */
87       @XmlElement(name = "path")
88       @SuppressWarnings("unused")
89       @Deprecated
90       private T getPathJAXB() { // NOPMD
91           try {
92               return getPath();
93           } catch (LazyInitializationException e) {
94               LOGGER.debug("ignored: {}", e.getLocalizedMessage());
95               return null;
96           }
97       }
98   
99       /**
100       * Sets the given {@code AbstractTreeElement} as path.
101       * 
102       * @param treeElement
103       *            the {@code AbstractTreeElement}
104       */
105      protected final void setPath(final T treeElement) {
106          this.path = treeElement;
107      }
108  
109      /**
110       * Returns the hits.
111       * 
112       * @return the hits
113       */
114      public final int getHits() {
115          return this.hits;
116      }
117  
118      /**
119       * Increases the hits by 1.
120       */
121      public final void increaseHits() {
122          this.hits++;
123      }
124  
125      /**
126       * Increases the hits by the given value v.
127       * 
128       * @param v
129       *            the value to increase the counter by
130       */
131      public final void increaseHits(final int v) {
132          this.hits += v;
133      }
134  
135      /**
136       * @return string representation of the {@code AbstractTreeAggregation}
137       */
138      @Override
139      public String toString() {
140          return "{" + super.toString() + ", path: " + getPath() + ", h: "
141                  + getHits() + "}";
142      }
143  
144  }
145