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.Visit;
23   import org.torweg.pulse.component.statistics.model.aggregation.AverageTimePerVisitAggregation;
24   import org.torweg.pulse.service.PulseException;
25   import org.torweg.pulse.util.time.Duration;
26   
27   /**
28    * Aggregates information about the "average time per {@code Visit}".
29    * 
30    * @author Daniel Dietz
31    * @version $Revision: 1541 $
32    * 
33    */
34   public final class AverageTimePerVisitAggregator extends
35           AbstractAggregator<AverageTimePerVisitAggregation> {
36   
37       /**
38        * Does the actual aggregation.
39        * 
40        * @param visit
41        *            the {@code Visit}
42        * @param s
43        *            the current Hibernate<sup>TM</sup>-{@code Session}
44        * 
45        * @see org.torweg.pulse.component.statistics.aggregator.AbstractBaseAggregator#aggregate
46        *      (org.torweg.pulse.component.statistics.model.Visit,
47        *      org.hibernate.Session,
48        *      org.torweg.pulse.component.statistics.DataAggregationJobletConfiguration)
49        */
50       @Override
51       public void aggregate(final Visit visit, final Session s) {
52   
53           // retrieve the matching aggregation
54           AverageTimePerVisitAggregation aggregation = getAggregation(visit
55                   .getLastRecord().getStatisticsServer(),
56                   visit.getFirstFullDay(), s);
57   
58           // aggregate
59           boolean success = aggregation.aggregate(visit, s);
60           if (!success) {
61               throw new PulseException(success + ": " + aggregation
62                       + ".aggregate(" + visit + ")");
63           }
64   
65           // persist
66           s.saveOrUpdate(aggregation);
67       }
68   
69       /**
70        * Returns a (loaded/new if none could be loaded)
71        * {@code AverageTimePerVisitAggregation} for the given
72        * {@code StatisticsServer} and the given {@code Duration}.
73        * 
74        * @param server
75        *            the {@code StatisticsServer}
76        * @param fullDay
77        *            the {@code StatisticsServer}
78        * @param s
79        *            the current Hibernate<sup>TM</sup>-{@code Session}
80        * 
81        * @return a {@code AverageTimePerVisitAggregation}
82        */
83       private AverageTimePerVisitAggregation getAggregation(
84               final StatisticsServer server, final Duration fullDay,
85               final Session s) {
86   
87           AverageTimePerVisitAggregation aggregation = getAggregation(
88                   AverageTimePerVisitAggregation.class, server, fullDay, s);
89   
90           if (aggregation == null) {
91               aggregation = new AverageTimePerVisitAggregation(server, fullDay);
92           }
93   
94           return aggregation;
95       }
96   
97   }
98