| 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 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 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 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public FilmsDatasource(Collection<Film> films) { |
| 54 | 0 | super(); |
| 55 | |
|
| 56 | 0 | this.films = new ArrayList<Film>(films); |
| 57 | 0 | } |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 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 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 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 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 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 | |
} |