Coverage Report - org.jtheque.films.services.impl.utils.file.exports.datasources.FilmsDatasource
 
Classes in this File Line Coverage Branch Coverage Complexity
FilmsDatasource
0 %
0/46
0 %
0/24
2.833
 
 1  
 package org.jtheque.films.services.impl.utils.file.exports.datasources;
 2  
 
 3  
 import net.sf.jasperreports.engine.JRDataSource;
 4  
 import net.sf.jasperreports.engine.JRException;
 5  
 import net.sf.jasperreports.engine.JRField;
 6  
 import org.jtheque.core.managers.Managers;
 7  
 import org.jtheque.core.managers.properties.IPropertiesManager;
 8  
 import org.jtheque.films.persistence.od.able.Film;
 9  
 import org.jtheque.primary.od.able.Kind;
 10  
 import org.jtheque.primary.od.able.Person;
 11  
 import org.jtheque.utils.StringUtils;
 12  
 
 13  
 import java.util.ArrayList;
 14  
 import java.util.Arrays;
 15  
 import java.util.Collection;
 16  
 import java.util.List;
 17  
 
 18  
 /*
 19  
  * This file is part of JTheque.
 20  
  *
 21  
  * JTheque is free software: you can redistribute it and/or modify
 22  
  * it under the terms of the GNU General Public License as published by
 23  
  * the Free Software Foundation, either version 3 of the License.
 24  
  *
 25  
  * JTheque is distributed in the hope that it will be useful,
 26  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 27  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 28  
  * GNU General Public License for more details.
 29  
  *
 30  
  * You should have received a copy of the GNU General Public License
 31  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 32  
  */
 33  
 
 34  
 /**
 35  
  * A films jasper data source.
 36  
  *
 37  
  * @author Baptiste Wicht
 38  
  */
 39  
 public final class FilmsDatasource implements JRDataSource {
 40  
     private final List<Film> films;
 41  
 
 42  0
     private int index = -1;
 43  
     private static final int SECONDS_IN_A_MINUTE = 60;
 44  
 
 45  0
     private static final Collection<String> REFLECTION_FIELD = Arrays.asList("title", "year", "note", "resume", "image");
 46  0
     private static final Collection<String> REFLECTION_FOREIGN_FIELD = Arrays.asList("type", "realizer", "language");
 47  
 
 48  
     /**
 49  
      * Construct a new FilmsDatasource for a list of films.
 50  
      *
 51  
      * @param films The films to provide in the data source.
 52  
      */
 53  
     public FilmsDatasource(Collection<Film> films) {
 54  0
         super();
 55  
 
 56  0
         this.films = new ArrayList<Film>(films);
 57  0
     }
 58  
 
 59  
     /**
 60  
      * Construct a new FilmsDatasource for a film.
 61  
      *
 62  
      * @param film The film to provide in the data source.
 63  
      */
 64  
     public FilmsDatasource(Film film) {
 65  0
         super();
 66  
 
 67  0
         films = new ArrayList<Film>(1);
 68  0
         films.add(film);
 69  0
     }
 70  
 
 71  
     @Override
 72  
     public boolean next() throws JRException {
 73  0
         index++;
 74  
 
 75  0
         return index < films.size();
 76  
     }
 77  
 
 78  
     @Override
 79  
     public Object getFieldValue(JRField field) throws JRException {
 80  0
         Film film = films.get(index);
 81  
 
 82  0
         String fieldName = field.getName();
 83  
 
 84  0
         Object value = null;
 85  
 
 86  0
         if ("kinds".equals(fieldName)) {
 87  0
             value = getKindsDescription(film);
 88  0
         } else if ("duration".equals(fieldName)) {
 89  0
             int hours = film.getDuration() / SECONDS_IN_A_MINUTE;
 90  0
             int minutes = film.getDuration() % SECONDS_IN_A_MINUTE;
 91  
 
 92  0
             value = hours + "h" + minutes;
 93  0
         } else if ("actors".equals(fieldName)) {
 94  0
             value = getActorsDescription(film);
 95  0
         } else if (REFLECTION_FIELD.contains(fieldName)) {
 96  0
             Object property = Managers.getManager(IPropertiesManager.class).getPropertyQuickly(film, fieldName);
 97  
 
 98  0
             value = property == null ? "" : property.toString();
 99  0
         } else if (REFLECTION_FOREIGN_FIELD.contains(fieldName)) {
 100  0
             Object property = Managers.getManager(IPropertiesManager.class).getPropertyQuickly(film, "the" + StringUtils.setFirstLetterOnlyUpper(fieldName));
 101  
 
 102  0
             value = property == null ? "" : property.toString();
 103  
         }
 104  
 
 105  0
         return value;
 106  
     }
 107  
 
 108  
     /**
 109  
      * Return the actors description of the film.
 110  
      *
 111  
      * @param film The film to get the descriptions from.
 112  
      * @return The String description of the actors of the film.
 113  
      */
 114  
     private static Object getActorsDescription(Film film) {
 115  0
         StringBuilder builder = new StringBuilder(200);
 116  
 
 117  0
         boolean firstActor = true;
 118  0
         for (Person actor : film.getActors()) {
 119  0
             if (firstActor) {
 120  0
                 builder.append(actor.getDisplayableText());
 121  0
                 firstActor = false;
 122  
             } else {
 123  0
                 builder.append(", ").append(actor.getDisplayableText());
 124  
             }
 125  
         }
 126  
 
 127  0
         return builder.toString();
 128  
     }
 129  
 
 130  
     /**
 131  
      * Return the kinds description of the film.
 132  
      *
 133  
      * @param film The film to get the descriptions from.
 134  
      * @return The String description of the kinds of the film.
 135  
      */
 136  
     private static Object getKindsDescription(Film film) {
 137  0
         StringBuilder builder = new StringBuilder(50);
 138  
 
 139  0
         boolean firstKind = true;
 140  0
         for (Kind kind : film.getKinds()) {
 141  0
             if (firstKind) {
 142  0
                 builder.append(kind.getDisplayableText());
 143  0
                 firstKind = false;
 144  
             } else {
 145  0
                 builder.append(", ").append(kind.getDisplayableText());
 146  
             }
 147  
         }
 148  
 
 149  0
         return builder.toString();
 150  
     }
 151  
 }