Coverage Report - org.jtheque.films.stats.services.impl.utils.StatsCalculator
 
Classes in this File Line Coverage Branch Coverage Complexity
StatsCalculator
0 %
0/75
0 %
0/20
1.526
 
 1  
 package org.jtheque.films.stats.services.impl.utils;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jfree.data.general.DefaultPieDataset;
 20  
 import org.jtheque.films.persistence.od.able.Film;
 21  
 import org.jtheque.films.services.able.IActorService;
 22  
 import org.jtheque.films.services.able.IFilmsService;
 23  
 import org.jtheque.films.services.able.IRealizersService;
 24  
 import org.jtheque.primary.od.able.Kind;
 25  
 import org.jtheque.primary.od.able.Person;
 26  
 import org.jtheque.utils.count.Counter;
 27  
 import org.jtheque.utils.count.Counters;
 28  
 
 29  
 import javax.annotation.Resource;
 30  
 import java.util.Collection;
 31  
 import java.util.HashMap;
 32  
 import java.util.Map;
 33  
 
 34  
 /**
 35  
  * This manage enable us to generate statistics. This class is a singleton.
 36  
  *
 37  
  * @author Baptiste Wicht
 38  
  */
 39  
 public final class StatsCalculator implements StatsInformations {
 40  0
     private static final StatsInformations INSTANCE = new StatsCalculator();
 41  
 
 42  
     @Resource
 43  
     private IFilmsService filmsService;
 44  
 
 45  
     @Resource
 46  
     private IActorService actorService;
 47  
 
 48  
     @Resource
 49  
     private IRealizersService realizersService;
 50  
 
 51  0
     private final Map<String, DefaultPieDataset> sets = new HashMap<String, DefaultPieDataset>(7);
 52  0
     private final Map<String, Counters> data = new HashMap<String, Counters>(8);
 53  
 
 54  
     static final String GENERALS = "generals";
 55  
 
 56  
     private static final String FILMS_COUNTER = "films";
 57  
     private static final String ACTORS_COUNTER = "actors";
 58  
     private static final String REALIZERS_COUNTER = "realizers";
 59  
 
 60  
     /**
 61  
      * Private constructor, create a new <code>Statistics</code>.
 62  
      */
 63  
     private StatsCalculator() {
 64  0
         super();
 65  
 
 66  0
         data.put(GENERALS, new Counters());
 67  0
         data.get(GENERALS).addCounter(FILMS_COUNTER);
 68  0
         data.get(GENERALS).addCounter(ACTORS_COUNTER);
 69  0
         data.get(GENERALS).addCounter(REALIZERS_COUNTER);
 70  0
         data.get(GENERALS).addCounter("duration");
 71  
 
 72  0
         sets.put(FILMS_COUNTER + "-kinds", new DefaultPieDataset());
 73  0
         sets.put(FILMS_COUNTER + "-types", new DefaultPieDataset());
 74  0
         sets.put(FILMS_COUNTER + "-notes", new DefaultPieDataset());
 75  0
         sets.put(REALIZERS_COUNTER + "-notes", new DefaultPieDataset());
 76  0
         sets.put(REALIZERS_COUNTER + "-countries", new DefaultPieDataset());
 77  0
         sets.put(ACTORS_COUNTER + "-notes", new DefaultPieDataset());
 78  0
         sets.put(ACTORS_COUNTER + "-countries", new DefaultPieDataset());
 79  0
     }
 80  
 
 81  
     /**
 82  
      * Return the unique instance of <code>Statistics</code>.
 83  
      *
 84  
      * @return The instance of the class
 85  
      */
 86  
     public static StatsInformations getInstance() {
 87  0
         return INSTANCE;
 88  
     }
 89  
 
 90  
     @Override
 91  
     public void generateStats() {
 92  0
         CounterUtils.clearButGenerals(data);
 93  
 
 94  0
         extractStatisticsOfFilms(data);
 95  0
         extractStatisticsOfActors(data);
 96  0
         extractStatisticsOfRealizers(data);
 97  
 
 98  0
         populateDataset(sets.get(FILMS_COUNTER + "-kinds"), "kinds", data);
 99  0
         populateDataset(sets.get(FILMS_COUNTER + "-types"), "types", data);
 100  0
         populateDataset(sets.get(FILMS_COUNTER + "-notes"), "f-notes", data);
 101  0
         populateDataset(sets.get(ACTORS_COUNTER + "-countries"), "a-countries", data);
 102  0
         populateDataset(sets.get(ACTORS_COUNTER + "-notes"), "a-notes", data);
 103  0
         populateDataset(sets.get(REALIZERS_COUNTER + "-countries"), "r-countries", data);
 104  0
         populateDataset(sets.get(REALIZERS_COUNTER + "-notes"), "r-notes", data);
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Extract the statistics of the films.
 109  
      *
 110  
      * @param data The data of stats.
 111  
      */
 112  
     private void extractStatisticsOfFilms(Map<String, Counters> data) {
 113  0
         data.put("kinds", new Counters());
 114  0
         data.put("types", new Counters());
 115  0
         data.put("f-notes", new Counters());
 116  0
         data.get(GENERALS).getCounter(FILMS_COUNTER).clear();
 117  0
         data.get(GENERALS).getCounter("duration").clear();
 118  
 
 119  0
         for (Film film : filmsService.getFilms()) {
 120  0
             data.get(GENERALS).getCounter("duration").add(film.getDuration());
 121  0
             data.get(GENERALS).getCounter(FILMS_COUNTER).increment();
 122  
 
 123  0
             if (film.hasKinds()) {
 124  0
                 for (Kind kind : film.getKinds()) {
 125  0
                     data.get("kinds").getCounterOrAdd(kind.getDisplayableText()).increment();
 126  
                 }
 127  
             }
 128  
 
 129  0
             if (film.hasType()) {
 130  0
                 data.get("types").getCounterOrAdd(film.getTheType().getDisplayableText()).increment();
 131  
             }
 132  
 
 133  0
             if (film.getNote() != null) {
 134  0
                 data.get("f-notes").getCounterOrAdd(film.getNote().getInternationalizedText()).increment();
 135  
             }
 136  
         }
 137  0
     }
 138  
 
 139  
     /**
 140  
      * Extract the statistics of the realizers.
 141  
      *
 142  
      * @param data The data of stats.
 143  
      */
 144  
     private void extractStatisticsOfRealizers(Map<String, Counters> data) {
 145  0
         extractStatisticsPersons(data, REALIZERS_COUNTER, 'r');
 146  0
     }
 147  
 
 148  
     /**
 149  
      * Extract the statistics of the actors.
 150  
      *
 151  
      * @param data The data of stats.
 152  
      */
 153  
     private void extractStatisticsOfActors(Map<String, Counters> data) {
 154  0
         extractStatisticsPersons(data, ACTORS_COUNTER, 'a');
 155  0
     }
 156  
 
 157  
     /**
 158  
      * Extract the statistics of the persons.
 159  
      *
 160  
      * @param data The data of stats.
 161  
      * @param name The name of counter.
 162  
      * @param c    The prefix for intern counters.
 163  
      */
 164  
     private void extractStatisticsPersons(Map<String, Counters> data, String name, char c) {
 165  0
         data.put(c + "-countries", new Counters());
 166  0
         data.put(c + "-notes", new Counters());
 167  0
         data.get(GENERALS).getCounter(name).clear();
 168  
 
 169  0
         Collection<? extends Person> persons = ACTORS_COUNTER.equals(name) ? actorService.getActors() : realizersService.getRealizers();
 170  
 
 171  0
         for (Person r : persons) {
 172  0
             data.get(GENERALS).getCounter(name).increment();
 173  
 
 174  0
             if (r.getTheCountry() != null) {
 175  0
                 data.get(c + "-countries").getCounterOrAdd(r.getTheCountry().getDisplayableText()).increment();
 176  
             }
 177  
 
 178  0
             if (r.getNote() != null) {
 179  0
                 data.get(c + "-notes").getCounterOrAdd(r.getNote().getInternationalizedText()).increment();
 180  
             }
 181  
         }
 182  0
     }
 183  
 
 184  
     /**
 185  
      * Populate a dataset with the contents of the 2 lists.
 186  
      *
 187  
      * @param dataset The dataset to populate.
 188  
      * @param name    The name of the map.
 189  
      * @param datas   The datas.
 190  
      */
 191  
     private static void populateDataset(DefaultPieDataset dataset, String name, Map<String, Counters> datas) {
 192  0
         for (Map.Entry<String, Counter> counter : datas.get(name)) {
 193  0
             dataset.setValue(counter.getKey(), counter.getValue().getValue());
 194  
         }
 195  0
     }
 196  
 
 197  
     @Override
 198  
     public int getNumberOfFilms() {
 199  0
         return data.get(GENERALS).getCounter(FILMS_COUNTER).getValue();
 200  
     }
 201  
 
 202  
     @Override
 203  
     public int getNumberOfActors() {
 204  0
         return data.get(GENERALS).getCounter(ACTORS_COUNTER).getValue();
 205  
     }
 206  
 
 207  
     @Override
 208  
     public int getNumberOfRealizers() {
 209  0
         return data.get(GENERALS).getCounter(REALIZERS_COUNTER).getValue();
 210  
     }
 211  
 
 212  
     @Override
 213  
     public int getTotalDurationOfFilms() {
 214  0
         return data.get(GENERALS).getCounter("duration").getValue();
 215  
     }
 216  
 
 217  
     @Override
 218  
     public DefaultPieDataset getStatsOfFilmByKind() {
 219  0
         return sets.get("films-kinds");
 220  
     }
 221  
 
 222  
     @Override
 223  
     public DefaultPieDataset getStatsOfFilmByType() {
 224  0
         return sets.get("films-types");
 225  
     }
 226  
 
 227  
     @Override
 228  
     public DefaultPieDataset getStatsOfFilmByNote() {
 229  0
         return sets.get("films-notes");
 230  
     }
 231  
 
 232  
     @Override
 233  
     public DefaultPieDataset getStatsOfActorsByCountry() {
 234  0
         return sets.get("actors-countries");
 235  
     }
 236  
 
 237  
     @Override
 238  
     public DefaultPieDataset getStatsOfActorsByNote() {
 239  0
         return sets.get("actors-notes");
 240  
     }
 241  
 
 242  
     @Override
 243  
     public DefaultPieDataset getStatsOfRealizersByCountry() {
 244  0
         return sets.get(REALIZERS_COUNTER + "-countries");
 245  
     }
 246  
 
 247  
     @Override
 248  
     public DefaultPieDataset getStatsOfRealizersByNote() {
 249  0
         return sets.get(REALIZERS_COUNTER + "-notes");
 250  
     }
 251  
 }