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.aggregator;
19   
20   import org.hibernate.Session;
21   import org.torweg.pulse.component.statistics.model.StatisticsServer;
22   import org.torweg.pulse.component.statistics.model.aggregation.AbstractAggregation;
23   import org.torweg.pulse.util.time.Duration;
24   
25   /**
26    * An abstract base class to derive aggregators from.
27    * 
28    * @param <T>
29    *            the {@code AbstractAggregation} this aggregator operates on
30    * 
31    * @author Daniel Dietz
32    * @version $Revision: 1541 $
33    * 
34    */
35   public abstract class AbstractAggregator<T extends AbstractAggregation> extends
36           AbstractBaseAggregator {
37   
38       /**
39        * Loads the {@code AbstractAggregation} of the given type
40        * <tt>&lt;T&gt;</tt> for the given {@code StatisticsServer} and the
41        * given {@code Duration}.
42        * 
43        * @param clazz
44        *            the {@code Class&lt;T&gt;} to be loaded
45        * @param statisticsServer
46        *            the {@code StatisticsServer}
47        * @param duration
48        *            the {@code Duration}
49        * @param s
50        *            the Hibernate<sup>TM</sup>-{@code Session} to be used for
51        *            loading
52        * 
53        * @return an {@code AbstractAggregation} of the type
54        *         <tt>&lt;T&gt;</tt>
55        */
56       @SuppressWarnings("unchecked")
57       protected final T getAggregation(final Class<T> clazz,
58               final StatisticsServer statisticsServer, final Duration duration,
59               final Session s) {
60           return (T) s.createQuery(
61                   "from " + clazz.getSimpleName() + " aggr where "
62                           + "aggr.statisticsServer=? and "
63                           + "aggr.startTime >= ? and aggr.endTime <= ?")
64           // server
65                   .setParameter(0, statisticsServer)
66                   // start
67                   .setParameter(1, duration.getStartMillis())
68                   // end
69                   .setParameter(2, duration.getEndMillis()).uniqueResult();
70       }
71   
72   }
73