1 package fr.ifremer.tutti.service.export.pdf;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.collect.Lists;
26 import com.google.common.collect.Maps;
27 import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
28 import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
29 import fr.ifremer.tutti.persistence.entities.referential.Species;
30 import fr.ifremer.tutti.persistence.entities.referential.Speciess;
31 import fr.ifremer.tutti.persistence.entities.referential.TaxonCache;
32 import fr.ifremer.tutti.persistence.entities.referential.TaxonCaches;
33 import fr.ifremer.tutti.service.AbstractTuttiService;
34 import fr.ifremer.tutti.service.PdfGeneratorService;
35 import fr.ifremer.tutti.service.PersistenceService;
36 import fr.ifremer.tutti.service.TuttiServiceContext;
37 import fr.ifremer.tutti.service.catches.WeightComputingService;
38 import fr.ifremer.tutti.service.export.ExportBatchEntry;
39 import fr.ifremer.tutti.service.export.ExportCatchContext;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 import java.io.File;
44 import java.util.List;
45 import java.util.Locale;
46 import java.util.Map;
47
48 import static org.nuiton.i18n.I18n.t;
49
50
51
52
53
54 public class CatchesPdfExportService extends AbstractTuttiService {
55
56
57 private static final Log log = LogFactory.getLog(CatchesPdfExportService.class);
58
59 protected PersistenceService persistenceService;
60
61 protected WeightComputingService weightComputingService;
62
63 protected PdfGeneratorService pdfGeneratorService;
64
65 public CatchesPdfExportService() {
66 super();
67
68 }
69
70 @Override
71 public void setServiceContext(TuttiServiceContext context) {
72 super.setServiceContext(context);
73 persistenceService = getService(PersistenceService.class);
74 weightComputingService = getService(WeightComputingService.class);
75 pdfGeneratorService = getService(PdfGeneratorService.class);
76 }
77
78
79
80
81
82
83
84
85 public void generateCruisePDFFile(File targetFile,
86 Integer cruiseId,
87 Locale locale) {
88
89
90 List<Integer> allFishingOperation =
91 persistenceService.getAllFishingOperationIds(cruiseId);
92
93 List<Map<String, Object>> operations = Lists.newArrayList();
94 for (Integer operationId : allFishingOperation) {
95
96 prepareOperation(operationId, operations);
97
98 }
99
100 generatePdf(targetFile, locale, operations);
101 }
102
103
104
105
106
107
108
109
110
111 public void generateFishingOperationPDFFile(File targetFile,
112 Integer fishingOperationId,
113 Locale locale) {
114
115 List<Map<String, Object>> operations = Lists.newArrayList();
116
117 prepareOperation(fishingOperationId, operations);
118
119 generatePdf(targetFile, locale, operations);
120 }
121
122 protected void prepareOperation(Integer fishingOperationId, List<Map<String, Object>> operations) {
123
124
125 boolean withCatchBatch =
126 persistenceService.isFishingOperationWithCatchBatch(
127 fishingOperationId);
128
129 if (!withCatchBatch) {
130 if (log.isWarnEnabled()) {
131 log.warn("Skip fishing operation " + fishingOperationId +
132 " since no catchBatch associated.");
133 }
134 return;
135 }
136
137 ExportCatchContext exportContext = ExportCatchContext.newExportContext(
138 persistenceService,
139 weightComputingService,
140 fishingOperationId,
141 false);
142
143
144 Map<String, Object> op = createOperation(exportContext.getFishingOperation());
145
146 float totalWeight = exportContext.getCatchTotalWeight();
147
148 op.put("totalWeight", totalWeight);
149 op.put("totalSortedWeight", exportContext.getCatchTotalSortedWeight());
150
151 List<PdfExportBatchEntry> catchList = Lists.newArrayList();
152
153
154 if (exportContext.withSpeciesBatches()) {
155
156 prepareOperationSpecies(exportContext, totalWeight, catchList);
157
158 }
159
160
161 if (exportContext.withBenthosBatches()) {
162
163 prepareOperationBenthos(exportContext, totalWeight, catchList);
164
165 }
166
167
168 ExportBatchEntry inertLivingNotItemizedCatch = exportContext.getInertAndLivingNotItemizedCatch();
169
170 if (inertLivingNotItemizedCatch.getSortedWeight() > 0f) {
171
172 PdfExportBatchEntry pdfExportBatchEntry =
173 new PdfExportBatchEntry(
174 t("tutti.service.operations.exportCatchesReport.specialRows.inertAndLivinngNotItemized.code"),
175 "",
176 t("tutti.service.operations.exportCatchesReport.specialRows.inertAndLivinngNotItemized.name"),
177 inertLivingNotItemizedCatch.getSortedWeight(),
178 inertLivingNotItemizedCatch.getTotalWeight(),
179 totalWeight);
180 catchList.add(pdfExportBatchEntry);
181 }
182
183 if (log.isDebugEnabled()) {
184
185
186 float computedTotalWeight = 0f;
187 float computedPercentage = 0f;
188 for (PdfExportBatchEntry entry : catchList) {
189 computedTotalWeight += entry.getTotalWeight();
190 computedPercentage += entry.getPercentage();
191 }
192 log.debug("TotalWeight: " + totalWeight);
193 log.debug("ComputedTotalWeight: " + computedTotalWeight);
194 log.debug("ComputedPercentage: " + computedPercentage);
195 }
196 op.put("catches", catchList);
197 operations.add(op);
198 }
199
200 protected void prepareOperationSpecies(ExportCatchContext exportContext, float totalWeight, List<PdfExportBatchEntry> catchList) {
201
202 List<ExportBatchEntry> speciesBatchEntries = exportContext.getSpeciesBatchEntry(false);
203
204 TaxonCache taxonCache = TaxonCaches.createSpeciesCache(persistenceService, context.getDataContext().getProtocol());
205
206 for (ExportBatchEntry entry : speciesBatchEntries) {
207
208 SpeciesBatch batch = entry.getBatch();
209 Species species = batch.getSpecies();
210
211 taxonCache.load(species);
212
213 String code = Speciess.getSurveyCodeOrRefTaxCode(species);
214
215 PdfExportBatchEntry pdfEntry = new PdfExportBatchEntry(code,
216 species.getName(),
217 species.getVernacularCode(),
218 entry.getSortedWeight(),
219 entry.getTotalWeight(),
220 totalWeight);
221 catchList.add(pdfEntry);
222 }
223 }
224
225 protected void prepareOperationBenthos(ExportCatchContext exportContext, float totalWeight, List<PdfExportBatchEntry> catchList) {
226
227 List<ExportBatchEntry> benthosBatchEntries = exportContext.getBenthosBatchEntry(false);
228
229 float sortedWeight = 0f;
230 float benthosTotalWeight = 0f;
231
232 for (ExportBatchEntry entry : benthosBatchEntries) {
233 sortedWeight += entry.getSortedWeight();
234 benthosTotalWeight += entry.getTotalWeight();
235 }
236
237 PdfExportBatchEntry pdfEntry = new PdfExportBatchEntry(
238 t("tutti.service.operations.exportCatchesReport.specialRows.benthos.code"),
239 "",
240 t("tutti.service.operations.exportCatchesReport.specialRows.benthos.name"),
241 sortedWeight,
242 benthosTotalWeight,
243 totalWeight);
244 catchList.add(pdfEntry);
245
246 }
247
248 protected void generatePdf(File targetFile, Locale locale, List<Map<String, Object>> operations) {
249
250 Map<String, Object> data = Maps.newHashMap();
251 data.put("operations", operations);
252
253 pdfGeneratorService.generatePdf(targetFile, locale, "catchesReport.ftl", data);
254
255 }
256
257 protected Map<String, Object> createOperation(FishingOperation fishingOperation) {
258 Map<String, Object> op = Maps.newHashMap();
259 op.put("number", fishingOperation.getFishingOperationNumber());
260 op.put("station", fishingOperation.getStationNumber());
261 op.put("rigNumber", fishingOperation.getMultirigAggregation());
262 op.put("startDate", fishingOperation.getGearShootingStartDate());
263 op.put("endDate", fishingOperation.getGearShootingEndDate());
264
265 return op;
266 }
267
268 }